KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.tasklist.core;
22
23 import org.openide.nodes.AbstractNode;
24 import org.openide.nodes.Children;
25 import org.openide.nodes.Node;
26 import org.openide.util.RequestProcessor;
27
28 import javax.swing.*;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31
32 /**
33  * Node visualization of TaskList. It creates children
34  * by taking default node from contained tasks.
35  *
36  * @author Petr Kuzel
37  */

38 public class TaskListNode extends AbstractNode {
39
40     /**
41      * Creates plain tasklist node. Properties that cannot
42      * be derrived from passed tasklist should be provided by
43      * client. It covers displayName etc.
44      *
45      * @param tasklist to be visualized never <code>null</code>
46      */

47     public TaskListNode(ObservableList tasklist) {
48         super(new TaskListChildren(tasklist));
49     }
50
51     /**
52      * Creates plain tasklist node. Properties that cannot
53      * be derrived from passed tasklist should be provided by
54      * client. It covers displayName etc.
55      *
56      * @param tasklist to be visualized never <code>null</code>
57      */

58     public TaskListNode(ObservableList tasklist, NodeFactory nodeFactory) {
59       super(new TaskListChildren(tasklist));
60       TaskListChildren list = (TaskListChildren) getChildren();
61       list.setNodeFactory(nodeFactory);
62     }
63
64     public Action[] getActions(boolean context) {
65         return new Action[0];
66     }
67
68
69     /** Creates custom child nodes for TaskListNode */
70     public static interface NodeFactory {
71
72         /** Default task.createNode() */
73         Node createNode(Object JavaDoc task);
74     }
75
76     public void destroy() throws java.io.IOException JavaDoc {
77         // explicitly destroy all children, it's not done automatically
78
Enumeration JavaDoc en = getChildren().nodes();
79         while (en.hasMoreElements()) {
80             Node next = (Node) en.nextElement();
81             next.destroy();
82         }
83         super.destroy();
84     }
85     
86     static class TaskListChildren extends Children.Keys implements TaskListener, Runnable JavaDoc {
87
88         private ObservableList list;
89         private NodeFactory nodeFactory;
90         private static int BATCH_INTERVAL_MS = 59;
91         private volatile RequestProcessor.Task batchSetKeys;
92         private volatile boolean active = false;
93
94         TaskListChildren(ObservableList list) {
95             assert list != null;
96             this.list = list;
97         }
98
99         protected void addNotify() {
100             super.addNotify();
101             setKeys(list.getTasks());
102             list.addTaskListener(this);
103             active = true;
104         }
105
106         protected void removeNotify() {
107             active = false;
108             list.removeTaskListener(this);
109             setKeys(Collections.EMPTY_SET);
110             super.removeNotify();
111         }
112
113         protected Node[] createNodes(Object JavaDoc key) {
114             Task task = (Task) key;
115             Node[] nodes;
116             if (nodeFactory == null) {
117                 nodes = task.createNode();
118             } else {
119                 nodes = new Node[] {nodeFactory.createNode(task)};
120             }
121             return nodes;
122         }
123
124         public void setNodeFactory(NodeFactory nodeFactory) {
125             this.nodeFactory = nodeFactory;
126         }
127
128         // do not update keys too often it's rather heavyweight operation
129
// batch all request that come in BATCH_INTERVAL_MS into one real update
130
private void batchSetKeys() {
131             if (batchSetKeys == null) {
132                 batchSetKeys = RequestProcessor.getDefault().post(this, BATCH_INTERVAL_MS);
133             }
134         }
135
136         // TaskListener implementation ~~~~~~~~~~~~~~~
137

138         public void selectedTask(Task t) {
139         }
140
141         public void warpedTask(Task t) {
142         }
143
144         public void addedTask(Task t) {
145             batchSetKeys();
146         }
147
148         public void removedTask(Task pt, Task t, int index) {
149             batchSetKeys();
150         }
151
152         public void structureChanged(Task t) {
153             batchSetKeys();
154         }
155
156         // called from random request processor thread
157
public void run() {
158             batchSetKeys = null;
159             if (active) {
160                 setKeys(list.getTasks());
161             }
162         }
163
164
165     }
166 }
167
Popular Tags