KickJava   Java API By Example, From Geeks To Geeks.

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


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

66 public class ChoiceView extends JComboBox implements Externalizable {
67     /** generated Serialized Version UID */
68     static final long serialVersionUID = 2522310031223476067L;
69
70     /** The local reference to the explorerManager. It is transient
71     * because it will be reset in initializeManager() after deserialization.*/

72     transient private ExplorerManager manager;
73
74     /** Listens on ExplorerManager. */
75     transient private PropertyIL iListener;
76
77     /** model to use */
78     transient private NodeListModel model;
79
80     /** Value of property showExploredContext. */
81     private boolean showExploredContext = true;
82
83     // init .................................................................................
84

85     /** Default constructor. */
86     public ChoiceView() {
87         super();
88         initializeChoice();
89     }
90
91     /** Initialize view. */
92     private void initializeChoice() {
93         setRenderer(new NodeRenderer());
94
95         setModel(model = createModel());
96
97         iListener = new PropertyIL();
98     }
99
100     // XXX [PENDING] setting new model via setModel() is in fact ignored, see model
101
// field -> which 'replaces' normal combo model thus the underlying one making
102
// useless.
103

104     /*
105     * Write view's state to output stream.
106     */

107     public void writeExternal(ObjectOutput out) throws IOException {
108         out.writeObject(showExploredContext ? Boolean.TRUE : Boolean.FALSE);
109     }
110
111     /*
112     * Reads view's state form output stream.
113     */

114     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
115         showExploredContext = ((Boolean JavaDoc) in.readObject()).booleanValue();
116     }
117
118     //
119
// To override
120
//
121

122     /** Creates the model that this view should show.
123     */

124     protected NodeListModel createModel() {
125         return new NodeListModel();
126     }
127
128     // main methods .........................................................................
129

130     /** Set showing of explored contexts.
131     * @param b <code>true</code> to show the explored context, <code>false</code> the root context
132     */

133     public void setShowExploredContext(boolean b) {
134         showExploredContext = b;
135         updateChoice();
136     }
137
138     /**
139     * Get explored context toggle.
140     * @return whether currently showing explored context (default <code>false</code>)
141     */

142     public boolean getShowExploredContext() {
143         return showExploredContext;
144     }
145
146     // main methods .........................................................................
147

148     /* Initializes view.
149     */

150     public void addNotify() {
151         manager = ExplorerManager.find(this);
152         manager.addVetoableChangeListener(iListener);
153         manager.addPropertyChangeListener(iListener);
154
155         updateChoice();
156
157         addActionListener(iListener);
158
159         super.addNotify();
160     }
161
162     /* Deinitializes view.
163     */

164     public void removeNotify() {
165         super.removeNotify();
166
167         removeActionListener(iListener);
168
169         manager.removeVetoableChangeListener(iListener);
170         manager.removePropertyChangeListener(iListener);
171     }
172
173     private void updateSelection() {
174         Node[] nodes = manager.getSelectedNodes();
175
176         if (nodes.length > 0) {
177             setSelectedItem(VisualizerNode.getVisualizer(null, nodes[0]));
178         } else {
179             setSelectedItem(showExploredContext ? manager.getExploredContext() : manager.getRootContext());
180         }
181     }
182
183     private void updateChoice() {
184         if (showExploredContext) {
185             model.setNode(manager.getExploredContext());
186         } else {
187             model.setNode(manager.getRootContext());
188         }
189
190         updateSelection();
191     }
192
193     // innerclasses .........................................................................
194

195     /* The inner adaptor class for listening to the ExplorerManager's property and vetoable changes. */
196     final class PropertyIL extends Object JavaDoc implements PropertyChangeListener, VetoableChangeListener,
197         java.awt.event.ActionListener JavaDoc {
198         public void vetoableChange(PropertyChangeEvent evt)
199         throws PropertyVetoException {
200             if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
201                 Node[] nodes = (Node[]) evt.getNewValue();
202
203                 if (nodes.length > 1) {
204                     throw new PropertyVetoException("", evt); // we do not allow multiple selection // NOI18N
205
}
206             }
207         }
208
209         public void propertyChange(PropertyChangeEvent evt) {
210             ChoiceView.this.removeActionListener(this);
211
212             try {
213                 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
214                     Node[] selectedNodes = (Node[]) evt.getNewValue();
215                     updateSelection();
216
217                     return;
218                 }
219
220                 if (!showExploredContext && ExplorerManager.PROP_ROOT_CONTEXT.equals(evt.getPropertyName())) {
221                     updateChoice();
222
223                     return;
224                 }
225
226                 if (showExploredContext && ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) {
227                     updateChoice();
228
229                     return;
230                 }
231             } finally {
232                 ChoiceView.this.addActionListener(this);
233             }
234         }
235
236         public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
237             int s = getSelectedIndex();
238
239             if ((s < 0) || (s >= model.getSize())) {
240                 return;
241             }
242
243             Node n = Visualizer.findNode(model.getElementAt(s));
244
245             manager.removeVetoableChangeListener(this);
246             manager.removePropertyChangeListener(this);
247
248             try {
249                 manager.setSelectedNodes(new Node[] { n });
250             } catch (PropertyVetoException ex) {
251                 updateChoice(); // no selection change allowed
252
} finally {
253                 manager.addVetoableChangeListener(this);
254                 manager.addPropertyChangeListener(this);
255             }
256         }
257     }
258 }
259
Popular Tags