KickJava   Java API By Example, From Geeks To Geeks.

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


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.core;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import org.netbeans.modules.tasklist.core.filter.Filter;
27 import org.openide.nodes.FilterNode;
28 import org.openide.nodes.Node;
29 import org.openide.nodes.NodeMemberEvent;
30
31 /**
32  * Children for a FilteredTaskNode
33  *
34  * @author Tor Norbye
35  */

36 final class FilteredTaskChildren extends FilterNode.Children {
37     private static final Logger JavaDoc LOGGER = TLUtils.getLogger(
38         FilteredTaskChildren.class);
39     
40     static {
41         LOGGER.setLevel(Level.OFF);
42     }
43     
44     private Filter filter;
45     private TaskListView view;
46
47     public FilteredTaskChildren(TaskListView view, Node n, Filter filter) {
48         super(n);
49         this.filter = filter;
50         this.view = view;
51     }
52
53     @Override JavaDoc
54     protected Node[] createNodes(Node key) {
55         if (!(key instanceof Node))
56             return new Node[0];
57         
58         Node n = (Node) key;
59         Task task = TaskNode.getTask(n);
60         if (filter.accept(task)) {
61             org.openide.nodes.Children children;
62             if (n.getChildren() == org.openide.nodes.Children.LEAF) {
63                 children = org.openide.nodes.Children.LEAF;
64             } else {
65                 children = new FilteredTaskChildren(view, n, filter);
66             }
67             return new Node[] { new FilterTaskNode(n, children, false) };
68         }
69         
70         /* TODO if (filter.isFlattened()) {
71             // Add all matching subtasks
72             ArrayList matches = new ArrayList();
73             findMatches(matches, task);
74
75             Node[] nodes = new Node[matches.size()];
76             ListIterator it = matches.listIterator();
77             int index = 0;
78             while (it.hasNext()) {
79                 Task t = (Task)it.next();
80                 org.openide.nodes.Children children;
81                 Node tn = t.createNode()[0];
82                 if (!t.hasSubtasks()) {
83                     children = org.openide.nodes.Children.LEAF;
84                 } else {
85                     children = new FilteredTaskChildren(view, tn, filter);
86                 }
87                 nodes[index++] = new FilterTaskNode(n, children,
88                     false);
89             }
90             return nodes;
91         } */

92         
93         // Perhaps the node has subtasks which accept. If so,
94
// we check those as well...
95
if (hasMatch(task)) {
96             // Yes - add task, but mark it with a
97
// special nonmatching icon.
98
org.openide.nodes.Children children;
99             if (n.getChildren() == org.openide.nodes.Children.LEAF) {
100                 children = org.openide.nodes.Children.LEAF;
101             } else {
102                 children = new FilteredTaskChildren(view, n, filter);
103             }
104             return new Node[] {
105                 new FilterTaskNode(n, children, true) };
106         }
107         
108         return new Node[0];
109     }
110
111     /**
112      * Return true iff the task n or one of its children matches
113      * the filter
114      */

115     private boolean hasMatch(Task n) {
116         if (filter.accept(n))
117             return true;
118
119         Iterator JavaDoc it = n.subtasksIterator();
120         while (it.hasNext()) {
121             if (hasMatch((Task) it.next()))
122                 return true;
123         }
124         return false;
125     }
126
127     public void filterChildrenAdded(NodeMemberEvent ev) {
128         super.filterChildrenAdded(ev);
129         if (view != null) {
130             view.updateFilterCount();
131         }
132     }
133
134     public void filterChildrenRemoved(NodeMemberEvent ev) {
135         super.filterChildrenRemoved(ev);
136         if (view != null) {
137             view.updateFilterCount();
138         }
139     }
140 }
141
Popular Tags