This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 7551
Collapse All | Expand All

(-)a/openide.nodes/src/org/openide/nodes/NodeFinder.java (+150 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
38
 */
39
40
package org.openide.nodes;
41
42
import java.util.logging.Logger;
43
import org.openide.util.Lookup;
44
import org.openide.util.Parameters;
45
46
/**
47
 * Tool for finding nodes within a subtree based on content.
48
 * One or more implementations may be placed in the {@linkplain Node#getLookup lookup}
49
 * of a node, in which case it knows where to find the node representing an object.
50
 * @see NodeOp#createPath
51
 */
52
public abstract class NodeFinder {
53
54
    private static final Logger LOG = Logger.getLogger(NodeFinder.class.getName());
55
56
    /**
57
     * Simple finder implementation which just searches children recursively.
58
     * The first child of {@linkplain Children#getNodes(boolean) getNodes(true)}
59
     * in which the item can be found is returned, else null.
60
     * Useful for "heterogeneous" parent nodes which have assorted children any
61
     * of which might contain the item, such as a project's root node.
62
     */
63
    public static final NodeFinder NAIVE = new NodeFinder() {
64
        public Node findNode(Node root, Object item) {
65
            for (Node child : root.getChildren().getNodes(true)) {
66
                Node n = findOrNull(child, item);
67
                if (n != null) {
68
                    return n;
69
                }
70
            }
71
            return null;
72
        }
73
    };
74
75
    /** No-op constructor for implementations. */
76
    protected NodeFinder() {}
77
78
    /**
79
     * Attempts to locate a child of this node containing a given item in its lookup.
80
     * @param root a root node to begin the search on; this may be the node in
81
     *             whose lookup the finder was placed, or a {@link FilterNode}
82
     *             or similar derivative of that node; also permits a singleton finder
83
     *             instance to be placed in the lookup of a whole class of nodes
84
     * @param item an object expected to be found in the lookup of some descendant of {@code root}
85
     * @return a (strict) descendant of {@code root} which either contains {@code item}
86
     *         in its {@linkplain Node#getLookup lookup}, or which is known to be
87
     *         the ancestor of a node which does; or {@code null} if nothing is
88
     *         known about the location of this particular item or class of item
89
     *         underneath the given root
90
     */
91
    public abstract Node findNode(Node root, Object item);
92
93
    /**
94
     * Attempts to locate a child node containing a given item in its lookup.
95
     * The algorithm is as follows:
96
     * <ol>
97
     * <li>If the root node contains the object, that is returned.
98
     * <li>If some finder present on the root node returns a real node,
99
     *     that is searched recursively and a result is returned if found.
100
     * <li>Otherwise, null is returned.
101
     * </ol>
102
     * @param root a root node to begin the search on
103
     * @param item an object expected to be found in the lookup of some descendant of the root node
104
     * @return a descendant of {@code root} (or the root itself) which contains {@code item}
105
     *         in its {@linkplain Node#getLookup lookup}; or {@code null} if none could be found
106
     */
107
    public static Node findOrNull(Node root, Object item) {
108
        Parameters.notNull("root", root);
109
        Parameters.notNull("item", item);
110
        if (contains(root, item)) {
111
            return root;
112
        }
113
        for (NodeFinder finder : root.getLookup().lookupAll(NodeFinder.class)) {
114
            Node n = finder.findNode(root, item);
115
            if (n == Node.EMPTY) {
116
                return null;
117
            }
118
            if (n != null) {
119
                if (!isStrictAncestor(root, n)) {
120
                    LOG.warning(finder + " returned " + n + " which is not a strict descendant of " + root);
121
                    continue;
122
                }
123
                Node r = findOrNull(n, item);
124
                if (r != null) {
125
                    return r;
126
                } else {
127
                    LOG.warning(finder + " returned " + n + " which did not in fact contain any descendant with " + item);
128
                }
129
            }
130
        }
131
        return null;
132
    }
133
134
    private static boolean contains(Node n, Object item) {
135
        Object r = n.getLookup().lookup(new Lookup.Template<Object>(null, null, item));
136
        if (r == null) {
137
            return false;
138
        }
139
        if (!r.equals(item)) {
140
            LOG.warning("lookup of " + n + " claimed to find " + item + " but actually found " + r);
141
        }
142
        return true;
143
    }
144
145
    private static boolean isStrictAncestor(Node root, Node n) {
146
        Node p = n.getParentNode();
147
        return p != null && (p == root || isStrictAncestor(root, p));
148
    }
149
150
}

Return to bug 7551