aede063a1cda8be5842b15e5586bec583d8b9a0f
[public/netxms.git] / src / java / netxms-eclipse / AlarmViewer / src / org / netxms / ui / eclipse / alarmviewer / AlarmComparator.java
1 /**
2 *
3 */
4 package org.netxms.ui.eclipse.alarmviewer;
5
6 import org.eclipse.jface.viewers.TableViewer;
7 import org.eclipse.jface.viewers.Viewer;
8 import org.eclipse.jface.viewers.ViewerComparator;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.widgets.TableColumn;
11 import org.netxms.client.NXCSession;
12 import org.netxms.client.events.Alarm;
13 import org.netxms.client.objects.GenericObject;
14 import org.netxms.ui.eclipse.alarmviewer.widgets.AlarmList;
15 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
16
17 /**
18 * @author victor
19 *
20 */
21 public class AlarmComparator extends ViewerComparator
22 {
23 /**
24 *
25 */
26 public AlarmComparator()
27 {
28 super();
29 }
30
31
32 /**
33 * Compare two numbers and return -1, 0, or 1
34 */
35 private int compareNumbers(long n1, long n2)
36 {
37 return (n1 < n2) ? -1 : ((n1 > n2) ? 1 : 0);
38 }
39
40 /* (non-Javadoc)
41 * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
42 */
43 @Override
44 public int compare(Viewer viewer, Object e1, Object e2)
45 {
46 TableColumn sortColumn = ((TableViewer)viewer).getTable().getSortColumn();
47 if (sortColumn == null)
48 return 0;
49
50 int rc;
51 switch((Integer)sortColumn.getData("ID")) //$NON-NLS-1$
52 {
53 case AlarmList.COLUMN_SEVERITY:
54 rc = compareNumbers(((Alarm)e1).getCurrentSeverity(), ((Alarm)e2).getCurrentSeverity());
55 break;
56 case AlarmList.COLUMN_STATE:
57 rc = compareNumbers(((Alarm)e1).getState(), ((Alarm)e2).getState());
58 break;
59 case AlarmList.COLUMN_SOURCE:
60 GenericObject obj1 = ((NXCSession)ConsoleSharedData.getSession()).findObjectById(((Alarm)e1).getSourceObjectId());
61 GenericObject obj2 = ((NXCSession)ConsoleSharedData.getSession()).findObjectById(((Alarm)e2).getSourceObjectId());
62 String name1 = (obj1 != null) ? obj1.getObjectName() : Messages.AlarmComparator_Unknown;
63 String name2 = (obj2 != null) ? obj2.getObjectName() : Messages.AlarmComparator_Unknown;
64 rc = name1.compareToIgnoreCase(name2);
65 break;
66 case AlarmList.COLUMN_MESSAGE:
67 rc = ((Alarm)e1).getMessage().compareToIgnoreCase(((Alarm)e2).getMessage());
68 break;
69 case AlarmList.COLUMN_COUNT:
70 rc = compareNumbers(((Alarm)e1).getRepeatCount(), ((Alarm)e2).getRepeatCount());
71 break;
72 case AlarmList.COLUMN_CREATED:
73 rc = ((Alarm)e1).getCreationTime().compareTo(((Alarm)e2).getCreationTime());
74 break;
75 case AlarmList.COLUMN_LASTCHANGE:
76 rc = ((Alarm)e1).getLastChangeTime().compareTo(((Alarm)e2).getLastChangeTime());
77 break;
78 default:
79 rc = 0;
80 break;
81 }
82 int dir = ((TableViewer)viewer).getTable().getSortDirection();
83 return (dir == SWT.UP) ? rc : -rc;
84 }
85 }