1. /*
  2. * @(#)SynthEditorPaneUI.java 1.8 04/07/23
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.synth;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.text.*;
  11. import javax.swing.plaf.*;
  12. import javax.swing.plaf.basic.BasicEditorPaneUI;
  13. import java.beans.PropertyChangeEvent;
  14. import sun.swing.plaf.synth.SynthUI;
  15. /**
  16. * Provides the look and feel for a JEditorPane in the
  17. * Synth look and feel.
  18. *
  19. * @author Shannon Hickey
  20. * @version 1.8 07/23/04
  21. */
  22. class SynthEditorPaneUI extends BasicEditorPaneUI implements SynthUI {
  23. private SynthStyle style;
  24. /*
  25. * I would prefer to use UIResource instad of this.
  26. * Unfortunately Boolean is a final class
  27. */
  28. private Boolean localTrue = new Boolean(true);
  29. private Boolean localFalse = new Boolean(false);
  30. /**
  31. * Creates a UI for the JTextPane.
  32. *
  33. * @param c the JTextPane component
  34. * @return the UI
  35. */
  36. public static ComponentUI createUI(JComponent c) {
  37. return new SynthEditorPaneUI();
  38. }
  39. protected void installDefaults() {
  40. JComponent c = getComponent();
  41. Object clientProperty =
  42. c.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
  43. if (clientProperty == null
  44. || clientProperty == localFalse) {
  45. c.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
  46. localTrue);
  47. }
  48. updateStyle((JTextComponent)getComponent());
  49. }
  50. protected void uninstallDefaults() {
  51. SynthContext context = getContext(getComponent(), ENABLED);
  52. JComponent c = getComponent();
  53. c.putClientProperty("caretAspectRatio", null);
  54. style.uninstallDefaults(context);
  55. context.dispose();
  56. style = null;
  57. Object clientProperty =
  58. c.getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
  59. if (clientProperty == localTrue) {
  60. getComponent().putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
  61. Boolean.FALSE);
  62. }
  63. super.uninstallDefaults();
  64. }
  65. /**
  66. * This method gets called when a bound property is changed
  67. * on the associated JTextComponent. This is a hook
  68. * which UI implementations may change to reflect how the
  69. * UI displays bound properties of JTextComponent subclasses.
  70. * This is implemented to rebuild the ActionMap based upon an
  71. * EditorKit change.
  72. *
  73. * @param evt the property change event
  74. */
  75. protected void propertyChange(PropertyChangeEvent evt) {
  76. if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
  77. updateStyle((JTextComponent)evt.getSource());
  78. }
  79. super.propertyChange(evt);
  80. }
  81. private void updateStyle(JTextComponent comp) {
  82. SynthContext context = getContext(comp, ENABLED);
  83. SynthStyle oldStyle = style;
  84. style = SynthLookAndFeel.updateStyle(context, this);
  85. if (style != oldStyle) {
  86. SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
  87. if (oldStyle != null) {
  88. uninstallKeyboardActions();
  89. installKeyboardActions();
  90. }
  91. }
  92. context.dispose();
  93. }
  94. public SynthContext getContext(JComponent c) {
  95. return getContext(c, getComponentState(c));
  96. }
  97. private SynthContext getContext(JComponent c, int state) {
  98. return SynthContext.getContext(SynthContext.class, c,
  99. SynthLookAndFeel.getRegion(c), style, state);
  100. }
  101. private int getComponentState(JComponent c) {
  102. return SynthLookAndFeel.getComponentState(c);
  103. }
  104. public void update(Graphics g, JComponent c) {
  105. SynthContext context = getContext(c);
  106. SynthLookAndFeel.update(context, g);
  107. paintBackground(context, g, c);
  108. paint(context, g);
  109. context.dispose();
  110. }
  111. protected void paint(SynthContext context, Graphics g) {
  112. super.paint(g, getComponent());
  113. }
  114. protected void paintBackground(Graphics g) {
  115. // Overriden to do nothing, all our painting is done from update/paint.
  116. }
  117. void paintBackground(SynthContext context, Graphics g, JComponent c) {
  118. context.getPainter().paintEditorPaneBackground(context, g, 0, 0,
  119. c.getWidth(), c.getHeight());
  120. }
  121. public void paintBorder(SynthContext context, Graphics g, int x,
  122. int y, int w, int h) {
  123. context.getPainter().paintEditorPaneBorder(context, g, x, y, w, h);
  124. }
  125. }