author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 55 | 5ecee29e98d8 |
child 1017 | 8d24d37ceed8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2004-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
55
5ecee29e98d8
6655515: MBeans tab: operation return values of type Component displayed as String
lmalvent
parents:
2
diff
changeset
|
25 |
|
2 | 26 |
package sun.tools.jconsole.inspector; |
27 |
||
28 |
import java.util.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import javax.swing.table.*; |
|
31 |
import javax.swing.event.*; |
|
32 |
||
33 |
// Imports for picking up mouse events from the JTable. |
|
34 |
||
35 |
import java.awt.event.MouseAdapter; |
|
36 |
import java.awt.event.MouseEvent; |
|
37 |
import java.awt.event.InputEvent; |
|
38 |
import javax.swing.JTable; |
|
39 |
import javax.swing.table.JTableHeader; |
|
40 |
import javax.swing.table.TableColumnModel; |
|
41 |
||
42 |
@SuppressWarnings("serial") |
|
43 |
public class TableSorter extends DefaultTableModel implements MouseListener { |
|
44 |
private boolean ascending = true; |
|
45 |
private TableColumnModel columnModel; |
|
46 |
private JTable tableView; |
|
47 |
private Vector<TableModelListener> listenerList; |
|
48 |
private int sortColumn = 0; |
|
49 |
||
50 |
private int[] invertedIndex; |
|
51 |
||
52 |
public TableSorter() { |
|
53 |
super(); |
|
54 |
listenerList = new Vector<TableModelListener>(); |
|
55 |
} |
|
56 |
||
57 |
public TableSorter(Object[] columnNames, int numRows) { |
|
58 |
super(columnNames,numRows); |
|
59 |
listenerList = new Vector<TableModelListener>(); |
|
60 |
} |
|
61 |
||
62 |
public void newDataAvailable(TableModelEvent e) { |
|
63 |
super.newDataAvailable(e); |
|
64 |
invertedIndex = new int[getRowCount()]; |
|
65 |
for (int i=0;i<invertedIndex.length;i++) { |
|
66 |
invertedIndex[i] = i; |
|
67 |
} |
|
68 |
sort(this.sortColumn); |
|
69 |
} |
|
70 |
||
71 |
public void addTableModelListener(TableModelListener l) { |
|
72 |
listenerList.add(l); |
|
73 |
super.addTableModelListener(l); |
|
74 |
} |
|
75 |
||
76 |
public void removeTableModelListener(TableModelListener l) { |
|
77 |
listenerList.remove(l); |
|
78 |
super.removeTableModelListener(l); |
|
79 |
} |
|
80 |
||
81 |
private void removeListeners() { |
|
82 |
for(TableModelListener tnl : listenerList) |
|
83 |
super.removeTableModelListener(tnl); |
|
84 |
} |
|
85 |
||
86 |
private void restoreListeners() { |
|
87 |
for(TableModelListener tnl : listenerList) |
|
88 |
super.addTableModelListener(tnl); |
|
89 |
} |
|
90 |
||
91 |
@SuppressWarnings("unchecked") |
|
92 |
public int compare(Object o1, Object o2) { |
|
93 |
if (o1==null) |
|
94 |
return 1; |
|
95 |
if (o2==null) |
|
96 |
return -1; |
|
97 |
//two object of the same class and that are comparable |
|
98 |
else if ((o1.getClass().equals(o2.getClass())) && |
|
99 |
(o1 instanceof Comparable)) { |
|
100 |
return (((Comparable) o1).compareTo(o2)); |
|
101 |
} |
|
102 |
else { |
|
103 |
return o1.toString().compareTo(o2.toString()); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
public void sort(int column) { |
|
108 |
// remove registered listeners |
|
109 |
removeListeners(); |
|
110 |
// do the sort |
|
111 |
//n2sort(column); |
|
112 |
quickSort(0,getRowCount()-1,column); |
|
113 |
// restore registered listeners |
|
114 |
restoreListeners(); |
|
115 |
this.sortColumn = column; |
|
116 |
// update row heights in XMBeanAttributes (required by expandable cells) |
|
117 |
if (tableView instanceof XMBeanAttributes) { |
|
118 |
XMBeanAttributes attrs = (XMBeanAttributes) tableView; |
|
119 |
for (int i = 0; i < getRowCount(); i++) { |
|
120 |
Vector data = (Vector) dataVector.elementAt(i); |
|
121 |
attrs.updateRowHeight(data.elementAt(1), i); |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
private synchronized boolean compareS(Object s1, Object s2) { |
|
127 |
if (ascending) |
|
128 |
return (compare(s1,s2) > 0); |
|
129 |
else |
|
130 |
return (compare(s1,s2) < 0); |
|
131 |
} |
|
132 |
||
133 |
private synchronized boolean compareG(Object s1, Object s2) { |
|
134 |
if (ascending) |
|
135 |
return (compare(s1,s2) < 0); |
|
136 |
else |
|
137 |
return (compare(s1,s2) > 0); |
|
138 |
} |
|
139 |
||
140 |
private synchronized void quickSort(int lo0,int hi0, int key) { |
|
141 |
int lo = lo0; |
|
142 |
int hi = hi0; |
|
143 |
Object mid; |
|
144 |
||
145 |
if ( hi0 > lo0) |
|
146 |
{ |
|
147 |
mid = getValueAt( ( lo0 + hi0 ) / 2 , key); |
|
148 |
||
149 |
while( lo <= hi ) |
|
150 |
{ |
|
151 |
/* find the first element that is greater than |
|
152 |
* or equal to the partition element starting |
|
153 |
* from the left Index. |
|
154 |
*/ |
|
155 |
while( ( lo < hi0 ) && |
|
156 |
( compareS(mid,getValueAt(lo,key)) )) |
|
157 |
++lo; |
|
158 |
||
159 |
/* find an element that is smaller than or equal to |
|
160 |
* the partition element starting from the right Index. |
|
161 |
*/ |
|
162 |
while( ( hi > lo0 ) && |
|
163 |
( compareG(mid,getValueAt(hi,key)) )) |
|
164 |
--hi; |
|
165 |
||
166 |
// if the indexes have not crossed, swap |
|
167 |
if( lo <= hi ) |
|
168 |
{ |
|
169 |
swap(lo, hi, key); |
|
170 |
++lo; |
|
171 |
--hi; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
/* If the right index has not reached the |
|
176 |
* left side of array |
|
177 |
* must now sort the left partition. |
|
178 |
*/ |
|
179 |
if( lo0 < hi ) |
|
180 |
quickSort(lo0, hi , key); |
|
181 |
||
182 |
/* If the left index has not reached the right |
|
183 |
* side of array |
|
184 |
* must now sort the right partition. |
|
185 |
*/ |
|
186 |
if( lo <= hi0 ) |
|
187 |
quickSort(lo, hi0 , key); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
public void n2sort(int column) { |
|
192 |
for (int i = 0; i < getRowCount(); i++) { |
|
193 |
for (int j = i+1; j < getRowCount(); j++) { |
|
194 |
if (compare(getValueAt(i,column),getValueAt(j,column)) == -1) { |
|
195 |
swap(i, j, column); |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
private Vector getRow(int row) { |
|
202 |
return (Vector) dataVector.elementAt(row); |
|
203 |
} |
|
204 |
||
205 |
@SuppressWarnings("unchecked") |
|
206 |
private void setRow(Vector data, int row) { |
|
207 |
dataVector.setElementAt(data,row); |
|
208 |
} |
|
209 |
||
210 |
public void swap(int i, int j, int column) { |
|
211 |
Vector data = getRow(i); |
|
212 |
setRow(getRow(j),i); |
|
213 |
setRow(data,j); |
|
214 |
||
215 |
int a = invertedIndex[i]; |
|
216 |
invertedIndex[i] = invertedIndex[j]; |
|
217 |
invertedIndex[j] = a; |
|
218 |
} |
|
219 |
||
220 |
public void sortByColumn(int column) { |
|
221 |
sortByColumn(column, !ascending); |
|
222 |
} |
|
223 |
||
224 |
public void sortByColumn(int column, boolean ascending) { |
|
225 |
this.ascending = ascending; |
|
226 |
sort(column); |
|
227 |
} |
|
228 |
||
229 |
public int[] getInvertedIndex() { |
|
230 |
return invertedIndex; |
|
231 |
} |
|
232 |
||
233 |
// Add a mouse listener to the Table to trigger a table sort |
|
234 |
// when a column heading is clicked in the JTable. |
|
235 |
public void addMouseListenerToHeaderInTable(JTable table) { |
|
236 |
tableView = table; |
|
237 |
columnModel = tableView.getColumnModel(); |
|
238 |
JTableHeader th = tableView.getTableHeader(); |
|
239 |
th.addMouseListener(this); |
|
240 |
} |
|
241 |
||
242 |
public void mouseClicked(MouseEvent e) { |
|
243 |
int viewColumn = columnModel.getColumnIndexAtX(e.getX()); |
|
244 |
int column = tableView.convertColumnIndexToModel(viewColumn); |
|
245 |
if (e.getClickCount() == 1 && column != -1) { |
|
246 |
tableView.invalidate(); |
|
247 |
sortByColumn(column); |
|
248 |
tableView.validate(); |
|
249 |
tableView.repaint(); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
public void mousePressed(MouseEvent e) { |
|
254 |
} |
|
255 |
||
256 |
public void mouseEntered(MouseEvent e) { |
|
257 |
} |
|
258 |
||
259 |
public void mouseExited(MouseEvent e) { |
|
260 |
} |
|
261 |
||
262 |
public void mouseReleased(MouseEvent e) { |
|
263 |
} |
|
264 |
} |