Source Code Cross Referenced for RecentFilesTableModel.java in  » IDE » J » org » armedbear » j » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » J » org.armedbear.j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.