author | peterz |
Thu, 28 Jan 2010 17:06:54 +0300 | |
changeset 4848 | ffcc849b9351 |
parent 4394 | 92a8ec883f5d |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1639 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing.plaf.synth; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.text.*; |
|
30 |
import javax.swing.plaf.*; |
|
31 |
import javax.swing.plaf.basic.BasicTextFieldUI; |
|
32 |
import java.awt.*; |
|
33 |
import java.awt.event.FocusEvent; |
|
34 |
import java.awt.event.FocusListener; |
|
35 |
import java.beans.PropertyChangeEvent; |
|
36 |
||
37 |
||
38 |
/** |
|
4394 | 39 |
* Provides the Synth L&F UI delegate for {@link javax.swing.JTextField}. |
2 | 40 |
* <p> |
41 |
* <strong>Warning:</strong> |
|
42 |
* Serialized objects of this class will not be compatible with |
|
43 |
* future Swing releases. The current serialization support is |
|
44 |
* appropriate for short term storage or RMI between applications running |
|
45 |
* the same version of Swing. As of 1.4, support for long term storage |
|
46 |
* of all JavaBeans<sup><font size="-2">TM</font></sup> |
|
47 |
* has been added to the <code>java.beans</code> package. |
|
48 |
* Please see {@link java.beans.XMLEncoder}. |
|
49 |
* |
|
50 |
* @author Shannon Hickey |
|
4394 | 51 |
* @since 1.7 |
2 | 52 |
*/ |
4394 | 53 |
public class SynthTextFieldUI extends BasicTextFieldUI implements SynthUI { |
54 |
private Handler handler = new Handler(); |
|
2 | 55 |
private SynthStyle style; |
56 |
||
57 |
/** |
|
58 |
* Creates a UI for a JTextField. |
|
59 |
* |
|
60 |
* @param c the text field |
|
4394 | 61 |
* @return the UI object |
2 | 62 |
*/ |
63 |
public static ComponentUI createUI(JComponent c) { |
|
64 |
return new SynthTextFieldUI(); |
|
65 |
} |
|
66 |
||
67 |
private void updateStyle(JTextComponent comp) { |
|
68 |
SynthContext context = getContext(comp, ENABLED); |
|
69 |
SynthStyle oldStyle = style; |
|
70 |
||
71 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
72 |
||
73 |
if (style != oldStyle) { |
|
74 |
SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix()); |
|
75 |
||
76 |
if (oldStyle != null) { |
|
77 |
uninstallKeyboardActions(); |
|
78 |
installKeyboardActions(); |
|
79 |
} |
|
80 |
} |
|
81 |
context.dispose(); |
|
82 |
} |
|
83 |
||
84 |
static void updateStyle(JTextComponent comp, SynthContext context, |
|
85 |
String prefix) { |
|
86 |
SynthStyle style = context.getStyle(); |
|
87 |
||
88 |
Color color = comp.getCaretColor(); |
|
89 |
if (color == null || color instanceof UIResource) { |
|
90 |
comp.setCaretColor( |
|
91 |
(Color)style.get(context, prefix + ".caretForeground")); |
|
92 |
} |
|
93 |
||
94 |
Color fg = comp.getForeground(); |
|
95 |
if (fg == null || fg instanceof UIResource) { |
|
96 |
fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND); |
|
97 |
if (fg != null) { |
|
98 |
comp.setForeground(fg); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
Object ar = style.get(context, prefix + ".caretAspectRatio"); |
|
103 |
if (ar instanceof Number) { |
|
104 |
comp.putClientProperty("caretAspectRatio", ar); |
|
105 |
} |
|
106 |
||
107 |
context.setComponentState(SELECTED | FOCUSED); |
|
108 |
||
109 |
Color s = comp.getSelectionColor(); |
|
110 |
if (s == null || s instanceof UIResource) { |
|
111 |
comp.setSelectionColor( |
|
112 |
style.getColor(context, ColorType.TEXT_BACKGROUND)); |
|
113 |
} |
|
114 |
||
115 |
Color sfg = comp.getSelectedTextColor(); |
|
116 |
if (sfg == null || sfg instanceof UIResource) { |
|
117 |
comp.setSelectedTextColor( |
|
118 |
style.getColor(context, ColorType.TEXT_FOREGROUND)); |
|
119 |
} |
|
120 |
||
121 |
context.setComponentState(DISABLED); |
|
122 |
||
123 |
Color dfg = comp.getDisabledTextColor(); |
|
124 |
if (dfg == null || dfg instanceof UIResource) { |
|
125 |
comp.setDisabledTextColor( |
|
126 |
style.getColor(context, ColorType.TEXT_FOREGROUND)); |
|
127 |
} |
|
128 |
||
129 |
Insets margin = comp.getMargin(); |
|
130 |
if (margin == null || margin instanceof UIResource) { |
|
131 |
margin = (Insets)style.get(context, prefix + ".margin"); |
|
132 |
||
133 |
if (margin == null) { |
|
134 |
// Some places assume margins are non-null. |
|
135 |
margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; |
|
136 |
} |
|
137 |
comp.setMargin(margin); |
|
138 |
} |
|
139 |
||
140 |
Caret caret = comp.getCaret(); |
|
141 |
if (caret instanceof UIResource) { |
|
142 |
Object o = style.get(context, prefix + ".caretBlinkRate"); |
|
143 |
if (o != null && o instanceof Integer) { |
|
144 |
Integer rate = (Integer)o; |
|
145 |
caret.setBlinkRate(rate.intValue()); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
||
4394 | 150 |
/** |
151 |
* @inheritDoc |
|
152 |
*/ |
|
153 |
@Override |
|
2 | 154 |
public SynthContext getContext(JComponent c) { |
4394 | 155 |
return getContext(c, SynthLookAndFeel.getComponentState(c)); |
2 | 156 |
} |
157 |
||
158 |
private SynthContext getContext(JComponent c, int state) { |
|
159 |
return SynthContext.getContext(SynthContext.class, c, |
|
160 |
SynthLookAndFeel.getRegion(c), style, state); |
|
161 |
} |
|
162 |
||
4394 | 163 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
164 |
* Notifies this UI delegate to repaint the specified component. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
165 |
* This method paints the component background, then calls |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
166 |
* the {@link #paint(SynthContext,Graphics)} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
167 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
168 |
* <p>In general, this method does not need to be overridden by subclasses. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
169 |
* All Look and Feel rendering code should reside in the {@code paint} method. |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
170 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
171 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
172 |
* @param c the component being painted |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
173 |
* @see #paint(SynthContext,Graphics) |
4394 | 174 |
*/ |
175 |
@Override |
|
2 | 176 |
public void update(Graphics g, JComponent c) { |
177 |
SynthContext context = getContext(c); |
|
178 |
||
179 |
SynthLookAndFeel.update(context, g); |
|
180 |
paintBackground(context, g, c); |
|
181 |
paint(context, g); |
|
182 |
context.dispose(); |
|
183 |
} |
|
184 |
||
185 |
/** |
|
4394 | 186 |
* Paints the specified component. |
187 |
* <p>This is routed to the {@link #paintSafely} method under |
|
188 |
* the guarantee that the model does not change from the view of this |
|
189 |
* thread while it is rendering (if the associated model is |
|
190 |
* derived from {@code AbstractDocument}). This enables the |
|
2 | 191 |
* model to potentially be updated asynchronously. |
4394 | 192 |
* |
193 |
* @param context context for the component being painted |
|
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
194 |
* @param g the {@code Graphics} object used for painting |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
195 |
* @see #update(Graphics,JComponent) |
2 | 196 |
*/ |
197 |
protected void paint(SynthContext context, Graphics g) { |
|
198 |
super.paint(g, getComponent()); |
|
199 |
} |
|
200 |
||
201 |
void paintBackground(SynthContext context, Graphics g, JComponent c) { |
|
202 |
context.getPainter().paintTextFieldBackground(context, g, 0, 0, |
|
203 |
c.getWidth(), c.getHeight()); |
|
204 |
} |
|
205 |
||
4394 | 206 |
/** |
207 |
* @inheritDoc |
|
208 |
*/ |
|
209 |
@Override |
|
2 | 210 |
public void paintBorder(SynthContext context, Graphics g, int x, |
211 |
int y, int w, int h) { |
|
212 |
context.getPainter().paintTextFieldBorder(context, g, x, y, w, h); |
|
213 |
} |
|
214 |
||
4394 | 215 |
/** |
216 |
* @inheritDoc |
|
217 |
* Overridden to do nothing. |
|
218 |
*/ |
|
219 |
@Override |
|
2 | 220 |
protected void paintBackground(Graphics g) { |
221 |
// Overriden to do nothing, all our painting is done from update/paint. |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* This method gets called when a bound property is changed |
|
226 |
* on the associated JTextComponent. This is a hook |
|
227 |
* which UI implementations may change to reflect how the |
|
228 |
* UI displays bound properties of JTextComponent subclasses. |
|
229 |
* This is implemented to do nothing (i.e. the response to |
|
230 |
* properties in JTextComponent itself are handled prior |
|
231 |
* to calling this method). |
|
232 |
* |
|
233 |
* @param evt the property change event |
|
234 |
*/ |
|
4394 | 235 |
@Override |
2 | 236 |
protected void propertyChange(PropertyChangeEvent evt) { |
237 |
if (SynthLookAndFeel.shouldUpdateStyle(evt)) { |
|
238 |
updateStyle((JTextComponent)evt.getSource()); |
|
239 |
} |
|
240 |
super.propertyChange(evt); |
|
241 |
} |
|
242 |
||
4394 | 243 |
/** |
244 |
* @inheritDoc |
|
245 |
*/ |
|
246 |
@Override |
|
2 | 247 |
protected void installDefaults() { |
248 |
// Installs the text cursor on the component |
|
249 |
super.installDefaults(); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
250 |
updateStyle(getComponent()); |
4394 | 251 |
getComponent().addFocusListener(handler); |
2 | 252 |
} |
253 |
||
4394 | 254 |
/** |
255 |
* @inheritDoc |
|
256 |
*/ |
|
257 |
@Override |
|
2 | 258 |
protected void uninstallDefaults() { |
259 |
SynthContext context = getContext(getComponent(), ENABLED); |
|
260 |
||
261 |
getComponent().putClientProperty("caretAspectRatio", null); |
|
4394 | 262 |
getComponent().removeFocusListener(handler); |
2 | 263 |
|
264 |
style.uninstallDefaults(context); |
|
265 |
context.dispose(); |
|
266 |
style = null; |
|
267 |
super.uninstallDefaults(); |
|
268 |
} |
|
269 |
||
4394 | 270 |
private final class Handler implements FocusListener { |
271 |
public void focusGained(FocusEvent e) { |
|
272 |
getComponent().repaint(); |
|
273 |
} |
|
274 |
||
275 |
public void focusLost(FocusEvent e) { |
|
276 |
getComponent().repaint(); |
|
277 |
} |
|
2 | 278 |
} |
279 |
} |