1. /*
  2. * @(#)SynthTextFieldUI.java 1.10 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.BasicTextFieldUI;
  13. import java.awt.*;
  14. import java.beans.PropertyChangeEvent;
  15. import sun.swing.plaf.synth.SynthUI;
  16. /**
  17. * Basis of a look and feel for a JTextField in the Synth
  18. * look and feel.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is
  23. * appropriate for short term storage or RMI between applications running
  24. * the same version of Swing. As of 1.4, support for long term storage
  25. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  26. * has been added to the <code>java.beans</code> package.
  27. * Please see {@link java.beans.XMLEncoder}.
  28. *
  29. * @author Shannon Hickey
  30. * @version 1.10 06/24/04
  31. */
  32. class SynthTextFieldUI extends BasicTextFieldUI implements SynthUI {
  33. private SynthStyle style;
  34. /**
  35. * Creates a UI for a JTextField.
  36. *
  37. * @param c the text field
  38. * @return the UI
  39. */
  40. public static ComponentUI createUI(JComponent c) {
  41. return new SynthTextFieldUI();
  42. }
  43. public SynthTextFieldUI() {
  44. super();
  45. }
  46. private void updateStyle(JTextComponent comp) {
  47. SynthContext context = getContext(comp, ENABLED);
  48. SynthStyle oldStyle = style;
  49. style = SynthLookAndFeel.updateStyle(context, this);
  50. if (style != oldStyle) {
  51. SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
  52. if (oldStyle != null) {
  53. uninstallKeyboardActions();
  54. installKeyboardActions();
  55. }
  56. }
  57. context.dispose();
  58. }
  59. static void updateStyle(JTextComponent comp, SynthContext context,
  60. String prefix) {
  61. SynthStyle style = context.getStyle();
  62. Color color = comp.getCaretColor();
  63. if (color == null || color instanceof UIResource) {
  64. comp.setCaretColor(
  65. (Color)style.get(context, prefix + ".caretForeground"));
  66. }
  67. Color fg = comp.getForeground();
  68. if (fg == null || fg instanceof UIResource) {
  69. fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND);
  70. if (fg != null) {
  71. comp.setForeground(fg);
  72. }
  73. }
  74. Object ar = style.get(context, prefix + ".caretAspectRatio");
  75. if (ar instanceof Number) {
  76. comp.putClientProperty("caretAspectRatio", ar);
  77. }
  78. context.setComponentState(SELECTED | FOCUSED);
  79. Color s = comp.getSelectionColor();
  80. if (s == null || s instanceof UIResource) {
  81. comp.setSelectionColor(
  82. style.getColor(context, ColorType.TEXT_BACKGROUND));
  83. }
  84. Color sfg = comp.getSelectedTextColor();
  85. if (sfg == null || sfg instanceof UIResource) {
  86. comp.setSelectedTextColor(
  87. style.getColor(context, ColorType.TEXT_FOREGROUND));
  88. }
  89. context.setComponentState(DISABLED);
  90. Color dfg = comp.getDisabledTextColor();
  91. if (dfg == null || dfg instanceof UIResource) {
  92. comp.setDisabledTextColor(
  93. style.getColor(context, ColorType.TEXT_FOREGROUND));
  94. }
  95. Insets margin = comp.getMargin();
  96. if (margin == null || margin instanceof UIResource) {
  97. margin = (Insets)style.get(context, prefix + ".margin");
  98. if (margin == null) {
  99. // Some places assume margins are non-null.
  100. margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
  101. }
  102. comp.setMargin(margin);
  103. }
  104. Caret caret = comp.getCaret();
  105. if (caret instanceof UIResource) {
  106. Object o = style.get(context, prefix + ".caretBlinkRate");
  107. if (o != null && o instanceof Integer) {
  108. Integer rate = (Integer)o;
  109. caret.setBlinkRate(rate.intValue());
  110. }
  111. }
  112. }
  113. public SynthContext getContext(JComponent c) {
  114. return getContext(c, getComponentState(c));
  115. }
  116. private SynthContext getContext(JComponent c, int state) {
  117. return SynthContext.getContext(SynthContext.class, c,
  118. SynthLookAndFeel.getRegion(c), style, state);
  119. }
  120. private int getComponentState(JComponent c) {
  121. return SynthLookAndFeel.getComponentState(c);
  122. }
  123. public void update(Graphics g, JComponent c) {
  124. SynthContext context = getContext(c);
  125. SynthLookAndFeel.update(context, g);
  126. paintBackground(context, g, c);
  127. paint(context, g);
  128. context.dispose();
  129. }
  130. /**
  131. * Paints the interface. This is routed to the
  132. * paintSafely method under the guarantee that
  133. * the model won't change from the view of this thread
  134. * while it's rendering (if the associated model is
  135. * derived from AbstractDocument). This enables the
  136. * model to potentially be updated asynchronously.
  137. */
  138. protected void paint(SynthContext context, Graphics g) {
  139. super.paint(g, getComponent());
  140. }
  141. void paintBackground(SynthContext context, Graphics g, JComponent c) {
  142. context.getPainter().paintTextFieldBackground(context, g, 0, 0,
  143. c.getWidth(), c.getHeight());
  144. }
  145. public void paintBorder(SynthContext context, Graphics g, int x,
  146. int y, int w, int h) {
  147. context.getPainter().paintTextFieldBorder(context, g, x, y, w, h);
  148. }
  149. protected void paintBackground(Graphics g) {
  150. // Overriden to do nothing, all our painting is done from update/paint.
  151. }
  152. /**
  153. * This method gets called when a bound property is changed
  154. * on the associated JTextComponent. This is a hook
  155. * which UI implementations may change to reflect how the
  156. * UI displays bound properties of JTextComponent subclasses.
  157. * This is implemented to do nothing (i.e. the response to
  158. * properties in JTextComponent itself are handled prior
  159. * to calling this method).
  160. *
  161. * @param evt the property change event
  162. */
  163. protected void propertyChange(PropertyChangeEvent evt) {
  164. if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
  165. updateStyle((JTextComponent)evt.getSource());
  166. }
  167. super.propertyChange(evt);
  168. }
  169. protected void installDefaults() {
  170. updateStyle((JTextComponent)getComponent());
  171. }
  172. protected void uninstallDefaults() {
  173. SynthContext context = getContext(getComponent(), ENABLED);
  174. getComponent().putClientProperty("caretAspectRatio", null);
  175. style.uninstallDefaults(context);
  176. context.dispose();
  177. style = null;
  178. super.uninstallDefaults();
  179. }
  180. public void installUI(JComponent c) {
  181. super.installUI(c);
  182. }
  183. }