1 20 21 package org.armedbear.j; 22 23 import java.text.SimpleDateFormat ; 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.List ; 27 import javax.swing.event.TableModelEvent ; 28 import javax.swing.table.AbstractTableModel ; 29 30 public final class RecentFilesTableModel extends AbstractTableModel  31 { 32 private static final int NAME = 0; 33 private static final int LOCATION = 1; 34 private static final int LAST_VISIT = 2; 35 private static final int FIRST_VISIT = 3; 36 37 private static final int ASCENDING = 0; 38 private static final int DESCENDING = 1; 39 40 private final String [] columnNames = 41 {"Name", "Location", "Last Visit", "First Visit"}; 42 43 private static SimpleDateFormat timeFormat = new SimpleDateFormat ("h:mm a"); 44 private static SimpleDateFormat shortDateFormat = 45 new SimpleDateFormat ("EEE h:mm a"); 46 private static SimpleDateFormat fullDateFormat = 47 new SimpleDateFormat ("MMM d yyyy h:mm a"); 48 49 private final List data = RecentFiles.getInstance().getEntries(); 50 51 private int[] indexes; 52 53 private Calendar startOfDay; 54 private Calendar startOfWeek; 55 56 private int sortColumn = LAST_VISIT; 57 private int sortOrder = DESCENDING; 58 59 public RecentFilesTableModel() 60 { 61 indexes = new int[data.size()]; 62 63 for (int i = 0; i < indexes.length; i++) 64 indexes[i] = i; 65 66 startOfDay = Calendar.getInstance(); 67 startOfDay.setTime(new Date (System.currentTimeMillis())); 68 startOfDay.set(Calendar.HOUR_OF_DAY, 0); 69 startOfDay.set(Calendar.HOUR, 0); 70 startOfDay.set(Calendar.MINUTE, 0); 71 startOfDay.set(Calendar.SECOND, 0); 72 startOfDay.set(Calendar.MILLISECOND, 0); 73 startOfDay.set(Calendar.AM_PM, 0); 74 startOfWeek = Calendar.getInstance(); 75 startOfWeek.setTime(startOfDay.getTime()); 76 startOfWeek.add(Calendar.DATE, -6); 77 } 78 79 public int getColumnCount() 80 { 81 return columnNames.length; 82 } 83 84 public int getRowCount() 85 { 86 return data.size(); 87 } 88 89 public String getColumnName(int col) 90 { 91 return columnNames[col]; 92 } 93 94 private String format(long date) 95 { 96 Date d = new Date (date); 97 98 Calendar c = Calendar.getInstance(); 99 100 c.setTime(d); 101 102 if (c.before(startOfWeek)) 103 return fullDateFormat.format(d); 104 105 if (c.before(startOfDay)) 106 return shortDateFormat.format(d); 107 108 return timeFormat.format(d); 109 } 110 111 public RecentFilesEntry getEntryAtRow(int row) 112 { 113 int i = indexes[row]; 114 return (RecentFilesEntry) data.get(i); 115 } 116 117 public int getRowForEntry(RecentFilesEntry entry) 118 { 119 for (int row = 0; row < indexes.length; row ++) { 120 int i = indexes[row]; 121 if (entry == data.get(i)) 122 return row; 123 } 124 return -1; 125 } 126 127 public Object getValueAt(int row, int col) 128 { 129 int i = indexes[row]; 130 RecentFilesEntry entry = (RecentFilesEntry) data.get(i); 131 if (entry != null) { 132 switch (col) { 133 case NAME: 134 return entry.name; 135 case LOCATION: 136 return entry.location; 137 case FIRST_VISIT: 138 return format(entry.firstVisit); 139 case LAST_VISIT: 140 return format(entry.lastVisit); 141 } 142 } 143 return null; 144 } 145 146 public void sortByColumn(int column) 147 { 148 if (column == sortColumn) { 149 sortByColumn(column, sortOrder == DESCENDING ? ASCENDING : DESCENDING); 151 } else { 152 switch (column) { 154 case NAME: 155 sortByColumn(NAME, ASCENDING); 156 break; 157 case LOCATION: 158 sortByColumn(LOCATION, ASCENDING); 159 break; 160 case FIRST_VISIT: 161 sortByColumn(FIRST_VISIT, DESCENDING); 162 break; 163 case LAST_VISIT: 164 sortByColumn(LAST_VISIT, DESCENDING); 165 break; 166 } 167 } 168 fireTableChanged(new TableModelEvent (this)); 169 } 170 171 private void sortByColumn(int column, int order) 172 { 173 if (order == DESCENDING) { 174 for (int i = 0; i < getRowCount(); i++) { 175 for (int j = i + 1; j < getRowCount(); j++) { 176 if (compareByColumn(indexes[i], indexes[j], column) < 0) 177 swap(i, j); 178 } 179 } 180 } else { 181 for (int i = 0; i < getRowCount(); i++) { 182 for (int j = i + 1; j < getRowCount(); j++) { 183 if (compareByColumn(indexes[i], indexes[j], column) > 0) 184 swap(i, j); 185 } 186 } 187 } 188 sortColumn = column; 189 sortOrder = order; 190 } 191 192 private void sortByName() 193 { 194 for (int i = 0; i < getRowCount(); i++) { 195 for (int j = i + 1; j < getRowCount(); j++) { 196 if (compareByColumn(indexes[i], indexes[j], 0) > 0) 197 swap(i, j); 198 } 199 } 200 } 201 202 private void sortByLocation() 203 { 204 for (int i = 0; i < getRowCount(); i++) { 205 for (int j = i + 1; j < getRowCount(); j++) { 206 if (compareByColumn(indexes[i], indexes[j], 1) > 0) 207 swap(i, j); 208 } 209 } 210 } 211 212 private void sortByFirstVisit() 213 { 214 for (int i = 0; i < getRowCount(); i++) { 215 for (int j = i + 1; j < getRowCount(); j++) { 216 if (compareByColumn(indexes[i], indexes[j], 2) < 0) 217 swap(i, j); 218 } 219 } 220 } 221 222 private void sortByLastVisit() 223 { 224 for (int i = 0; i < getRowCount(); i++) { 225 for (int j = i + 1; j < getRowCount(); j++) { 226 if (compareByColumn(indexes[i], indexes[j], 3) < 0) 227 swap(i, j); 228 } 229 } 230 } 231 232 private int compareByColumn(int i, int j, int column) 233 { 234 RecentFilesEntry entry1 = (RecentFilesEntry) data.get(i); 235 RecentFilesEntry entry2 = (RecentFilesEntry) data.get(j); 236 switch (column) { 237 case NAME: 238 return entry1.name.compareTo(entry2.name); 239 case LOCATION: 240 return entry1.location.compareTo(entry2.location); 241 case FIRST_VISIT: 242 if (entry1.firstVisit < entry2.firstVisit) 243 return -1; 244 if (entry1.firstVisit == entry2.firstVisit) 245 return 0; 246 return 1; 247 case LAST_VISIT: 248 if (entry1.lastVisit < entry2.lastVisit) 249 return -1; 250 if (entry1.lastVisit == entry2.lastVisit) 251 return 0; 252 return 1; 253 default: 254 Debug.assertTrue(false); 255 } 256 return 0; 257 } 258 259 private void swap(int i, int j) 260 { 261 int tmp = indexes[i]; 262 indexes[i] = indexes[j]; 263 indexes[j] = tmp; 264 } 265 266 public Class getColumnClass(int col) 267 { 268 return String .class; 269 } 270 } 271 | Popular Tags |