1 19 20 package org.netbeans.modules.web.monitor.client; 21 22 import java.util.*; 23 import javax.swing.table.TableModel ; 24 import javax.swing.table.AbstractTableModel ; 25 import javax.swing.event.TableModelEvent ; 26 import javax.swing.event.TableModelListener ; 27 import org.openide.util.NbBundle; 28 29 38 public class DisplayTableSorter extends AbstractTableModel implements 39 TableModelListener { 40 41 private int[] index = null; 42 private int sort = DisplayTable.NEUTRAL; 43 protected TableModel model; 44 45 private static final boolean debug = false; 46 47 public DisplayTableSorter(TableModel model) { 48 setModel(model); 49 resetIndices(); 50 } 51 52 public void sort(int sort) { 56 57 this.sort = sort; 58 if(debug) log(" sort: " + String.valueOf(this.sort)); if(sort == DisplayTable.NEUTRAL) { 60 tableChanged(new TableModelEvent (this)); 61 return; 62 } 63 64 if(debug) { 65 StringBuffer buf = 66 new StringBuffer ("Order of indices before sorting: "); for(int i=0; i<index.length; ++i) { 68 buf.append(String.valueOf(index[i])); 69 buf.append(", "); } 71 log(buf.toString()); 72 } 73 74 resetIndices(); 75 sort((int[])index.clone(), index, 0, index.length); 76 77 if(debug) { 78 StringBuffer buf = 79 new StringBuffer ("Order of indices after sorting: "); for(int i=0; i<index.length; ++i) { 81 buf.append(String.valueOf(index[i])); 82 buf.append(", "); } 84 log(buf.toString()); 85 } 86 87 tableChanged(new TableModelEvent (this)); 88 } 89 90 public void sort(int[] from, int[] to, int low, int high) { 91 92 if (high - low < 2) { 93 return; 94 } 95 96 int middle = (low + high)/2; 97 sort(to, from, low, middle); 98 sort(to, from, middle, high); 99 100 int p = low; 101 int q = middle; 102 103 if (high - low >= 4 && compare(from[middle-1], from[middle]) <= 0) { 104 for (int i = low; i < high; i++) { 105 to[i] = from[i]; 106 } 107 return; 108 } 109 110 for (int i = low; i < high; i++) { 111 if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) { 112 to[i] = from[p++]; 113 } 114 else { 115 to[i] = from[q++]; 116 } 117 } 118 } 119 120 public int compare(int row1, int row2) { 121 122 123 String str1 = (String )model.getValueAt(row1, 0); 124 String str2 = (String )model.getValueAt(row2, 0); 125 126 127 if(debug) log(" comparing " + row1 + ":" + str1 + " and " + row2 + ":" + str2); 130 int result = str1.compareTo(str2); 131 if(result == 0) return 0; 132 boolean ascending = (sort == DisplayTable.A2Z); 133 return ascending ? result : -result; 134 } 135 136 137 138 public void resetIndices() { 139 int rowCount = getRowCount(); 140 141 if(debug) log("rows=" + String.valueOf(rowCount)); 143 144 index = new int[rowCount]; 147 148 for (int row = 0; row < rowCount; row++) { 150 index[row] = row; 151 } 152 } 153 154 155 public TableModel getModel() { 156 return model; 157 } 158 159 public void setModel(TableModel model) { 160 this.model = model; 161 model.addTableModelListener(this); 162 } 163 164 167 public Object getValueAt(int aRow, int aColumn) { 168 if(sort == DisplayTable.NEUTRAL) return model.getValueAt(aRow, 169 aColumn); 170 171 if(debug) { 172 String value = (String )(model.getValueAt(index[aRow], aColumn)); 173 value = value + String.valueOf(aRow) + "+" + index[aRow]; return value; 175 } 176 return model.getValueAt(index[aRow], aColumn); 177 } 178 179 public void setValueAt(Object aValue, int aRow, int aColumn) { 180 if(sort == DisplayTable.NEUTRAL) { 181 model.setValueAt(aValue, aRow, aColumn); 182 return; 183 } 184 model.setValueAt(aValue, index[aRow], aColumn); 185 } 186 187 public int getRowCount() { 188 return (model == null) ? 0 : model.getRowCount(); 189 } 190 191 public int getColumnCount() { 192 return (model == null) ? 0 : model.getColumnCount(); 193 } 194 195 public String getColumnName(int aColumn) { 196 return model.getColumnName(aColumn); 197 } 198 199 public Class getColumnClass(int aColumn) { 200 return model.getColumnClass(aColumn); 201 } 202 203 public boolean isCellEditable(int row, int column) { 204 return model.isCellEditable(row, column); 205 } 206 207 public void tableChanged(TableModelEvent e) { 208 fireTableChanged(e); 209 } 210 211 private void log(String s) { 212 System.out.println("DisplayTableSorter::" + s); } 214 215 } | Popular Tags |