1 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 ; 26 import javax.swing.event.DocumentListener ; 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 54 public class ClassChooserPanel extends javax.swing.JPanel 55 implements ExplorerManager.Provider, PropertyChangeListener, 56 VetoableChangeListener, DataFilter, EnhancedCustomPropertyEditor { 57 58 60 private MultiDataContainer rootContainer; 61 62 64 private ExplorerManager manager; 65 66 69 private Node packageRootNode; 70 71 74 TreePathWalker walker; 75 76 TypeIdentPE propertyEditor; 77 78 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 walker = new TreePathWalker(getExplorerManager()); 96 walker.setTextComponent(className); 97 } 98 99 104 private void initComponents() { jLabel1 = new javax.swing.JLabel (); 106 className = new javax.swing.JTextField (); 107 jLabel2 = new javax.swing.JLabel (); 108 jTextField1 = new javax.swing.JTextField (); 109 jLabel3 = new javax.swing.JLabel (); 110 beanTreeView1 = new org.openide.explorer.view.BeanTreeView(); 111 112 setLayout(new java.awt.GridBagLayout ()); 113 java.awt.GridBagConstraints gridBagConstraints1; 114 115 jLabel1.setText("Name:"); gridBagConstraints1 = new java.awt.GridBagConstraints (); 117 gridBagConstraints1.gridy = 0; 118 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 8, 0); 119 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 120 add(jLabel1, gridBagConstraints1); 121 122 className.setText("jTextField2"); gridBagConstraints1 = new java.awt.GridBagConstraints (); 124 gridBagConstraints1.gridwidth = java.awt.GridBagConstraints.REMAINDER; 125 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 126 gridBagConstraints1.insets = new java.awt.Insets (0, 8, 8, 0); 127 gridBagConstraints1.weightx = 1.0; 128 add(className, gridBagConstraints1); 129 130 jLabel2.setText("Full name:"); gridBagConstraints1 = new java.awt.GridBagConstraints (); 132 gridBagConstraints1.gridy = 1; 133 gridBagConstraints1.insets = new java.awt.Insets (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 (); 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 (0, 8, 8, 0); 143 gridBagConstraints1.weightx = 0.75; 144 add(jTextField1, gridBagConstraints1); 145 146 jLabel3.setText("Browse:"); gridBagConstraints1 = new java.awt.GridBagConstraints (); 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 (); 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 } 163 private void classNameKeyTyped(java.awt.event.KeyEvent evt) { } 166 169 public ExplorerManager getExplorerManager() { 170 if (manager == null) 171 manager = ExplorerManager.find(this); 172 return manager; 173 } 174 175 176 private javax.swing.JLabel jLabel1; 178 private javax.swing.JTextField className; 179 private javax.swing.JLabel jLabel2; 180 private javax.swing.JTextField jTextField1; 181 private javax.swing.JLabel jLabel3; 182 private org.openide.explorer.view.BeanTreeView beanTreeView1; 183 185 private class Ch extends FilterNode.Children { 186 Ch(Node original) { 187 super(original); 188 } 189 190 protected Node[] createNodes(Object 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 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 223 public Object getPropertyValue() throws IllegalStateException { 224 Node[] sel = getExplorerManager().getSelectedNodes(); 225 if (sel.length != 1) 226 throw new IllegalStateException ("Exactly one node has to be selected"); Node n = sel[0]; 228 ClassElement el = (ClassElement)n.getCookie(ClassElement.class); 229 if (el == null) 230 throw new IllegalStateException ("Class node is not selected"); 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); } 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 ex) { 258 enable = false; 259 } 260 } 261 propertyEditor.enableOK(enable); 262 } 263 } 264 } 265 | Popular Tags |