001: /*
002: * RecentFilesTableModel.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package org.armedbear.j;
022:
023: import java.text.SimpleDateFormat;
024: import java.util.Calendar;
025: import java.util.Date;
026: import java.util.List;
027: import javax.swing.event.TableModelEvent;
028: import javax.swing.table.AbstractTableModel;
029:
030: public final class RecentFilesTableModel extends AbstractTableModel {
031: private static final int NAME = 0;
032: private static final int LOCATION = 1;
033: private static final int LAST_VISIT = 2;
034: private static final int FIRST_VISIT = 3;
035:
036: private static final int ASCENDING = 0;
037: private static final int DESCENDING = 1;
038:
039: private final String[] columnNames = { "Name", "Location",
040: "Last Visit", "First Visit" };
041:
042: private static SimpleDateFormat timeFormat = new SimpleDateFormat(
043: "h:mm a");
044: private static SimpleDateFormat shortDateFormat = new SimpleDateFormat(
045: "EEE h:mm a");
046: private static SimpleDateFormat fullDateFormat = new SimpleDateFormat(
047: "MMM d yyyy h:mm a");
048:
049: private final List data = RecentFiles.getInstance().getEntries();
050:
051: private int[] indexes;
052:
053: private Calendar startOfDay;
054: private Calendar startOfWeek;
055:
056: private int sortColumn = LAST_VISIT;
057: private int sortOrder = DESCENDING;
058:
059: public RecentFilesTableModel() {
060: indexes = new int[data.size()];
061:
062: for (int i = 0; i < indexes.length; i++)
063: indexes[i] = i;
064:
065: startOfDay = Calendar.getInstance();
066: startOfDay.setTime(new Date(System.currentTimeMillis()));
067: startOfDay.set(Calendar.HOUR_OF_DAY, 0);
068: startOfDay.set(Calendar.HOUR, 0);
069: startOfDay.set(Calendar.MINUTE, 0);
070: startOfDay.set(Calendar.SECOND, 0);
071: startOfDay.set(Calendar.MILLISECOND, 0);
072: startOfDay.set(Calendar.AM_PM, 0);
073: startOfWeek = Calendar.getInstance();
074: startOfWeek.setTime(startOfDay.getTime());
075: startOfWeek.add(Calendar.DATE, -6);
076: }
077:
078: public int getColumnCount() {
079: return columnNames.length;
080: }
081:
082: public int getRowCount() {
083: return data.size();
084: }
085:
086: public String getColumnName(int col) {
087: return columnNames[col];
088: }
089:
090: private String format(long date) {
091: Date d = new Date(date);
092:
093: Calendar c = Calendar.getInstance();
094:
095: c.setTime(d);
096:
097: if (c.before(startOfWeek))
098: return fullDateFormat.format(d);
099:
100: if (c.before(startOfDay))
101: return shortDateFormat.format(d);
102:
103: return timeFormat.format(d);
104: }
105:
106: public RecentFilesEntry getEntryAtRow(int row) {
107: int i = indexes[row];
108: return (RecentFilesEntry) data.get(i);
109: }
110:
111: public int getRowForEntry(RecentFilesEntry entry) {
112: for (int row = 0; row < indexes.length; row++) {
113: int i = indexes[row];
114: if (entry == data.get(i))
115: return row;
116: }
117: return -1;
118: }
119:
120: public Object getValueAt(int row, int col) {
121: int i = indexes[row];
122: RecentFilesEntry entry = (RecentFilesEntry) data.get(i);
123: if (entry != null) {
124: switch (col) {
125: case NAME:
126: return entry.name;
127: case LOCATION:
128: return entry.location;
129: case FIRST_VISIT:
130: return format(entry.firstVisit);
131: case LAST_VISIT:
132: return format(entry.lastVisit);
133: }
134: }
135: return null;
136: }
137:
138: public void sortByColumn(int column) {
139: if (column == sortColumn) {
140: // Sorting on same column. Reverse sort order.
141: sortByColumn(column, sortOrder == DESCENDING ? ASCENDING
142: : DESCENDING);
143: } else {
144: // Sorting on a different column. Use default sort order for that column.
145: switch (column) {
146: case NAME:
147: sortByColumn(NAME, ASCENDING);
148: break;
149: case LOCATION:
150: sortByColumn(LOCATION, ASCENDING);
151: break;
152: case FIRST_VISIT:
153: sortByColumn(FIRST_VISIT, DESCENDING);
154: break;
155: case LAST_VISIT:
156: sortByColumn(LAST_VISIT, DESCENDING);
157: break;
158: }
159: }
160: fireTableChanged(new TableModelEvent(this ));
161: }
162:
163: private void sortByColumn(int column, int order) {
164: if (order == DESCENDING) {
165: for (int i = 0; i < getRowCount(); i++) {
166: for (int j = i + 1; j < getRowCount(); j++) {
167: if (compareByColumn(indexes[i], indexes[j], column) < 0)
168: swap(i, j);
169: }
170: }
171: } else {
172: for (int i = 0; i < getRowCount(); i++) {
173: for (int j = i + 1; j < getRowCount(); j++) {
174: if (compareByColumn(indexes[i], indexes[j], column) > 0)
175: swap(i, j);
176: }
177: }
178: }
179: sortColumn = column;
180: sortOrder = order;
181: }
182:
183: private void sortByName() {
184: for (int i = 0; i < getRowCount(); i++) {
185: for (int j = i + 1; j < getRowCount(); j++) {
186: if (compareByColumn(indexes[i], indexes[j], 0) > 0)
187: swap(i, j);
188: }
189: }
190: }
191:
192: private void sortByLocation() {
193: for (int i = 0; i < getRowCount(); i++) {
194: for (int j = i + 1; j < getRowCount(); j++) {
195: if (compareByColumn(indexes[i], indexes[j], 1) > 0)
196: swap(i, j);
197: }
198: }
199: }
200:
201: private void sortByFirstVisit() {
202: for (int i = 0; i < getRowCount(); i++) {
203: for (int j = i + 1; j < getRowCount(); j++) {
204: if (compareByColumn(indexes[i], indexes[j], 2) < 0)
205: swap(i, j);
206: }
207: }
208: }
209:
210: private void sortByLastVisit() {
211: for (int i = 0; i < getRowCount(); i++) {
212: for (int j = i + 1; j < getRowCount(); j++) {
213: if (compareByColumn(indexes[i], indexes[j], 3) < 0)
214: swap(i, j);
215: }
216: }
217: }
218:
219: private int compareByColumn(int i, int j, int column) {
220: RecentFilesEntry entry1 = (RecentFilesEntry) data.get(i);
221: RecentFilesEntry entry2 = (RecentFilesEntry) data.get(j);
222: switch (column) {
223: case NAME:
224: return entry1.name.compareTo(entry2.name);
225: case LOCATION:
226: return entry1.location.compareTo(entry2.location);
227: case FIRST_VISIT:
228: if (entry1.firstVisit < entry2.firstVisit)
229: return -1;
230: if (entry1.firstVisit == entry2.firstVisit)
231: return 0;
232: return 1;
233: case LAST_VISIT:
234: if (entry1.lastVisit < entry2.lastVisit)
235: return -1;
236: if (entry1.lastVisit == entry2.lastVisit)
237: return 0;
238: return 1;
239: default:
240: Debug.assertTrue(false);
241: }
242: return 0;
243: }
244:
245: private void swap(int i, int j) {
246: int tmp = indexes[i];
247: indexes[i] = indexes[j];
248: indexes[j] = tmp;
249: }
250:
251: public Class getColumnClass(int col) {
252: return String.class;
253: }
254: }
|