KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > nodes > DefaultHandle


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.openide.nodes;
20
21 import java.util.logging.Level JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24
25 /** Simple implementation of <code>Node.Handle</code>.
26 * When created by {@link #createHandle} it
27 * looks for the parent of the node and stores the node's name.
28 * When {@link #getNode} is then called, it tries to restore the
29 * parent and then to walk down to the child.
30 * <p>Note that if most nodes use <code>DefaultHandle</code>, this
31 * may walk up to the root recursively. Otherwise, some other sort
32 * of handle may provide the termination case.
33 *
34 * @author Jaroslav Tulach, Jesse Glick
35 */

36 public final class DefaultHandle extends Object JavaDoc implements Node.Handle {
37     private static final long serialVersionUID = -8739127064355983273L;
38
39     /** parent handle */
40     private Node.Handle parent;
41
42     /** path to the node (just one hop, really) */
43     private String JavaDoc path;
44
45     /* Create a new handle.
46     * @param parent handle for the parent node
47     * @param path desired name of child
48     */

49     DefaultHandle(Node.Handle parent, String JavaDoc path) {
50         this.parent = parent;
51         this.path = path;
52     }
53
54     /** Find the node.
55     * @return the found node
56     * @exception IOException if the parent cannot be recreated
57     * @exception NodeNotFoundException if the path is not valid (exception may be examined for details)
58     */

59     public Node getNode() throws java.io.IOException JavaDoc {
60         Node parentNode = parent.getNode();
61         Node child = parentNode.getChildren().findChild(path);
62
63         if (child != null) {
64             return child;
65         } else {
66             throw new NodeNotFoundException(parentNode, path, 0);
67         }
68     }
69
70     /** Create a handle for a given node.
71     * A handle cannot be created under these circumstances:
72     * <ol>
73     * <li>The node has no name.
74     * <li>The node has no parent.
75     * <li>The parent has no handle.
76     * <li>The parent is incapable of finding its child by the supplied name.
77     * </ol>
78     * @param node the node to create a handler for
79     * @return the handler, or <code>null</code> if a handle cannot be created
80     */

81     public static DefaultHandle createHandle(final Node node) {
82         try {
83             Children.PR.enterReadAccess();
84
85             String JavaDoc childPath = node.getName();
86
87             if (childPath == null) {
88                 return null;
89             }
90
91             Node parentNode = node.getParentNode();
92
93             if (parentNode == null) {
94                 return null;
95             }
96
97             Node foundChild = parentNode.getChildren().findChild(childPath);
98             if (foundChild != node) {
99                 Logger.getLogger(DefaultHandle.class.getName()).log(Level.WARNING,
100                         "parent could not find own child: node={0} parentNode={1} childPath={2} foundChild={3}",
101                         new Object JavaDoc[] {node, parentNode, childPath, foundChild});
102                 return null;
103             }
104
105             Node.Handle parentHandle = parentNode.getHandle();
106
107             if (parentHandle == null) {
108                 return null;
109             }
110
111             return new DefaultHandle(parentHandle, childPath);
112         } finally {
113             Children.PR.exitReadAccess();
114         }
115     }
116
117     public String JavaDoc toString() {
118         return "DefaultHandle[" + parent + "|" + path + "]"; // NOI18N
119
}
120 }
121
Popular Tags