1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.mindtree.techworks.insight.model;
25
26 import java.util.*;
27
28 import com.mindtree.techworks.insight.spi.LogEvent;
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class ModelDataSorter implements Comparator {
43
44
45
46
47 public static final int ASCENDING = 10;
48 public static final int DESCENDING = 20;
49 public static final int NOTSORTED = 30;
50
51
52
53
54 private Map columnSortMap = new HashMap();
55
56
57
58
59
60 private int sortColumn = -1;
61
62
63
64
65 private boolean isAscending = true;
66
67
68
69
70
71
72
73
74 public void sortData(int columnIndex, List data) {
75 if (this.sortColumn > -1) {
76 return;
77 }
78 this.sortColumn = columnIndex;
79 String columnKey = String.valueOf(columnIndex);
80 Object columnSortOrder = columnSortMap.get(columnKey);
81 if (columnSortOrder != null) {
82 this.isAscending = !(((Integer)columnSortOrder).intValue() == ASCENDING);
83 }
84 Collections.sort(data, this);
85 columnSortMap.put(columnKey,this.isAscending ? new Integer(ASCENDING) : new Integer(DESCENDING));
86
87 Iterator iterator = columnSortMap.keySet().iterator();
88 while(iterator.hasNext()) {
89 String key = (String)iterator.next();
90 if (!key.equals(columnKey)) {
91 columnSortMap.put(key, new Integer(NOTSORTED));
92 }
93 }
94 this.sortColumn = -1;
95 }
96
97
98
99
100
101
102 public int getSortOrder(int columnIndex) {
103 int result = NOTSORTED;
104 String columnKey = String.valueOf(columnIndex);
105 Object columnSortOrder = columnSortMap.get(columnKey);
106 if (columnSortOrder != null) {
107 return ((Integer)columnSortOrder).intValue();
108 }
109 return result;
110 }
111
112
113
114
115
116 public int compare(Object arg0, Object arg1) {
117 int result = 0;
118 LogEvent le1 = (LogEvent)arg0;
119 LogEvent le2 = (LogEvent)arg1;
120 switch(this.sortColumn) {
121 case LogEventTableModel.CATEGORY_INDEX:
122 result = le1.getLoggerName().compareTo(le2.getLoggerName());
123 break;
124 case LogEventTableModel.EXCEPTION_NAME_INDEX:
125 result = le1.getExceptionName().compareTo(le2.getExceptionName());
126 break;
127 case LogEventTableModel.MESSAGE_INDEX:
128 result = le1.getMessage().toString().compareTo(le2.getMessage().toString());
129 break;
130 case LogEventTableModel.PRIORITY_INDEX:
131 result = le1.getLevel().toInt() - le2.getLevel().toInt();
132 break;
133 case LogEventTableModel.THREAD_INDEX:
134 result = le1.getThreadName().compareTo(le2.getThreadName());
135 break;
136 case LogEventTableModel.TIME_INDEX:
137 result = (int)(le1.getTimeStamp() - le2.getTimeStamp());
138 break;
139 case LogEventTableModel.RELATIVETIME_INDEX:
140 result = (int)(le1.getRelativeTime() - le2.getRelativeTime());
141 break;
142 case LogEventTableModel.METHOD_INDEX:
143 if (le1.getLocationInformation().getMethodName() == null || le2.getLocationInformation().getMethodName() == null) {
144 result = 0;
145 break;
146 }
147 result = le1.getLocationInformation().getMethodName().compareTo(le2.getLocationInformation().getMethodName());
148 break;
149 case LogEventTableModel.LINE_INDEX:
150 if (le1.getLocationInformation().getLineNumber() == null || le2.getLocationInformation().getLineNumber() == null) {
151 result = 0;
152 break;
153 }
154 result = le1.getLocationInformation().getLineNumber().compareTo(le2.getLocationInformation().getLineNumber());
155 break;
156 case LogEventTableModel.FILE_INDEX:
157 if (le1.getLocationInformation().getFileName() == null || le2.getLocationInformation().getFileName() == null) {
158 result = 0;
159 break;
160 }
161 result = le1.getLocationInformation().getFileName().compareTo(le2.getLocationInformation().getFileName());
162 break;
163 case LogEventTableModel.CLASS_INDEX:
164 if (le1.getLocationInformation().getClassName() == null || le2.getLocationInformation().getClassName() == null) {
165 result = 0;
166 break;
167 }
168 result = le1.getLocationInformation().getClassName().compareTo(le2.getLocationInformation().getClassName());
169 break;
170 }
171 return (this.isAscending ? result : (-1)*result);
172 }
173
174
175
176
177 public void resetSortParams() {
178 this.isAscending = true;
179 this.sortColumn = -1;
180 columnSortMap.clear();
181 }
182
183 }