author | alexsch |
Fri, 15 Apr 2016 19:15:12 +0400 | |
changeset 37698 | 4d798c873df0 |
parent 25859 | 3317bb8137f4 |
child 47140 | 519fd8ec4ce1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 2002, 2014, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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 |
/** |
|
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
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 |
|
20458 | 46 |
* of all JavaBeans™ |
2 | 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 |
*/ |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
53 |
@SuppressWarnings("serial") // Same-version serialization only |
4394 | 54 |
public class SynthTextFieldUI extends BasicTextFieldUI implements SynthUI { |
55 |
private Handler handler = new Handler(); |
|
2 | 56 |
private SynthStyle style; |
57 |
||
58 |
/** |
|
59 |
* Creates a UI for a JTextField. |
|
60 |
* |
|
61 |
* @param c the text field |
|
4394 | 62 |
* @return the UI object |
2 | 63 |
*/ |
64 |
public static ComponentUI createUI(JComponent c) { |
|
65 |
return new SynthTextFieldUI(); |
|
66 |
} |
|
67 |
||
68 |
private void updateStyle(JTextComponent comp) { |
|
69 |
SynthContext context = getContext(comp, ENABLED); |
|
70 |
SynthStyle oldStyle = style; |
|
71 |
||
72 |
style = SynthLookAndFeel.updateStyle(context, this); |
|
73 |
||
74 |
if (style != oldStyle) { |
|
75 |
SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix()); |
|
76 |
||
77 |
if (oldStyle != null) { |
|
78 |
uninstallKeyboardActions(); |
|
79 |
installKeyboardActions(); |
|
80 |
} |
|
81 |
} |
|
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 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
151 |
* {@inheritDoc} |
4394 | 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) { |
|
25100 | 159 |
return SynthContext.getContext(c, style, state); |
2 | 160 |
} |
161 |
||
4394 | 162 |
/** |
4848
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
163 |
* 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
|
164 |
* 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
|
165 |
* 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
|
166 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
167 |
* <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
|
168 |
* 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
|
169 |
* |
ffcc849b9351
6912118: Incosistency in several SynthUI classes between inherited specs ofupdate() and paint() methods
peterz
parents:
4394
diff
changeset
|
170 |
* @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
|
171 |
* @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
|
172 |
* @see #paint(SynthContext,Graphics) |
4394 | 173 |
*/ |
174 |
@Override |
|
2 | 175 |
public void update(Graphics g, JComponent c) { |
176 |
SynthContext context = getContext(c); |
|
177 |
||
178 |
SynthLookAndFeel.update(context, g); |
|
179 |
paintBackground(context, g, c); |
|
180 |
paint(context, g); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
4394 | 184 |
* Paints the specified component. |
185 |
* <p>This is routed to the {@link #paintSafely} method under |
|
186 |
* the guarantee that the model does not change from the view of this |
|
187 |
* thread while it is rendering (if the associated model is |
|
188 |
* derived from {@code AbstractDocument}). This enables the |
|
2 | 189 |
* model to potentially be updated asynchronously. |
4394 | 190 |
* |
191 |
* @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
|
192 |
* @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
|
193 |
* @see #update(Graphics,JComponent) |
2 | 194 |
*/ |
195 |
protected void paint(SynthContext context, Graphics g) { |
|
196 |
super.paint(g, getComponent()); |
|
197 |
} |
|
198 |
||
199 |
void paintBackground(SynthContext context, Graphics g, JComponent c) { |
|
200 |
context.getPainter().paintTextFieldBackground(context, g, 0, 0, |
|
201 |
c.getWidth(), c.getHeight()); |
|
202 |
} |
|
203 |
||
4394 | 204 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
205 |
* {@inheritDoc} |
4394 | 206 |
*/ |
207 |
@Override |
|
2 | 208 |
public void paintBorder(SynthContext context, Graphics g, int x, |
209 |
int y, int w, int h) { |
|
210 |
context.getPainter().paintTextFieldBorder(context, g, x, y, w, h); |
|
211 |
} |
|
212 |
||
4394 | 213 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
214 |
* {@inheritDoc} |
4394 | 215 |
* Overridden to do nothing. |
216 |
*/ |
|
217 |
@Override |
|
2 | 218 |
protected void paintBackground(Graphics g) { |
219 |
// Overriden to do nothing, all our painting is done from update/paint. |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* This method gets called when a bound property is changed |
|
224 |
* on the associated JTextComponent. This is a hook |
|
225 |
* which UI implementations may change to reflect how the |
|
226 |
* UI displays bound properties of JTextComponent subclasses. |
|
227 |
* This is implemented to do nothing (i.e. the response to |
|
228 |
* properties in JTextComponent itself are handled prior |
|
229 |
* to calling this method). |
|
230 |
* |
|
231 |
* @param evt the property change event |
|
232 |
*/ |
|
4394 | 233 |
@Override |
2 | 234 |
protected void propertyChange(PropertyChangeEvent evt) { |
235 |
if (SynthLookAndFeel.shouldUpdateStyle(evt)) { |
|
236 |
updateStyle((JTextComponent)evt.getSource()); |
|
237 |
} |
|
238 |
super.propertyChange(evt); |
|
239 |
} |
|
240 |
||
4394 | 241 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
242 |
* {@inheritDoc} |
4394 | 243 |
*/ |
244 |
@Override |
|
2 | 245 |
protected void installDefaults() { |
246 |
// Installs the text cursor on the component |
|
247 |
super.installDefaults(); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
248 |
updateStyle(getComponent()); |
4394 | 249 |
getComponent().addFocusListener(handler); |
2 | 250 |
} |
251 |
||
4394 | 252 |
/** |
20168
137788883a22
8025070: [javadoc] fix some javadoc errors in javax/swing/plaf/synth
alexsch
parents:
7668
diff
changeset
|
253 |
* {@inheritDoc} |
4394 | 254 |
*/ |
255 |
@Override |
|
2 | 256 |
protected void uninstallDefaults() { |
257 |
SynthContext context = getContext(getComponent(), ENABLED); |
|
258 |
||
259 |
getComponent().putClientProperty("caretAspectRatio", null); |
|
4394 | 260 |
getComponent().removeFocusListener(handler); |
2 | 261 |
|
262 |
style.uninstallDefaults(context); |
|
263 |
style = null; |
|
264 |
super.uninstallDefaults(); |
|
265 |
} |
|
266 |
||
4394 | 267 |
private final class Handler implements FocusListener { |
268 |
public void focusGained(FocusEvent e) { |
|
269 |
getComponent().repaint(); |
|
270 |
} |
|
271 |
||
272 |
public void focusLost(FocusEvent e) { |
|
273 |
getComponent().repaint(); |
|
274 |
} |
|
2 | 275 |
} |
276 |
} |