KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > TaskChildren


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 package org.netbeans.modules.tasklist.core;
20
21 import java.util.*;
22
23 import org.openide.nodes.Node;
24 import org.openide.nodes.Children;
25 import org.openide.util.WeakListeners;
26
27
28 /**
29  * Children object for the Task; used to track
30  * TaskList modifications and update nodes appropriately.
31  *
32  * @author Tor Norbye
33  * @author Petr Kuzel
34  */

35 public class TaskChildren extends Children.Keys {
36
37     private final Task parent;
38     private Monitor monitor;
39
40     public TaskChildren(Task parent) {
41         this.parent = parent;
42     }
43
44     private void refreshKeys() {
45         Collection keys;
46         if (parent.hasSubtasks() == false) {
47             keys = Collections.EMPTY_SET;
48         } else {
49
50 // It does not work and does not save any Node instance creation
51
// // threat task clones as equal
52
// // XXX we may need to refresh nodes that share key
53
// // why do all pay here this extra overhead?
54
//
55
// int size = parent.getSubtasks().size();
56
// ArrayList list = new ArrayList(size);
57
// keys2tasks = new WeakHashMap(size*2);
58
// Iterator it = parent.getSubtasks().iterator();
59
// while (it.hasNext()) {
60
// Task task = (Task) it.next();
61
// Object key = task.getKey();
62
// list.add(key);
63
// keys2tasks.put(key, task);
64
// }
65
// setKeys(list);
66
keys = parent.getSubtasks();
67         }
68
69         Task[] tasks = (Task[])keys.toArray(new Task[keys.size()]);
70         setKeys(tasks);
71     }
72
73     /**
74      * Called when the parent node is expanded; now we need
75      * to create nodes for the children.
76      */

77     protected void addNotify() {
78         super.addNotify();
79         assert monitor == null : "Dangling addNotify()"; // NOI18N
80
monitor = new Monitor();
81
82         // weak listener must be used here because children
83
// all listening on tasklist that has different
84
// lifetime than parent node nor is driven by it.
85
TaskListener l = (TaskListener) WeakListeners.create(TaskListener.class, monitor, parent);
86         parent.addTaskListener(l);
87         refreshKeys();
88     }
89     
90     /** Called when the parent node is collapsed: cleanup */
91     protected void removeNotify() {
92         assert monitor != null : "Dangling removeNotify()"; // NOI18N
93
// parent.getList().removeTaskListener(monitor);
94
monitor = null;
95         setKeys(Collections.EMPTY_SET);
96         super.removeNotify();
97     }
98     
99     /**
100      * Create nodes for the specified key object (a task)
101      * @param key The task used as a parent key
102      * @return Node for the key task's children
103      */

104     protected Node[] createNodes(Object JavaDoc key) {
105         // interpret your key here...usually one node generated, but could be zero or more
106
// return new Node[] { new TodoNode((MyParameter) key) };
107
//return new Node[] { new TodoNode(key) };
108

109 // Task item = (Task)keys2tasks.get(key);
110
// assert item != null : "The key was held by Children.Keys!";
111
// return ((Task)key).createNode(); // XXX I do not like this
112
// model-view 1:1
113

114       Task task = (Task)key;
115       return new Node[] { createNode(task) };
116     }
117
118     public Object JavaDoc clone() {
119       return new TaskChildren(this.parent);
120     }
121     
122
123
124     /**
125      * A factory method for creating new task nodes
126      */

127     protected TaskNode createNode(Task task) {
128       return new TaskNode(task);
129     }
130
131
132     // Monitor tasklist and react to changes ~~~~~~~~~~~~~~~~~~~~~
133

134     private class Monitor implements TaskListener {
135         public Monitor () {}
136         
137         public void selectedTask(Task t) {
138             // it's node job
139
}
140
141         public void warpedTask(Task t) {
142             // it's node job
143
}
144
145         public void addedTask(Task t) {
146             if (t.getParent() == parent) {
147                 refreshKeys();
148             }
149         }
150
151         public void removedTask(Task pt, Task t, int index) {
152             if (t.getParent() == parent) {
153                 refreshKeys();
154             }
155         }
156
157         public void structureChanged(Task t) {
158             if (t == null || t.isParentOf(parent)) {
159                 refreshKeys();
160             }
161         }
162
163     }
164 }
165
Popular Tags