KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > BeanTreeView


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.explorer.view;
20
21 import org.openide.explorer.*;
22 import org.openide.explorer.ExplorerManager.Provider;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.Node.Property;
25 import org.openide.util.*;
26
27 import java.awt.*;
28 import java.awt.event.*;
29
30 import java.beans.*;
31
32 import java.util.*;
33
34 import javax.swing.*;
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.event.*;
37 import javax.swing.tree.*;
38
39
40 /** Displays {@link Node} hierarchy as a tree of all nodes.
41  *
42  * <p>
43  * This class is a <q>view</q>
44  * to use it properly you need to add it into a component which implements
45  * {@link Provider}. Good examples of that can be found
46  * in {@link ExplorerUtils}. Then just use
47  * {@link Provider#getExplorerManager} call to get the {@link ExplorerManager}
48  * and control its state.
49  * </p>
50  * <p>
51  * There can be multiple <q>views</q> under one container implementing {@link Provider}. Select from
52  * range of predefined ones or write your own:
53  * </p>
54  * <ul>
55  * <li>{@link org.openide.explorer.view.BeanTreeView} - shows a tree of nodes</li>
56  * <li>{@link org.openide.explorer.view.ContextTreeView} - shows a tree of nodes without leaf nodes</li>
57  * <li>{@link org.openide.explorer.view.ListView} - shows a list of nodes</li>
58  * <li>{@link org.openide.explorer.view.IconView} - shows a rows of nodes with bigger icons</li>
59  * <li>{@link org.openide.explorer.view.ChoiceView} - creates a combo box based on the explored nodes</li>
60  * <li>{@link org.openide.explorer.view.TreeTableView} - shows tree of nodes together with a set of their {@link Property}</li>
61  * <li>{@link org.openide.explorer.view.MenuView} - can create a {@link JMenu} structure based on structure of {@link Node}s</li>
62  * </ul>
63  * <p>
64  * All of these views use {@link ExplorerManager#find} to walk up the AWT hierarchy and locate the
65  * {@link ExplorerManager} to use as a controler. They attach as listeners to
66  * it and also call its setter methods to update the shared state based on the
67  * user action. Not all views make sence together, but for example
68  * {@link org.openide.explorer.view.ContextTreeView} and {@link org.openide.explorer.view.ListView} were designed to complement
69  * themselves and behaves like windows explorer. The {@link org.openide.explorer.propertysheet.PropertySheetView}
70  * for example should be able to work with any other view.
71  * </p>
72 */

73 public class BeanTreeView extends TreeView {
74     /** generated Serialized Version UID */
75     static final long serialVersionUID = 3841322840231536380L;
76
77     /** Constructor.
78     */

79     public BeanTreeView() {
80         // we should have no border, window system will provide borders
81
setBorder(BorderFactory.createEmptyBorder());
82     }
83
84     void initializeTree() {
85         super.initializeTree();
86     }
87
88     /** Create a new model.
89     * The default implementation creates a {@link NodeTreeModel}.
90     * @return the model
91     */

92     protected NodeTreeModel createModel() {
93         return new NodeTreeModel();
94     }
95
96     /** Can select any nodes.
97     */

98     protected boolean selectionAccept(Node[] nodes) {
99         return true;
100     }
101
102     /* Synchronizes selected nodes from the manager of this Explorer.
103     */

104     protected void showSelection(TreePath[] treePaths) {
105         tree.getSelectionModel().setSelectionPaths(treePaths);
106
107         if (treePaths.length == 1) {
108             showPathWithoutExpansion(treePaths[0]);
109         }
110     }
111
112     /* Called whenever the value of the selection changes.
113     * @param nodes nodes
114     * @param em explorer manager
115     */

116     protected void selectionChanged(Node[] nodes, ExplorerManager em)
117     throws PropertyVetoException {
118         if (nodes.length > 0) {
119             Node context = nodes[0].getParentNode();
120
121             for (int i = 1; i < nodes.length; i++) {
122                 if (context != nodes[i].getParentNode()) {
123                     em.setSelectedNodes(nodes);
124
125                     return;
126                 }
127             }
128
129             // May not set explored context above the root context:
130
if (em.getRootContext().getParentNode() == context) {
131                 em.setExploredContextAndSelection(null, nodes);
132             } else {
133                 em.setExploredContextAndSelection(context, nodes);
134             }
135         } else {
136             em.setSelectedNodes(nodes);
137         }
138     }
139
140     /** Expand the given path and makes it visible.
141     * @param path the path
142     */

143     protected void showPath(TreePath path) {
144         tree.expandPath(path);
145         showPathWithoutExpansion(path);
146     }
147
148     /** Make a path visible.
149     * @param path the path
150     */

151     private void showPathWithoutExpansion(TreePath path) {
152         Rectangle rect = tree.getPathBounds(path);
153
154         if (rect != null) { //PENDING
155
tree.scrollRectToVisible(rect);
156         }
157     }
158
159     /** Delegate the setEnable method to Jtree
160      * @param enabled whether to enable the tree
161      */

162     public void setEnabled(boolean enabled) {
163         this.tree.setEnabled(enabled);
164     }
165
166     /** Is the tree enabled
167      * @return boolean
168      */

169     public boolean isEnabled() {
170         if (this.tree == null) {
171             // E.g. in JDK 1.5 w/ GTK L&F, may be called from TreeView's
172
// super (JScrollPane) constructor, so tree is uninitialized
173
return true;
174         }
175
176         return this.tree.isEnabled();
177     }
178 }
179
Popular Tags