KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > model > UserTaskList


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.model;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import javax.swing.event.EventListenerList JavaDoc;
29 import org.netbeans.modules.tasklist.core.util.ObjectListEvent;
30 import org.netbeans.modules.tasklist.core.util.ObjectListListener;
31 import org.netbeans.modules.tasklist.usertasks.DueTasksNotifier;
32
33 /**
34  * This class represents the tasklist itself
35  *
36  * @author Tor Norbye
37  * @author Trond Norbye
38  * @author tl
39  */

40 public class UserTaskList {
41     /**
42      * Callback for the UserTaskList.process method
43      */

44     public static interface UserTaskProcessor {
45         /**
46          * This method will be called for each user task.
47          *
48          * @param ut reference to the task
49          */

50         public void process(UserTask ut);
51     }
52     
53     private UserTaskObjectList tasks;
54
55     /**
56      * this value is used by ICalImport/ExportFormat to store additional
57      * not editable parameters
58      */

59     public Object JavaDoc userObject;
60     
61     private DueTasksNotifier dueTasksNotifier;
62
63     /** <ChangeListener> */
64     private EventListenerList JavaDoc listeners = new EventListenerList JavaDoc();
65     
66     /**
67      * Creates a new instance.
68      */

69     public UserTaskList() {
70         tasks = new UserTaskObjectList(this);
71         tasks.addListener(new ObjectListListener() {
72             public void listChanged(ObjectListEvent ev) {
73                 UserTaskList.this.fireChange();
74             }
75         });
76     }
77     
78     /**
79      * Searches for owners through all tasks.
80      *
81      * @return all found categories
82      */

83     public String JavaDoc[] getOwners() {
84         Iterator JavaDoc it = this.getSubtasks().iterator();
85         Set JavaDoc<String JavaDoc> cat = new java.util.HashSet JavaDoc<String JavaDoc>();
86         while (it.hasNext()) {
87             UserTask ut = (UserTask) it.next();
88             findOwners(ut, cat);
89         }
90         return cat.toArray(new String JavaDoc[cat.size()]);
91     }
92     
93     /**
94      * Searches for owners
95      *
96      * @param task search for owners in this task and all of it's subtasks
97      * recursively
98      * @param cat container for found owners. <String>
99      */

100     private static void findOwners(UserTask task, Set JavaDoc<String JavaDoc> cat) {
101         if (task.getOwner().length() != 0)
102             cat.add(task.getOwner());
103         
104         Iterator JavaDoc it = task.getSubtasks().iterator();
105         while (it.hasNext()) {
106             findOwners((UserTask) it.next(), cat);
107         }
108     }
109     
110     /**
111      * Searches for categories through all tasks.
112      *
113      * @return all found categories
114      */

115     public String JavaDoc[] getCategories() {
116         Iterator JavaDoc it = this.getSubtasks().iterator();
117         Set JavaDoc<String JavaDoc> cat = new java.util.HashSet JavaDoc<String JavaDoc>();
118         while (it.hasNext()) {
119             UserTask ut = (UserTask) it.next();
120             findCategories(ut, cat);
121         }
122         return cat.toArray(new String JavaDoc[cat.size()]);
123     }
124     
125     /**
126      * Searches for categories
127      *
128      * @param task search for categories in this task and all of it's subtasks
129      * recursively
130      * @param cat container for found categories. String[]
131      */

132     private static void findCategories(UserTask task, Set JavaDoc<String JavaDoc> cat) {
133         if (task.getCategory().length() != 0)
134             cat.add(task.getCategory());
135         
136         Iterator JavaDoc it = task.getSubtasks().iterator();
137         while (it.hasNext()) {
138             findCategories((UserTask) it.next(), cat);
139         }
140     }
141     
142     // Look up a particular item by uid
143
public UserTask findItem(Iterator JavaDoc tasks, String JavaDoc uid) {
144         while (tasks.hasNext()) {
145             UserTask task = (UserTask)tasks.next();
146             if (task.getUID().equals(uid)) {
147                 return task;
148             }
149             if (!task.getSubtasks().isEmpty()) {
150                 UserTask f = findItem(task.getSubtasks().iterator(), uid);
151                 if (f != null) {
152                     return f;
153                 }
154             }
155         }
156         return null;
157     }
158
159     /**
160      * Process all tasks including subtasks in the depth-first order.
161      *
162      * @param p a callback that will be called for each task
163      * @param list a list of user tasks
164      */

165     public static void processDepthFirst(UserTaskProcessor p, UserTaskObjectList list) {
166         for (int i = 0; i < list.size(); i++) {
167             UserTask ut = list.getUserTask(i);
168             processDepthFirst(p, ut.getSubtasks());
169             p.process(ut);
170         }
171     }
172         
173     /**
174      * Returns top-level tasks holded by this list.
175      *
176      * @return list of top-level tasks
177      */

178     public final UserTaskObjectList getSubtasks() {
179         return tasks;
180     }
181
182     /**
183      * Should be called after closing a view. Removes all annotations.
184      */

185     public void destroy() {
186         Iterator JavaDoc it = getSubtasks().iterator();
187         while (it.hasNext()) {
188             UserTask ut = (UserTask) it.next();
189             ut.destroy();
190         }
191     }
192     
193     /**
194      * Returns all subtasks (searches recursively).
195      *
196      * @return list of UserTask
197      */

198     public List JavaDoc getAllSubtasks() {
199         List JavaDoc<UserTask> ret = new ArrayList JavaDoc<UserTask>();
200         collectAllSubtasks(ret, getSubtasks());
201         return ret;
202     }
203     
204     /**
205      * Collects all tasks recursively.
206      *
207      * @param ret output
208      * @param tasks a list of UserTasks
209      */

210     private void collectAllSubtasks(List JavaDoc<UserTask> ret, UserTaskObjectList tasks) {
211         for (int i = 0; i < tasks.size(); i++) {
212             ret.add(tasks.getUserTask(i));
213             collectAllSubtasks(ret, tasks.getUserTask(i).getSubtasks());
214         }
215     }
216     
217     /**
218      * Adds a change listener. The listener will be notified whenever some
219      * change occures to a task at any level in this task list.
220      *
221      * @param l a listener
222      */

223     public void addChangeListener(ChangeListener JavaDoc l) {
224         listeners.add(ChangeListener JavaDoc.class, l);
225     }
226     
227     /**
228      * Removes a change listener.
229      *
230      * @param l a listener.
231      */

232     public void removeChangeListener(ChangeListener JavaDoc l) {
233         listeners.remove(ChangeListener JavaDoc.class, l);
234     }
235     
236     /**
237      * Fires a ChangeEvent
238      */

239     void fireChange() {
240         // Guaranteed to return a non-null array
241
Object JavaDoc[] list = listeners.getListenerList();
242
243         // Process the listeners last to first, notifying
244
// those that are interested in this event
245
ChangeEvent JavaDoc changeEvent = null;
246         for (int i = list.length - 2; i >= 0; i -= 2) {
247             if (list[i] == ChangeListener JavaDoc.class) {
248                 // Lazily create the event:
249
if (changeEvent == null)
250                     changeEvent = new ChangeEvent JavaDoc(this);
251                 ((ChangeListener JavaDoc) list[i+1]).stateChanged(changeEvent);
252             }
253         }
254     }
255     
256     /* For debugging purposes, only. Writes directly to serr.
257     public void print() {
258         System.err.println("\nTask List:\n-------------");
259         Iterator it = tasks.iterator();
260         while (it.hasNext()) {
261             Task next = (Task) it.next();
262             recursivePrint(next, 0);
263         }
264
265         System.err.println("\n\n");
266     }
267
268     private void recursivePrint(Task node, int depth) {
269         if (depth > 20) { // probably invalid list
270             Thread.dumpStack();
271             return;
272         }
273         for (int i = 0; i < depth; i++) {
274             System.err.print(" ");
275         }
276         System.err.println(node);
277         if (node.getSubtasks() != null) {
278             List l = node.getSubtasks();
279             ListIterator it = l.listIterator();
280             while (it.hasNext()) {
281                 Task task = (Task) it.next();
282                 recursivePrint(task, depth + 1);
283             }
284         }
285     }*/

286
287     // TaskListener impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288
}
289
Popular Tags