1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.util.*; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 import org.openide.*; 27 import org.openide.explorer.*; 28 import org.openide.nodes.*; 29 import org.openide.util.HelpCtx; 30 import org.openide.util.Lookup; 31 32 37 @SuppressWarnings ("deprecation") 38 public class ServiceTypePanel extends org.netbeans.beaninfo.ExplorerPanel { 39 40 private int width_components=0,width_leftcomponent=0; 41 42 44 private Class <? extends ServiceType> clazz; 45 46 47 private List<ServiceType> services; 48 49 50 private ServiceType none; 51 52 56 private boolean createNew = false; 57 58 static final long serialVersionUID =861345226525021334L; 59 64 public ServiceTypePanel(Class <? extends ServiceType> clazz, String name, ServiceType none, boolean createNew) { 65 this.clazz = clazz; 66 this.none = none; 67 this.createNew = createNew; 68 update (); 69 70 initComponents (); 71 72 handleDividerLocation(); 74 75 label.setText(name); 76 listView1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(ServiceTypePanel.class).getString("ACSD_ServiceTypeList")); 77 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(ServiceTypePanel.class).getString("ACSD_ServiceTypePanel")); 78 79 getExplorerManager ().addPropertyChangeListener (new java.beans.PropertyChangeListener () { 80 public void propertyChange (java.beans.PropertyChangeEvent ev) { 81 if ( ExplorerManager.PROP_SELECTED_NODES.equals(ev.getPropertyName()) ) { 82 firePropertyChange( DialogDescriptor.PROP_HELP_CTX, null, null ); 83 } 84 firePropertyChange (); 85 } 86 }); 87 if (name.length() > 0) { 88 label.setDisplayedMnemonic(name.charAt(0)); 89 } 90 } 91 92 private void handleDividerLocation() { 93 int listWidth = listView1.getPreferredSize().width; 96 int propWidth = propertySheetView1.getPreferredSize().width; 97 int splitWidth = jSplitPane1.getPreferredSize().width; 98 int location = (int)((float)listWidth/(listWidth + propWidth) * splitWidth); 99 if(location > 0) { 100 jSplitPane1.setDividerLocation(location); 101 } 102 } 103 104 public HelpCtx getHelpCtx () { 105 return HelpCtx.DEFAULT_HELP; 106 } 107 108 110 public void setServiceType (ServiceType s) { 111 if (s == null) { 112 return; 113 } 114 115 int i = -1; for (int n = 0; n < services.size(); n++) { 117 if ((services.get(n)).getName().equals(s.getName())) { 118 i = n; 119 } 120 } 121 122 if (i < 0) { 123 ServiceType oldNone = none; 126 none = s; 127 update(); none = oldNone; 129 i = services.indexOf (s); 130 } 131 132 if (i < 0) { 133 Logger.getAnonymousLogger().warning("ServiceTypePanel: Unable to add service " + s.getName()); i = 0; 135 } 136 137 Node[] nodes = getExplorerManager ().getRootContext ().getChildren ().getNodes (); 138 if (i >= nodes.length) return; 139 140 try { 141 getExplorerManager ().setSelectedNodes (new Node[] { 142 nodes[i] 143 }); 144 } catch (java.beans.PropertyVetoException ex) { 145 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex); 146 } 147 148 firePropertyChange (); 149 } 150 151 154 public ServiceType getServiceType () { 155 Node[] arr = getExplorerManager ().getSelectedNodes (); 156 if (arr.length > 0) { 157 return ((MN) arr[0]).getServiceType (); 158 } 159 return null; 160 } 161 162 164 void firePropertyChange () { 165 firePropertyChange ("serviceType", null, null); } 167 168 170 private void update () { 171 Children ch = new Children.Array (); 172 AbstractNode n = new AbstractNode (ch); 173 174 ch.add ((Node[])nodes ().toArray (new Node[nodes().size()])); 175 176 getExplorerManager ().setRootContext (n); 177 setActivatedNodes(new Node[0]); 178 } 179 180 185 private List<Node> nodes () { 186 services = new ArrayList<ServiceType> (20); 187 List<Node> l = new LinkedList<Node> (); 188 ServiceType.Registry registry = Lookup.getDefault().lookup(ServiceType.Registry.class); 189 Enumeration<? extends ServiceType> en = registry.services (clazz); 190 while (en.hasMoreElements ()) { 191 try { 192 ServiceType service = en.nextElement (); 193 if (createNew) { 194 ServiceType newObject = service.getClass().newInstance(); 196 l.add(new MN(newObject)); 197 services.add(newObject); 198 } else { 199 l.add (new MN (service)); 200 services.add (service); 201 } 202 } catch (java.beans.IntrospectionException ex) { 203 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex); 204 } catch (InstantiationException ex) { 205 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex); 206 } catch (IllegalAccessException ex) { 207 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex); 208 } 209 } 210 try { 211 if (none != null) { 212 l.add (new MN (none)); 213 services.add (none); 214 } 215 } catch (java.beans.IntrospectionException ex) { 216 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex); 217 } 218 return l; 219 } 220 221 226 private void initComponents() { jSplitPane1 = new javax.swing.JSplitPane (); 228 listView1 = new org.openide.explorer.view.ListView(); 229 propertySheetView1 = new org.openide.explorer.propertysheet.PropertySheetView(); 230 label = new javax.swing.JLabel (); 231 232 setLayout(new java.awt.BorderLayout (0, 2)); 233 234 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 0, 11))); 235 jSplitPane1.setDividerSize(5); 236 jSplitPane1.addComponentListener(new java.awt.event.ComponentAdapter () { 237 public void componentResized(java.awt.event.ComponentEvent evt) { 238 jSplitPane1ComponentResized(evt); 239 } 240 }); 241 242 listView1.setDefaultProcessor(new java.awt.event.ActionListener () { public void actionPerformed(java.awt.event.ActionEvent e) {} }); 243 listView1.setPopupAllowed(false); 244 listView1.setSelectionMode(1); 245 listView1.setTraversalAllowed(false); 246 listView1.addComponentListener(new java.awt.event.ComponentAdapter () { 247 public void componentResized(java.awt.event.ComponentEvent evt) { 248 listView1ComponentResized(evt); 249 } 250 }); 251 252 jSplitPane1.setLeftComponent(listView1); 253 254 jSplitPane1.setRightComponent(propertySheetView1); 255 256 add(jSplitPane1, java.awt.BorderLayout.CENTER); 257 258 label.setLabelFor(listView1); 259 add(label, java.awt.BorderLayout.NORTH); 260 261 } 263 private void listView1ComponentResized(java.awt.event.ComponentEvent evt) { width_leftcomponent=listView1.getWidth(); 265 } 267 private void jSplitPane1ComponentResized(java.awt.event.ComponentEvent evt) { int width,locator; 269 if(width_components>0&&width_leftcomponent>0) { 270 width=listView1.getWidth()+propertySheetView1.getWidth(); 271 locator=width*width_leftcomponent/width_components; 272 jSplitPane1.setDividerLocation(locator); 273 width_leftcomponent=locator; 274 width_components=width; 275 } else { 276 width_leftcomponent=listView1.getWidth(); 277 width_components=width_leftcomponent+propertySheetView1.getWidth(); 278 } 279 } 281 282 private void removeButtonPressed (java.awt.event.ActionEvent evt) { } 285 private void addButtonPressed (java.awt.event.ActionEvent evt) { } 288 289 private javax.swing.JSplitPane jSplitPane1; 291 private javax.swing.JLabel label; 292 private org.openide.explorer.view.ListView listView1; 293 private org.openide.explorer.propertysheet.PropertySheetView propertySheetView1; 294 296 297 private final class MN extends BeanNode { 298 public MN (ServiceType t) throws java.beans.IntrospectionException { 299 super (t); 300 } 301 302 public ServiceType getServiceType () { 303 return (ServiceType)getBean (); 304 } 305 306 public Node.PropertySet[] getPropertySets () { 308 final Node.PropertySet[] sets = super.getPropertySets (); 309 if (createNew) { 310 return sets; } 312 Node.PropertySet[] nue = new Node.PropertySet[sets.length]; 313 for (int i = 0; i < sets.length; i++) { 314 final int ii = i; 315 nue[i] = new Node.PropertySet () { 316 { 317 this.setName (sets[ii].getName ()); 318 this.setDisplayName (sets[ii].getDisplayName ()); 319 this.setShortDescription (sets[ii].getShortDescription ()); 320 } 321 public Node.Property[] getProperties () { 322 Node.Property[] props = sets[ii].getProperties (); 323 List<Node.Property> nueprops = new ArrayList<Node.Property> (); 324 for (int j = 0; j < props.length; j++) 325 if (! "name".equals (props[j].getName ())) nueprops.add (props[j]); 327 return nueprops.toArray (new Node.Property[nueprops.size ()]); 328 } 329 }; 330 } 331 return nue; 332 } 333 } 334 } 335 | Popular Tags |