1 19 20 package org.netbeans.modules.form; 21 22 import java.beans.*; 23 import java.util.*; 24 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 25 import org.openide.util.NbBundle; 26 27 33 34 public class ComponentChooserEditor implements PropertyEditor, 35 FormAwareEditor, 36 XMLPropertyEditor, 37 NamedPropertyEditor 38 { 39 public static final int ALL_COMPONENTS = 0; 40 public static final int NONVISUAL_COMPONENTS = 3; 41 44 private static final String NULL_REF = "null"; private static final String INVALID_REF = "default"; 47 private static String noneText = null; 48 private static String invalidText = null; 49 private static String defaultText = null; 50 51 private FormModel formModel; 52 private List components; 53 private Class [] beanTypes = null; 54 private int componentCategory = 0; 55 56 private Object defaultValue; 57 private ComponentRef value; 58 59 private PropertyChangeSupport changeSupport; 60 61 public ComponentChooserEditor() { 62 } 63 64 public ComponentChooserEditor(Class [] componentTypes) { 65 beanTypes = componentTypes; 66 } 67 68 71 public void setValue(Object value) { 72 defaultValue = null; 73 if (value == null || value instanceof ComponentRef) 74 this.value = (ComponentRef) value; 75 76 else if (value instanceof RADComponent) 77 this.value = new ComponentRef((RADComponent)value); 78 else if (value instanceof String ) 79 this.value = new ComponentRef((String )value); 80 else { 81 this.value = null; 82 defaultValue = value; 83 } 84 85 87 firePropertyChange(); 88 } 89 90 public Object getValue() { 91 if (value != null && INVALID_REF.equals(value.getDescription())) 92 return BeanSupport.NO_VALUE; 94 return isDefaultValue() ? defaultValue : value; 95 } 96 97 public String [] getTags() { 98 List compList = getComponents(); 99 100 int extraValues = 0; 101 int count = 0; 102 String [] names; 103 104 if( isDefaultValue() ) { 105 extraValues = 2; 106 count = compList.size() + extraValues; 107 names = new String [count]; 108 names[0] = defaultString(); 109 } else { 110 extraValues = 1; 111 count = compList.size() + extraValues; 112 names = new String [count]; 113 } 114 names[extraValues - 1] = noneString(); 115 116 if (count > extraValues) { 117 for (int i=extraValues; i < count; i++) 118 names[i] = ((RADComponent)compList.get(i-extraValues)).getName(); 119 Arrays.sort(names, 1, count); 120 } 121 122 return names; 123 } 124 125 private boolean isDefaultValue() { 126 return value == null && defaultValue != null; 127 } 128 129 public String getAsText() { 130 if (isDefaultValue()) 131 return defaultString(); 132 if (value == null) 133 return noneString(); 134 if (value.getComponent() == null) 135 return invalidString(); 136 137 String str = value.getDescription(); 138 return NULL_REF.equals(str) ? noneString() : str; 139 } 140 141 public void setAsText(String str) { 142 if (str == null || str.equals("") || str.equals(noneString())) setValue(null); 144 else { 145 if(defaultString().equals(str)) { 146 setValue(defaultValue); 148 } else { 149 setValue(str); 150 } 151 152 } 153 154 } 155 156 public String getJavaInitializationString() { 157 return value != null ? value.getJavaInitString() : null; 158 } 159 160 public void addPropertyChangeListener(PropertyChangeListener l) { 161 synchronized (this) { 162 if (changeSupport == null) 163 changeSupport = new PropertyChangeSupport(this); 164 } 165 changeSupport.addPropertyChangeListener(l); 166 } 167 168 public void removePropertyChangeListener(PropertyChangeListener l) { 169 if (changeSupport != null) 170 changeSupport.removePropertyChangeListener(l); 171 } 172 173 public boolean isPaintable() { 174 return false; 175 } 176 177 public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { 178 } 179 180 public java.awt.Component getCustomEditor() { 181 return null; 182 } 183 184 public boolean supportsCustomEditor() { 185 return false; 186 } 187 188 190 public void setFormModel(FormModel model) { 192 formModel = model; 193 } 194 195 public FormModel getFormModel() { 196 return formModel; 197 } 198 199 public void setBeanTypes(Class [] types) { 200 beanTypes = types; 201 } 202 203 public Class [] getBeanTypes() { 204 return beanTypes; 205 } 206 207 public void setComponentCategory(int cat) { 208 componentCategory = cat; 209 } 210 211 public int getComponentCategory() { 212 return componentCategory; 213 } 214 215 218 private static final String XML_COMPONENT = "ComponentRef"; private static final String ATTR_NAME = "name"; 221 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 222 String nameStr; 223 if (value != null) 224 nameStr = value.getComponent() != null ? 225 value.getDescription() : INVALID_REF; 226 else 227 nameStr = NULL_REF; 228 229 org.w3c.dom.Element el = doc.createElement(XML_COMPONENT); 230 el.setAttribute(ATTR_NAME, nameStr); 231 return el; 232 } 233 234 public void readFromXML(org.w3c.dom.Node element) 235 throws java.io.IOException 236 { 237 if (!XML_COMPONENT.equals(element.getNodeName())) 238 throw new java.io.IOException (); 239 240 org.w3c.dom.NamedNodeMap attributes; 241 org.w3c.dom.Node nameAttr; 242 String name; 243 244 if ((attributes = element.getAttributes()) != null 245 && (nameAttr = attributes.getNamedItem(ATTR_NAME)) != null 246 && (name = nameAttr.getNodeValue()) != null) 247 { 248 value = new ComponentRef(name); 249 } 250 } 251 252 254 protected List getComponents() { 255 if (components == null) 256 components = new ArrayList(); 257 else 258 components.clear(); 259 260 if (formModel != null) { 261 Collection<RADComponent> comps; 262 if (componentCategory == NONVISUAL_COMPONENTS) 263 comps = formModel.getNonVisualComponents(); 264 else 265 comps = formModel.getAllComponents(); 266 267 for (RADComponent metacomp : comps) 268 if (acceptBean(metacomp)) 269 components.add(metacomp); 270 } 271 272 return components; 273 } 274 275 protected boolean acceptBean(RADComponent comp) { 276 if (beanTypes == null) 277 return true; 278 279 boolean match = false; 280 for (int i=0; i < beanTypes.length && !match; i++) 281 match = beanTypes[i].isAssignableFrom(comp.getBeanClass()); 282 283 return match; 284 } 285 286 protected String noneString() { 287 if (noneText == null) 288 noneText = FormUtils.getBundleString("CTL_NoComponent"); return noneText; 290 } 291 292 protected String defaultString() { 293 if (defaultText == null) 294 defaultText = FormUtils.getBundleString("CTL_DefaultComponent"); return defaultText; 296 } 297 298 protected String invalidString() { 299 if (invalidText == null) 300 invalidText = FormUtils.getBundleString("CTL_InvalidReference"); return invalidText; 302 } 303 304 306 protected final void firePropertyChange() { 307 if (changeSupport != null) 308 changeSupport.firePropertyChange(null, null, null); 309 } 310 311 public String getDisplayName() { 313 return NbBundle.getBundle(getClass()).getString("CTL_ComponentChooserEditor_DisplayName"); } 315 316 318 private class ComponentRef implements RADComponent.ComponentReference, 319 FormDesignValue 320 { 321 private String componentName; 322 private RADComponent component; 323 324 ComponentRef(String name) { 325 componentName = name; 326 } 327 328 ComponentRef(RADComponent metacomp) { 329 componentName = metacomp.getName(); 330 component = metacomp; 331 } 332 333 public FormDesignValue copy(FormProperty formProperty) { 334 return null; 335 } 336 337 public boolean equals(Object obj) { 338 boolean equal; 339 340 if (obj instanceof ComponentRef) { 341 ComponentRef ref = (ComponentRef)obj; 342 343 equal = (ref.component == component); 344 if (componentName == null) { 345 equal = equal && (ref.componentName == null); 346 } else { 347 equal = equal && componentName.equals(ref.componentName); 348 } 349 } else { 350 equal = (obj instanceof RADComponent && obj == component); 351 } 352 353 return equal; 354 } 355 356 String getJavaInitString() { 357 checkComponent(); 358 359 if (component != null) { 360 if (component == component.getFormModel().getTopRADComponent()) 361 return "this"; } 363 else if (!NULL_REF.equals(componentName)) 364 return null; 366 return componentName; 367 } 368 369 public RADComponent getComponent() { 370 checkComponent(); 371 return component; 372 } 373 374 375 public String getDescription() { 376 checkComponent(); 377 return componentName; 378 } 379 380 381 public Object getDesignValue() { 382 checkComponent(); 383 return component != null ? 384 component.getBeanInstance(): IGNORED_VALUE; 385 } 386 387 private void checkComponent() { 388 if (component == null 389 && !NULL_REF.equals(componentName) 390 && !INVALID_REF.equals(componentName)) 391 { 392 List compList = getComponents(); 393 Iterator it = compList.iterator(); 394 while (it.hasNext()) { 395 RADComponent comp = (RADComponent) it.next(); 396 if (comp.getName().equals(componentName)) { 397 if (comp.isInModel()) 398 component = comp; 399 break; 400 } 401 } 402 } 403 else if (component != null) { 404 if (!component.isInModel()) 405 component = null; 406 else 407 componentName = component.getName(); 408 } 409 } 410 } 411 } 412 | Popular Tags |