Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / util / TableSorter.js
1 Clazz.declarePackage ("jalview.util");
2 Clazz.load (["java.awt.event.MouseAdapter", "javax.swing.Icon", "javax.swing.event.TableModelListener", "javax.swing.table.AbstractTableModel", "$.TableCellRenderer", "java.util.ArrayList", "$.HashMap"], "jalview.util.TableSorter", ["java.awt.Color", "java.util.Arrays", "javax.swing.JLabel", "javax.swing.event.TableModelEvent"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.tableModel = null;
5 this.viewToModel = null;
6 this.modelToView = null;
7 this.tableHeader = null;
8 this.mouseListener = null;
9 this.tableModelListener = null;
10 this.columnComparators = null;
11 this.sortingColumns = null;
12 if (!Clazz.isClassDefined ("jalview.util.TableSorter.Row")) {
13 jalview.util.TableSorter.$TableSorter$Row$ ();
14 }
15 if (!Clazz.isClassDefined ("jalview.util.TableSorter.TableModelHandler")) {
16 jalview.util.TableSorter.$TableSorter$TableModelHandler$ ();
17 }
18 if (!Clazz.isClassDefined ("jalview.util.TableSorter.MouseHandler")) {
19 jalview.util.TableSorter.$TableSorter$MouseHandler$ ();
20 }
21 if (!Clazz.isClassDefined ("jalview.util.TableSorter.SortableHeaderRenderer")) {
22 jalview.util.TableSorter.$TableSorter$SortableHeaderRenderer$ ();
23 }
24 Clazz.instantialize (this, arguments);
25 }, jalview.util, "TableSorter", javax.swing.table.AbstractTableModel);
26 Clazz.prepareFields (c$, function () {
27 this.columnComparators =  new java.util.HashMap ();
28 this.sortingColumns =  new java.util.ArrayList ();
29 });
30 Clazz.makeConstructor (c$, 
31 function () {
32 Clazz.superConstructor (this, jalview.util.TableSorter, []);
33 this.mouseListener = Clazz.innerTypeInstance (jalview.util.TableSorter.MouseHandler, this, null);
34 this.tableModelListener = Clazz.innerTypeInstance (jalview.util.TableSorter.TableModelHandler, this, null);
35 });
36 Clazz.makeConstructor (c$, 
37 function (tableModel) {
38 this.construct ();
39 this.setTableModel (tableModel);
40 }, "javax.swing.table.TableModel");
41 Clazz.makeConstructor (c$, 
42 function (tableModel, tableHeader) {
43 this.construct ();
44 this.setTableHeader (tableHeader);
45 this.setTableModel (tableModel);
46 }, "javax.swing.table.TableModel,javax.swing.table.JTableHeader");
47 Clazz.defineMethod (c$, "clearSortingState", 
48  function () {
49 this.viewToModel = null;
50 this.modelToView = null;
51 });
52 Clazz.defineMethod (c$, "getTableModel", 
53 function () {
54 return this.tableModel;
55 });
56 Clazz.defineMethod (c$, "setTableModel", 
57 function (tableModel) {
58 if (this.tableModel != null) {
59 this.tableModel.removeTableModelListener (this.tableModelListener);
60 }this.tableModel = tableModel;
61 if (this.tableModel != null) {
62 this.tableModel.addTableModelListener (this.tableModelListener);
63 }this.clearSortingState ();
64 this.fireTableStructureChanged ();
65 }, "javax.swing.table.TableModel");
66 Clazz.defineMethod (c$, "getTableHeader", 
67 function () {
68 return this.tableHeader;
69 });
70 Clazz.defineMethod (c$, "setTableHeader", 
71 function (tableHeader) {
72 if (this.tableHeader != null) {
73 this.tableHeader.removeMouseListener (this.mouseListener);
74 var defaultRenderer = this.tableHeader.getDefaultRenderer ();
75 if (Clazz.instanceOf (defaultRenderer, jalview.util.TableSorter.SortableHeaderRenderer)) {
76 this.tableHeader.setDefaultRenderer ((defaultRenderer).tableCellRenderer);
77 }}this.tableHeader = tableHeader;
78 if (this.tableHeader != null) {
79 this.tableHeader.addMouseListener (this.mouseListener);
80 this.tableHeader.setDefaultRenderer (Clazz.innerTypeInstance (jalview.util.TableSorter.SortableHeaderRenderer, this, null, this.tableHeader.getDefaultRenderer ()));
81 }}, "javax.swing.table.JTableHeader");
82 Clazz.defineMethod (c$, "isSorting", 
83 function () {
84 return this.sortingColumns.size () != 0;
85 });
86 Clazz.defineMethod (c$, "getDirective", 
87  function (column) {
88 for (var i = 0; i < this.sortingColumns.size (); i++) {
89 var directive = this.sortingColumns.get (i);
90 if (directive.column == column) {
91 return directive;
92 }}
93 return jalview.util.TableSorter.EMPTY_DIRECTIVE;
94 }, "~N");
95 Clazz.defineMethod (c$, "getSortingStatus", 
96 function (column) {
97 return this.getDirective (column).direction;
98 }, "~N");
99 Clazz.defineMethod (c$, "sortingStatusChanged", 
100  function () {
101 this.clearSortingState ();
102 this.fireTableDataChanged ();
103 if (this.tableHeader != null) {
104 this.tableHeader.repaint ();
105 }});
106 Clazz.defineMethod (c$, "setSortingStatus", 
107 function (column, status) {
108 var directive = this.getDirective (column);
109 if (directive !== jalview.util.TableSorter.EMPTY_DIRECTIVE) {
110 this.sortingColumns.remove (directive);
111 }if (status != 0) {
112 this.sortingColumns.add ( new jalview.util.TableSorter.Directive (column, status));
113 }this.sortingStatusChanged ();
114 }, "~N,~N");
115 Clazz.defineMethod (c$, "getHeaderRendererIcon", 
116 function (column, size) {
117 var directive = this.getDirective (column);
118 if (directive === jalview.util.TableSorter.EMPTY_DIRECTIVE) {
119 return null;
120 }return  new jalview.util.TableSorter.Arrow (directive.direction == -1, size, this.sortingColumns.indexOf (directive));
121 }, "~N,~N");
122 Clazz.defineMethod (c$, "cancelSorting", 
123  function () {
124 this.sortingColumns.clear ();
125 this.sortingStatusChanged ();
126 });
127 Clazz.defineMethod (c$, "setColumnComparator", 
128 function (type, comparator) {
129 if (comparator == null) {
130 this.columnComparators.remove (type);
131 } else {
132 this.columnComparators.put (type, comparator);
133 }}, "Class,java.util.Comparator");
134 Clazz.defineMethod (c$, "getComparator", 
135 function (column) {
136 var columnType = this.tableModel.getColumnClass (column);
137 var comparator = this.columnComparators.get (columnType);
138 if (comparator != null) {
139 return comparator;
140 }if (Comparable.isAssignableFrom (columnType)) {
141 return jalview.util.TableSorter.COMPARABLE_COMAPRATOR;
142 }return jalview.util.TableSorter.LEXICAL_COMPARATOR;
143 }, "~N");
144 Clazz.defineMethod (c$, "getViewToModel", 
145  function () {
146 if (this.viewToModel == null) {
147 var tableModelRowCount = this.tableModel.getRowCount ();
148 this.viewToModel =  new Array (tableModelRowCount);
149 for (var row = 0; row < tableModelRowCount; row++) {
150 this.viewToModel[row] = Clazz.innerTypeInstance (jalview.util.TableSorter.Row, this, null, row);
151 }
152 if (this.isSorting ()) {
153 java.util.Arrays.sort (this.viewToModel);
154 }}return this.viewToModel;
155 });
156 Clazz.defineMethod (c$, "modelIndex", 
157 function (viewIndex) {
158 return this.getViewToModel ()[viewIndex].modelIndex;
159 }, "~N");
160 Clazz.defineMethod (c$, "getModelToView", 
161  function () {
162 if (this.modelToView == null) {
163 var n = this.getViewToModel ().length;
164 this.modelToView =  Clazz.newIntArray (n, 0);
165 for (var i = 0; i < n; i++) {
166 this.modelToView[this.modelIndex (i)] = i;
167 }
168 }return this.modelToView;
169 });
170 Clazz.defineMethod (c$, "getRowCount", 
171 function () {
172 return (this.tableModel == null) ? 0 : this.tableModel.getRowCount ();
173 });
174 Clazz.defineMethod (c$, "getColumnCount", 
175 function () {
176 return (this.tableModel == null) ? 0 : this.tableModel.getColumnCount ();
177 });
178 Clazz.overrideMethod (c$, "getColumnName", 
179 function (column) {
180 return this.tableModel.getColumnName (column);
181 }, "~N");
182 Clazz.overrideMethod (c$, "getColumnClass", 
183 function (column) {
184 return this.tableModel.getColumnClass (column);
185 }, "~N");
186 Clazz.overrideMethod (c$, "isCellEditable", 
187 function (row, column) {
188 return this.tableModel.isCellEditable (this.modelIndex (row), column);
189 }, "~N,~N");
190 Clazz.defineMethod (c$, "getValueAt", 
191 function (row, column) {
192 return this.tableModel.getValueAt (this.modelIndex (row), column);
193 }, "~N,~N");
194 Clazz.overrideMethod (c$, "setValueAt", 
195 function (aValue, row, column) {
196 this.tableModel.setValueAt (aValue, this.modelIndex (row), column);
197 }, "~O,~N,~N");
198 c$.$TableSorter$Row$ = function () {
199 Clazz.pu$h(self.c$);
200 c$ = Clazz.decorateAsClass (function () {
201 Clazz.prepareCallback (this, arguments);
202 this.modelIndex = 0;
203 Clazz.instantialize (this, arguments);
204 }, jalview.util.TableSorter, "Row", null, Comparable);
205 Clazz.makeConstructor (c$, 
206 function (a) {
207 this.modelIndex = a;
208 }, "~N");
209 Clazz.overrideMethod (c$, "compareTo", 
210 function (a) {
211 var b = this.modelIndex;
212 var c = (a).modelIndex;
213 for (var d = this.b$["jalview.util.TableSorter"].sortingColumns.iterator (); d.hasNext (); ) {
214 var e = d.next ();
215 var f = e.column;
216 var g = this.b$["jalview.util.TableSorter"].tableModel.getValueAt (b, f);
217 var h = this.b$["jalview.util.TableSorter"].tableModel.getValueAt (c, f);
218 var i = 0;
219 if (g == null && h == null) {
220 i = 0;
221 } else if (g == null) {
222 i = -1;
223 } else if (h == null) {
224 i = 1;
225 } else {
226 i = this.b$["jalview.util.TableSorter"].getComparator (f).compare (g, h);
227 }if (i != 0) {
228 return e.direction == -1 ? -i : i;
229 }}
230 return 0;
231 }, "~O");
232 c$ = Clazz.p0p ();
233 };
234 c$.$TableSorter$TableModelHandler$ = function () {
235 Clazz.pu$h(self.c$);
236 c$ = Clazz.decorateAsClass (function () {
237 Clazz.prepareCallback (this, arguments);
238 Clazz.instantialize (this, arguments);
239 }, jalview.util.TableSorter, "TableModelHandler", null, javax.swing.event.TableModelListener);
240 Clazz.overrideMethod (c$, "tableChanged", 
241 function (a) {
242 if (!this.b$["jalview.util.TableSorter"].isSorting ()) {
243 this.b$["jalview.util.TableSorter"].clearSortingState ();
244 this.b$["jalview.util.TableSorter"].fireTableChanged (a);
245 return;
246 }if (a.getFirstRow () == -1) {
247 this.b$["jalview.util.TableSorter"].cancelSorting ();
248 this.b$["jalview.util.TableSorter"].fireTableChanged (a);
249 return;
250 }var b = a.getColumn ();
251 if (a.getFirstRow () == a.getLastRow () && b != -1 && this.b$["jalview.util.TableSorter"].getSortingStatus (b) == 0 && this.b$["jalview.util.TableSorter"].modelToView != null) {
252 var c = this.b$["jalview.util.TableSorter"].getModelToView ()[a.getFirstRow ()];
253 this.b$["jalview.util.TableSorter"].fireTableChanged ( new javax.swing.event.TableModelEvent (this.b$["jalview.util.TableSorter"], c, c, b, a.getType ()));
254 return;
255 }this.b$["jalview.util.TableSorter"].clearSortingState ();
256 this.b$["jalview.util.TableSorter"].fireTableDataChanged ();
257 return;
258 }, "javax.swing.event.TableModelEvent");
259 c$ = Clazz.p0p ();
260 };
261 c$.$TableSorter$MouseHandler$ = function () {
262 Clazz.pu$h(self.c$);
263 c$ = Clazz.decorateAsClass (function () {
264 Clazz.prepareCallback (this, arguments);
265 Clazz.instantialize (this, arguments);
266 }, jalview.util.TableSorter, "MouseHandler", java.awt.event.MouseAdapter);
267 Clazz.overrideMethod (c$, "mouseClicked", 
268 function (a) {
269 var b = a.getSource ();
270 var c = b.getColumnModel ();
271 var d = c.getColumnIndexAtX (a.getX ());
272 var e = c.getColumn (d).getModelIndex ();
273 if (e != -1) {
274 var f = this.b$["jalview.util.TableSorter"].getSortingStatus (e);
275 if (!a.isControlDown ()) {
276 this.b$["jalview.util.TableSorter"].cancelSorting ();
277 }f = f + (a.isShiftDown () ? -1 : 1);
278 f = (f + 4) % 3 - 1;
279 this.b$["jalview.util.TableSorter"].setSortingStatus (e, f);
280 }}, "java.awt.event.MouseEvent");
281 c$ = Clazz.p0p ();
282 };
283 c$.$TableSorter$SortableHeaderRenderer$ = function () {
284 Clazz.pu$h(self.c$);
285 c$ = Clazz.decorateAsClass (function () {
286 Clazz.prepareCallback (this, arguments);
287 this.tableCellRenderer = null;
288 Clazz.instantialize (this, arguments);
289 }, jalview.util.TableSorter, "SortableHeaderRenderer", null, javax.swing.table.TableCellRenderer);
290 Clazz.makeConstructor (c$, 
291 function (a) {
292 this.tableCellRenderer = a;
293 }, "javax.swing.table.TableCellRenderer");
294 Clazz.defineMethod (c$, "getTableCellRendererComponent", 
295 function (a, b, c, d, e, f) {
296 var g = this.tableCellRenderer.getTableCellRendererComponent (a, b, c, d, e, f);
297 if (Clazz.instanceOf (g, javax.swing.JLabel)) {
298 var h = g;
299 h.setHorizontalTextPosition (2);
300 var i = a.convertColumnIndexToModel (f);
301 h.setIcon (this.b$["jalview.util.TableSorter"].getHeaderRendererIcon (i, h.getFont ().getSize ()));
302 }return g;
303 }, "javax.swing.JTable,~O,~B,~B,~N,~N");
304 c$ = Clazz.p0p ();
305 };
306 c$.$TableSorter$1$ = function () {
307 Clazz.pu$h(self.c$);
308 c$ = Clazz.declareAnonymous (jalview.util, "TableSorter$1", null, java.util.Comparator);
309 Clazz.defineMethod (c$, "compare", 
310 function (o1, o2) {
311 return (o1).compareTo (o2);
312 }, "~O,~O");
313 c$ = Clazz.p0p ();
314 };
315 c$.$TableSorter$2$ = function () {
316 Clazz.pu$h(self.c$);
317 c$ = Clazz.declareAnonymous (jalview.util, "TableSorter$2", null, java.util.Comparator);
318 Clazz.defineMethod (c$, "compare", 
319 function (o1, o2) {
320 return o1.toString ().compareTo (o2.toString ());
321 }, "~O,~O");
322 c$ = Clazz.p0p ();
323 };
324 Clazz.pu$h(self.c$);
325 c$ = Clazz.decorateAsClass (function () {
326 this.descending = false;
327 this.size = 0;
328 this.priority = 0;
329 Clazz.instantialize (this, arguments);
330 }, jalview.util.TableSorter, "Arrow", null, javax.swing.Icon);
331 Clazz.makeConstructor (c$, 
332 function (a, b, c) {
333 this.descending = a;
334 this.size = b;
335 this.priority = c;
336 }, "~B,~N,~N");
337 Clazz.overrideMethod (c$, "paintIcon", 
338 function (a, b, c, d) {
339 var e = a == null ? java.awt.Color.GRAY : a.getBackground ();
340 var f = Clazz.doubleToInt (Clazz.doubleToInt (this.size / 2) * Math.pow (0.8, this.priority));
341 var g = this.descending ? f : -f;
342 d = d + Clazz.doubleToInt (5 * this.size / 6) + (this.descending ? -g : 0);
343 var h = this.descending ? 1 : -1;
344 b.translate (c, d);
345 b.setColor (e.darker ());
346 b.drawLine (Clazz.doubleToInt (f / 2), g, 0, 0);
347 b.drawLine (Clazz.doubleToInt (f / 2), g + h, 0, h);
348 b.setColor (e.brighter ());
349 b.drawLine (Clazz.doubleToInt (f / 2), g, f, 0);
350 b.drawLine (Clazz.doubleToInt (f / 2), g + h, f, h);
351 if (this.descending) {
352 b.setColor (e.darker ().darker ());
353 } else {
354 b.setColor (e.brighter ().brighter ());
355 }b.drawLine (f, 0, 0, 0);
356 b.setColor (e);
357 b.translate (-c, -d);
358 }, "java.awt.Component,java.awt.Graphics,~N,~N");
359 Clazz.overrideMethod (c$, "getIconWidth", 
360 function () {
361 return this.size;
362 });
363 Clazz.overrideMethod (c$, "getIconHeight", 
364 function () {
365 return this.size;
366 });
367 c$ = Clazz.p0p ();
368 Clazz.pu$h(self.c$);
369 c$ = Clazz.decorateAsClass (function () {
370 this.column = 0;
371 this.direction = 0;
372 Clazz.instantialize (this, arguments);
373 }, jalview.util.TableSorter, "Directive");
374 Clazz.makeConstructor (c$, 
375 function (a, b) {
376 this.column = a;
377 this.direction = b;
378 }, "~N,~N");
379 c$ = Clazz.p0p ();
380 Clazz.defineStatics (c$,
381 "DESCENDING", -1,
382 "NOT_SORTED", 0,
383 "ASCENDING", 1);
384 c$.EMPTY_DIRECTIVE = c$.prototype.EMPTY_DIRECTIVE =  new jalview.util.TableSorter.Directive (-1, 0);
385 c$.COMPARABLE_COMAPRATOR = c$.prototype.COMPARABLE_COMAPRATOR = ((Clazz.isClassDefined ("jalview.util.TableSorter$1") ? 0 : jalview.util.TableSorter.$TableSorter$1$ ()), Clazz.innerTypeInstance (jalview.util.TableSorter$1, this, null));
386 c$.LEXICAL_COMPARATOR = c$.prototype.LEXICAL_COMPARATOR = ((Clazz.isClassDefined ("jalview.util.TableSorter$2") ? 0 : jalview.util.TableSorter.$TableSorter$2$ ()), Clazz.innerTypeInstance (jalview.util.TableSorter$2, this, null));
387 });