1. /*
  2. * @(#)SynthTextAreaUI.java 1.8 04/06/24
  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 javax.swing.*;
  9. import javax.swing.text.*;
  10. import javax.swing.event.*;
  11. import javax.swing.plaf.*;
  12. import javax.swing.plaf.basic.BasicTextAreaUI;
  13. import java.awt.*;
  14. import java.beans.PropertyChangeEvent;
  15. import sun.swing.plaf.synth.SynthUI;
  16. /**
  17. * Provides the look and feel for a plain text editor in the
  18. * Synth look and feel. In this implementation the default UI
  19. * is extended to act as a simple view factory.
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is
  24. * appropriate for short term storage or RMI between applications running
  25. * the same version of Swing. As of 1.4, support for long term storage
  26. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  27. * has been added to the <code>java.beans</code> package.
  28. * Please see {@link java.beans.XMLEncoder}.
  29. *
  30. * @author Shannon Hickey
  31. * @version 1.8 06/24/04
  32. */
  33. class SynthTextAreaUI extends BasicTextAreaUI implements SynthUI {
  34. private SynthStyle style;
  35. /**
  36. * Creates a UI for a JTextArea.
  37. *
  38. * @param ta a text area
  39. * @return the UI
  40. */
  41. public static ComponentUI createUI(JComponent ta) {
  42. return new SynthTextAreaUI();
  43. }
  44. protected void installDefaults() {
  45. updateStyle((JTextComponent)getComponent());
  46. }
  47. protected void uninstallDefaults() {
  48. SynthContext context = getContext(getComponent(), ENABLED);
  49. getComponent().putClientProperty("caretAspectRatio", null);
  50. style.uninstallDefaults(context);
  51. context.dispose();
  52. style = null;
  53. super.uninstallDefaults();
  54. }
  55. public void installUI(JComponent c) {
  56. super.installUI(c);
  57. }
  58. private void updateStyle(JTextComponent comp) {
  59. SynthContext context = getContext(comp, ENABLED);
  60. SynthStyle oldStyle = style;
  61. style = SynthLookAndFeel.updateStyle(context, this);
  62. if (style != oldStyle) {
  63. SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
  64. if (oldStyle != null) {
  65. uninstallKeyboardActions();
  66. installKeyboardActions();
  67. }
  68. }
  69. context.dispose();
  70. }
  71. public SynthContext getContext(JComponent c) {
  72. return getContext(c, getComponentState(c));
  73. }
  74. private SynthContext getContext(JComponent c, int state) {
  75. return SynthContext.getContext(SynthContext.class, c,
  76. SynthLookAndFeel.getRegion(c), style, state);
  77. }
  78. private int getComponentState(JComponent c) {
  79. return SynthLookAndFeel.getComponentState(c);
  80. }
  81. public void update(Graphics g, JComponent c) {
  82. SynthContext context = getContext(c);
  83. SynthLookAndFeel.update(context, g);
  84. context.getPainter().paintTextAreaBackground(context,
  85. g, 0, 0, c.getWidth(), c.getHeight());
  86. paint(context, g);
  87. context.dispose();
  88. }
  89. protected void paint(SynthContext context, Graphics g) {
  90. super.paint(g, getComponent());
  91. }
  92. protected void paintBackground(Graphics g) {
  93. // Overriden to do nothing, all our painting is done from update/paint.
  94. }
  95. public void paintBorder(SynthContext context, Graphics g, int x,
  96. int y, int w, int h) {
  97. context.getPainter().paintTextAreaBorder(context, g, x, y, w, h);
  98. }
  99. /**
  100. * This method gets called when a bound property is changed
  101. * on the associated JTextComponent. This is a hook
  102. * which UI implementations may change to reflect how the
  103. * UI displays bound properties of JTextComponent subclasses.
  104. * This is implemented to rebuild the View when the
  105. * <em>WrapLine</em> or the <em>WrapStyleWord</em> property changes.
  106. *
  107. * @param evt the property change event
  108. */
  109. protected void propertyChange(PropertyChangeEvent evt) {
  110. if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
  111. updateStyle((JTextComponent)evt.getSource());
  112. }
  113. super.propertyChange(evt);
  114. }
  115. }