1 19 package org.netbeans.modules.form.editors2; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.beans.*; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import java.util.ResourceBundle ; 27 import javax.swing.*; 28 import javax.swing.border.Border ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Node ; 33 34 import org.jdesktop.layout.GroupLayout; 35 import org.jdesktop.layout.LayoutStyle; 36 37 import org.openide.awt.Mnemonics; 38 import org.openide.explorer.propertysheet.ExPropertyEditor; 39 import org.openide.explorer.propertysheet.PropertyEnv; 40 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 41 import org.openide.util.NbBundle; 42 43 import org.netbeans.modules.form.*; 44 import org.netbeans.modules.form.codestructure.CodeVariable; 45 46 51 public class FontEditor implements ExPropertyEditor, XMLPropertyEditor, 52 FormAwareEditor, PropertyChangeListener { 53 56 private PropertyEditor delegate; 57 60 private NbFont propertyValue; 61 64 private RADProperty property; 65 68 private PropertyChangeSupport propChangeSupport; 69 72 private boolean ignoreUpdates = true; 73 74 public FontEditor() { 75 propertyValue = new NbFont(); 76 propChangeSupport = new PropertyChangeSupport(this); 77 delegate = PropertyEditorManager.findEditor(Font.class); 78 if (delegate == null) { 79 throw new IllegalStateException ("FontEditor delegate not found."); } 81 if (!(delegate instanceof XMLPropertyEditor)) { 82 throw new IllegalStateException ("FontEditor delegate doesn't implement XMLPropertyEditor."); } 84 delegate.addPropertyChangeListener(this); 85 } 86 87 public void setValue(Object value) { 88 if ((value instanceof Font) || (value == null)) { 89 propertyValue = new NbFont(); 90 propertyValue.absolute = true; 91 propertyValue.font = (Font)value; 92 } else if (value instanceof NbFont) { 93 propertyValue = ((NbFont)value).copy(); 94 } else { 95 throw new IllegalArgumentException (); 96 } 97 if (property != null) { 98 propertyValue.property = property; 99 } 100 delegate.setValue(propertyValue.getDesignValue()); 101 } 102 103 public Object getValue() { 104 return propertyValue.absolute ? propertyValue.font : propertyValue; 105 } 106 107 public boolean isPaintable() { 108 return delegate.isPaintable(); 109 } 110 111 public void paintValue(Graphics gfx, Rectangle box) { 112 delegate.paintValue(gfx, box); 113 } 114 115 public String getJavaInitializationString() { 116 String exp; 117 if (propertyValue.absolute) { 118 exp = delegate.getJavaInitializationString(); 119 } else { 120 RADComponent comp = property.getRADComponent(); 121 CodeVariable var = comp.getCodeExpression().getVariable(); 122 String varName = (var == null) ? null : var.getName(); 123 String readMethod = property.getPropertyDescriptor().getReadMethod().getName(); 124 String getter = readMethod + "()"; if (varName != null) { 126 getter = varName + '.' + getter; 127 } 128 exp = getter + ".deriveFont("; boolean styleChanged = (propertyValue.italic != null) || (propertyValue.bold != null); 130 if (styleChanged) { 131 String styleExp = null; 132 if (propertyValue.italic != null) { 133 styleExp = getter + ".getStyle()"; if (Boolean.TRUE.equals(propertyValue.italic)) { 135 styleExp += " | "; } else{ 137 styleExp += " & ~"; } 139 styleExp += "java.awt.Font.ITALIC"; } 141 if (styleExp == null) { 142 styleExp = getter + ".getStyle()"; } else { 144 styleExp = "(" + styleExp + ")"; } 146 if (propertyValue.bold != null) { 147 if (Boolean.TRUE.equals(propertyValue.bold)) { 148 styleExp += " | "; } else{ 150 styleExp += " & ~"; } 152 styleExp += "java.awt.Font.BOLD"; } 154 exp += styleExp; 155 } 156 if (propertyValue.absoluteSize) { 157 exp += styleChanged ? ", " : "(float)"; exp += propertyValue.size + ")"; } else { 160 if (propertyValue.size == 0) { 161 if (styleChanged) { 162 exp += ')'; 163 } else { 164 exp = getter; 165 } 166 } else { 167 if (styleChanged) { 168 exp += ", "; } 170 exp += getter + ".getSize()"; if (propertyValue.size > 0) { 172 exp += '+'; 173 } 174 exp += propertyValue.size; 175 if (!styleChanged) exp += "f"; exp += ")"; } 178 } 179 } 180 return exp; 181 } 182 183 public String getAsText() { 184 return propertyValue.absolute ? delegate.getAsText() : propertyValue.getDescription(); 185 } 186 187 public void setAsText(String text) { 188 return; 189 } 190 191 public String [] getTags() { 192 return null; 193 } 194 195 private WeakReference lastSwitchBox; 198 public Component getCustomEditor() { 199 propertyValue = ((NbFont)propertyValue).copy(); 203 final Component absolute = propertyValue.absolute ? delegate.getCustomEditor() : null; 204 String switchBoxText = NbBundle.getMessage(FontEditor.class, "CTL_DeriveFont"); final JCheckBox switchBox = new JCheckBox(); 206 lastSwitchBox = new WeakReference (switchBox); 207 if (property == null) { 208 switchBox.setVisible(false); 209 } else { 210 Mnemonics.setLocalizedText(switchBox, switchBoxText); 211 switchBox.setSelected(!propertyValue.absolute); 212 } 213 final RelativeFontPanel relative = new RelativeFontPanel(); 214 Component pane = propertyValue.absolute ? absolute : relative; 215 if (!propertyValue.absolute) relative.updateFromPropertyValue(); 216 217 final JPanel editor = new JPanel(); 218 final GroupLayout layout = new GroupLayout(editor); 219 editor.setLayout(layout); 220 layout.setHorizontalGroup(layout.createParallelGroup() 221 .add(layout.createSequentialGroup() 222 .addContainerGap() 223 .add(switchBox)) 224 .add(pane)); 225 layout.setVerticalGroup(layout.createSequentialGroup() 226 .addContainerGap() 227 .add(switchBox) 228 .addPreferredGap(LayoutStyle.RELATED) 229 .add(pane) 230 .addContainerGap()); 231 232 switchBox.addItemListener(new ItemListener() { 233 private Component absoluteInLayout = absolute; 234 public void itemStateChanged(ItemEvent e) { 235 if (switchBox.isSelected()) { 236 layout.replace(absoluteInLayout, relative); 237 propertyValue.absolute = false; 238 convertToRelative(); 239 relative.updateFromPropertyValue(); 240 } else { 241 absoluteInLayout = delegate.getCustomEditor(); 242 layout.replace(relative, absoluteInLayout); 243 propertyValue.absolute = true; 244 } 245 firePropertyChange(); 246 editor.revalidate(); 247 editor.repaint(); 248 } 249 }); 250 251 return editor; 252 } 253 254 private void convertToRelative() { 255 if (propertyValue.font == null) return; 256 Font defaultFont = (Font)property.getDefaultValue(); 257 if (propertyValue.absoluteSize) { 258 propertyValue.size = propertyValue.font.getSize(); 259 } else { 260 if (defaultFont == null) return; 261 propertyValue.size = propertyValue.font.getSize() - defaultFont.getSize(); 262 } 263 if (defaultFont == null) return; 264 int absoluteStyle = propertyValue.font.getStyle(); 265 int defaultStyle = defaultFont.getStyle(); 266 boolean aItalic = ((absoluteStyle & Font.ITALIC) != 0); 267 boolean dItalic = ((defaultStyle & Font.ITALIC) != 0); 268 if (aItalic && !dItalic) { 269 propertyValue.italic = Boolean.TRUE; 270 } 271 if (!aItalic && dItalic) { 272 propertyValue.italic = Boolean.FALSE; 273 } 274 if ((propertyValue.italic != null) && (aItalic == dItalic) 275 && (aItalic != propertyValue.italic.booleanValue())) { 276 propertyValue.italic = null; 277 } 278 boolean aBold = ((absoluteStyle & Font.BOLD) != 0); 279 boolean dBold = ((defaultStyle & Font.BOLD) != 0); 280 if (aBold && !dBold) { 281 propertyValue.bold = Boolean.TRUE; 282 } 283 if (!aBold && dBold) { 284 propertyValue.bold = Boolean.FALSE; 285 } 286 if ((propertyValue.bold != null) && (aBold == dBold) 287 && (aBold != propertyValue.bold.booleanValue())) { 288 propertyValue.bold = null; 289 } 290 } 291 292 public boolean supportsCustomEditor() { 293 return true; 294 } 295 296 public void addPropertyChangeListener(PropertyChangeListener listener) { 297 propChangeSupport.addPropertyChangeListener(listener); 298 } 299 300 public void removePropertyChangeListener(PropertyChangeListener listener) { 301 propChangeSupport.removePropertyChangeListener(listener); 302 } 303 304 public void propertyChange(PropertyChangeEvent evt) { 306 Font font = (Font)delegate.getValue(); 307 propertyValue.font = font; 308 if (!ignoreUpdates) { 309 firePropertyChange(); 310 } 311 } 312 313 private void updateDelegate() { 314 ignoreUpdates = true; 315 delegate.setValue(propertyValue.getDesignValue()); 316 ignoreUpdates = false; 317 firePropertyChange(); 318 } 319 320 private void firePropertyChange() { 321 propChangeSupport.firePropertyChange("", null, null); } 323 324 public void attachEnv(PropertyEnv env) { 326 FeatureDescriptor prop = env.getFeatureDescriptor(); 327 if ((prop instanceof RADProperty) && (env.getBeans().length == 1)) { 330 property = (RADProperty)prop; 331 if (propertyValue != null) { 332 propertyValue.property = property; 333 } 334 } else { 335 if (lastSwitchBox != null) { Object switchBox = lastSwitchBox.get(); 337 if (switchBox != null) { 338 AbstractButton button = ((AbstractButton)switchBox); 339 button.setVisible(false); 340 if (button.isSelected()) button.setSelected(false); 341 } 342 } 343 property = null; 344 } 345 } 346 347 349 public static final String XML_FONT_ROOT = "FontInfo"; 351 public static final String XML_FONT = "Font"; 353 public static final String ATTR_RELATIVE = "relative"; 355 public static final String ATTR_RELATIVE_SIZE = "relativeSize"; 357 public static final String ATTR_SIZE = "size"; 359 public static final String ATTR_ITALIC_CHANGE = "italic"; 361 public static final String ATTR_BOLD_CHANGE = "bold"; 363 public static final String ATTR_COMP_NAME = "component"; 365 public static final String ATTR_PROP_NAME = "property"; 367 public void readFromXML(Node element) throws IOException { 368 if (!XML_FONT_ROOT.equals(element.getNodeName())) { 369 ((XMLPropertyEditor)delegate).readFromXML(element); 371 setValue(delegate.getValue()); 372 return; 373 } 374 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 375 boolean relative = Boolean.valueOf(attributes.getNamedItem(ATTR_RELATIVE).getNodeValue()).booleanValue(); 376 propertyValue = new NbFont(); 377 propertyValue.absolute = !relative; 378 org.w3c.dom.NodeList subnodes = element.getChildNodes(); 379 for (int i=0; i<subnodes.getLength(); i++){ 380 org.w3c.dom.Node subnode = subnodes.item(i); 381 if (subnode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 382 if (relative) { 383 if (!XML_FONT.equals(subnode.getNodeName())) { 384 throw new java.io.IOException (); 385 } 386 attributes = subnode.getAttributes(); 387 propertyValue.absoluteSize = !Boolean.valueOf(attributes.getNamedItem(ATTR_RELATIVE_SIZE).getNodeValue()).booleanValue(); 388 propertyValue.size = Integer.parseInt(attributes.getNamedItem(ATTR_SIZE).getNodeValue()); 389 org.w3c.dom.Node italicChange = attributes.getNamedItem(ATTR_ITALIC_CHANGE); 390 if (italicChange != null) { 391 propertyValue.italic = Boolean.valueOf(italicChange.getNodeValue()); 392 } 393 org.w3c.dom.Node boldChange = attributes.getNamedItem(ATTR_BOLD_CHANGE); 394 if (boldChange != null) { 395 propertyValue.bold = Boolean.valueOf(boldChange.getNodeValue()); 396 } 397 String compName = attributes.getNamedItem(ATTR_COMP_NAME).getNodeValue(); 398 RADComponent component = formModel.findRADComponent(compName); 399 String propName = attributes.getNamedItem(ATTR_PROP_NAME).getNodeValue(); 400 property = (RADProperty)component.getPropertyByName(propName); 401 propertyValue.property = property; 402 delegate.setValue(propertyValue.getDesignValue()); 403 } else { 404 ((XMLPropertyEditor)delegate).readFromXML(subnode); 405 propertyValue.font = (Font)delegate.getValue(); 406 } 407 break; 408 } 409 } 410 } 411 412 public Node storeToXML(Document doc) { 413 org.w3c.dom.Element el = doc.createElement(XML_FONT_ROOT); 414 el.setAttribute(ATTR_RELATIVE, Boolean.toString(!propertyValue.absolute)); 415 if (propertyValue.absolute) { 416 org.w3c.dom.Node absNode = ((XMLPropertyEditor)delegate).storeToXML(doc); 417 el.appendChild(absNode); 418 } else { 419 org.w3c.dom.Element subel = doc.createElement(XML_FONT); 420 el.appendChild(subel); 421 subel.setAttribute(ATTR_RELATIVE_SIZE, Boolean.toString(!propertyValue.absoluteSize)); 422 subel.setAttribute(ATTR_SIZE, Integer.toString(propertyValue.size)); 423 if (propertyValue.italic != null) { 424 subel.setAttribute(ATTR_ITALIC_CHANGE, propertyValue.italic.toString()); 425 } 426 if (propertyValue.bold != null) { 427 subel.setAttribute(ATTR_BOLD_CHANGE, propertyValue.bold.toString()); 428 } 429 subel.setAttribute(ATTR_COMP_NAME, property.getRADComponent().getName()); 430 subel.setAttribute(ATTR_PROP_NAME, property.getName()); 431 } 432 return el; 433 } 434 435 private FormModel formModel; 437 public void setFormModel(FormModel model) { 438 this.formModel = model; 439 } 440 441 static class NbFont implements FormDesignValue { 442 446 boolean absolute = false; 447 450 Font font; 451 455 Boolean italic; 456 460 Boolean bold; 461 464 boolean absoluteSize; 465 469 int size; 470 473 FormProperty property; 474 475 public Object getDesignValue() { 476 Font value; 477 if (absolute) { 478 value = font; 479 } else { 480 value = defaultValue(property); 481 if (value != null) { 482 int origStyle = value.getStyle(); 483 int style = origStyle; 484 if (italic != null) { 485 if (italic.booleanValue()) { 486 style |= Font.ITALIC; 487 } else { 488 style &= ~Font.ITALIC; 489 } 490 } 491 if (bold != null) { 492 if (bold.booleanValue()) { 493 style |= Font.BOLD; 494 } else { 495 style &= ~Font.BOLD; 496 } 497 } 498 int origSize = value.getSize(); 499 int newSize = (absoluteSize) ? size : (size + origSize); 500 if ((style != origStyle) || (origSize != newSize)) { 501 value = value.deriveFont(style, newSize); 502 } 503 } 504 } 505 return value; 506 } 507 508 private Font defaultValue(FormProperty property) { 509 if ((property instanceof RADProperty) && FormLAF.getUsePreviewDefaults()) { 510 RADProperty radProp = (RADProperty)property; 511 PropertyDescriptor propDesc = radProp.getPropertyDescriptor(); 512 java.lang.reflect.Method readMethod = propDesc.getReadMethod(); 513 if (readMethod != null) { 514 try { 515 Class clazz = radProp.getRADComponent().getBeanClass(); 516 Object beanInstance = BeanSupport.createBeanInstance(clazz); 517 return (Font)readMethod.invoke(beanInstance, new Object [0]); 518 } catch (Exception e) { 519 } 520 } 521 } 522 return (Font)property.getDefaultValue(); 523 } 524 525 public String getDescription() { 526 ResourceBundle bundle = NbBundle.getBundle(FontEditor.class); 527 String description; 528 if (absolute) { 529 String style = null; 530 switch (font.getStyle()) { 531 case Font.PLAIN: style = bundle.getString("CTL_FontStylePlain"); break; case Font.BOLD: style = bundle.getString("CTL_FontStyleBold"); break; case Font.ITALIC: style = bundle.getString("CTL_FontStyleItalic"); break; case Font.BOLD|Font.ITALIC: style = bundle.getString("CTL_FontStyleBoldItalic"); break; default: style = Integer.toString(font.getStyle()); break; 536 } 537 description = font.getName() + ' ' + font.getSize() + ' ' + style; 538 } else { 539 description = Integer.toString(size); 540 if (!absoluteSize && (size > 0)) { 541 description = '+' + description; 542 } 543 if (italic != null) { 544 description += " " + (italic.booleanValue() ? '+' : '-') + bundle.getString("CTL_FontStyleItalic"); } 546 if (bold != null) { 547 description += " " + (bold.booleanValue() ? '+' : '-') + bundle.getString("CTL_FontStyleBold"); } 549 if (description.charAt(0) == '0') description = description.substring(Math.min(2, description.length())); 550 } 551 return description; 552 } 553 554 public FormDesignValue copy(FormProperty targetFormProperty) { 555 NbFont copy = copy(); 556 copy.property = targetFormProperty; 557 return copy; 558 } 559 560 NbFont copy() { 561 NbFont newValue = new NbFont(); 562 newValue.absolute = absolute; 563 newValue.font = font; 564 newValue.italic = italic; 565 newValue.bold = bold; 566 newValue.absoluteSize = absoluteSize; 567 newValue.size = size; 568 newValue.property = property; 569 return newValue; 570 } 571 572 } 573 574 577 private class RelativeFontPanel extends JPanel { 578 private JRadioButton absoluteChoice; 579 private JSpinner absoluteSize; 580 private JRadioButton addBoldChoice; 581 private JRadioButton addItalicChoice; 582 private JCheckBox italicCheckBox; 583 private JRadioButton relativeChoice; 584 private JSpinner relativeSize; 585 private JRadioButton removeBoldChoice; 586 private JRadioButton removeItalicChoice; 587 private JCheckBox thicknessCheckBox; 588 589 RelativeFontPanel() { 590 initComponents(); 591 } 592 593 void updateFromPropertyValue() { 594 ignoreUpdates = true; 595 boolean changeItalic = (propertyValue.italic != null); 596 italicCheckBox.setSelected(changeItalic); 597 addItalicChoice.setEnabled(changeItalic); 598 removeItalicChoice.setEnabled(changeItalic); 599 if (!changeItalic) { 600 addItalicChoice.setSelected(true); 601 } else if (Boolean.TRUE.equals(propertyValue.italic)) { 602 addItalicChoice.setSelected(true); 603 } else if (Boolean.FALSE.equals(propertyValue.italic)) { 604 removeItalicChoice.setSelected(true); 605 } 606 boolean changeBold = (propertyValue.bold != null); 607 thicknessCheckBox.setSelected(changeBold); 608 addBoldChoice.setEnabled(changeBold); 609 removeBoldChoice.setEnabled(changeBold); 610 if (!changeBold) { 611 addBoldChoice.setSelected(true); 612 } else if (Boolean.TRUE.equals(propertyValue.bold)) { 613 addBoldChoice.setSelected(true); 614 } else if (Boolean.FALSE.equals(propertyValue.bold)) { 615 removeBoldChoice.setSelected(true); 616 } 617 absoluteSize.setEnabled(propertyValue.absoluteSize); 618 relativeSize.setEnabled(!propertyValue.absoluteSize); 619 if (propertyValue.absoluteSize) { 620 absoluteSize.setValue(new Integer (propertyValue.size)); 621 absoluteChoice.setSelected(true); 622 synchronizeSizeControls(); 623 } else { 624 relativeSize.setValue(new Integer (propertyValue.size)); 625 relativeChoice.setSelected(true); 626 synchronizeSizeControls(); 627 } 628 ignoreUpdates = false; 629 } 630 631 private void synchronizeSizeControls() { 632 if (propertyValue.absoluteSize) { 633 Font defaultFont = (Font)property.getDefaultValue(); 634 if (defaultFont != null) { 635 relativeSize.setValue(new Integer (propertyValue.size - defaultFont.getSize())); 636 } 637 } else { 638 Font font = (Font)propertyValue.getDesignValue(); 639 absoluteSize.setValue(new Integer (font == null ? 12 : font.getSize())); 640 } 641 } 642 643 private void initComponents() { 644 relativeSize = new JSpinner(new SpinnerNumberModel(0, Short.MIN_VALUE, Short.MAX_VALUE, 1)); 645 relativeSize.setEditor(new JSpinner.NumberEditor(relativeSize, "+#;-#")); absoluteSize = new JSpinner(new SpinnerNumberModel(12, 1, Short.MAX_VALUE, 1)); 647 648 ResourceBundle bundle = NbBundle.getBundle(FontEditor.class); 649 JLabel fontSizeLabel = new JLabel(bundle.getString("CTL_FontSize")); JLabel fontStyleLabel = new JLabel(bundle.getString("CTL_FontStyle")); 652 absoluteChoice = new JRadioButton(); 653 Mnemonics.setLocalizedText(absoluteChoice, bundle.getString("CTL_AbsoluteFontSize")); 655 relativeChoice = new JRadioButton(); 656 Mnemonics.setLocalizedText(relativeChoice, bundle.getString("CTL_RelativeFontSize")); 658 italicCheckBox = new JCheckBox(); 659 Mnemonics.setLocalizedText(italicCheckBox, bundle.getString("CTL_ChangeItalic")); 661 addItalicChoice = new JRadioButton(); 662 Mnemonics.setLocalizedText(addItalicChoice, bundle.getString("CTL_AddItalic")); 664 removeItalicChoice = new JRadioButton(); 665 Mnemonics.setLocalizedText(removeItalicChoice, bundle.getString("CTL_RemoveItalic")); 667 thicknessCheckBox = new JCheckBox(); 668 Mnemonics.setLocalizedText(thicknessCheckBox, bundle.getString("CTL_ChangeBold")); 670 addBoldChoice = new JRadioButton(); 671 Mnemonics.setLocalizedText(addBoldChoice, bundle.getString("CTL_AddBold")); 673 removeBoldChoice = new JRadioButton(); 674 Mnemonics.setLocalizedText(removeBoldChoice, bundle.getString("CTL_RemoveBold")); 676 Listener listener = new Listener (); 678 relativeChoice.addItemListener(listener); 679 thicknessCheckBox.addItemListener(listener); 680 italicCheckBox.addItemListener(listener); 681 relativeSize.addChangeListener(listener); 682 absoluteSize.addChangeListener(listener); 683 addItalicChoice.addItemListener(listener); 684 addBoldChoice.addItemListener(listener); 685 686 ButtonGroup italicGroup = new ButtonGroup(); 688 italicGroup.add(addItalicChoice); 689 italicGroup.add(removeItalicChoice); 690 691 ButtonGroup thicknessGroup = new ButtonGroup(); 692 thicknessGroup.add(addBoldChoice); 693 thicknessGroup.add(removeBoldChoice); 694 695 ButtonGroup fontSizeGroup = new ButtonGroup(); 696 fontSizeGroup.add(absoluteChoice); 697 fontSizeGroup.add(relativeChoice); 698 699 Border emptyBorder = BorderFactory.createEmptyBorder(0, 0, 0, 0); 701 Insets emptyInsets = new Insets(0, 0, 0, 0); 702 703 absoluteChoice.setBorder(emptyBorder); 704 absoluteChoice.setMargin(emptyInsets); 705 relativeChoice.setBorder(emptyBorder); 706 relativeChoice.setMargin(emptyInsets); 707 italicCheckBox.setBorder(emptyBorder); 708 italicCheckBox.setMargin(emptyInsets); 709 addItalicChoice.setBorder(emptyBorder); 710 addItalicChoice.setMargin(emptyInsets); 711 removeItalicChoice.setBorder(emptyBorder); 712 removeItalicChoice.setMargin(emptyInsets); 713 thicknessCheckBox.setBorder(emptyBorder); 714 thicknessCheckBox.setMargin(emptyInsets); 715 addBoldChoice.setBorder(emptyBorder); 716 addBoldChoice.setMargin(emptyInsets); 717 removeBoldChoice.setBorder(emptyBorder); 718 removeBoldChoice.setMargin(emptyInsets); 719 720 GroupLayout layout = new GroupLayout(this); 721 setLayout(layout); 722 layout.setHorizontalGroup(layout.createSequentialGroup() 723 .addContainerGap() 724 .add(layout.createParallelGroup() 725 .add(fontSizeLabel) 726 .add(layout.createSequentialGroup() 727 .add(layout.createParallelGroup() 728 .add(relativeChoice) 729 .add(absoluteChoice)) 730 .addPreferredGap(LayoutStyle.RELATED) 731 .add(layout.createParallelGroup() 732 .add(relativeSize, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE) 733 .add(absoluteSize, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)))) 734 .addPreferredGap(LayoutStyle.UNRELATED) 735 .add(layout.createParallelGroup() 736 .add(fontStyleLabel) 737 .add(layout.createSequentialGroup() 738 .add(layout.createParallelGroup() 739 .add(italicCheckBox) 740 .add(layout.createSequentialGroup() 741 .add(17) 742 .add(layout.createParallelGroup() 743 .add(addItalicChoice) 744 .add(removeItalicChoice)))) 745 .addPreferredGap(LayoutStyle.RELATED) 746 .add(layout.createParallelGroup() 747 .add(thicknessCheckBox) 748 .add(layout.createSequentialGroup() 749 .add(17) 750 .add(layout.createParallelGroup() 751 .add(removeBoldChoice) 752 .add(addBoldChoice)))))) 753 .addContainerGap()); 754 layout.setVerticalGroup(layout.createSequentialGroup() 755 .addContainerGap() 756 .add(layout.createParallelGroup(GroupLayout.BASELINE) 757 .add(fontSizeLabel) 758 .add(fontStyleLabel)) 759 .addPreferredGap(LayoutStyle.RELATED) 760 .add(layout.createParallelGroup(GroupLayout.BASELINE) 761 .add(relativeChoice) 762 .add(relativeSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 763 .add(italicCheckBox) 764 .add(thicknessCheckBox)) 765 .addPreferredGap(LayoutStyle.RELATED) 766 .add(layout.createParallelGroup(GroupLayout.BASELINE) 767 .add(absoluteChoice) 768 .add(absoluteSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 769 .add(addItalicChoice) 770 .add(addBoldChoice)) 771 .addPreferredGap(LayoutStyle.RELATED) 772 .add(layout.createParallelGroup(GroupLayout.BASELINE) 773 .add(removeItalicChoice) 774 .add(removeBoldChoice)) 775 .addContainerGap()); 776 } 777 778 private class Listener implements ItemListener, ChangeListener { 779 public void itemStateChanged(ItemEvent e) { 780 if (ignoreUpdates) return; 781 ignoreUpdates = true; 782 Object source = e.getSource(); 783 if (source == relativeChoice) { 784 boolean relative = relativeChoice.isSelected(); 785 relativeSize.setEnabled(relative); 786 absoluteSize.setEnabled(!relative); 787 propertyValue.absoluteSize = !relative; 788 propertyValue.size = ((Number )((relative ? relativeSize : absoluteSize).getValue())).intValue(); 789 } else if (source == italicCheckBox) { 790 boolean changeItalic = italicCheckBox.isSelected(); 791 addItalicChoice.setEnabled(changeItalic); 792 removeItalicChoice.setEnabled(changeItalic); 793 propertyValue.italic = changeItalic ? Boolean.valueOf(addItalicChoice.isSelected()) : null; 794 } else if (source == thicknessCheckBox) { 795 boolean changeBold = thicknessCheckBox.isSelected(); 796 addBoldChoice.setEnabled(changeBold); 797 removeBoldChoice.setEnabled(changeBold); 798 propertyValue.bold = changeBold ? Boolean.valueOf(addBoldChoice.isSelected()) : null; 799 } else if (source == addBoldChoice) { 800 propertyValue.bold = Boolean.valueOf(addBoldChoice.isSelected()); 801 } else if (source == addItalicChoice) { 802 propertyValue.italic = Boolean.valueOf(addItalicChoice.isSelected()); 803 } 804 ignoreUpdates = false; 805 updateDelegate(); 806 } 807 808 public void stateChanged(ChangeEvent e) { 809 if (ignoreUpdates) return; 810 ignoreUpdates = true; 811 Object source = e.getSource(); 812 if (source == relativeSize) { 813 propertyValue.size = ((Number )relativeSize.getValue()).intValue(); 814 synchronizeSizeControls(); 815 } else if(source == absoluteSize) { 816 propertyValue.size = ((Number )absoluteSize.getValue()).intValue(); 817 synchronizeSizeControls(); 818 } 819 ignoreUpdates = false; 820 updateDelegate(); 821 } 822 } 823 } 824 825 } | Popular Tags |