]>
Commit | Line | Data |
---|---|---|
1 | /** @file | |
2 | ||
3 | The file is used to sort FrameworkModules of Fpd file | |
4 | ||
5 | Copyright (c) 2006, Intel Corporation | |
6 | All rights reserved. This program and the accompanying materials | |
7 | are licensed and made available under the terms and conditions of the BSD License | |
8 | which accompanies this distribution. The full text of the license may be found at | |
9 | http://opensource.org/licenses/bsd-license.php | |
10 | ||
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. | |
13 | ||
14 | **/ | |
15 | package org.tianocore.frameworkwizard.platform.ui; | |
16 | ||
17 | import java.awt.event.*; | |
18 | import java.util.*; | |
19 | ||
20 | import javax.swing.event.TableModelEvent; | |
21 | import javax.swing.event.TableModelListener; | |
22 | import javax.swing.table.*; | |
23 | ||
24 | ||
25 | public class TableSorter extends AbstractTableModel { | |
26 | /** | |
27 | * | |
28 | */ | |
29 | private static final long serialVersionUID = 1L; | |
30 | ||
31 | protected DefaultTableModel tableModel; | |
32 | private TableRow[] rowInView; | |
33 | private int[] viewPos; | |
34 | ||
35 | public static final String DESCENDING = "down"; | |
36 | public static final String NOT_SORTED = "none"; | |
37 | public static final String ASCENDING = "up"; | |
38 | ||
39 | private JTableHeader tableHeader; | |
40 | private MouseListener mouseListener = new MouseHandler(); | |
41 | private TableModelListener tableModelListener = new TableModelHandler(); | |
42 | ||
43 | private HashMap<Integer, String> sortingOrders = new HashMap<Integer, String>(); | |
44 | ||
45 | public TableSorter(DefaultTableModel tableModel) { | |
46 | setTableModel(tableModel); | |
47 | } | |
48 | ||
49 | ||
50 | private void resetSortState() { | |
51 | rowInView = null; | |
52 | viewPos = null; | |
53 | } | |
54 | ||
55 | public DefaultTableModel getTableModel() { | |
56 | return tableModel; | |
57 | } | |
58 | ||
59 | public void setTableModel(DefaultTableModel dtm) { | |
60 | if (tableModel != null) { | |
61 | tableModel.removeTableModelListener(tableModelListener); | |
62 | } | |
63 | ||
64 | tableModel = dtm; | |
65 | if (tableModel != null) { | |
66 | tableModel.addTableModelListener(tableModelListener); | |
67 | } | |
68 | ||
69 | resetSortState(); | |
70 | fireTableStructureChanged(); | |
71 | } | |
72 | ||
73 | public JTableHeader getTableHeader() { | |
74 | return tableHeader; | |
75 | } | |
76 | ||
77 | public void setTableHeader(JTableHeader th) { | |
78 | if (tableHeader != null) { | |
79 | tableHeader.removeMouseListener(mouseListener); | |
80 | } | |
81 | tableHeader = th; | |
82 | if (tableHeader != null) { | |
83 | tableHeader.addMouseListener(mouseListener); | |
84 | ||
85 | } | |
86 | } | |
87 | ||
88 | private String getSortState(int column) { | |
89 | ||
90 | Integer i = new Integer(column); | |
91 | if (sortingOrders.get(i) != null) { | |
92 | return sortingOrders.get(i); | |
93 | } | |
94 | return NOT_SORTED; | |
95 | } | |
96 | ||
97 | private void sortStateChanged() { | |
98 | resetSortState(); | |
99 | fireTableDataChanged(); | |
100 | ||
101 | } | |
102 | ||
103 | public void setSortState(int column, String status) { | |
104 | Integer i = new Integer(column); | |
105 | sortingOrders.put(i, status); | |
106 | sortStateChanged(); | |
107 | } | |
108 | ||
109 | private TableRow[] getSortedViewRows() { | |
110 | if (rowInView == null) { | |
111 | int rowCount = tableModel.getRowCount(); | |
112 | rowInView = new TableRow[rowCount]; | |
113 | int i = 0; | |
114 | while ( i < rowCount ) { | |
115 | rowInView[i] = new TableRow(i); | |
116 | ++i; | |
117 | } | |
118 | ||
119 | if (sortingOrders.size() != 0) { | |
120 | Arrays.sort(rowInView); | |
121 | } | |
122 | } | |
123 | return rowInView; | |
124 | } | |
125 | ||
126 | public int getModelRowIndex(int viewIndex) { | |
127 | TableRow[] rArray = getSortedViewRows(); | |
128 | return rArray[viewIndex].modelIndex; | |
129 | } | |
130 | ||
131 | public int[] getViewIndexArray() { | |
132 | if (viewPos == null) { | |
133 | int n = getSortedViewRows().length; | |
134 | viewPos = new int[n]; | |
135 | for (int i = 0; i < n; i++) { | |
136 | viewPos[getModelRowIndex(i)] = i; | |
137 | } | |
138 | } | |
139 | return viewPos; | |
140 | } | |
141 | ||
142 | ||
143 | ||
144 | public int getRowCount() { | |
145 | if (tableModel == null) { | |
146 | return 0; | |
147 | } | |
148 | return tableModel.getRowCount(); | |
149 | } | |
150 | ||
151 | public String getColumnName(int col) { | |
152 | return tableModel.getColumnName(col); | |
153 | } | |
154 | ||
155 | public int getColumnCount() { | |
156 | if (tableModel == null) { | |
157 | return 0; | |
158 | } | |
159 | return tableModel.getColumnCount(); | |
160 | } | |
161 | ||
162 | public Class<?> getColumnClass(int col) { | |
163 | return tableModel.getColumnClass(col); | |
164 | } | |
165 | ||
166 | public boolean isCellEditable(int row, int col) { | |
167 | int modelIndex = getModelRowIndex(row); | |
168 | return tableModel.isCellEditable(modelIndex, col); | |
169 | } | |
170 | ||
171 | public Object getValueAt(int row, int col) { | |
172 | int modelIndex = getModelRowIndex(row); | |
173 | return tableModel.getValueAt(modelIndex, col); | |
174 | } | |
175 | ||
176 | public void setValueAt(Object val, int row, int col) { | |
177 | int modelIndex = getModelRowIndex(row); | |
178 | tableModel.setValueAt(val, modelIndex, col); | |
179 | } | |
180 | ||
181 | // Helper classes | |
182 | ||
183 | private class TableRow implements Comparable { | |
184 | private int modelIndex; | |
185 | ||
186 | public TableRow(int index) { | |
187 | this.modelIndex = index; | |
188 | } | |
189 | ||
190 | public int compareTo(Object o) { | |
191 | int row1 = modelIndex; | |
192 | int row2 = ((TableRow) o).modelIndex; | |
193 | ||
194 | Iterator<Integer> mapIter = sortingOrders.keySet().iterator(); | |
195 | ||
196 | while (mapIter.hasNext()) { | |
197 | ||
198 | Integer column = mapIter.next(); | |
199 | Object o1 = tableModel.getValueAt(row1, column); | |
200 | Object o2 = tableModel.getValueAt(row2, column); | |
201 | ||
202 | int comparison = 0; | |
203 | if (o1 == null && o2 == null) { | |
204 | comparison = 0; | |
205 | } else if (o1 == null) { | |
206 | comparison = -1; | |
207 | } else if (o2 == null) { | |
208 | comparison = 1; | |
209 | } else { | |
210 | comparison = o1.toString().compareTo(o2.toString());; | |
211 | } | |
212 | if (comparison != 0) { | |
213 | if (getSortState(column.intValue()).equals(DESCENDING)) { | |
214 | return -comparison; | |
215 | } | |
216 | return comparison; | |
217 | } | |
218 | } | |
219 | return 0; | |
220 | } | |
221 | } | |
222 | ||
223 | private class TableModelHandler implements TableModelListener { | |
224 | public void tableChanged(TableModelEvent e) { | |
225 | if (sortingOrders.size() != 0) { | |
226 | resetSortState(); | |
227 | fireTableChanged(e); | |
228 | return; | |
229 | } | |
230 | if (e.getFirstRow() == TableModelEvent.HEADER_ROW) { | |
231 | ||
232 | fireTableChanged(e); | |
233 | return; | |
234 | } | |
235 | int column = e.getColumn(); | |
236 | if (e.getFirstRow() == e.getLastRow() | |
237 | && column != TableModelEvent.ALL_COLUMNS | |
238 | && getSortState(column).equals(NOT_SORTED) | |
239 | && viewPos != null) { | |
240 | int viewIndex = getViewIndexArray()[e.getFirstRow()]; | |
241 | fireTableChanged(new TableModelEvent(TableSorter.this, | |
242 | viewIndex, viewIndex, | |
243 | column, e.getType())); | |
244 | return; | |
245 | } | |
246 | ||
247 | resetSortState(); | |
248 | fireTableDataChanged(); | |
249 | return; | |
250 | } | |
251 | } | |
252 | ||
253 | private class MouseHandler extends MouseAdapter { | |
254 | public void mouseClicked(MouseEvent e) { | |
255 | JTableHeader h = (JTableHeader) e.getSource(); | |
256 | TableColumnModel columnModel = h.getColumnModel(); | |
257 | int viewColumn = columnModel.getColumnIndexAtX(e.getX()); | |
258 | int column = columnModel.getColumn(viewColumn).getModelIndex(); | |
259 | if (column == 0) { | |
260 | String status = getSortState(column); | |
261 | ||
262 | ||
263 | if (status.equals(ASCENDING)) { | |
264 | status = DESCENDING; | |
265 | } | |
266 | else if (status.equals(DESCENDING)) { | |
267 | status = NOT_SORTED; | |
268 | } | |
269 | else if (status.equals(NOT_SORTED)) { | |
270 | status = ASCENDING; | |
271 | } | |
272 | setSortState(column, status); | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | } |