KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > UserTasksTreeTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import javax.swing.tree.TreeNode JavaDoc;
29
30 import javax.swing.tree.TreePath JavaDoc;
31
32 import org.netbeans.modules.tasklist.core.filter.Filter;
33 import org.netbeans.modules.tasklist.usertasks.treetable.AdvancedTreeTableNode;
34 import org.netbeans.modules.tasklist.usertasks.treetable.BooleanComparator;
35 import org.netbeans.modules.tasklist.usertasks.treetable.DefaultMutableTreeTableNode;
36 import org.netbeans.modules.tasklist.usertasks.treetable.DefaultTreeTableModel;
37 import org.netbeans.modules.tasklist.usertasks.treetable.FilterIntf;
38 import org.netbeans.modules.tasklist.usertasks.treetable.NotComparator;
39 import org.netbeans.modules.tasklist.core.table.SortingModel;
40 import org.netbeans.modules.tasklist.usertasks.treetable.StringIgnoreCaseComparator;
41 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
42 import org.openide.text.Line;
43 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
44 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
45
46 /**
47  * TT model for user tasks
48  *
49  * @author tl
50  */

51 public class UserTasksTreeTableModel extends DefaultTreeTableModel {
52     public static final int SUMMARY = 0;
53     public static final int PRIORITY = 1;
54     public static final int DONE = 2;
55     public static final int PERCENT_COMPLETE = 3;
56     public static final int EFFORT = 4;
57     public static final int REMAINING_EFFORT = 5;
58     public static final int SPENT_TIME = 6;
59     public static final int DETAILS = 7;
60     public static final int FILE_BASE_NAME = 8;
61     public static final int LINE_NUMBER = 9;
62     public static final int CATEGORY = 10;
63     public static final int CREATED = 11;
64     public static final int LAST_EDITED = 12;
65     public static final int COMPLETED_DATE = 13;
66     public static final int DUE_DATE = 14;
67     public static final int OWNER = 15;
68     public static final int START = 16;
69     public static final int SPENT_TIME_TODAY = 17;
70
71     /**
72      * Comparator for due dates.
73      */

74     private static class DueDateComparator implements Comparator JavaDoc<Date JavaDoc> {
75         public int compare(Date JavaDoc o1, Date JavaDoc o2) {
76             if (o1 == null && o2 == null)
77                 return 0;
78             if (o1 == null)
79                 return 1;
80             if (o2 == null)
81                 return -1;
82             return o1.compareTo(o2);
83         }
84     }
85     
86     /**
87      * Compares to java.lang.Strings. "" and null is bigger than everything
88      * else.
89      */

90     private static class TextComparator implements Comparator JavaDoc<String JavaDoc> {
91         public int compare(String JavaDoc f1, String JavaDoc f2) {
92             boolean empty1 = f1 == null || f1.length() == 0;
93             boolean empty2 = f2 == null || f2.length() == 0;
94             
95             if (empty1 && empty2)
96                 return 0;
97             if (empty1)
98                 return 1;
99             if (empty2)
100                 return -1;
101             return f1.compareToIgnoreCase(f2);
102         }
103     }
104     
105     /**
106      * Compares lines of 2 errors
107      */

108     public static class LinesComparator implements Comparator JavaDoc {
109         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
110             Line f1 = (Line) o1;
111             Line f2 = (Line) o2;
112             if (f1 == null && f2 == null)
113                 return 0;
114             if (f1 == null)
115                 return -1;
116             if (f2 == null)
117                 return 1;
118             return f1.getLineNumber() - f2.getLineNumber();
119         }
120     }
121     
122     /**
123      * Comparator for the "effort" column
124      */

125     private static class EffortComparator implements Comparator JavaDoc {
126         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
127             if (o1 == null && o2 == null)
128                 return 0;
129             if (o1 == null)
130                 return -1;
131             if (o2 == null)
132                 return 1;
133             UserTask ut1 = (UserTask) o1;
134             UserTask ut2 = (UserTask) o2;
135             return ut1.getEffort() - ut2.getEffort();
136         }
137     }
138
139     /**
140      * Comparator for the "category" column
141      */

142     private static class CategoryComparator implements Comparator JavaDoc {
143         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
144             if (o1 == null && o2 == null)
145                 return 0;
146             if (o1 == null)
147                 return -1;
148             if (o2 == null)
149                 return 1;
150             UserTask ut1 = (UserTask) o1;
151             UserTask ut2 = (UserTask) o2;
152             String JavaDoc cat1 = ut1.getCategory();
153             String JavaDoc cat2 = ut2.getCategory();
154             if (cat1.length() == 0 && cat2.length() == 0)
155                 return 0;
156             if (cat1.length() == 0)
157                 return 1;
158             if (cat2.length() == 0)
159                 return -1;
160             return cat1.compareToIgnoreCase(cat2);
161         }
162     }
163
164     /**
165      * Comparator for the "owner" column
166      */

167     private static class OwnerComparator implements Comparator JavaDoc {
168         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
169             if (o1 == null && o2 == null)
170                 return 0;
171             if (o1 == null)
172                 return -1;
173             if (o2 == null)
174                 return 1;
175             UserTask ut1 = (UserTask) o1;
176             UserTask ut2 = (UserTask) o2;
177             String JavaDoc owner1 = ut1.getOwner();
178             String JavaDoc owner2 = ut2.getOwner();
179             if (owner1.length() == 0 && owner2.length() == 0)
180                 return 0;
181             if (owner1.length() == 0)
182                 return 1;
183             if (owner2.length() == 0)
184                 return -1;
185             return owner1.compareToIgnoreCase(owner2);
186         }
187     }
188
189     
190     public static final String JavaDoc[] COLUMN_PROPERTIES = {
191         "summary", // NOI18N
192
"priority", // NOI18N
193
"done", // NOI18N
194
"percentComplete", // NOI18N
195
"effort", // NOI18N
196
"remainingEffort", // NOI18N
197
"spentTime", // NOI18N
198
"details", // NOI18N
199
"file", // NOI18N
200
"line", // NOI18N
201
"category", // NOI18N
202
"created", // NOI18N
203
"edited", // NOI18N
204
"completedDate", // NOI18N
205
"due", // NOI18N
206
"owner", // NOI18N
207
"start", // NOI18N
208
"spentTimeToday" // NOI18N
209
};
210     
211     private static final Comparator JavaDoc[] COMPARATORS = {
212         new StringIgnoreCaseComparator(),
213         new PriorityComparator(),
214         new NotComparator(new BooleanComparator()),
215         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
216         new NotComparator(new EffortComparator()),
217         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
218         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
219         new TextComparator(),
220         new TextComparator(),
221         SortingModel.DEFAULT_COMPARATOR,
222         new CategoryComparator(),
223         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
224         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
225         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
226         new DueDateComparator(),
227         new OwnerComparator(),
228         new NotComparator(SortingModel.DEFAULT_COMPARATOR),
229         new NotComparator(SortingModel.DEFAULT_COMPARATOR)
230     };
231     
232     private static final Class JavaDoc[] COLUMN_CLASS = {
233         null,
234         Integer JavaDoc.class,
235         Boolean JavaDoc.class,
236         Integer JavaDoc.class,
237         UserTask.class,
238         Integer JavaDoc.class,
239         Integer JavaDoc.class,
240         String JavaDoc.class,
241         String JavaDoc.class,
242         Integer JavaDoc.class,
243         UserTask.class,
244         String JavaDoc.class,
245         Long JavaDoc.class,
246         Long JavaDoc.class,
247         Long JavaDoc.class,
248         String JavaDoc.class,
249         Date JavaDoc.class,
250         Integer JavaDoc.class
251     };
252     
253     private static final String JavaDoc[] COLUMNS = {
254         org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnSummary"), // NOI18N
255
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnPriority"), // NOI18N
256
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnDone"), // NOI18N
257
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnPercentComplete"), // NOI18N
258
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnEffort"), // NOI18N
259
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnRemEffort"), // NOI18N
260
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnSpentTime"), // NOI18N
261
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnDetails"), // NOI18N
262
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnFile"), // NOI18N
263
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnLine"), // NOI18N
264
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnCategory"), // NOI18N
265
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnCreated"), // NOI18N
266
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnEdited"), // NOI18N
267
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnCompletedDate"), // NOI18N
268
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnDue"), // NOI18N
269
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnOwner"), // NOI18N
270
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnStart"), // NOI18N
271
org.openide.util.NbBundle.getMessage(UserTasksTreeTableModel.class, "ColumnSpentTimeToday") // NOI18N
272
};
273             
274     private UserTaskList utl;
275     
276     /**
277      * Creates a new instance of UserTasksTreeTableModel
278      *
279      * @param root root node
280      * @param sm a sorting model
281      * @param filter a filter
282      */

283     public UserTasksTreeTableModel(UserTaskList utl, SortingModel sm,
284     final Filter filter) {
285         super(null, COLUMNS);
286         this.utl = utl;
287         for (int i = 0; i < COMPARATORS.length; i++) {
288             sm.setColumnComparator(i, COMPARATORS[i]);
289         }
290         
291         FilterIntf fi = null;
292         if (filter != null) {
293             fi = new FilterIntf() {
294                 public boolean accept(Object JavaDoc obj) {
295                     if (obj instanceof UserTask) {
296                         return filter.accept((UserTask) obj);
297                     } else {
298                         return true;
299                     }
300                 }
301             };
302         }
303         this.root = new UserTaskListTreeTableNode(fi, this, utl, null);
304         //this.root = new UTListDependenciesTreeTableNode(fi, this, utl, null);
305
}
306     
307     /**
308      * Should be called after this model was remove from a TreeTable.
309      * Destroys all nodes
310      */

311     public void destroy() {
312         ((AdvancedTreeTableNode) root).destroy();
313     }
314     
315     /**
316      * Returns the user task list associated with this model
317      *
318      * @return user task list
319      */

320     public UserTaskList getUserTaskList() {
321         return utl;
322     }
323     
324     /**
325      * Finds the path to the specified user task
326      *
327      * @param a user task from this model
328      * @return path to the task or null
329      */

330     public TreePath JavaDoc findPathTo(UserTask ut) {
331         List JavaDoc<UserTask> path = new ArrayList JavaDoc<UserTask>();
332         while (ut != null) {
333             path.add(0, ut);
334             ut = ut.getParent();
335         }
336         List JavaDoc<TreeNode JavaDoc> retp = new ArrayList JavaDoc<TreeNode JavaDoc>();
337         retp.add((TreeNode JavaDoc) getRoot());
338         
339         DefaultMutableTreeTableNode n = (DefaultMutableTreeTableNode) getRoot();
340         for(int i = 0; i < path.size(); i++) {
341             Object JavaDoc obj = path.get(i);
342             boolean found = false;
343             for (int j = 0; j < n.getChildCount(); j++) {
344                 if (((DefaultMutableTreeTableNode) n.getChildAt(j)).
345                     getUserObject() == obj) {
346                     found = true;
347                     retp.add(n);
348                     break;
349                 }
350             }
351             if (!found)
352                 return null;
353         }
354         
355         return new TreePath JavaDoc(retp.toArray());
356     }
357     
358     public Class JavaDoc getColumnClass(int column) {
359         if (column == 0)
360             return super.getColumnClass(0);
361         return COLUMN_CLASS[column];
362     }
363     
364     /**
365      * Sorts the tree according to the specified sorting model
366      */

367     public void sort(SortingModel sm) {
368         final int sortedColumn = sm.getSortedColumn();
369         if (sortedColumn == -1) {
370             ((UserTaskListTreeTableNode) getRoot()).setComparator(null);
371             return;
372         }
373         
374         @SuppressWarnings JavaDoc("unchecked")
375         Comparator JavaDoc<Object JavaDoc> c = COMPARATORS[sortedColumn];
376         if (c == null)
377             return;
378         
379         final Comparator JavaDoc<Object JavaDoc> c2;
380         if (!sm.isSortOrderDescending())
381             c2 = new NotComparator<Object JavaDoc>(c);
382         else
383             c2 = c;
384         
385         Comparator JavaDoc<AdvancedTreeTableNode> comparator =
386                 new Comparator JavaDoc<AdvancedTreeTableNode>() {
387             public int compare(AdvancedTreeTableNode obj1,
388                     AdvancedTreeTableNode obj2) {
389                 UserTaskTreeTableNode n1 = (UserTaskTreeTableNode) obj1;
390                 UserTaskTreeTableNode n2 = (UserTaskTreeTableNode) obj2;
391                 return c2.compare(
392                     n1.getValueAt(sortedColumn), n2.getValueAt(sortedColumn));
393             }
394         };
395
396         ((UserTaskListTreeTableNode) getRoot()).setComparator(comparator);
397     }
398     
399     public void fireTreeNodesChanged(Object JavaDoc source, Object JavaDoc[] path,
400     int[] childIndices, Object JavaDoc[] children) {
401         super.fireTreeNodesChanged(source, path, childIndices, children);
402     }
403     
404     public void fireTreeNodesInserted(Object JavaDoc source, Object JavaDoc[] path,
405     int[] childIndices, Object JavaDoc[] children) {
406         super.fireTreeNodesInserted(source, path, childIndices, children);
407     }
408     
409     public void fireTreeNodesRemoved(Object JavaDoc source, Object JavaDoc[] path,
410     int[] childIndices, Object JavaDoc[] children) {
411         super.fireTreeNodesRemoved(source, path, childIndices, children);
412     }
413     
414     public void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path) {
415         super.fireTreeStructureChanged(source, path);
416     }
417     
418     public void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path,
419     int[] childIndices, Object JavaDoc[] children) {
420         super.fireTreeStructureChanged(source, path, childIndices, children);
421     }
422 }
423
Popular Tags