KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > todolist > gui > TodolistItemTable


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Jonathan Riboux <jonathan.riboux@wanadoo.Fr>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.applications.todolist.gui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.event.MouseAdapter JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Comparator JavaDoc;
31
32 import javax.swing.DefaultListSelectionModel JavaDoc;
33 import javax.swing.JTable JavaDoc;
34 import javax.swing.JTextArea JavaDoc;
35 import javax.swing.event.ListSelectionListener JavaDoc;
36 import javax.swing.table.AbstractTableModel JavaDoc;
37 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
38 import javax.swing.table.TableColumnModel JavaDoc;
39
40 import org.lucane.applications.todolist.TodolistItem;
41
42 public class TodolistItemTable extends JTable JavaDoc {
43     public TodolistItemTable() {
44         super();
45
46         setDefaultRenderer(String JavaDoc.class, new ColoredCellRenderer());
47         
48         setModel(new TodolistItemTableModel());
49         setSelectionModel(new DefaultListSelectionModel JavaDoc());
50         
51         for (int i=1; i<=6; i++) {
52             getColumnModel().getColumn(i).setMinWidth(70);
53             getColumnModel().getColumn(i).setMaxWidth(70);
54             getColumnModel().getColumn(i).setResizable(false);
55         }
56         setRowHeight(getRowHeight()*2);
57         getTableHeader().addMouseListener(new MouseAdapter JavaDoc() {
58             public void mouseClicked(MouseEvent JavaDoc e) {
59                 TableColumnModel JavaDoc columnModel = getColumnModel();
60                 int col = columnModel.getColumnIndexAtX(e.getX());
61                 if(e.getClickCount()==1 && col>=0) {
62                     ((TodolistItemTableModel)getModel()).sort(col);
63                 }
64             }
65         });
66     }
67
68     public static void setColumnsNames(String JavaDoc [] names) {
69         TodolistItemTableModel.setColumnsNames(names);
70     }
71     
72     public void addListSelectionListener(ListSelectionListener JavaDoc lsl) {
73         getSelectionModel().addListSelectionListener(lsl);
74     }
75     
76     public TodolistItem getSelectedTodolistItem() {
77         int selectedIndex = getSelectionModel().getMinSelectionIndex();
78         if (selectedIndex>=0)
79             return (TodolistItem)((TodolistItemTableModel)getModel()).getValueAt(selectedIndex);
80         return null;
81     }
82
83     public void add(TodolistItem tli) {
84         ((TodolistItemTableModel)getModel()).add(tli);
85     }
86     
87     public void remove(TodolistItem tli) {
88         ((TodolistItemTableModel)getModel()).remove(tli);
89     }
90     
91     public void clear() {
92         ((TodolistItemTableModel)getModel()).clear();
93     }
94     
95 }
96
97 class TodolistItemTableModel extends AbstractTableModel JavaDoc {
98
99     private static String JavaDoc[] columnsNames = {"Name", "Priority", "Complete", "Estimated start", "Estimated end", "real start", "Real end"};
100     private static final int[] COLUMNS_SORT = {TodolistItemsSorter.NAME, TodolistItemsSorter.PRIORITY, TodolistItemsSorter.COMPLETED, TodolistItemsSorter.ESTIMATED_START, TodolistItemsSorter.ESTIMATED_END, TodolistItemsSorter.REAL_START, TodolistItemsSorter.REAL_END};
101     private ArrayList JavaDoc items;
102     private Comparator JavaDoc comparator;
103     
104     private int sortedCol = -1;
105     private int sortDirection = TodolistItemsSorter.ASC;
106     
107     public TodolistItemTableModel() {
108         items = new ArrayList JavaDoc();
109         comparator = new TodolistItemsSorter(TodolistItemsSorter.NAME, TodolistItemsSorter.ASC);
110     }
111
112     public static void setColumnsNames(String JavaDoc [] names) {
113         columnsNames = names;
114     }
115
116     public void setComparator(Comparator JavaDoc comparator) {
117         this.comparator = comparator;
118     }
119
120     public void sort(int columnIndex) {
121         if (columnIndex==sortedCol) {
122             sortDirection = sortDirection == TodolistItemsSorter.ASC
123                     ? TodolistItemsSorter.DESC
124                     : TodolistItemsSorter.ASC;
125         } else {
126             sortedCol = columnIndex;
127             sortDirection = TodolistItemsSorter.ASC;
128         }
129         
130         comparator = new TodolistItemsSorter(COLUMNS_SORT[sortedCol], sortDirection);
131         Collections.sort(items, comparator);
132         fireTableDataChanged();
133     }
134     
135     public void add(TodolistItem item) {
136         items.add(item);
137         Collections.sort(items, comparator);
138         fireTableDataChanged();
139     }
140     
141     public void remove(TodolistItem item) {
142         items.remove(item);
143         fireTableDataChanged();
144     }
145     
146     public Class JavaDoc getColumnClass(int col) {
147         return String JavaDoc.class;
148     }
149
150     public String JavaDoc getColumnName(int col) {
151         return columnsNames[col];
152     }
153
154     public boolean isCellEditable(int row, int col) {
155         return false;
156     }
157
158     public void setValueAt(Object JavaDoc value, int row, int col) {
159         super.setValueAt(value, row, col);
160     }
161     
162     public int getColumnCount() {
163         return columnsNames.length;
164     }
165
166     public int getRowCount() {
167         return items.size();
168     }
169
170     public Object JavaDoc getValueAt(int row, int col) {
171         TodolistItem item = (TodolistItem)items.get(row);
172
173         DateFormat JavaDoc df1 = DateFormat.getDateInstance(DateFormat.SHORT);
174         DateFormat JavaDoc df2 = DateFormat.getTimeInstance(DateFormat.SHORT);
175
176         switch (col) {
177             case 0 :
178                 return item.getName();
179             case 1 :
180                 return TodolistItem.getPriorityLabels()[item.getPriority()];
181             case 2 :
182                 return TodolistItem.getCompleteLabels()[item.isCompleted()?1:0];
183             case 3 :
184                 if (item.getEstimatedStartDate()==null) return "";
185                 return "<HTML>"+df1.format(item.getEstimatedStartDate())+"<BR>"+df2.format(item.getEstimatedStartDate())+"</HTML>";
186             case 4 :
187                 if (item.getEstimatedEndDate()==null) return "";
188                 return "<HTML>"+df1.format(item.getEstimatedEndDate())+"<BR>"+df2.format(item.getEstimatedEndDate())+"</HTML>";
189             case 5 :
190                 if (item.getStartDate()==null) return "";
191                 return "<HTML>"+df1.format(item.getStartDate())+"<BR>"+df2.format(item.getStartDate())+"</HTML>";
192             case 6 :
193                 if (item.getEndDate()==null) return "";
194                 return "<HTML>"+df1.format(item.getEndDate())+"<BR>"+df2.format(item.getEndDate())+"</HTML>";
195         }
196         return "";
197     }
198
199     public Object JavaDoc getValueAt(int row) {
200         try {
201             return ((TodolistItem)items.get(row));
202         } catch (IndexOutOfBoundsException JavaDoc e) {
203             return null;
204         }
205     }
206     
207     public void clear() {
208         items.clear();
209         fireTableRowsDeleted(0, 0);
210     }
211 }
212
213 class TodolistItemsSorter implements Comparator JavaDoc {
214
215     public static final int ASC = 1;
216     public static final int DESC = -1;
217
218     public static final int NAME = 0;
219     public static final int PRIORITY = 1;
220     public static final int COMPLETED = 2;
221     public static final int ESTIMATED_START = 3;
222     public static final int ESTIMATED_END = 4;
223     public static final int REAL_START = 5;
224     public static final int REAL_END = 6;
225     
226     private int sortBy = NAME;
227     private int direction = ASC;
228     
229     public TodolistItemsSorter(int sortBy, int direction) {
230         this.sortBy = sortBy;
231         this.direction = direction;
232     }
233     
234     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
235         TodolistItem tli1 = (TodolistItem) o1;
236         TodolistItem tli2 = (TodolistItem) o2;
237         int res = 0;
238         switch (sortBy) {
239             case NAME :
240                 res = direction*(tli1.getName().compareTo(tli2.getName()));
241                 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
242                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
243                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
244                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
245                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
246                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
247                 break;
248             case PRIORITY :
249                 res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
250                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
251                 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
252                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
253                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
254                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
255                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
256                 break;
257             case COMPLETED :
258                 res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
259                 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
260                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
261                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
262                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
263                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
264                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
265                 break;
266             case REAL_START :
267                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
268                 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
269                 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
270                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
271                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
272                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
273                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
274                 break;
275             case REAL_END :
276                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
277                 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
278                 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
279                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
280                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
281                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
282                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
283                 break;
284             case ESTIMATED_START :
285                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
286                 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
287                 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
288                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
289                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
290                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
291                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
292                 break;
293             case ESTIMATED_END :
294                 if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
295                 if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
296                 if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
297                 if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
298                 if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
299                 if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
300                 if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
301                 break;
302         }
303         return res;
304     }
305     private static int compareDates (Date JavaDoc d1, Date JavaDoc d2) {
306         if (d1 == null && d2 == null) return 0;
307         if (d1 == null) return 1;
308         if (d2 == null) return -1;
309         return d1.compareTo(d2);
310     }
311 }
312
313 class ColoredCellRenderer extends DefaultTableCellRenderer JavaDoc {
314     
315     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
316             boolean isSelected, boolean hasFocus, int row, int column) {
317         
318         Component JavaDoc cmp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
319         if (column>=3) {
320             Component JavaDoc cmp2 = new JTextArea JavaDoc(value.toString());
321         }
322         if (!isSelected) {
323             TodolistItem tli = (TodolistItem) ((TodolistItemTableModel)table.getModel()).getValueAt(row);
324             if (!tli.isCompleted()) {
325                 switch (tli.getPriority()) {
326                     case TodolistItem.PRIORITY_LOW :
327                         cmp.setBackground(new Color JavaDoc(223, 255, 223));
328                         break;
329                     case TodolistItem.PRIORITY_MEDIUM :
330                         cmp.setBackground(new Color JavaDoc(255, 255, 223));
331                         break;
332                     case TodolistItem.PRIORITY_HIGH :
333                         cmp.setBackground(new Color JavaDoc(255, 223, 223));
334                         break;
335                     default :
336                         cmp.setBackground(Color.white);
337                         break;
338                 }
339             } else {
340                 cmp.setBackground(new Color JavaDoc(223, 223, 223));
341             }
342
343             /*if (column==3 || column==4) {
344                 Date d1 = tli.getEstimatedStartDate();
345                 Date d2 = tli.getEstimatedEndDate();
346                 Date now = new Date();
347                 if (d1!=null && d2!=null && d1.compareTo(now)>=0 && d2.compareTo(now)<=0) {
348                     cmp.setFont(cmp.getFont().deriveFont(Font.BOLD));
349                     cmp.setBackground(Color.RED);
350                 }
351             }
352             if (column==5 || column==6) {
353                 Date d1 = tli.getStartDate();
354                 Date d2 = tli.getEndDate();
355                 Date now = new Date();
356                 if (d1!=null && d2!=null && d1.compareTo(now)>=0 && d2.compareTo(now)<=0)
357                     cmp.setFont(cmp.getFont().deriveFont(Font.BOLD));
358             }*/

359             
360             return cmp;
361         } else {
362             return cmp;
363         }
364     }
365 }
366
Popular Tags