KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthTextFieldUI


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
8 package javax.swing.plaf.synth;
9
10 import javax.swing.*;
11 import javax.swing.text.*;
12 import javax.swing.event.*;
13 import javax.swing.plaf.*;
14 import javax.swing.plaf.basic.BasicTextFieldUI JavaDoc;
15 import java.awt.*;
16 import java.beans.PropertyChangeEvent JavaDoc;
17
18 import sun.swing.plaf.synth.SynthUI;
19
20 /**
21  * Basis of a look and feel for a JTextField in the Synth
22  * look and feel.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @author Shannon Hickey
34  * @version 1.10 06/24/04
35  */

36 class SynthTextFieldUI extends BasicTextFieldUI JavaDoc implements SynthUI {
37     private SynthStyle JavaDoc style;
38
39     /**
40      * Creates a UI for a JTextField.
41      *
42      * @param c the text field
43      * @return the UI
44      */

45     public static ComponentUI createUI(JComponent c) {
46         return new SynthTextFieldUI JavaDoc();
47     }
48
49     public SynthTextFieldUI() {
50         super();
51     }
52
53     private void updateStyle(JTextComponent comp) {
54         SynthContext JavaDoc context = getContext(comp, ENABLED);
55         SynthStyle JavaDoc oldStyle = style;
56
57         style = SynthLookAndFeel.updateStyle(context, this);
58
59         if (style != oldStyle) {
60             SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
61
62             if (oldStyle != null) {
63                 uninstallKeyboardActions();
64                 installKeyboardActions();
65             }
66         }
67         context.dispose();
68     }
69
70     static void updateStyle(JTextComponent comp, SynthContext JavaDoc context,
71             String JavaDoc prefix) {
72         SynthStyle JavaDoc style = context.getStyle();
73
74         Color color = comp.getCaretColor();
75         if (color == null || color instanceof UIResource) {
76             comp.setCaretColor(
77                 (Color)style.get(context, prefix + ".caretForeground"));
78         }
79         
80         Color fg = comp.getForeground();
81         if (fg == null || fg instanceof UIResource) {
82             fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND);
83             if (fg != null) {
84                 comp.setForeground(fg);
85             }
86         }
87
88         Object JavaDoc ar = style.get(context, prefix + ".caretAspectRatio");
89         if (ar instanceof Number JavaDoc) {
90             comp.putClientProperty("caretAspectRatio", ar);
91         }
92
93         context.setComponentState(SELECTED | FOCUSED);
94         
95         Color s = comp.getSelectionColor();
96         if (s == null || s instanceof UIResource) {
97             comp.setSelectionColor(
98                 style.getColor(context, ColorType.TEXT_BACKGROUND));
99         }
100         
101         Color sfg = comp.getSelectedTextColor();
102         if (sfg == null || sfg instanceof UIResource) {
103             comp.setSelectedTextColor(
104                 style.getColor(context, ColorType.TEXT_FOREGROUND));
105         }
106             
107         context.setComponentState(DISABLED);
108             
109         Color dfg = comp.getDisabledTextColor();
110         if (dfg == null || dfg instanceof UIResource) {
111             comp.setDisabledTextColor(
112                 style.getColor(context, ColorType.TEXT_FOREGROUND));
113         }
114             
115         Insets margin = comp.getMargin();
116         if (margin == null || margin instanceof UIResource) {
117             margin = (Insets)style.get(context, prefix + ".margin");
118
119             if (margin == null) {
120                 // Some places assume margins are non-null.
121
margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
122             }
123             comp.setMargin(margin);
124         }
125             
126         Caret caret = comp.getCaret();
127         if (caret instanceof UIResource) {
128             Object JavaDoc o = style.get(context, prefix + ".caretBlinkRate");
129             if (o != null && o instanceof Integer JavaDoc) {
130                 Integer JavaDoc rate = (Integer JavaDoc)o;
131                 caret.setBlinkRate(rate.intValue());
132             }
133         }
134     }
135
136     public SynthContext JavaDoc getContext(JComponent c) {
137         return getContext(c, getComponentState(c));
138     }
139
140     private SynthContext JavaDoc getContext(JComponent c, int state) {
141         return SynthContext.getContext(SynthContext JavaDoc.class, c,
142                     SynthLookAndFeel.getRegion(c), style, state);
143     }
144
145     private int getComponentState(JComponent c) {
146         return SynthLookAndFeel.getComponentState(c);
147     }
148
149     public void update(Graphics g, JComponent c) {
150         SynthContext JavaDoc context = getContext(c);
151
152         SynthLookAndFeel.update(context, g);
153         paintBackground(context, g, c);
154         paint(context, g);
155         context.dispose();
156     }
157
158     /**
159      * Paints the interface. This is routed to the
160      * paintSafely method under the guarantee that
161      * the model won't change from the view of this thread
162      * while it's rendering (if the associated model is
163      * derived from AbstractDocument). This enables the
164      * model to potentially be updated asynchronously.
165      */

166     protected void paint(SynthContext JavaDoc context, Graphics g) {
167         super.paint(g, getComponent());
168     }
169
170     void paintBackground(SynthContext JavaDoc context, Graphics g, JComponent c) {
171         context.getPainter().paintTextFieldBackground(context, g, 0, 0,
172                                                 c.getWidth(), c.getHeight());
173     }
174
175     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
176                             int y, int w, int h) {
177         context.getPainter().paintTextFieldBorder(context, g, x, y, w, h);
178     }
179
180     protected void paintBackground(Graphics g) {
181         // Overriden to do nothing, all our painting is done from update/paint.
182
}
183
184     /**
185      * This method gets called when a bound property is changed
186      * on the associated JTextComponent. This is a hook
187      * which UI implementations may change to reflect how the
188      * UI displays bound properties of JTextComponent subclasses.
189      * This is implemented to do nothing (i.e. the response to
190      * properties in JTextComponent itself are handled prior
191      * to calling this method).
192      *
193      * @param evt the property change event
194      */

195     protected void propertyChange(PropertyChangeEvent JavaDoc evt) {
196         if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
197             updateStyle((JTextComponent)evt.getSource());
198         }
199         super.propertyChange(evt);
200     }
201
202     protected void installDefaults() {
203         updateStyle((JTextComponent)getComponent());
204     }
205
206     protected void uninstallDefaults() {
207         SynthContext JavaDoc context = getContext(getComponent(), ENABLED);
208
209         getComponent().putClientProperty("caretAspectRatio", null);
210
211         style.uninstallDefaults(context);
212         context.dispose();
213         style = null;
214         super.uninstallDefaults();
215     }
216
217     public void installUI(JComponent c) {
218         super.installUI(c);
219     }
220 }
221
Popular Tags