KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > editors > ClassChooserPanel


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 package org.netbeans.modules.java.ui.editors;
21
22 import java.beans.*;
23 import java.util.*;
24
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import javax.swing.event.DocumentListener JavaDoc;
27
28 import org.openide.cookies.SourceCookie;
29
30 import org.openide.explorer.ExplorerManager;
31 import org.openide.explorer.view.BeanTreeView;
32 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
33
34 import org.openide.loaders.DataFilter;
35 import org.openide.loaders.DataObject;
36
37 import org.openide.nodes.Children;
38 import org.openide.nodes.Node;
39 import org.openide.nodes.NodeNotFoundException;
40 import org.openide.nodes.NodeOp;
41 import org.openide.nodes.FilterNode;
42
43 import org.openide.src.*;
44
45 import org.netbeans.modules.java.tools.MultiDataContainer;
46 import org.netbeans.modules.java.ui.nodes.PackageNode;
47
48 /**
49  * This panel helps to choose a Class representation from the given
50  * classpath.
51  *
52  * @author Svatopluk Dedic (sdedic@netbeans.org)
53  */

54 public class ClassChooserPanel extends javax.swing.JPanel JavaDoc
55     implements ExplorerManager.Provider, PropertyChangeListener,
56     VetoableChangeListener, DataFilter, EnhancedCustomPropertyEditor {
57
58     /**
59      */

60     private MultiDataContainer rootContainer;
61     
62     /**
63      */

64     private ExplorerManager manager;
65     
66     /**
67      * Root of the package/class selection hierarchy.
68      */

69     private Node packageRootNode;
70     
71     /**
72      * Object used to synchronize the explorer with the input line component.
73      */

74     TreePathWalker walker;
75     
76     TypeIdentPE propertyEditor;
77     
78     /** Creates new form TypeSelector */
79     public ClassChooserPanel(TypeIdentPE propertyEditor, Collection classPathRoots) {
80         this.propertyEditor = propertyEditor;
81         
82         initComponents();
83         rootContainer = new MultiDataContainer(classPathRoots, this);
84     }
85     
86     public void addNotify() {
87         super.addNotify();
88
89         Node n;
90         n = new PackageNode("", rootContainer);
91         packageRootNode = new FilterNode(n, new Ch(n));
92         getExplorerManager().setRootContext(packageRootNode);
93
94         // initialize the tree walker
95
walker = new TreePathWalker(getExplorerManager());
96         walker.setTextComponent(className);
97     }
98
99     /** This method is called from within the constructor to
100      * initialize the form.
101      * WARNING: Do NOT modify this code. The content of this method is
102      * always regenerated by the Form Editor.
103      */

104     private void initComponents() {//GEN-BEGIN:initComponents
105
jLabel1 = new javax.swing.JLabel JavaDoc();
106         className = new javax.swing.JTextField JavaDoc();
107         jLabel2 = new javax.swing.JLabel JavaDoc();
108         jTextField1 = new javax.swing.JTextField JavaDoc();
109         jLabel3 = new javax.swing.JLabel JavaDoc();
110         beanTreeView1 = new org.openide.explorer.view.BeanTreeView();
111         
112         setLayout(new java.awt.GridBagLayout JavaDoc());
113         java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
114         
115         jLabel1.setText("Name:"); // NOI18N
116
gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
117         gridBagConstraints1.gridy = 0;
118         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 0, 8, 0);
119         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
120         add(jLabel1, gridBagConstraints1);
121         
122         className.setText("jTextField2"); // NOI18N
123
gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
125         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
126         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 8, 8, 0);
127         gridBagConstraints1.weightx = 1.0;
128         add(className, gridBagConstraints1);
129         
130         jLabel2.setText("Full name:"); // NOI18N
131
gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
132         gridBagConstraints1.gridy = 1;
133         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 0, 8, 0);
134         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
135         add(jLabel2, gridBagConstraints1);
136         
137         jTextField1.setEditable(false);
138         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints1.gridy = 1;
140         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
141         gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
142         gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 8, 8, 0);
143         gridBagConstraints1.weightx = 0.75;
144         add(jTextField1, gridBagConstraints1);
145         
146         jLabel3.setText("Browse:"); // NOI18N
147
gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
149         gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
150         add(jLabel3, gridBagConstraints1);
151         
152         gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
153         gridBagConstraints1.gridx = 0;
154         gridBagConstraints1.gridy = 3;
155         gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER;
156         gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
157         gridBagConstraints1.weightx = 1.0;
158         gridBagConstraints1.weighty = 1.0;
159         add(beanTreeView1, gridBagConstraints1);
160         
161     }//GEN-END:initComponents
162

163     private void classNameKeyTyped(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_classNameKeyTyped
164
}//GEN-LAST:event_classNameKeyTyped
165

166     /** Get the explorer manager.
167      * @return the manager
168      */

169     public ExplorerManager getExplorerManager() {
170         if (manager == null)
171             manager = ExplorerManager.find(this);
172         return manager;
173     }
174
175
176     // Variables declaration - do not modify//GEN-BEGIN:variables
177
private javax.swing.JLabel JavaDoc jLabel1;
178     private javax.swing.JTextField JavaDoc className;
179     private javax.swing.JLabel JavaDoc jLabel2;
180     private javax.swing.JTextField JavaDoc jTextField1;
181     private javax.swing.JLabel JavaDoc jLabel3;
182     private org.openide.explorer.view.BeanTreeView beanTreeView1;
183     // End of variables declaration//GEN-END:variables
184

185     private class Ch extends FilterNode.Children {
186         Ch(Node original) {
187             super(original);
188         }
189         
190         protected Node[] createNodes(Object JavaDoc key) {
191             Node origNode = (Node)key;
192             ClassElement c = (ClassElement)origNode.getCookie(ClassElement.class);
193             if (c != null &&
194                 !acceptsClass(c))
195                 return new Node[0];
196             else {
197                 Children ch = origNode.getChildren() == Children.LEAF ? Children.LEAF :
198                     new Ch(origNode);
199                 return new Node[] {
200                     new FilterNode(origNode, ch)
201                 };
202             }
203         }
204     }
205     
206     /**
207      * Determines whether to display a particular class' node or not.
208      */

209     private boolean acceptsClass(ClassElement c) {
210         return c.isClassOrInterface();
211     }
212     
213     public boolean acceptDataObject(DataObject obj) {
214         return obj.getCookie(SourceCookie.class) != null ||
215                obj.getCookie(DataObject.Container.class) != null;
216     }
217     
218     /** Get the customized property value.
219      * @return the property value
220      * @exception InvalidStateException when the custom property editor does not contain a valid property value
221      * (and thus it should not be set)
222      */

223     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
224         Node[] sel = getExplorerManager().getSelectedNodes();
225         if (sel.length != 1)
226             throw new IllegalStateException JavaDoc("Exactly one node has to be selected"); // NOI18N
227
Node n = sel[0];
228         ClassElement el = (ClassElement)n.getCookie(ClassElement.class);
229         if (el == null)
230             throw new IllegalStateException JavaDoc("Class node is not selected"); // NOI18N
231
return el.getName();
232     }
233     
234     public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
235         if (evt.getSource() == getExplorerManager() &&
236             ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
237             Node[] ns = (Node[])evt.getNewValue();
238             if (ns.length > 1)
239                 throw new PropertyVetoException("Multiple selection is not allowed.", evt); // NOI18N
240
}
241     }
242     
243     public void propertyChange(PropertyChangeEvent evt) {
244         if (evt.getSource() == getExplorerManager() &&
245             ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
246             Node[] ns = (Node[])evt.getNewValue();
247             boolean enable = ns.length == 1;
248             ClassElement el = null;
249             
250             if (enable) {
251                 el = (ClassElement)ns[0].getCookie(ClassElement.class);
252                 enable &= el != null;
253             }
254             if (enable) {
255                 try {
256                     propertyEditor.checkInputConstraints(null, el);
257                 } catch (IllegalArgumentException JavaDoc ex) {
258                     enable = false;
259                 }
260             }
261             propertyEditor.enableOK(enable);
262         }
263     }
264 }
265
Popular Tags