1 19 package org.openide.explorer.propertysheet; 20 21 import org.openide.nodes.*; 22 import org.openide.util.NbBundle; 23 import org.openide.util.Utilities; 24 import org.openide.util.actions.SystemAction; 25 import org.openide.util.datatransfer.NewType; 26 27 import java.awt.Component ; 28 29 import java.beans.*; 30 31 import java.lang.reflect.Array ; 32 import java.lang.reflect.InvocationTargetException ; 33 34 import java.util.*; 35 36 import javax.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 38 import org.netbeans.modules.openide.explorer.UIException; 39 40 41 44 class IndexedPropertyEditor extends Object implements ExPropertyEditor { 45 private Object [] array; 50 private PropertyEnv env; 51 private PropertyChangeSupport propertySupport = new PropertyChangeSupport(this); 52 private Node.IndexedProperty indexedProperty = null; 53 private IndexedEditorPanel currentEditorPanel; 54 55 public IndexedPropertyEditor() { 58 } 59 60 public void attachEnv(PropertyEnv env) { 63 this.env = env; 64 env.setChangeImmediate(false); 65 66 FeatureDescriptor details = env.getFeatureDescriptor(); 67 68 if (details instanceof Node.IndexedProperty) { 69 indexedProperty = (Node.IndexedProperty) details; 70 } else { 71 throw new IllegalStateException ("This is not an array: " + details); } 73 } 74 75 public void setValue(Object value) { 78 if (value == null) { 79 array = null; 80 firePropertyChange(); 81 82 return; 83 } 84 85 if (!value.getClass().isArray()) { 86 throw new IllegalArgumentException ( 87 (env != null) ? ("Property whose value is not an array " + env.getFeatureDescriptor().getName()) 88 : "Unknown property - not attached yet." 89 ); } 91 92 if (value.getClass().getComponentType().isPrimitive()) { 93 array = Utilities.toObjectArray(value); 94 } else { 95 array = (Object []) Array.newInstance(value.getClass().getComponentType(), ((Object []) value).length); 96 System.arraycopy(value, 0, array, 0, array.length); 97 } 98 99 firePropertyChange(); 100 } 101 102 public Object getValue() { 103 if (array == null) { 104 return null; 105 } 106 107 if (indexedProperty.getElementType().isPrimitive()) { 108 return Utilities.toPrimitiveArray(array); 109 } 110 111 return array; 112 } 113 114 public boolean isPaintable() { 115 return false; 116 } 117 118 public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { 119 } 120 121 public String getJavaInitializationString(int index) { 122 if (array[index] == null) { 123 return "null"; } 125 126 try { 127 indexedProperty.getIndexedPropertyEditor().setValue(array[index]); 128 129 return indexedProperty.getIndexedPropertyEditor().getJavaInitializationString(); 130 } catch (NullPointerException e) { 131 return "null"; } 133 } 134 135 public String getJavaInitializationString() { 136 if (array == null) { 137 return ""; } 139 140 StringBuffer buf = new StringBuffer ("new "); buf.append(indexedProperty.getElementType().getName()); 142 143 if (array.length == 0) { 145 buf.append("[0]"); } else 147 { 149 buf.append("[] {\n\t"); 151 for (int i = 0; i < array.length; i++) { 152 try { 153 indexedProperty.getIndexedPropertyEditor().setValue(array[i]); 154 buf.append(indexedProperty.getIndexedPropertyEditor().getJavaInitializationString()); 155 } catch (NullPointerException e) { 156 buf.append("null"); } 158 159 if (i != (array.length - 1)) { 160 buf.append(",\n\t"); } else { 162 buf.append("\n"); } 164 } 165 166 buf.append("}"); } 168 169 return buf.toString(); 170 } 171 172 public String getAsText() { 173 if (array == null) { 174 return "null"; } 176 177 StringBuffer buf = new StringBuffer ("["); PropertyEditor p = null; 179 180 if (indexedProperty != null) { 181 p = indexedProperty.getIndexedPropertyEditor(); 182 } 183 184 for (int i = 0; i < array.length; i++) { 185 if (p != null) { 186 p.setValue(array[i]); 187 188 buf.append(p.getAsText()); 190 } else { 191 buf.append("null"); } 193 194 if (i != (array.length - 1)) { 195 buf.append(", "); } 197 } 198 199 buf.append("]"); 201 return buf.toString(); 202 } 203 204 public void setAsText(String text) throws java.lang.IllegalArgumentException { 205 if (text.equals("null")) { setValue(null); 207 208 return; 209 } 210 211 if (text.equals("[]")) { setValue(Array.newInstance(indexedProperty.getElementType(), 0)); 213 214 return; 215 } 216 217 int i1 = text.indexOf('['); 218 219 if ((i1 < 0) || ((i1 + 1) >= text.length())) { 220 i1 = 0; 221 } else { 222 i1++; 223 } 224 225 int i2 = text.lastIndexOf(']'); 226 227 if (i2 < 0) { 228 i2 = text.length(); 229 } 230 231 if ((i2 < i1) || (i2 > text.length())) { 232 return; 233 } else { 234 } 235 236 try { 237 PropertyEditor p = indexedProperty.getIndexedPropertyEditor(); 238 239 if (p == null) { throw new IllegalStateException ("Indexed type has no property " + "editor"); 241 } 242 243 text = text.substring(i1, i2); 244 245 StringTokenizer tok = new StringTokenizer(text, ","); java.util.List <Object > list = new LinkedList<Object >(); 247 248 while (tok.hasMoreTokens()) { 249 String s = tok.nextToken(); 250 p.setAsText(s.trim()); 251 list.add(p.getValue()); 252 } 253 254 Object [] a = list.toArray((Object []) Array.newInstance(getConvertedType(), list.size())); 255 setValue(a); 256 } catch (Exception x) { 257 IllegalArgumentException iae = new IllegalArgumentException (); 258 UIException.annotateUser(iae, getString("EXC_ErrorInIndexedSetter"), 259 getString("EXC_ErrorInIndexedSetter"), x, 260 new Date()); 261 throw iae; 262 } 263 } 264 265 public String [] getTags() { 266 return null; 267 } 268 269 public Component getCustomEditor() { 270 if (array == null) { 271 array = (Object []) Array.newInstance(getConvertedType(), 0); 272 firePropertyChange(); 273 } 274 275 Node dummy = new DisplayIndexedNode(0); 276 277 Node.Property prop = dummy.getPropertySets()[0].getProperties()[0]; 281 282 Node.Property[] np = new Node.Property[] { prop }; 283 currentEditorPanel = new IndexedEditorPanel(createRootNode(), np); 284 285 return currentEditorPanel; 286 } 287 288 public boolean supportsCustomEditor() { 289 return true; 290 } 291 292 public void addPropertyChangeListener(PropertyChangeListener listener) { 293 propertySupport.addPropertyChangeListener(listener); 294 } 295 296 public void removePropertyChangeListener(PropertyChangeListener listener) { 297 propertySupport.removePropertyChangeListener(listener); 298 } 299 300 private Node createRootNode() { 302 DisplayIndexedNode[] n = new DisplayIndexedNode[array.length]; 303 304 for (int i = 0; i < n.length; i++) { 305 n[i] = new DisplayIndexedNode(i); 306 } 307 308 MyIndexedRootNode idr = new MyIndexedRootNode(n); 309 Index ind = (Index) idr.getCookie(Index.class); 310 311 for (int i = 0; i < n.length; i++) { 312 ind.addChangeListener(org.openide.util.WeakListeners.change(n[i], ind)); 313 } 314 315 return idr; 316 } 317 318 321 private Class getConvertedType() { 322 Class type = indexedProperty.getElementType(); 323 324 if (type.isPrimitive()) { 325 type = Utilities.getObjectType(type); 326 } 327 328 return type; 329 } 330 331 void firePropertyChange() { 332 propertySupport.firePropertyChange("value", null, null); } 334 335 private static String getString(String key) { 336 return NbBundle.getBundle(IndexedPropertyEditor.class).getString(key); 337 } 338 339 343 private Object defaultValue() { 344 Object value = null; 345 346 if (indexedProperty.getElementType().isPrimitive()) { 347 if (getConvertedType().equals(Integer .class)) { 348 value = new Integer (0); 349 } 350 351 if (getConvertedType().equals(Boolean .class)) { 352 value = Boolean.FALSE; 353 } 354 355 if (getConvertedType().equals(Byte .class)) { 356 value = new Byte ((byte) 0); 357 } 358 359 if (getConvertedType().equals(Character .class)) { 360 value = new Character ('\u0000'); 361 } 362 363 if (getConvertedType().equals(Double .class)) { 364 value = new Double (0d); 365 } 366 367 if (getConvertedType().equals(Float .class)) { 368 value = new Float (0f); 369 } 370 371 if (getConvertedType().equals(Long .class)) { 372 value = new Long (0L); 373 } 374 375 if (getConvertedType().equals(Short .class)) { 376 value = new Short ((short) 0); 377 } 378 } else { 379 try { 380 value = getConvertedType().newInstance(); 381 } catch (Exception x) { 382 } 385 } 386 387 return value; 388 } 389 390 394 class DisplayIndexedNode extends AbstractNode implements ChangeListener { 395 private int index; 396 397 public DisplayIndexedNode(int index) { 398 super(Children.LEAF); 399 this.index = index; 400 setName(Integer.toString(index)); 401 setDisplayName(Integer.toString(index)); 402 } 403 404 protected SystemAction[] createActions() { 405 try { 406 return new SystemAction[] { 407 SystemAction.get(Class.forName("org.openide.actions.MoveUpAction").asSubclass(SystemAction.class)), SystemAction.get(Class.forName("org.openide.actions.MoveDownAction").asSubclass(SystemAction.class)), }; 410 } catch (ClassNotFoundException cnfe) { 411 } 414 415 return null; 416 } 417 418 protected Sheet createSheet() { 420 Sheet sheet = super.createSheet(); 421 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 422 423 if (props == null) { 424 props = Sheet.createPropertiesSet(); 425 sheet.put(props); 426 } 427 428 props.put(new ValueProp()); 429 430 return sheet; 431 } 432 433 438 public void destroy() throws java.io.IOException { 439 Object [] newArray = (Object []) Array.newInstance(getConvertedType(), array.length - 1); 440 System.arraycopy(array, 0, newArray, 0, index); 441 System.arraycopy(array, index + 1, newArray, index, array.length - index - 1); 442 443 array = newArray; 445 IndexedPropertyEditor.this.firePropertyChange(); 446 447 if (currentEditorPanel != null) { 448 currentEditorPanel.getExplorerManager().setRootContext(createRootNode()); 449 } 450 } 451 452 456 public void stateChanged(ChangeEvent e) { 457 Node parent = getParentNode(); 458 Index i = (Index) parent.getCookie(Index.class); 459 460 if (i != null) { 461 int currentIndex = i.indexOf(this); 462 463 if (currentIndex != index) { 464 if (currentIndex > index) { 465 Object tmp = array[index]; 469 array[index] = array[currentIndex]; 470 array[currentIndex] = tmp; 471 } 472 473 index = currentIndex; 475 DisplayIndexedNode.this.firePropertyChange(null, null, null); 476 setDisplayName(Integer.toString(index)); 477 IndexedPropertyEditor.this.firePropertyChange(); 478 } 479 } 480 } 481 482 private class ValueProp extends PropertySupport { 483 public ValueProp() { 484 super( 485 indexedProperty.getName(), indexedProperty.getElementType(), indexedProperty.getDisplayName(), 486 indexedProperty.getShortDescription(), indexedProperty.canRead(), indexedProperty.canWrite() 487 ); 488 } 489 490 public Object getValue() { 491 if (index < array.length) { 492 return array[index]; 493 } else { 494 return null; 495 } 496 } 497 498 public void setValue(Object value) 499 throws IllegalArgumentException , IllegalAccessException , InvocationTargetException { 500 Object oldVal = array[index]; 501 array[index] = value; 502 DisplayIndexedNode.this.firePropertyChange(this.getName(), oldVal, value); 503 IndexedPropertyEditor.this.firePropertyChange(); 504 } 505 506 public PropertyEditor getPropertyEditor() { 507 return indexedProperty.getIndexedPropertyEditor(); 508 } 509 } 510 } 511 512 515 private class MyIndexedRootNode extends IndexedNode { 516 public MyIndexedRootNode(Node[] ch) { 517 getChildren().add(ch); 518 setName("IndexedRoot"); setDisplayName(NbBundle.getBundle(IndexedPropertyEditor.class).getString("CTL_Index")); 520 } 521 522 public NewType[] getNewTypes() { 523 NewType nt = new NewType() { 524 public void create() { 525 if (array != null) { 526 Object [] newArray = (Object []) Array.newInstance(getConvertedType(), array.length + 1); 527 System.arraycopy(array, 0, newArray, 0, array.length); 528 529 array = newArray; 531 array[array.length - 1] = defaultValue(); 532 } else { 533 array = (Object []) Array.newInstance(getConvertedType(), 1); 535 array[0] = defaultValue(); 536 } 537 538 IndexedPropertyEditor.this.firePropertyChange(); 539 540 DisplayIndexedNode din = new DisplayIndexedNode(array.length - 1); 541 getChildren().add(new Node[] { din }); 542 543 Index i = (Index) getCookie(Index.class); 544 i.addChangeListener(org.openide.util.WeakListeners.change(din, i)); 545 } 546 }; 547 548 return new NewType[] { nt }; 549 } 550 } 551 } 552 | Popular Tags |