KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > FilteredNode


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.i18n;
22
23 import org.openide.nodes.FilterNode;
24 import org.openide.nodes.Node;
25
26
27
28
29 /**
30  * This class implements a filter node that can be used to display a
31  * hierarchy of nodes filtered by a criteriea defined by
32  * <code>NodeFilter</code>. We don't use NodeAcceptor because of
33  * efficiency - node acceptor requires an array as a parameter,
34  * which we don't want to create.
35  */

36 public class FilteredNode extends FilterNode {
37
38   private NodeFilter filter;
39   private String JavaDoc newName = null;
40
41   /**
42    * Decides which nodes should be included in the hierarchy and which
43    * not.
44    */

45   public interface NodeFilter {
46     public boolean acceptNode(Node node) ;
47   }
48
49   public FilteredNode(Node original, NodeFilter filter ) {
50       this(original, filter, null);
51   }
52
53
54
55   public FilteredNode(Node original, NodeFilter filter, String JavaDoc newName) {
56     super(original, new FilteredChildren(original, filter));
57     this.filter = filter;
58     this.newName = newName;
59   }
60
61   public String JavaDoc getDisplayName() {
62     if (newName != null) return newName; else return super.getDisplayName();
63   }
64     
65   public Node cloneNode() {
66     return new FilteredNode(this.getOriginal().cloneNode(), this.filter);
67   }
68
69     
70
71
72   /**
73    * A mutualy recursive children that ensure propagation of the
74    * filter to deeper levels of hiearachy. That is, it creates
75    * FilteredNodes filtered by the same filter.
76    */

77   public static class FilteredChildren extends FilterNode.Children {
78     private NodeFilter filter;
79
80     public FilteredChildren(Node original, NodeFilter filter) {
81       super(original);
82       this.filter = filter;
83     }
84
85     protected Node copyNode(Node node) {
86       return new FilteredNode(node, this.filter);
87     }
88
89     protected Node[] createNodes(Object JavaDoc key) {
90       if (filter.acceptNode((Node)key))
91     return super.createNodes(key);
92       else
93     return new Node [0];
94     }
95
96   }
97
98 }
99
Popular Tags