1 19 20 package org.netbeans.modules.form.editors2; 21 22 import java.awt.*; 23 import java.awt.event.ComponentAdapter ; 24 import java.awt.event.ComponentEvent ; 25 import java.beans.*; 26 import java.util.*; 27 import javax.swing.*; 28 import java.text.MessageFormat ; 29 import java.io.IOException ; 30 import javax.swing.border.*; 31 32 import org.openide.ErrorManager; 33 import org.openide.awt.Mnemonics; 34 import org.openide.nodes.*; 35 import org.openide.explorer.view.ListView; 36 import org.openide.explorer.propertysheet.PropertySheetView; 37 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 38 import org.openide.explorer.*; 39 40 import org.netbeans.modules.form.*; 41 import org.netbeans.modules.form.palette.*; 42 import org.openide.nodes.Node.PropertySet; 43 44 51 52 public final class BorderEditor extends PropertyEditorSupport 53 implements FormAwareEditor, 54 XMLPropertyEditor, 55 NamedPropertyEditor, 56 BeanPropertyEditor 57 { 58 59 private static final String UNKNOWN_BORDER_BASE = 60 "org/netbeans/modules/form/editors2/unknownBorder.gif"; 62 private static final String NO_BORDER_BASE = 63 "org/netbeans/modules/form/editors2/nullBorder.gif"; 65 private static FormProperty[] EMPTY_PROPERTIES = new FormProperty[0]; 66 67 70 private Object current; 71 72 private FormPropertyContext propertyContext; 73 private BorderDesignSupport borderSupport; 74 75 private BorderPanel bPanel; 77 78 81 public BorderEditor() { 82 bPanel = null; 83 current = null; 84 } 85 86 public void setFormModel(FormModel model) { 88 propertyContext = createFormPropertyContext(model); 89 } 90 91 public static FormPropertyContext createFormPropertyContext(FormModel model) { 92 return new FormPropertyContext.DefaultImpl(model); 93 } 94 95 98 public Object getValue() { 99 return current; 100 } 101 102 public void setValue(Object value) { 103 if (current == value) 104 return; 105 106 current = value; 107 borderSupport = null; 108 109 if (value instanceof BorderDesignSupport) { 110 borderSupport = (BorderDesignSupport) value; 111 } else if (value != null) { 112 assert (value instanceof Border); 113 if (!(value instanceof javax.swing.plaf.UIResource )) 114 borderSupport = new BorderDesignSupport((Border)value); 115 } 116 117 if (borderSupport != null) { 118 borderSupport.setPropertyContext(propertyContext); 119 120 if (bPanel != null) 121 bPanel.setValue(value); 122 } 123 } 124 125 public String getAsText() { 126 return null; 127 } 128 129 public void setAsText(String string) { 130 } 131 132 public boolean isPaintable() { 133 return true; 134 } 135 136 public void paintValue(Graphics g, Rectangle rectangle) { 137 138 String valueText; 139 Object value = getValue(); 140 141 if (value == null) 142 valueText = getBundle().getString("LAB_NoBorder"); else if (borderSupport != null) 144 valueText = "[" + borderSupport.getDisplayName() + "]"; else 146 valueText = "[" + org.openide.util.Utilities.getShortClassName( value.getClass()) + "]"; 149 150 FontMetrics fm = g.getFontMetrics(); 151 g.drawString(valueText, rectangle.x, 152 rectangle.y + (rectangle.height - fm.getHeight()) / 2 + fm.getAscent()); 153 } 154 155 public String getJavaInitializationString() { 156 Object value = getValue(); 157 if (value == null) 158 return "null"; if (borderSupport != null) 160 return borderSupport.getJavaInitializationString(); 161 162 return null; 164 } 165 166 public boolean supportsCustomEditor() { 167 return true; 168 } 169 170 public Component getCustomEditor() { 171 if (bPanel == null) 172 bPanel = new BorderPanel(); 173 174 bPanel.setValue(current); 175 return bPanel; 176 } 177 178 181 182 public String getDisplayName() { 183 return getBundle().getString("CTL_BorderEditor_DisplayName"); } 185 186 188 192 void updateBorder(Node node) { 193 if (node instanceof NoBorderNode) { 194 borderSupport = null; 195 current = null; 196 } 197 else if (node instanceof UnknownBorderNode) { 198 current = ((UnknownBorderNode)node).getBorder(); 199 } 200 else { 201 borderSupport = ((BorderNode)node).getBorderSupport(); 202 current = borderSupport; 203 } 204 } 205 206 208 private static ResourceBundle getBundle() { 209 return org.openide.util.NbBundle.getBundle(BorderEditor.class); 210 } 211 212 215 final class BorderPanel extends JPanel 216 implements PropertyChangeListener, 217 VetoableChangeListener, 218 ExplorerManager.Provider 219 { 220 private ExplorerManager manager = new ExplorerManager (); 221 private Node selectNode = null; 222 private BorderPanel() { 223 getExplorerManager().addPropertyChangeListener(this); 224 getExplorerManager().addVetoableChangeListener(this); 225 226 setLayout(new BorderLayout()); 227 setBorder(new EmptyBorder(5, 5, 5, 5)); 228 229 ResourceBundle bundle = getBundle(); 230 231 ListView listView = new ListView(); 232 listView.getAccessibleContext().setAccessibleDescription( 233 bundle.getString("ACSD_AvailableBorders")); 235 JLabel label = new JLabel(); 236 Mnemonics.setLocalizedText(label, bundle.getString("LAB_AvailableBorders")); label.setLabelFor(listView); 238 239 JPanel panel = new JPanel(); 240 panel.setLayout(new BorderLayout(0, 2)); 241 panel.add(label, BorderLayout.NORTH); 242 panel.add(BorderLayout.CENTER, listView); 243 244 PropertySheetView sheetView = new PropertySheetView(); 245 246 JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 247 split.setTopComponent(panel); 248 split.setBottomComponent(sheetView); 249 split.setUI(new javax.swing.plaf.basic.BasicSplitPaneUI ()); 250 split.setBorder(BorderFactory.createEmptyBorder()); 251 split.setDividerLocation(170); 252 split.setContinuousLayout(true); 253 254 add(BorderLayout.CENTER, split); 255 256 getAccessibleContext().setAccessibleDescription( 257 bundle.getString("ACSD_BorderCustomEditor")); } 259 260 public void addNotify() { 261 super.addNotify(); 262 EventQueue.invokeLater(new Runnable (){ 263 public void run() { 264 try { 265 getExplorerManager().setSelectedNodes(new Node[] { selectNode }); 266 } 267 catch (PropertyVetoException e) {} } 269 }); 270 } 271 272 void setValue(Object border) { 273 ArrayList bordersList = new ArrayList(10); 274 selectNode = null; 275 276 PaletteItem[] items = PaletteUtils.getAllItems(); 277 for (int i = 0; i < items.length; i++) { 278 PaletteItem paletteItem = items[i]; 279 if (!paletteItem.isBorder()) 280 continue; 281 282 BorderDesignSupport nodeBDS = null; 283 try { 284 nodeBDS = new BorderDesignSupport(paletteItem.getComponentClass()); 288 } 289 catch (Exception ex) { 290 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 291 } 292 if (nodeBDS == null) 293 continue; 294 295 Node borderNode; 296 if (borderSupport != null 297 && borderSupport.getBorderClass() == nodeBDS.getBorderClass()) 298 { 299 try { 300 nodeBDS.setPropertyContext(propertyContext); 301 FormUtils.copyProperties(borderSupport.getProperties(), 302 nodeBDS.getProperties(), 303 FormUtils.CHANGED_ONLY|FormUtils.DISABLE_CHANGE_FIRING|FormUtils.DONT_CLONE_VALUES); 304 } 305 catch (Exception ex) { 306 ex.printStackTrace(); 307 continue; 308 } 309 borderNode = new BorderNode(nodeBDS, paletteItem.getNode()); 310 selectNode = borderNode; 311 } 312 else { 313 nodeBDS.setPropertyContext(propertyContext); 314 borderNode = new BorderNode(nodeBDS, paletteItem.getNode()); 315 } 316 317 bordersList.add(borderNode); 318 } 319 320 final Node root = new AbstractNode(new Children.Array()); 321 Node noBorder = new NoBorderNode(); 322 if (border == null) 323 selectNode = noBorder; 324 root.getChildren().add(new Node[] { noBorder }); 325 326 Node[] bordersArray = new Node[bordersList.size()]; 327 bordersList.toArray(bordersArray); 328 Arrays.sort(bordersArray, new Comparator() { 329 public int compare(Object o1, Object o2) { 330 return ((Node)o1).getDisplayName().compareTo( 331 ((Node)o2).getDisplayName()); 332 } 333 }); 334 root.getChildren().add(bordersArray); 335 336 if (selectNode == null) { 337 Node unknownBorder = new UnknownBorderNode(border); 338 root.getChildren().add(new Node[] { unknownBorder }); 339 selectNode = unknownBorder; 340 } 341 342 getExplorerManager().setRootContext(root); 343 } 344 345 public void propertyChange(PropertyChangeEvent evt) { 347 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 348 Node[] nodes = (Node[]) evt.getNewValue(); 349 if (nodes.length == 1) 350 updateBorder(nodes[0]); 351 } 359 } 360 361 public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException { 363 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 364 Node[] nodes =(Node[]) evt.getNewValue(); 365 if (nodes.length != 1) 366 throw new PropertyVetoException("", evt); } 368 } 369 370 public Dimension getPreferredSize() { 371 return new Dimension(360, 440); 372 } 373 374 public ExplorerManager getExplorerManager() { 375 return manager; 376 } 377 } 378 379 final class BorderNode extends FilterNode implements PropertyChangeListener { 380 381 private BorderDesignSupport nodeBorder; 382 private PropertySet[] properties; 383 384 BorderNode(BorderDesignSupport bds, Node paletteItemNode) { 385 super(paletteItemNode, Children.LEAF); 386 nodeBorder = bds; 387 } 388 389 public PropertySet[] getPropertySets () { 390 if (properties == null) { 391 Node.Property[] props = nodeBorder.getProperties(); 392 Sheet.Set propSet = Sheet.createPropertiesSet(); 393 propSet.put(props); 394 395 for (int i=0; i < props.length; i++) 396 if (props[i] instanceof FormProperty) 397 ((FormProperty)props[i]).addPropertyChangeListener(this); 398 399 properties = new PropertySet[] { propSet }; 400 } 401 return properties; 402 } 403 404 public BorderDesignSupport getBorderSupport() { 405 return nodeBorder; 406 } 407 408 public void propertyChange(PropertyChangeEvent evt) { 409 updateBorder(this); 411 } 412 } 413 414 static final class NoBorderNode extends AbstractNode { 415 NoBorderNode() { 416 super(Children.LEAF); 417 setDisplayName(getBundle().getString("LAB_NoBorder")); setIconBaseWithExtension(NO_BORDER_BASE); 419 } 420 } 421 422 static final class UnknownBorderNode extends AbstractNode { 423 private Object border; 424 425 UnknownBorderNode(Object border) { 426 super(Children.LEAF); 427 setBorder(border); 428 setIconBaseWithExtension(UNKNOWN_BORDER_BASE); 429 } 430 431 void setBorder(Object border) { 432 this.border = border; 433 String longName = border.getClass().getName(); 434 int dot = longName.lastIndexOf('.'); 435 String shortName =(dot < 0) ? longName : longName.substring(dot + 1); 436 setDisplayName(new MessageFormat ( 437 getBundle().getString("LAB_FMT_UnknownBorder")) .format(new Object [] { longName, shortName })); 439 } 440 441 Object getBorder() { 442 return border; 443 } 444 } 445 446 449 private static final String XML_BORDER = "Border"; private static final String ATTR_INFO = "info"; private static final String PROP_NAME = "PropertyName"; 453 459 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 460 Object value = getValue(); 461 if ((value instanceof BorderDesignSupport || value instanceof Border) 462 && borderSupport != null) 463 { 464 org.w3c.dom.Node storedNode = null; 465 466 Class borderClass = borderSupport.getBorderClass(); 469 470 if (borderClass.isAssignableFrom(TitledBorder.class)) 471 storedNode = storeTitledBorder(doc); 472 else if (borderClass.isAssignableFrom(EtchedBorder.class)) 473 storedNode = storeEtchedBorder(doc); 474 else if (borderClass.isAssignableFrom(LineBorder.class)) 475 storedNode = storeLineBorder(doc); 476 else if (borderClass.isAssignableFrom(EmptyBorder.class)) 477 storedNode = storeEmptyBorder(doc); 478 else if (borderClass.isAssignableFrom(CompoundBorder.class)) 479 storedNode = storeCompoundBorder(doc); 480 else if (SoftBevelBorder.class.isAssignableFrom(borderClass)) 481 storedNode = storeBevelBorder(doc, ID_BI_SOFTBEVEL); 482 else if (BevelBorder.class.isAssignableFrom(borderClass)) 483 storedNode = storeBevelBorder(doc, ID_BI_BEVEL); 484 else if (borderClass.isAssignableFrom(MatteBorder.class)) 485 storedNode = storeMatteBorder(doc); 486 487 490 return storedNode; 491 } 492 493 else if (value == null) { 494 return storeNullBorder(doc); 495 } 496 497 return null; } 499 500 509 public void readFromXML(org.w3c.dom.Node element) throws IOException { 510 if ( !XML_BORDER.equals(element.getNodeName()) ) 511 { 512 IOException ex = new IOException ("Missing \"Border\" XML element"); ErrorManager.getDefault().annotate( 514 ex, getBundle().getString("MSG_ERR_MissingMainElement")); throw ex; 516 } 517 518 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 519 String infoName = attributes.getNamedItem(ATTR_INFO).getNodeValue(); 520 if (ID_BI_NULL_BORDER.equals(infoName)) 521 return; 523 org.w3c.dom.Node readNode = null; 524 org.w3c.dom.NodeList children = element.getChildNodes(); 525 for (int i = 0; i < children.getLength(); i++) 526 if (children.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 527 readNode = children.item(i); 528 break; 529 } 530 if (readNode == null) { 531 IOException ex = new IOException ("Missing border data"); ErrorManager.getDefault().annotate( 533 ex, getBundle().getString("MSG_ERR_MissingBorderData")); throw ex; 535 } 536 537 if (ID_BI_TITLED.equals(infoName)) 538 readTitledBorder(readNode); 539 else if (ID_BI_ETCHED.equals(infoName)) 540 readEtchedBorder(readNode); 541 else if (ID_BI_LINE.equals(infoName)) 542 readLineBorder(readNode); 543 else if (ID_BI_EMPTY.equals(infoName)) 544 readEmptyBorder(readNode); 545 else if (ID_BI_COMPOUND.equals(infoName)) 546 readCompoundBorder(readNode); 547 else if (ID_BI_SOFTBEVEL.equals(infoName)) 548 readBevelBorder(readNode, SoftBevelBorder.class); 549 else if (ID_BI_BEVEL.equals(infoName)) 550 readBevelBorder(readNode, BevelBorder.class); 551 else if (ID_BI_MATTECOLOR.equals(infoName) 552 || ID_BI_MATTEICON.equals(infoName)) 553 readMatteBorder(readNode); 554 else if (ID_BI_NULL_BORDER.equals(infoName)) { borderSupport = null; 556 } 557 560 current = borderSupport; 561 } 562 563 566 private org.w3c.dom.Element createBorderInfoNode(org.w3c.dom.Document doc, 567 String name) { 568 org.w3c.dom.Element el = doc.createElement(XML_BORDER); 569 el.setAttribute(ATTR_INFO, name); 570 return el; 571 } 572 573 private static void writeProperty(String propName, FormProperty prop, 574 org.w3c.dom.Element el, 575 org.w3c.dom.Document doc) { 576 org.w3c.dom.Node valueNode = null; 577 578 PropertyEditor propEd = prop.getCurrentEditor(); 579 if (propEd instanceof XMLPropertyEditor) { 580 Object value; 581 try { 582 value = prop.getValue(); 583 } 584 catch (Exception ex) { 585 ex.printStackTrace(); return; 587 } 588 propEd.setValue(value); 589 valueNode = ((XMLPropertyEditor)propEd).storeToXML(doc); 590 if (valueNode != null) { 591 el.appendChild(valueNode); 592 if (valueNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) 593 ((org.w3c.dom.Element )valueNode).setAttribute(PROP_NAME, propName); 594 return; 595 } 596 } 597 598 616 } 617 618 private static Object readProperty(String xmlPropName, 619 String borderPropName, 620 BorderDesignSupport bSupport, 621 org.w3c.dom.Node element) 622 throws IOException 623 { 624 boolean valueRead = false; 625 Object value = null; 626 org.w3c.dom.Node propNode = null; 627 org.w3c.dom.NodeList items = element.getChildNodes(); 628 629 for (int i = 0; i < items.getLength(); i++) 630 if (items.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE 631 && ((org.w3c.dom.Element )items.item(i)).getAttribute(PROP_NAME) 632 .equals(xmlPropName)) { 633 propNode = items.item(i); 634 break; 635 } 636 637 if (propNode != null) { FormProperty prop = (FormProperty) 639 bSupport.getPropertyOfName(borderPropName); 640 if (prop == null) { 641 IOException ex = new IOException ("Unknown property"); 643 ErrorManager.getDefault().annotate( 644 ex, 645 MessageFormat.format( 646 getBundle().getString("FMT_ERR_UnknownProperty"), new Object [] { borderPropName, 648 bSupport.getBorderClass().getName() })); 649 throw ex; 650 } 651 652 IOException lastEx = null; 653 PropertyEditor editors[] = FormPropertyEditorManager.getAllEditors(prop); 654 655 for (int i=0; i < editors.length && !valueRead; i++) { 656 PropertyEditor prEd = editors[i]; 657 if (prEd instanceof XMLPropertyEditor) { 658 try { 659 prop.getPropertyContext().initPropertyEditor(prEd); 660 ((XMLPropertyEditor)prEd).readFromXML(propNode); 661 value = prEd.getValue(); 662 prop.setValue(value); 663 prop.setCurrentEditor(prEd); 664 valueRead = true; 665 } 666 catch (IOException ex) { 667 lastEx = ex; 668 } 669 catch (Exception ex) { 670 lastEx = new IOException (); 671 ErrorManager.getDefault().annotate(lastEx, ex); 672 } 673 } 674 } 675 if (!valueRead && lastEx != null) { 676 ErrorManager.getDefault().annotate( 677 lastEx, 678 MessageFormat.format( 679 getBundle().getString("FMT_ERR_CannotReadBorderProperty"), new Object [] { xmlPropName })); 681 throw lastEx; 682 } 683 } 684 727 728 return value; 729 } 730 731 734 private static final String ID_BI_NULL_BORDER = "null"; 736 private org.w3c.dom.Node storeNullBorder(org.w3c.dom.Document doc) { 737 try { 738 return createBorderInfoNode(doc, ID_BI_NULL_BORDER); 739 } 740 catch (Exception ex) { ex.printStackTrace(); 742 } 743 return null; 744 } 745 746 749 private static final String XML_TITLED_BORDER = "TitledBorder"; private static final String ID_BI_TITLED = "org.netbeans.modules.form.compat2.border.TitledBorderInfo"; 753 private static final String ATTR_TITLE = "title"; private static final String ATTR_TITLE_X = "titleX"; private static final String ATTR_BORDER = "innerBorder"; private static final String ATTR_JUSTIFICATION = "justification"; private static final String ATTR_POSITION = "position"; private static final String ATTR_FONT = "font"; private static final String ATTR_TITLE_COLOR = "color"; 761 private org.w3c.dom.Node storeTitledBorder(org.w3c.dom.Document doc) { 762 try { 763 org.w3c.dom.Element el = doc.createElement(XML_TITLED_BORDER); 764 FormProperty prop; 765 766 prop = (FormProperty)borderSupport.getPropertyOfName("border"); if (prop != null && prop.isChanged()) 768 writeProperty(ATTR_BORDER, prop, el, doc); 769 770 prop = (FormProperty)borderSupport.getPropertyOfName("title"); if (prop != null && prop.isChanged()) { 772 Object realValue = prop.getRealValue(); 773 el.setAttribute(ATTR_TITLE, realValue instanceof String ? 774 (String )realValue : ""); 776 Object value = prop.getValue(); 777 if (value instanceof FormDesignValue) 778 writeProperty(ATTR_TITLE_X, prop, el, doc); 780 } 781 782 prop = (FormProperty)borderSupport.getPropertyOfName("titleJustification"); if (prop != null && prop.isChanged()) 784 el.setAttribute(ATTR_JUSTIFICATION, prop.getRealValue().toString()); 785 786 prop = (FormProperty)borderSupport.getPropertyOfName("titlePosition"); if (prop != null && prop.isChanged()) 788 el.setAttribute(ATTR_POSITION, prop.getRealValue().toString()); 789 790 prop = (FormProperty)borderSupport.getPropertyOfName("titleFont"); if (prop != null && prop.isChanged()) 792 writeProperty(ATTR_FONT, prop, el, doc); 793 794 prop = (FormProperty)borderSupport.getPropertyOfName("titleColor"); if (prop != null && prop.isChanged()) 796 writeProperty(ATTR_TITLE_COLOR, prop, el, doc); 797 798 org.w3c.dom.Node nod = createBorderInfoNode(doc, ID_BI_TITLED); 799 nod.appendChild(el); 800 return nod; 801 } 802 catch (Exception ex) { ex.printStackTrace(); 804 } 805 return null; 806 } 807 808 public void readTitledBorder(org.w3c.dom.Node element) throws IOException { 809 if (!XML_TITLED_BORDER.equals(element.getNodeName())) 810 throw new IOException ("Invalid format: missing \""+XML_TITLED_BORDER+"\" element."); 812 try { 813 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 814 org.w3c.dom.Node node; 815 816 borderSupport = new BorderDesignSupport(TitledBorder.class); 817 borderSupport.setPropertyContext(propertyContext); 818 FormProperty prop; 819 820 readProperty(ATTR_BORDER, "border", borderSupport, element); 822 Object title = readProperty(ATTR_TITLE_X, "title", borderSupport, element); if (title == null && (node = attributes.getNamedItem(ATTR_TITLE)) != null 826 && (prop = (FormProperty)borderSupport 827 .getPropertyOfName("title")) != null) prop.setValue(node.getNodeValue()); 829 830 node = attributes.getNamedItem(ATTR_JUSTIFICATION); 831 if (node != null && (prop = (FormProperty)borderSupport 832 .getPropertyOfName("titleJustification")) != null) prop.setValue(new Integer (node.getNodeValue())); 834 835 node = attributes.getNamedItem(ATTR_POSITION); 836 if (node != null && (prop = (FormProperty)borderSupport 837 .getPropertyOfName("titlePosition")) != null) prop.setValue(new Integer (node.getNodeValue())); 839 840 readProperty(ATTR_FONT, "titleFont", borderSupport, element); 842 readProperty(ATTR_TITLE_COLOR, "titleColor", borderSupport, element); } 844 catch (IOException ex) { 845 throw ex; 846 } 847 catch (Exception ex) { 848 IOException ioex = new IOException (); 849 ErrorManager.getDefault().annotate(ioex, ex); 850 throw ioex; 851 } 852 } 853 854 857 private static final String XML_ETCHED_BORDER = "EtchetBorder"; private static final String ID_BI_ETCHED = "org.netbeans.modules.form.compat2.border.EtchedBorderInfo"; 861 private static final String ATTR_ETCH_TYPE = "bevelType"; private static final String ATTR_HIGHLIGHT = "highlight"; private static final String ATTR_SHADOW = "shadow"; 865 public org.w3c.dom.Node storeEtchedBorder(org.w3c.dom.Document doc) { 866 try { 867 org.w3c.dom.Element el = doc.createElement(XML_ETCHED_BORDER); 868 FormProperty prop; 869 870 prop = (FormProperty)borderSupport.getPropertyOfName("etchType"); if (prop != null && prop.isChanged()) 872 el.setAttribute(ATTR_ETCH_TYPE, prop.getRealValue().toString()); 873 874 prop = (FormProperty)borderSupport.getPropertyOfName("highlightColor"); if (prop != null && prop.isChanged()) 876 writeProperty(ATTR_HIGHLIGHT, prop, el, doc); 877 878 prop = (FormProperty)borderSupport.getPropertyOfName("shadowColor"); if (prop != null && prop.isChanged()) 880 writeProperty(ATTR_SHADOW, prop, el, doc); 881 882 org.w3c.dom.Node nod = createBorderInfoNode(doc, ID_BI_ETCHED); 883 nod.appendChild(el); 884 return nod; 885 } 886 catch (Exception ex) { ex.printStackTrace(); 888 } 889 return null; 890 } 891 892 public void readEtchedBorder(org.w3c.dom.Node element) throws IOException { 893 if (!XML_ETCHED_BORDER.equals(element.getNodeName())) 894 throw new IOException ("Invalid format: missing \""+XML_ETCHED_BORDER+"\" element."); 896 try { 897 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 898 org.w3c.dom.Node node; 899 900 borderSupport = new BorderDesignSupport(EtchedBorder.class); 901 borderSupport.setPropertyContext(propertyContext); 902 FormProperty prop; 903 904 node = attributes.getNamedItem(ATTR_ETCH_TYPE); 905 if (node != null && (prop = (FormProperty)borderSupport 906 .getPropertyOfName("etchType")) != null) prop.setValue(new Integer (node.getNodeValue())); 908 909 readProperty(ATTR_HIGHLIGHT, "highlightColor", borderSupport, element); 911 readProperty(ATTR_SHADOW, "shadowColor", borderSupport, element); } 913 catch (IOException ex) { 914 throw ex; 915 } 916 catch (Exception ex) { 917 IOException ioex = new IOException (); 918 ErrorManager.getDefault().annotate(ioex, ex); 919 throw ioex; 920 } 921 } 922 923 926 private static final String XML_LINE_BORDER = "LineBorder"; private static final String ID_BI_LINE = "org.netbeans.modules.form.compat2.border.LineBorderInfo"; 930 private static final String ATTR_THICKNESS = "thickness"; private static final String ATTR_LINE_COLOR = "color"; private static final String ATTR_CORNERS = "roundedCorners"; 934 public org.w3c.dom.Node storeLineBorder(org.w3c.dom.Document doc) { 935 try { 936 org.w3c.dom.Element el = doc.createElement(XML_LINE_BORDER); 937 FormProperty prop; 938 939 prop = (FormProperty)borderSupport.getPropertyOfName("lineColor"); if (prop != null && prop.isChanged()) 941 writeProperty(ATTR_LINE_COLOR, prop, el, doc); 942 943 prop = (FormProperty)borderSupport.getPropertyOfName("thickness"); if (prop != null && prop.isChanged()) 945 el.setAttribute(ATTR_THICKNESS, prop.getRealValue().toString()); 946 947 prop = (FormProperty)borderSupport.getPropertyOfName("roundedCorners"); if (prop != null && prop.isChanged()) 949 el.setAttribute(ATTR_CORNERS, prop.getRealValue().toString()); 950 951 org.w3c.dom.Node nod = createBorderInfoNode(doc, ID_BI_LINE); 952 nod.appendChild(el); 953 return nod; 954 } 955 catch (Exception ex) { ex.printStackTrace(); 957 } 958 return null; 959 } 960 961 public void readLineBorder(org.w3c.dom.Node element) throws IOException { 962 if (!XML_LINE_BORDER.equals(element.getNodeName())) 963 throw new IOException ("Invalid format: missing \""+XML_LINE_BORDER+"\" element."); 965 try { 966 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 967 org.w3c.dom.Node node; 968 969 borderSupport = new BorderDesignSupport(LineBorder.class); 970 borderSupport.setPropertyContext(propertyContext); 971 FormProperty prop; 972 973 readProperty(ATTR_LINE_COLOR, "lineColor", borderSupport, element); 975 node = attributes.getNamedItem(ATTR_THICKNESS); 976 if (node != null && (prop = (FormProperty)borderSupport 977 .getPropertyOfName("thickness")) != null) prop.setValue(new Integer (node.getNodeValue())); 979 980 node = attributes.getNamedItem(ATTR_CORNERS); 981 if (node != null && (prop = (FormProperty)borderSupport 982 .getPropertyOfName("roundedCorners")) != null) prop.setValue(Boolean.valueOf(node.getNodeValue())); 984 } 985 catch (IOException ex) { 986 throw ex; 987 } 988 catch (Exception ex) { 989 IOException ioex = new IOException (); 990 ErrorManager.getDefault().annotate(ioex, ex); 991 throw ioex; 992 } 993 } 994 995 998 private static final String XML_EMPTY_BORDER = "EmptyBorder"; private static final String ID_BI_EMPTY = "org.netbeans.modules.form.compat2.border.EmptyBorderInfo"; 1002 private static final String ATTR_TOP = "top"; private static final String ATTR_LEFT = "left"; private static final String ATTR_RIGHT = "right"; private static final String ATTR_BOTTOM = "bottom"; 1007 public org.w3c.dom.Node storeEmptyBorder(org.w3c.dom.Document doc) { 1008 try { 1009 org.w3c.dom.Element el = doc.createElement(XML_EMPTY_BORDER); 1010 FormProperty prop = (FormProperty)borderSupport.getPropertyOfName( 1011 "borderInsets"); Object value; 1013 if (prop != null && prop.isChanged() 1014 && (value = prop.getRealValue()) instanceof Insets) { 1015 Insets insets = (Insets)value; 1016 el.setAttribute(ATTR_TOP, Integer.toString(insets.top)); 1017 el.setAttribute(ATTR_LEFT, Integer.toString(insets.left)); 1018 el.setAttribute(ATTR_BOTTOM, Integer.toString(insets.bottom)); 1019 el.setAttribute(ATTR_RIGHT, Integer.toString(insets.right)); 1020 } 1021 1022 org.w3c.dom.Node nod = createBorderInfoNode(doc, ID_BI_EMPTY); 1023 nod.appendChild(el); 1024 return nod; 1025 } 1026 catch (Exception ex) { ex.printStackTrace(); 1028 } 1029 return null; 1030 } 1031 1032 public void readEmptyBorder(org.w3c.dom.Node element) throws IOException { 1033 if (!XML_EMPTY_BORDER.equals(element.getNodeName())) 1034 throw new IOException ("Invalid format: missing \""+XML_EMPTY_BORDER+"\" element."); 1036 try { 1037 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 1038 org.w3c.dom.Node node; 1039 1040 int top=1, left=1, bottom=1, right=1; 1041 1042 if ((node = attributes.getNamedItem(ATTR_TOP)) != null) 1043 top = Integer.parseInt(node.getNodeValue()); 1044 if ((node = attributes.getNamedItem(ATTR_LEFT)) != null) 1045 left = Integer.parseInt(node.getNodeValue()); 1046 if ((node = attributes.getNamedItem(ATTR_BOTTOM)) != null) 1047 bottom = Integer.parseInt(node.getNodeValue()); 1048 if ((node = attributes.getNamedItem(ATTR_RIGHT)) != null) 1049 right = Integer.parseInt(node.getNodeValue()); 1050 1051 borderSupport = new BorderDesignSupport(EmptyBorder.class); 1052 borderSupport.setPropertyContext(propertyContext); 1053 FormProperty prop; 1054 1055 if ((top != 1 || left != 1 || bottom != 1 || right != 1) 1056 && (prop = (FormProperty)borderSupport 1057 .getPropertyOfName("borderInsets")) != null) prop.setValue(new Insets(top,left,bottom,right)); 1059 } 1060 catch (Exception ex) { 1061 IOException ioex = new IOException (); 1062 ErrorManager.getDefault().annotate(ioex, ex); 1063 throw ioex; 1064 } 1065 } 1066 1067 1070 private static final String XML_COMPOUND_BORDER = "CompundBorder"; private static final String ID_BI_COMPOUND = "org.netbeans.modules.form.compat2.border.CompoundBorderInfo"; 1074 private static final String ATTR_OUTSIDE = "outside"; private static final String ATTR_INSIDE = "inside"; 1077 private org.w3c.dom.Node storeCompoundBorder(org.w3c.dom.Document doc) { 1078 try { 1079 org.w3c.dom.Element el = doc.createElement(XML_COMPOUND_BORDER); 1080 FormProperty prop; 1081 1082 prop = (FormProperty)borderSupport.getPropertyOfName("outsideBorder"); if (prop != null && prop.isChanged()) 1084 writeProperty(ATTR_OUTSIDE, prop, el, doc); 1085 1086 prop = (FormProperty)borderSupport.getPropertyOfName("insideBorder"); if (prop != null && prop.isChanged()) 1088 writeProperty(ATTR_INSIDE, prop, el, doc); 1089 1090 org.w3c.dom.Node nod = createBorderInfoNode(doc, ID_BI_COMPOUND); 1091 nod.appendChild(el); 1092 return nod; 1093 } 1094 catch (Exception ex) { ex.printStackTrace(); 1096 } 1097 return null; 1098 } 1099 1100 public void readCompoundBorder(org.w3c.dom.Node element) throws IOException { 1101 if (!XML_COMPOUND_BORDER.equals(element.getNodeName())) 1102 throw new IOException ("Invalid format: missing \""+XML_COMPOUND_BORDER+"\" element."); 1104 try { 1105 borderSupport = new BorderDesignSupport(CompoundBorder.class); 1106 borderSupport.setPropertyContext(propertyContext); 1107 1108 readProperty(ATTR_OUTSIDE, "outsideBorder", borderSupport, element); readProperty(ATTR_INSIDE, "insideBorder", borderSupport, element); } 1111 catch (IOException ex) { 1112 throw ex; 1113 } 1114 catch (Exception ex) { 1115 IOException ioex = new IOException (); 1116 ErrorManager.getDefault().annotate(ioex, ex); 1117 throw ioex; 1118 } 1119 } 1120 1121 1125 private static final String XML_BEVEL_BORDER = "BevelBorder"; private static final String ID_BI_BEVEL = "org.netbeans.modules.form.compat2.border.BevelBorderInfo"; private static final String ID_BI_SOFTBEVEL = "org.netbeans.modules.form.compat2.border.SoftBevelBorderInfo"; 1131 private static final String ATTR_BEVEL_TYPE = "bevelType"; private static final String ATTR_HIGHLIGHT_OUTER = "highlightOuter"; private static final String ATTR_HIGHLIGHT_INNER = "highlightInner"; private static final String ATTR_SHADOW_OUTER = "shadowOuter"; private static final String ATTR_SHADOW_INNER = "shadowInner"; 1137 public org.w3c.dom.Node storeBevelBorder(org.w3c.dom.Document doc, 1138 String infoId) { 1139 try { 1140 org.w3c.dom.Element el = doc.createElement(XML_BEVEL_BORDER); 1141 FormProperty prop; 1142 1143 prop = (FormProperty)borderSupport.getPropertyOfName("bevelType"); if (prop != null && prop.isChanged()) 1145 el.setAttribute(ATTR_BEVEL_TYPE, prop.getRealValue().toString()); 1146 1147 prop = (FormProperty)borderSupport.getPropertyOfName("highlightOuterColor"); if (prop != null && prop.isChanged()) 1149 writeProperty(ATTR_HIGHLIGHT_OUTER, prop, el, doc); 1150 1151 prop = (FormProperty)borderSupport.getPropertyOfName("highlightInnerColor"); if (prop != null && prop.isChanged()) 1153 writeProperty(ATTR_HIGHLIGHT_INNER, prop, el, doc); 1154 1155 prop = (FormProperty)borderSupport.getPropertyOfName("shadowOuterColor"); if (prop != null && prop.isChanged()) 1157 writeProperty(ATTR_SHADOW_OUTER, prop, el, doc); 1158 1159 prop = (FormProperty)borderSupport.getPropertyOfName("shadowInnerColor"); if (prop != null && prop.isChanged()) 1161 writeProperty(ATTR_SHADOW_INNER, prop, el, doc); 1162 1163 org.w3c.dom.Node nod = createBorderInfoNode(doc, infoId); 1164 nod.appendChild(el); 1165 return nod; 1166 } 1167 catch (Exception ex) { ex.printStackTrace(); 1169 } 1170 return null; 1171 } 1172 1173 public void readBevelBorder(org.w3c.dom.Node element, Class borderClass) 1174 throws IOException 1175 { 1176 if (!XML_BEVEL_BORDER.equals(element.getNodeName())) 1177 throw new IOException ("Invalid format: missing \""+XML_BEVEL_BORDER+"\" element."); 1179 try { 1180 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 1181 org.w3c.dom.Node node; 1182 1183 borderSupport = new BorderDesignSupport(borderClass); 1184 FormProperty prop; 1185 1186 node = attributes.getNamedItem(ATTR_BEVEL_TYPE); 1187 if (node != null && (prop = (FormProperty)borderSupport 1188 .getPropertyOfName("bevelType")) != null) prop.setValue(new Integer (node.getNodeValue())); 1190 1191 readProperty(ATTR_HIGHLIGHT_OUTER, "highlightOuterColor", borderSupport, element); readProperty(ATTR_HIGHLIGHT_INNER, "highlightInnerColor", borderSupport, element); readProperty(ATTR_SHADOW_OUTER, "shadowOuterColor", borderSupport, element); readProperty(ATTR_SHADOW_INNER, "shadowInnerColor", borderSupport, element); } 1196 catch (IOException ex) { 1197 throw ex; 1198 } 1199 catch (Exception ex) { 1200 IOException ioex = new IOException (); 1201 ErrorManager.getDefault().annotate(ioex, ex); 1202 throw ioex; 1203 } 1204 } 1205 1206 1210 private static final String XML_MATTE_COLOR_BORDER = "MatteColorBorder"; private static final String XML_MATTE_ICON_BORDER = "MatteIconBorder"; private static final String ID_BI_MATTECOLOR = "org.netbeans.modules.form.compat2.border.MatteColorBorderInfo"; private static final String ID_BI_MATTEICON = "org.netbeans.modules.form.compat2.border.MatteIconBorderInfo"; 1217 private static final String ATTR_MATTE_COLOR = "color"; private static final String ATTR_MATTE_ICON = "icon"; 1220 public org.w3c.dom.Node storeMatteBorder(org.w3c.dom.Document doc) { 1221 try { 1222 org.w3c.dom.Element el; 1223 String infoId; 1224 FormProperty prop; 1225 1226 prop = (FormProperty)borderSupport.getPropertyOfName("tileIcon"); if (prop.isChanged()) { 1228 el = doc.createElement(XML_MATTE_ICON_BORDER); 1229 infoId = ID_BI_MATTEICON; 1230 writeProperty(ATTR_MATTE_ICON, prop, el, doc); 1231 } 1232 else { 1233 el = doc.createElement(XML_MATTE_COLOR_BORDER); 1234 infoId = ID_BI_MATTECOLOR; 1235 } 1236 1237 prop = (FormProperty)borderSupport.getPropertyOfName("matteColor"); if (prop != null && prop.isChanged()) 1239 writeProperty(ATTR_MATTE_COLOR, prop, el, doc); 1240 1241 prop = (FormProperty)borderSupport.getPropertyOfName("borderInsets"); Object value; 1243 if (prop != null && prop.isChanged() 1244 && (value = prop.getRealValue()) instanceof Insets) { 1245 Insets insets = (Insets)value; 1246 el.setAttribute(ATTR_TOP, Integer.toString(insets.top)); 1247 el.setAttribute(ATTR_LEFT, Integer.toString(insets.left)); 1248 el.setAttribute(ATTR_BOTTOM, Integer.toString(insets.bottom)); 1249 el.setAttribute(ATTR_RIGHT, Integer.toString(insets.right)); 1250 } 1251 1252 org.w3c.dom.Node nod = createBorderInfoNode(doc, infoId); 1253 nod.appendChild(el); 1254 return nod; 1255 } 1256 catch (Exception ex) { ex.printStackTrace(); 1258 } 1259 return null; 1260 } 1261 1262 public void readMatteBorder(org.w3c.dom.Node element) throws IOException { 1263 if (!XML_MATTE_COLOR_BORDER.equals(element.getNodeName()) 1264 && !XML_MATTE_ICON_BORDER.equals(element.getNodeName())) 1265 throw new IOException ("Invalid format: missing \""+XML_MATTE_COLOR_BORDER+"\" or \""+XML_MATTE_ICON_BORDER+"\" element."); 1267 try { 1268 borderSupport = new BorderDesignSupport(MatteBorder.class); 1269 borderSupport.setPropertyContext(propertyContext); 1270 1271 readProperty(ATTR_MATTE_ICON, "tileIcon", borderSupport, element); readProperty(ATTR_MATTE_COLOR, "matteColor", borderSupport, element); 1274 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 1275 org.w3c.dom.Node node; 1276 FormProperty prop; 1277 1278 int top=1, left=1, bottom=1, right=1; 1279 1280 if ((node = attributes.getNamedItem(ATTR_TOP)) != null) 1281 top = Integer.parseInt(node.getNodeValue()); 1282 if ((node = attributes.getNamedItem(ATTR_LEFT)) != null) 1283 left = Integer.parseInt(node.getNodeValue()); 1284 if ((node = attributes.getNamedItem(ATTR_BOTTOM)) != null) 1285 bottom = Integer.parseInt(node.getNodeValue()); 1286 if ((node = attributes.getNamedItem(ATTR_RIGHT)) != null) 1287 right = Integer.parseInt(node.getNodeValue()); 1288 1289 if ((top != 1 || left != 1 || bottom != 1 || right != 1) 1290 && (prop = (FormProperty)borderSupport 1291 .getPropertyOfName("borderInsets")) != null) prop.setValue(new Insets(top,left,bottom,right)); 1293 } 1294 catch (IOException ex) { 1295 throw ex; 1296 } 1297 catch (Exception ex) { 1298 IOException ioex = new IOException (); 1299 ErrorManager.getDefault().annotate(ioex, ex); 1300 throw ioex; 1301 } 1302 } 1303 1304 public boolean valueIsBeanProperty() { 1305 return !isSupportedBorder(); 1306 } 1307 1308 private boolean isSupportedBorder() { 1309 Object value = getValue(); 1310 if ((value == null) || (value instanceof javax.swing.plaf.UIResource )) { 1311 return true; 1313 } 1314 Class borderClass = borderSupport.getBorderClass(); 1315 return borderClass.isAssignableFrom(TitledBorder.class) 1316 || borderClass.isAssignableFrom(EtchedBorder.class) 1317 || borderClass.isAssignableFrom(LineBorder.class) 1318 || borderClass.isAssignableFrom(EmptyBorder.class) 1319 || borderClass.isAssignableFrom(CompoundBorder.class) 1320 || SoftBevelBorder.class.isAssignableFrom(borderClass) 1321 || BevelBorder.class.isAssignableFrom(borderClass) 1322 || borderClass.isAssignableFrom(MatteBorder.class); 1323 } 1324 1325 public Node.Property[] getProperties() { 1326 Object value = getValue(); 1327 if ((value == null) || (value instanceof javax.swing.plaf.UIResource )) { 1328 return EMPTY_PROPERTIES; 1330 } 1331 return borderSupport.getProperties(); 1332 } 1333 1334 public void intializeFromType(Class type) throws Exception { 1335 borderSupport = new BorderDesignSupport(type); 1336 borderSupport.setPropertyContext(propertyContext); 1337 current = borderSupport; 1338 } 1339 1340} 1341 | Popular Tags |