1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.*; 23 import java.util.Enumeration ; 24 25 import org.openide.ServiceType; 26 import org.openide.explorer.propertysheet.*; 27 import org.openide.util.Lookup; 28 29 30 34 @SuppressWarnings ("deprecation") 35 public class ServiceTypeEditor extends java.beans.PropertyEditorSupport implements ExPropertyEditor { 36 37 38 private static final String PROPERTY_NEW_TYPE = "createNew"; 40 private static final String PROPERTY_SUPERCLASS = "superClass"; 42 43 private String [] tags; 44 45 46 private Class <? extends ServiceType> clazz; 47 48 49 private String message; 50 51 52 private PropertyEnv env; 53 54 59 private boolean createNewInstance = false; 60 61 63 public ServiceTypeEditor() { 64 this (ServiceType.class, "LAB_ChooseServiceType"); } 66 67 71 public ServiceTypeEditor(Class <?> clazz, String message) { 72 this.clazz = clazz.asSubclass(ServiceType.class); 73 this.message = message; 74 } 75 76 81 public void attachEnv(PropertyEnv env) { 82 this.env = env; 83 Object newObj = env.getFeatureDescriptor().getValue(PROPERTY_NEW_TYPE); 84 if (newObj instanceof Boolean ) { 85 createNewInstance = ((Boolean )newObj).booleanValue(); 86 } 87 Object sup = env.getFeatureDescriptor().getValue(PROPERTY_SUPERCLASS); 88 if (sup instanceof Class ) { 89 @SuppressWarnings ("unchecked") Class <? extends ServiceType> c = (Class <? extends ServiceType>)sup; 90 clazz = c; 91 } 92 } 93 94 96 private void updateTags () { 97 java.util.LinkedList <String > names = new java.util.LinkedList <String > (); 98 ServiceType.Registry registry = Lookup.getDefault () 99 .lookup (ServiceType.Registry.class); 100 Enumeration ee = registry.services (clazz); 101 while (ee.hasMoreElements()) { 102 ServiceType e = (ServiceType) ee.nextElement(); 103 names.add(e.getName()); 104 } 105 names.toArray(tags = new String [names.size()]); 106 } 107 108 110 116 public String getAsText() { 117 if (createNewInstance) { 118 return null; 119 } 120 ServiceType s = (ServiceType)getValue (); 121 if (s == null) { 122 return getString ("LAB_DefaultServiceType"); 123 } else { 124 return s.getName(); 125 } 126 } 127 128 134 public void setAsText(String text) { 135 if (createNewInstance) { 136 throw new IllegalArgumentException (); 138 } 139 140 ServiceType.Registry registry = Lookup.getDefault().lookup(ServiceType.Registry.class); 141 Enumeration en = registry.services (clazz); 142 while (en.hasMoreElements ()) { 143 ServiceType t = (ServiceType)en.nextElement (); 144 if (text.equals (t.getName ())) { 145 setValue (t); 146 return; 147 } 148 } 149 setValue (null); 150 } 151 152 153 public String [] getTags() { 154 if (!createNewInstance) { 155 updateTags (); 156 return tags; 157 } 158 return null; 159 } 160 161 public boolean supportsCustomEditor () { 162 return true; 163 } 164 165 public java.awt.Component getCustomEditor () { 166 final ServiceTypePanel s = new ServiceTypePanel (clazz, getString(message), null, createNewInstance); 167 168 s.setServiceType ((ServiceType)getValue ()); 169 s.addPropertyChangeListener (new PropertyChangeListener () { 171 public void propertyChange (PropertyChangeEvent ev) { 172 if ("serviceType".equals (ev.getPropertyName ())) { 173 setValue (s.getServiceType ()); 174 } 175 } 176 }); 177 return s; 178 } 179 180 private static String getString (String s) { 181 return org.openide.util.NbBundle.getBundle (ServiceTypeEditor.class).getString (s); 182 } 183 } 184 | Popular Tags |