]>
Commit | Line | Data |
---|---|---|
d78abb5a | 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 | **/ | |
add40ab2 | 15 | package org.tianocore.frameworkwizard.platform.ui; |
16 | ||
add40ab2 | 17 | import java.awt.event.*; |
18 | import java.util.*; | |
add40ab2 | 19 | |
add40ab2 | 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 | ||
d78abb5a | 31 | protected DefaultTableModel tableModel; |
32 | private TableRow[] rowInView; | |
33 | private int[] viewPos; | |
add40ab2 | 34 | |
d78abb5a | 35 | public static final String DESCENDING = "down"; |
36 | public static final String NOT_SORTED = "none"; | |
37 | public static final String ASCENDING = "up"; | |
add40ab2 | 38 | |
39 | private JTableHeader tableHeader; | |
d78abb5a | 40 | private MouseListener mouseListener = new MouseHandler(); |
41 | private TableModelListener tableModelListener = new TableModelHandler(); | |
add40ab2 | 42 | |
d78abb5a | 43 | private HashMap<Integer, String> sortingOrders = new HashMap<Integer, String>(); |
add40ab2 | 44 | |
d78abb5a | 45 | public TableSorter(DefaultTableModel tableModel) { |
add40ab2 | 46 | setTableModel(tableModel); |
47 | } | |
48 | ||
49 | ||
d78abb5a | 50 | private void resetSortState() { |
51 | rowInView = null; | |
52 | viewPos = null; | |
add40ab2 | 53 | } |
54 | ||
d78abb5a | 55 | public DefaultTableModel getTableModel() { |
add40ab2 | 56 | return tableModel; |
57 | } | |
58 | ||
d78abb5a | 59 | public void setTableModel(DefaultTableModel dtm) { |
60 | if (tableModel != null) { | |
61 | tableModel.removeTableModelListener(tableModelListener); | |
add40ab2 | 62 | } |
63 | ||
d78abb5a | 64 | tableModel = dtm; |
65 | if (tableModel != null) { | |
66 | tableModel.addTableModelListener(tableModelListener); | |
add40ab2 | 67 | } |
68 | ||
d78abb5a | 69 | resetSortState(); |
add40ab2 | 70 | fireTableStructureChanged(); |
71 | } | |
72 | ||
73 | public JTableHeader getTableHeader() { | |
74 | return tableHeader; | |
75 | } | |
76 | ||
d78abb5a | 77 | public void setTableHeader(JTableHeader th) { |
78 | if (tableHeader != null) { | |
79 | tableHeader.removeMouseListener(mouseListener); | |
add40ab2 | 80 | } |
d78abb5a | 81 | tableHeader = th; |
82 | if (tableHeader != null) { | |
83 | tableHeader.addMouseListener(mouseListener); | |
84 | ||
add40ab2 | 85 | } |
86 | } | |
87 | ||
d78abb5a | 88 | private String getSortState(int column) { |
89 | ||
90 | Integer i = new Integer(column); | |
91 | if (sortingOrders.get(i) != null) { | |
92 | return sortingOrders.get(i); | |
add40ab2 | 93 | } |
d78abb5a | 94 | return NOT_SORTED; |
add40ab2 | 95 | } |
96 | ||
d78abb5a | 97 | private void sortStateChanged() { |
98 | resetSortState(); | |
add40ab2 | 99 | fireTableDataChanged(); |
add40ab2 | 100 | |
add40ab2 | 101 | } |
102 | ||
d78abb5a | 103 | public void setSortState(int column, String status) { |
104 | Integer i = new Integer(column); | |
105 | sortingOrders.put(i, status); | |
106 | sortStateChanged(); | |
add40ab2 | 107 | } |
108 | ||
d78abb5a | 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; | |
add40ab2 | 117 | } |
118 | ||
d78abb5a | 119 | if (sortingOrders.size() != 0) { |
120 | Arrays.sort(rowInView); | |
add40ab2 | 121 | } |
122 | } | |
d78abb5a | 123 | return rowInView; |
add40ab2 | 124 | } |
125 | ||
d78abb5a | 126 | public int getModelRowIndex(int viewIndex) { |
127 | TableRow[] rArray = getSortedViewRows(); | |
128 | return rArray[viewIndex].modelIndex; | |
add40ab2 | 129 | } |
130 | ||
d78abb5a | 131 | public int[] getViewIndexArray() { |
132 | if (viewPos == null) { | |
133 | int n = getSortedViewRows().length; | |
134 | viewPos = new int[n]; | |
add40ab2 | 135 | for (int i = 0; i < n; i++) { |
d78abb5a | 136 | viewPos[getModelRowIndex(i)] = i; |
add40ab2 | 137 | } |
138 | } | |
d78abb5a | 139 | return viewPos; |
add40ab2 | 140 | } |
141 | ||
d78abb5a | 142 | |
add40ab2 | 143 | |
144 | public int getRowCount() { | |
d78abb5a | 145 | if (tableModel == null) { |
146 | return 0; | |
147 | } | |
148 | return tableModel.getRowCount(); | |
add40ab2 | 149 | } |
150 | ||
d78abb5a | 151 | public String getColumnName(int col) { |
152 | return tableModel.getColumnName(col); | |
add40ab2 | 153 | } |
154 | ||
d78abb5a | 155 | public int getColumnCount() { |
156 | if (tableModel == null) { | |
157 | return 0; | |
158 | } | |
159 | return tableModel.getColumnCount(); | |
add40ab2 | 160 | } |
161 | ||
d78abb5a | 162 | public Class<?> getColumnClass(int col) { |
163 | return tableModel.getColumnClass(col); | |
add40ab2 | 164 | } |
165 | ||
d78abb5a | 166 | public boolean isCellEditable(int row, int col) { |
167 | int modelIndex = getModelRowIndex(row); | |
168 | return tableModel.isCellEditable(modelIndex, col); | |
add40ab2 | 169 | } |
170 | ||
d78abb5a | 171 | public Object getValueAt(int row, int col) { |
172 | int modelIndex = getModelRowIndex(row); | |
173 | return tableModel.getValueAt(modelIndex, col); | |
add40ab2 | 174 | } |
175 | ||
d78abb5a | 176 | public void setValueAt(Object val, int row, int col) { |
177 | int modelIndex = getModelRowIndex(row); | |
178 | tableModel.setValueAt(val, modelIndex, col); | |
add40ab2 | 179 | } |
180 | ||
181 | // Helper classes | |
182 | ||
d78abb5a | 183 | private class TableRow implements Comparable { |
add40ab2 | 184 | private int modelIndex; |
185 | ||
d78abb5a | 186 | public TableRow(int index) { |
add40ab2 | 187 | this.modelIndex = index; |
188 | } | |
189 | ||
190 | public int compareTo(Object o) { | |
191 | int row1 = modelIndex; | |
d78abb5a | 192 | int row2 = ((TableRow) o).modelIndex; |
193 | ||
194 | Iterator<Integer> mapIter = sortingOrders.keySet().iterator(); | |
add40ab2 | 195 | |
d78abb5a | 196 | while (mapIter.hasNext()) { |
197 | ||
198 | Integer column = mapIter.next(); | |
add40ab2 | 199 | Object o1 = tableModel.getValueAt(row1, column); |
200 | Object o2 = tableModel.getValueAt(row2, column); | |
201 | ||
202 | int comparison = 0; | |
add40ab2 | 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) { | |
d78abb5a | 213 | if (getSortState(column.intValue()).equals(DESCENDING)) { |
214 | return -comparison; | |
215 | } | |
216 | return comparison; | |
add40ab2 | 217 | } |
218 | } | |
219 | return 0; | |
220 | } | |
221 | } | |
222 | ||
223 | private class TableModelHandler implements TableModelListener { | |
224 | public void tableChanged(TableModelEvent e) { | |
d78abb5a | 225 | if (sortingOrders.size() != 0) { |
226 | resetSortState(); | |
add40ab2 | 227 | fireTableChanged(e); |
228 | return; | |
229 | } | |
add40ab2 | 230 | if (e.getFirstRow() == TableModelEvent.HEADER_ROW) { |
d78abb5a | 231 | |
add40ab2 | 232 | fireTableChanged(e); |
233 | return; | |
234 | } | |
add40ab2 | 235 | int column = e.getColumn(); |
236 | if (e.getFirstRow() == e.getLastRow() | |
237 | && column != TableModelEvent.ALL_COLUMNS | |
d78abb5a | 238 | && getSortState(column).equals(NOT_SORTED) |
239 | && viewPos != null) { | |
240 | int viewIndex = getViewIndexArray()[e.getFirstRow()]; | |
add40ab2 | 241 | fireTableChanged(new TableModelEvent(TableSorter.this, |
242 | viewIndex, viewIndex, | |
243 | column, e.getType())); | |
244 | return; | |
245 | } | |
246 | ||
d78abb5a | 247 | resetSortState(); |
add40ab2 | 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(); | |
d78abb5a | 259 | if (column == 0) { |
260 | String status = getSortState(column); | |
261 | ||
add40ab2 | 262 | |
d78abb5a | 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); | |
add40ab2 | 273 | } |
add40ab2 | 274 | } |
275 | } | |
276 | ||
add40ab2 | 277 | } |