author | rupashka |
Wed, 17 Mar 2010 12:48:21 +0300 | |
changeset 5133 | 0a1777c5b601 |
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 |
package javax.swing.plaf.synth; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.beans.*; |
|
29 |
import java.io.*; |
|
30 |
import java.lang.ref.*; |
|
31 |
import java.net.*; |
|
32 |
import java.security.*; |
|
33 |
import java.text.*; |
|
34 |
import java.util.*; |
|
35 |
import javax.swing.*; |
|
36 |
import javax.swing.plaf.*; |
|
37 |
import javax.swing.plaf.basic.*; |
|
38 |
||
39 |
import sun.awt.*; |
|
40 |
import sun.security.action.*; |
|
41 |
import sun.swing.*; |
|
42 |
import sun.swing.plaf.synth.*; |
|
43 |
||
44 |
/** |
|
45 |
* SynthLookAndFeel provides the basis for creating a customized look and |
|
46 |
* feel. SynthLookAndFeel does not directly provide a look, all painting is |
|
47 |
* delegated. |
|
48 |
* You need to either provide a configuration file, by way of the |
|
49 |
* {@link #load} method, or provide your own {@link SynthStyleFactory} |
|
50 |
* to {@link #setStyleFactory}. Refer to the |
|
51 |
* <a href="package-summary.html">package summary</a> for an example of |
|
52 |
* loading a file, and {@link javax.swing.plaf.synth.SynthStyleFactory} for |
|
53 |
* an example of providing your own <code>SynthStyleFactory</code> to |
|
54 |
* <code>setStyleFactory</code>. |
|
55 |
* <p> |
|
56 |
* <strong>Warning:</strong> |
|
57 |
* This class implements {@link Serializable} as a side effect of it |
|
58 |
* extending {@link BasicLookAndFeel}. It is not intended to be serialized. |
|
59 |
* An attempt to serialize it will |
|
60 |
* result in {@link NotSerializableException}. |
|
61 |
* |
|
62 |
* @serial exclude |
|
63 |
* @since 1.5 |
|
64 |
* @author Scott Violet |
|
65 |
*/ |
|
66 |
public class SynthLookAndFeel extends BasicLookAndFeel { |
|
67 |
/** |
|
68 |
* Used in a handful of places where we need an empty Insets. |
|
69 |
*/ |
|
70 |
static final Insets EMPTY_UIRESOURCE_INSETS = new InsetsUIResource( |
|
71 |
0, 0, 0, 0); |
|
72 |
||
73 |
/** |
|
74 |
* AppContext key to get the current SynthStyleFactory. |
|
75 |
*/ |
|
76 |
private static final Object STYLE_FACTORY_KEY = |
|
77 |
new StringBuffer("com.sun.java.swing.plaf.gtk.StyleCache"); |
|
78 |
||
79 |
/** |
|
80 |
* The last SynthStyleFactory that was asked for from AppContext |
|
81 |
* <code>lastContext</code>. |
|
82 |
*/ |
|
83 |
private static SynthStyleFactory lastFactory; |
|
84 |
/** |
|
85 |
* If this is true it indicates there is more than one AppContext active |
|
86 |
* and that we need to make sure in getStyleCache the requesting |
|
87 |
* AppContext matches that of <code>lastContext</code> before returning |
|
88 |
* it. |
|
89 |
*/ |
|
90 |
private static boolean multipleApps; |
|
91 |
/** |
|
92 |
* AppContext lastLAF came from. |
|
93 |
*/ |
|
94 |
private static AppContext lastContext; |
|
95 |
||
96 |
// Refer to setSelectedUI |
|
97 |
static ComponentUI selectedUI; |
|
98 |
// Refer to setSelectedUI |
|
99 |
static int selectedUIState; |
|
100 |
||
101 |
/** |
|
102 |
* SynthStyleFactory for the this SynthLookAndFeel. |
|
103 |
*/ |
|
104 |
private SynthStyleFactory factory; |
|
105 |
||
106 |
/** |
|
107 |
* Map of defaults table entries. This is populated via the load |
|
108 |
* method. |
|
109 |
*/ |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
110 |
private Map<String, Object> defaultsMap; |
2 | 111 |
|
112 |
private Handler _handler; |
|
113 |
||
114 |
/** |
|
115 |
* Used by the renderers. For the most part the renderers are implemented |
|
116 |
* as Labels, which is problematic in so far as they are never selected. |
|
117 |
* To accomodate this SynthLabelUI checks if the current |
|
118 |
* UI matches that of <code>selectedUI</code> (which this methods sets), if |
|
119 |
* it does, then a state as set by this method is returned. This provides |
|
120 |
* a way for labels to have a state other than selected. |
|
121 |
*/ |
|
122 |
static void setSelectedUI(ComponentUI uix, boolean selected, |
|
123 |
boolean focused, boolean enabled, |
|
124 |
boolean rollover) { |
|
125 |
selectedUI = uix; |
|
126 |
selectedUIState = 0; |
|
127 |
if (selected) { |
|
128 |
selectedUIState = SynthConstants.SELECTED; |
|
129 |
if (focused) { |
|
130 |
selectedUIState |= SynthConstants.FOCUSED; |
|
131 |
} |
|
132 |
} |
|
133 |
else if (rollover && enabled) { |
|
134 |
selectedUIState |= |
|
135 |
SynthConstants.MOUSE_OVER | SynthConstants.ENABLED; |
|
136 |
if (focused) { |
|
137 |
selectedUIState |= SynthConstants.FOCUSED; |
|
138 |
} |
|
139 |
} |
|
140 |
else { |
|
141 |
if (enabled) { |
|
142 |
selectedUIState |= SynthConstants.ENABLED; |
|
2658 | 143 |
selectedUIState = SynthConstants.FOCUSED; |
2 | 144 |
} |
145 |
else { |
|
146 |
selectedUIState |= SynthConstants.DISABLED; |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Clears out the selected UI that was last set in setSelectedUI. |
|
153 |
*/ |
|
154 |
static void resetSelectedUI() { |
|
155 |
selectedUI = null; |
|
156 |
} |
|
157 |
||
158 |
||
159 |
/** |
|
160 |
* Sets the SynthStyleFactory that the UI classes provided by |
|
161 |
* synth will use to obtain a SynthStyle. |
|
162 |
* |
|
163 |
* @param cache SynthStyleFactory the UIs should use. |
|
164 |
*/ |
|
165 |
public static void setStyleFactory(SynthStyleFactory cache) { |
|
166 |
// We assume the setter is called BEFORE the getter has been invoked |
|
167 |
// for a particular AppContext. |
|
168 |
synchronized(SynthLookAndFeel.class) { |
|
169 |
AppContext context = AppContext.getAppContext(); |
|
170 |
if (!multipleApps && context != lastContext && |
|
171 |
lastContext != null) { |
|
172 |
multipleApps = true; |
|
173 |
} |
|
174 |
lastFactory = cache; |
|
175 |
lastContext = context; |
|
176 |
context.put(STYLE_FACTORY_KEY, cache); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Returns the current SynthStyleFactory. |
|
182 |
* |
|
183 |
* @return SynthStyleFactory |
|
184 |
*/ |
|
185 |
public static SynthStyleFactory getStyleFactory() { |
|
186 |
synchronized(SynthLookAndFeel.class) { |
|
187 |
if (!multipleApps) { |
|
188 |
return lastFactory; |
|
189 |
} |
|
190 |
AppContext context = AppContext.getAppContext(); |
|
191 |
||
192 |
if (lastContext == context) { |
|
193 |
return lastFactory; |
|
194 |
} |
|
195 |
lastContext = context; |
|
196 |
lastFactory = (SynthStyleFactory)AppContext.getAppContext().get |
|
197 |
(STYLE_FACTORY_KEY); |
|
198 |
return lastFactory; |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Returns the component state for the specified component. This should |
|
204 |
* only be used for Components that don't have any special state beyond |
|
205 |
* that of ENABLED, DISABLED or FOCUSED. For example, buttons shouldn't |
|
206 |
* call into this method. |
|
207 |
*/ |
|
208 |
static int getComponentState(Component c) { |
|
209 |
if (c.isEnabled()) { |
|
210 |
if (c.isFocusOwner()) { |
|
211 |
return SynthUI.ENABLED | SynthUI.FOCUSED; |
|
212 |
} |
|
213 |
return SynthUI.ENABLED; |
|
214 |
} |
|
215 |
return SynthUI.DISABLED; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Gets a SynthStyle for the specified region of the specified component. |
|
220 |
* This is not for general consumption, only custom UIs should call this |
|
221 |
* method. |
|
222 |
* |
|
223 |
* @param c JComponent to get the SynthStyle for |
|
224 |
* @param region Identifies the region of the specified component |
|
225 |
* @return SynthStyle to use. |
|
226 |
*/ |
|
227 |
public static SynthStyle getStyle(JComponent c, Region region) { |
|
228 |
return getStyleFactory().getStyle(c, region); |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Returns true if the Style should be updated in response to the |
|
233 |
* specified PropertyChangeEvent. This forwards to |
|
234 |
* <code>shouldUpdateStyleOnAncestorChanged</code> as necessary. |
|
235 |
*/ |
|
236 |
static boolean shouldUpdateStyle(PropertyChangeEvent event) { |
|
4394 | 237 |
LookAndFeel laf = UIManager.getLookAndFeel(); |
238 |
return (laf instanceof SynthLookAndFeel && |
|
239 |
((SynthLookAndFeel) laf).shouldUpdateStyleOnEvent(event)); |
|
2 | 240 |
} |
241 |
||
242 |
/** |
|
243 |
* A convience method that will reset the Style of StyleContext if |
|
244 |
* necessary. |
|
245 |
* |
|
246 |
* @return newStyle |
|
247 |
*/ |
|
248 |
static SynthStyle updateStyle(SynthContext context, SynthUI ui) { |
|
249 |
SynthStyle newStyle = getStyle(context.getComponent(), |
|
250 |
context.getRegion()); |
|
251 |
SynthStyle oldStyle = context.getStyle(); |
|
252 |
||
253 |
if (newStyle != oldStyle) { |
|
254 |
if (oldStyle != null) { |
|
255 |
oldStyle.uninstallDefaults(context); |
|
256 |
} |
|
257 |
context.setStyle(newStyle); |
|
258 |
newStyle.installDefaults(context, ui); |
|
259 |
} |
|
260 |
return newStyle; |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Updates the style associated with <code>c</code>, and all its children. |
|
265 |
* This is a lighter version of |
|
266 |
* <code>SwingUtilities.updateComponentTreeUI</code>. |
|
267 |
* |
|
268 |
* @param c Component to update style for. |
|
269 |
*/ |
|
270 |
public static void updateStyles(Component c) { |
|
271 |
if (c instanceof JComponent) { |
|
272 |
// Yes, this is hacky. A better solution is to get the UI |
|
273 |
// and cast, but JComponent doesn't expose a getter for the UI |
|
274 |
// (each of the UIs do), making that approach impractical. |
|
275 |
String name = c.getName(); |
|
276 |
c.setName(null); |
|
277 |
if (name != null) { |
|
278 |
c.setName(name); |
|
279 |
} |
|
280 |
((JComponent)c).revalidate(); |
|
281 |
} |
|
282 |
Component[] children = null; |
|
283 |
if (c instanceof JMenu) { |
|
284 |
children = ((JMenu)c).getMenuComponents(); |
|
285 |
} |
|
286 |
else if (c instanceof Container) { |
|
287 |
children = ((Container)c).getComponents(); |
|
288 |
} |
|
289 |
if (children != null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
290 |
for (Component child : children) { |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
291 |
updateStyles(child); |
2 | 292 |
} |
293 |
} |
|
4394 | 294 |
c.repaint(); |
2 | 295 |
} |
296 |
||
297 |
/** |
|
298 |
* Returns the Region for the JComponent <code>c</code>. |
|
299 |
* |
|
300 |
* @param c JComponent to fetch the Region for |
|
301 |
* @return Region corresponding to <code>c</code> |
|
302 |
*/ |
|
303 |
public static Region getRegion(JComponent c) { |
|
304 |
return Region.getRegion(c); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* A convenience method to return where the foreground should be |
|
309 |
* painted for the Component identified by the passed in |
|
310 |
* AbstractSynthContext. |
|
311 |
*/ |
|
312 |
static Insets getPaintingInsets(SynthContext state, Insets insets) { |
|
313 |
if (state.isSubregion()) { |
|
314 |
insets = state.getStyle().getInsets(state, insets); |
|
315 |
} |
|
316 |
else { |
|
317 |
insets = state.getComponent().getInsets(insets); |
|
318 |
} |
|
319 |
return insets; |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* A convenience method that handles painting of the background. |
|
324 |
* All SynthUI implementations should override update and invoke |
|
325 |
* this method. |
|
326 |
*/ |
|
327 |
static void update(SynthContext state, Graphics g) { |
|
328 |
paintRegion(state, g, null); |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* A convenience method that handles painting of the background for |
|
333 |
* subregions. All SynthUI's that have subregions should invoke |
|
334 |
* this method, than paint the foreground. |
|
335 |
*/ |
|
336 |
static void updateSubregion(SynthContext state, Graphics g, |
|
337 |
Rectangle bounds) { |
|
338 |
paintRegion(state, g, bounds); |
|
339 |
} |
|
340 |
||
341 |
private static void paintRegion(SynthContext state, Graphics g, |
|
342 |
Rectangle bounds) { |
|
343 |
JComponent c = state.getComponent(); |
|
344 |
SynthStyle style = state.getStyle(); |
|
345 |
int x, y, width, height; |
|
346 |
||
347 |
if (bounds == null) { |
|
348 |
x = 0; |
|
349 |
y = 0; |
|
350 |
width = c.getWidth(); |
|
351 |
height = c.getHeight(); |
|
352 |
} |
|
353 |
else { |
|
354 |
x = bounds.x; |
|
355 |
y = bounds.y; |
|
356 |
width = bounds.width; |
|
357 |
height = bounds.height; |
|
358 |
} |
|
359 |
||
360 |
// Fill in the background, if necessary. |
|
361 |
boolean subregion = state.isSubregion(); |
|
362 |
if ((subregion && style.isOpaque(state)) || |
|
363 |
(!subregion && c.isOpaque())) { |
|
364 |
g.setColor(style.getColor(state, ColorType.BACKGROUND)); |
|
365 |
g.fillRect(x, y, width, height); |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
static boolean isLeftToRight(Component c) { |
|
370 |
return c.getComponentOrientation().isLeftToRight(); |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Returns the ui that is of type <code>klass</code>, or null if |
|
375 |
* one can not be found. |
|
376 |
*/ |
|
377 |
static Object getUIOfType(ComponentUI ui, Class klass) { |
|
378 |
if (klass.isInstance(ui)) { |
|
379 |
return ui; |
|
380 |
} |
|
381 |
return null; |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* Creates the Synth look and feel <code>ComponentUI</code> for |
|
386 |
* the passed in <code>JComponent</code>. |
|
387 |
* |
|
388 |
* @param c JComponent to create the <code>ComponentUI</code> for |
|
389 |
* @return ComponentUI to use for <code>c</code> |
|
390 |
*/ |
|
391 |
public static ComponentUI createUI(JComponent c) { |
|
392 |
String key = c.getUIClassID().intern(); |
|
393 |
||
394 |
if (key == "ButtonUI") { |
|
395 |
return SynthButtonUI.createUI(c); |
|
396 |
} |
|
397 |
else if (key == "CheckBoxUI") { |
|
398 |
return SynthCheckBoxUI.createUI(c); |
|
399 |
} |
|
400 |
else if (key == "CheckBoxMenuItemUI") { |
|
401 |
return SynthCheckBoxMenuItemUI.createUI(c); |
|
402 |
} |
|
403 |
else if (key == "ColorChooserUI") { |
|
404 |
return SynthColorChooserUI.createUI(c); |
|
405 |
} |
|
406 |
else if (key == "ComboBoxUI") { |
|
407 |
return SynthComboBoxUI.createUI(c); |
|
408 |
} |
|
409 |
else if (key == "DesktopPaneUI") { |
|
410 |
return SynthDesktopPaneUI.createUI(c); |
|
411 |
} |
|
412 |
else if (key == "DesktopIconUI") { |
|
413 |
return SynthDesktopIconUI.createUI(c); |
|
414 |
} |
|
415 |
else if (key == "EditorPaneUI") { |
|
416 |
return SynthEditorPaneUI.createUI(c); |
|
417 |
} |
|
418 |
else if (key == "FileChooserUI") { |
|
419 |
return SynthFileChooserUI.createUI(c); |
|
420 |
} |
|
421 |
else if (key == "FormattedTextFieldUI") { |
|
422 |
return SynthFormattedTextFieldUI.createUI(c); |
|
423 |
} |
|
424 |
else if (key == "InternalFrameUI") { |
|
425 |
return SynthInternalFrameUI.createUI(c); |
|
426 |
} |
|
427 |
else if (key == "LabelUI") { |
|
428 |
return SynthLabelUI.createUI(c); |
|
429 |
} |
|
430 |
else if (key == "ListUI") { |
|
431 |
return SynthListUI.createUI(c); |
|
432 |
} |
|
433 |
else if (key == "MenuBarUI") { |
|
434 |
return SynthMenuBarUI.createUI(c); |
|
435 |
} |
|
436 |
else if (key == "MenuUI") { |
|
437 |
return SynthMenuUI.createUI(c); |
|
438 |
} |
|
439 |
else if (key == "MenuItemUI") { |
|
440 |
return SynthMenuItemUI.createUI(c); |
|
441 |
} |
|
442 |
else if (key == "OptionPaneUI") { |
|
443 |
return SynthOptionPaneUI.createUI(c); |
|
444 |
} |
|
445 |
else if (key == "PanelUI") { |
|
446 |
return SynthPanelUI.createUI(c); |
|
447 |
} |
|
448 |
else if (key == "PasswordFieldUI") { |
|
449 |
return SynthPasswordFieldUI.createUI(c); |
|
450 |
} |
|
451 |
else if (key == "PopupMenuSeparatorUI") { |
|
452 |
return SynthSeparatorUI.createUI(c); |
|
453 |
} |
|
454 |
else if (key == "PopupMenuUI") { |
|
455 |
return SynthPopupMenuUI.createUI(c); |
|
456 |
} |
|
457 |
else if (key == "ProgressBarUI") { |
|
458 |
return SynthProgressBarUI.createUI(c); |
|
459 |
} |
|
460 |
else if (key == "RadioButtonUI") { |
|
461 |
return SynthRadioButtonUI.createUI(c); |
|
462 |
} |
|
463 |
else if (key == "RadioButtonMenuItemUI") { |
|
464 |
return SynthRadioButtonMenuItemUI.createUI(c); |
|
465 |
} |
|
466 |
else if (key == "RootPaneUI") { |
|
467 |
return SynthRootPaneUI.createUI(c); |
|
468 |
} |
|
469 |
else if (key == "ScrollBarUI") { |
|
470 |
return SynthScrollBarUI.createUI(c); |
|
471 |
} |
|
472 |
else if (key == "ScrollPaneUI") { |
|
473 |
return SynthScrollPaneUI.createUI(c); |
|
474 |
} |
|
475 |
else if (key == "SeparatorUI") { |
|
476 |
return SynthSeparatorUI.createUI(c); |
|
477 |
} |
|
478 |
else if (key == "SliderUI") { |
|
479 |
return SynthSliderUI.createUI(c); |
|
480 |
} |
|
481 |
else if (key == "SpinnerUI") { |
|
482 |
return SynthSpinnerUI.createUI(c); |
|
483 |
} |
|
484 |
else if (key == "SplitPaneUI") { |
|
485 |
return SynthSplitPaneUI.createUI(c); |
|
486 |
} |
|
487 |
else if (key == "TabbedPaneUI") { |
|
488 |
return SynthTabbedPaneUI.createUI(c); |
|
489 |
} |
|
490 |
else if (key == "TableUI") { |
|
491 |
return SynthTableUI.createUI(c); |
|
492 |
} |
|
493 |
else if (key == "TableHeaderUI") { |
|
494 |
return SynthTableHeaderUI.createUI(c); |
|
495 |
} |
|
496 |
else if (key == "TextAreaUI") { |
|
497 |
return SynthTextAreaUI.createUI(c); |
|
498 |
} |
|
499 |
else if (key == "TextFieldUI") { |
|
500 |
return SynthTextFieldUI.createUI(c); |
|
501 |
} |
|
502 |
else if (key == "TextPaneUI") { |
|
503 |
return SynthTextPaneUI.createUI(c); |
|
504 |
} |
|
505 |
else if (key == "ToggleButtonUI") { |
|
506 |
return SynthToggleButtonUI.createUI(c); |
|
507 |
} |
|
508 |
else if (key == "ToolBarSeparatorUI") { |
|
509 |
return SynthSeparatorUI.createUI(c); |
|
510 |
} |
|
511 |
else if (key == "ToolBarUI") { |
|
512 |
return SynthToolBarUI.createUI(c); |
|
513 |
} |
|
514 |
else if (key == "ToolTipUI") { |
|
515 |
return SynthToolTipUI.createUI(c); |
|
516 |
} |
|
517 |
else if (key == "TreeUI") { |
|
518 |
return SynthTreeUI.createUI(c); |
|
519 |
} |
|
520 |
else if (key == "ViewportUI") { |
|
521 |
return SynthViewportUI.createUI(c); |
|
522 |
} |
|
523 |
return null; |
|
524 |
} |
|
525 |
||
526 |
||
527 |
/** |
|
528 |
* Creates a SynthLookAndFeel. |
|
529 |
* <p> |
|
530 |
* For the returned <code>SynthLookAndFeel</code> to be useful you need to |
|
531 |
* invoke <code>load</code> to specify the set of |
|
532 |
* <code>SynthStyle</code>s, or invoke <code>setStyleFactory</code>. |
|
533 |
* |
|
534 |
* @see #load |
|
535 |
* @see #setStyleFactory |
|
536 |
*/ |
|
537 |
public SynthLookAndFeel() { |
|
538 |
factory = new DefaultSynthStyleFactory(); |
|
539 |
_handler = new Handler(); |
|
540 |
} |
|
541 |
||
542 |
/** |
|
543 |
* Loads the set of <code>SynthStyle</code>s that will be used by |
|
544 |
* this <code>SynthLookAndFeel</code>. <code>resourceBase</code> is |
|
545 |
* used to resolve any path based resources, for example an |
|
546 |
* <code>Image</code> would be resolved by |
|
547 |
* <code>resourceBase.getResource(path)</code>. Refer to |
|
548 |
* <a href="doc-files/synthFileFormat.html">Synth File Format</a> |
|
549 |
* for more information. |
|
550 |
* |
|
551 |
* @param input InputStream to load from |
|
552 |
* @param resourceBase used to resolve any images or other resources |
|
553 |
* @throws ParseException if there is an error in parsing |
|
554 |
* @throws IllegalArgumentException if input or resourceBase is <code>null</code> |
|
555 |
*/ |
|
556 |
public void load(InputStream input, Class<?> resourceBase) throws |
|
557 |
ParseException { |
|
558 |
if (resourceBase == null) { |
|
559 |
throw new IllegalArgumentException( |
|
560 |
"You must supply a valid resource base Class"); |
|
561 |
} |
|
562 |
||
563 |
if (defaultsMap == null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
564 |
defaultsMap = new HashMap<String, Object>(); |
2 | 565 |
} |
566 |
||
567 |
new SynthParser().parse(input, (DefaultSynthStyleFactory) factory, |
|
568 |
null, resourceBase, defaultsMap); |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* Loads the set of <code>SynthStyle</code>s that will be used by |
|
573 |
* this <code>SynthLookAndFeel</code>. Path based resources are resolved |
|
574 |
* relatively to the specified <code>URL</code> of the style. For example |
|
575 |
* an <code>Image</code> would be resolved by |
|
576 |
* <code>new URL(synthFile, path)</code>. Refer to |
|
577 |
* <a href="doc-files/synthFileFormat.html">Synth File Format</a> for more |
|
578 |
* information. |
|
579 |
* |
|
580 |
* @param url the <code>URL</code> to load the set of |
|
581 |
* <code>SynthStyle</code> from |
|
582 |
* @throws ParseException if there is an error in parsing |
|
583 |
* @throws IllegalArgumentException if synthSet is <code>null</code> |
|
584 |
* @throws IOException if synthSet cannot be opened as an <code>InputStream</code> |
|
585 |
* @since 1.6 |
|
586 |
*/ |
|
587 |
public void load(URL url) throws ParseException, IOException { |
|
588 |
if (url == null) { |
|
589 |
throw new IllegalArgumentException( |
|
590 |
"You must supply a valid Synth set URL"); |
|
591 |
} |
|
592 |
||
593 |
if (defaultsMap == null) { |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
594 |
defaultsMap = new HashMap<String, Object>(); |
2 | 595 |
} |
596 |
||
597 |
InputStream input = url.openStream(); |
|
598 |
new SynthParser().parse(input, (DefaultSynthStyleFactory) factory, |
|
599 |
url, null, defaultsMap); |
|
600 |
} |
|
601 |
||
602 |
/** |
|
603 |
* Called by UIManager when this look and feel is installed. |
|
604 |
*/ |
|
2658 | 605 |
@Override |
2 | 606 |
public void initialize() { |
607 |
super.initialize(); |
|
608 |
DefaultLookup.setDefaultLookup(new SynthDefaultLookup()); |
|
609 |
setStyleFactory(factory); |
|
610 |
KeyboardFocusManager.getCurrentKeyboardFocusManager(). |
|
611 |
addPropertyChangeListener(_handler); |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Called by UIManager when this look and feel is uninstalled. |
|
616 |
*/ |
|
2658 | 617 |
@Override |
2 | 618 |
public void uninitialize() { |
619 |
KeyboardFocusManager.getCurrentKeyboardFocusManager(). |
|
620 |
removePropertyChangeListener(_handler); |
|
621 |
// We should uninstall the StyleFactory here, but unfortunately |
|
622 |
// there are a handful of things that retain references to the |
|
623 |
// LookAndFeel and expect things to work |
|
624 |
super.uninitialize(); |
|
625 |
} |
|
626 |
||
627 |
/** |
|
628 |
* Returns the defaults for this SynthLookAndFeel. |
|
629 |
* |
|
630 |
* @return Defaults table. |
|
631 |
*/ |
|
2658 | 632 |
@Override |
2 | 633 |
public UIDefaults getDefaults() { |
634 |
UIDefaults table = new UIDefaults(60, 0.75f); |
|
635 |
||
636 |
Region.registerUIs(table); |
|
637 |
table.setDefaultLocale(Locale.getDefault()); |
|
638 |
table.addResourceBundle( |
|
639 |
"com.sun.swing.internal.plaf.basic.resources.basic" ); |
|
640 |
table.addResourceBundle("com.sun.swing.internal.plaf.synth.resources.synth"); |
|
641 |
||
642 |
// SynthTabbedPaneUI supports rollover on tabs, GTK does not |
|
643 |
table.put("TabbedPane.isTabRollover", Boolean.TRUE); |
|
644 |
||
645 |
// These need to be defined for JColorChooser to work. |
|
646 |
table.put("ColorChooser.swatchesRecentSwatchSize", |
|
647 |
new Dimension(10, 10)); |
|
648 |
table.put("ColorChooser.swatchesDefaultRecentColor", Color.RED); |
|
649 |
table.put("ColorChooser.swatchesSwatchSize", new Dimension(10, 10)); |
|
650 |
||
5133
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
651 |
// These need to be defined for ImageView. |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
652 |
table.put("html.pendingImage", SwingUtilities2.makeIcon(getClass(), |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
653 |
BasicLookAndFeel.class, |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
654 |
"icons/image-delayed.png")); |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
655 |
table.put("html.missingImage", SwingUtilities2.makeIcon(getClass(), |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
656 |
BasicLookAndFeel.class, |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
657 |
"icons/image-failed.png")); |
0a1777c5b601
6933784: NIMBUS: ImageView getNoImageIcon and getLoadingImageIcon returns nulls instead of an icon
rupashka
parents:
4394
diff
changeset
|
658 |
|
2 | 659 |
// These are needed for PopupMenu. |
660 |
table.put("PopupMenu.selectedWindowInputMapBindings", new Object[] { |
|
661 |
"ESCAPE", "cancel", |
|
662 |
"DOWN", "selectNext", |
|
663 |
"KP_DOWN", "selectNext", |
|
664 |
"UP", "selectPrevious", |
|
665 |
"KP_UP", "selectPrevious", |
|
666 |
"LEFT", "selectParent", |
|
667 |
"KP_LEFT", "selectParent", |
|
668 |
"RIGHT", "selectChild", |
|
669 |
"KP_RIGHT", "selectChild", |
|
670 |
"ENTER", "return", |
|
671 |
"SPACE", "return" |
|
672 |
}); |
|
673 |
table.put("PopupMenu.selectedWindowInputMapBindings.RightToLeft", |
|
674 |
new Object[] { |
|
675 |
"LEFT", "selectChild", |
|
676 |
"KP_LEFT", "selectChild", |
|
677 |
"RIGHT", "selectParent", |
|
678 |
"KP_RIGHT", "selectParent", |
|
679 |
}); |
|
680 |
||
681 |
// enabled antialiasing depending on desktop settings |
|
682 |
flushUnreferenced(); |
|
683 |
Object aaTextInfo = getAATextInfo(); |
|
684 |
table.put(SwingUtilities2.AA_TEXT_PROPERTY_KEY, aaTextInfo); |
|
685 |
new AATextListener(this); |
|
686 |
||
687 |
if (defaultsMap != null) { |
|
688 |
table.putAll(defaultsMap); |
|
689 |
} |
|
690 |
return table; |
|
691 |
} |
|
692 |
||
693 |
/** |
|
694 |
* Returns true, SynthLookAndFeel is always supported. |
|
695 |
* |
|
696 |
* @return true. |
|
697 |
*/ |
|
2658 | 698 |
@Override |
2 | 699 |
public boolean isSupportedLookAndFeel() { |
700 |
return true; |
|
701 |
} |
|
702 |
||
703 |
/** |
|
704 |
* Returns false, SynthLookAndFeel is not a native look and feel. |
|
705 |
* |
|
706 |
* @return false |
|
707 |
*/ |
|
2658 | 708 |
@Override |
2 | 709 |
public boolean isNativeLookAndFeel() { |
710 |
return false; |
|
711 |
} |
|
712 |
||
713 |
/** |
|
714 |
* Returns a textual description of SynthLookAndFeel. |
|
715 |
* |
|
716 |
* @return textual description of synth. |
|
717 |
*/ |
|
2658 | 718 |
@Override |
2 | 719 |
public String getDescription() { |
720 |
return "Synth look and feel"; |
|
721 |
} |
|
722 |
||
723 |
/** |
|
724 |
* Return a short string that identifies this look and feel. |
|
725 |
* |
|
726 |
* @return a short string identifying this look and feel. |
|
727 |
*/ |
|
2658 | 728 |
@Override |
2 | 729 |
public String getName() { |
730 |
return "Synth look and feel"; |
|
731 |
} |
|
732 |
||
733 |
/** |
|
734 |
* Return a string that identifies this look and feel. |
|
735 |
* |
|
736 |
* @return a short string identifying this look and feel. |
|
737 |
*/ |
|
2658 | 738 |
@Override |
2 | 739 |
public String getID() { |
740 |
return "Synth"; |
|
741 |
} |
|
742 |
||
743 |
/** |
|
744 |
* Returns whether or not the UIs should update their |
|
745 |
* <code>SynthStyles</code> from the <code>SynthStyleFactory</code> |
|
746 |
* when the ancestor of the <code>JComponent</code> changes. A subclass |
|
747 |
* that provided a <code>SynthStyleFactory</code> that based the |
|
748 |
* return value from <code>getStyle</code> off the containment hierarchy |
|
749 |
* would override this method to return true. |
|
750 |
* |
|
751 |
* @return whether or not the UIs should update their |
|
752 |
* <code>SynthStyles</code> from the <code>SynthStyleFactory</code> |
|
753 |
* when the ancestor changed. |
|
754 |
*/ |
|
755 |
public boolean shouldUpdateStyleOnAncestorChanged() { |
|
756 |
return false; |
|
757 |
} |
|
758 |
||
759 |
/** |
|
4394 | 760 |
* Returns whether or not the UIs should update their styles when a |
761 |
* particular event occurs. |
|
762 |
* |
|
763 |
* @param ev a {@code PropertyChangeEvent} |
|
764 |
* @return whether or not the UIs should update their styles |
|
765 |
* @since 1.7 |
|
766 |
*/ |
|
767 |
protected boolean shouldUpdateStyleOnEvent(PropertyChangeEvent ev) { |
|
768 |
String eName = ev.getPropertyName(); |
|
769 |
if ("name" == eName || "componentOrientation" == eName) { |
|
770 |
return true; |
|
771 |
} |
|
772 |
if ("ancestor" == eName && ev.getNewValue() != null) { |
|
773 |
// Only update on an ancestor change when getting a valid |
|
774 |
// parent and the LookAndFeel wants this. |
|
775 |
return shouldUpdateStyleOnAncestorChanged(); |
|
776 |
} |
|
777 |
return false; |
|
778 |
} |
|
779 |
||
780 |
/** |
|
2 | 781 |
* Returns the antialiasing information as specified by the host desktop. |
782 |
* Antialiasing might be forced off if the desktop is GNOME and the user |
|
783 |
* has set his locale to Chinese, Japanese or Korean. This is consistent |
|
784 |
* with what GTK does. See com.sun.java.swing.plaf.gtk.GtkLookAndFeel |
|
785 |
* for more information about CJK and antialiased fonts. |
|
786 |
* |
|
787 |
* @return the text antialiasing information associated to the desktop |
|
788 |
*/ |
|
789 |
private static Object getAATextInfo() { |
|
790 |
String language = Locale.getDefault().getLanguage(); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
791 |
String desktop = |
2 | 792 |
AccessController.doPrivileged(new GetPropertyAction("sun.desktop")); |
793 |
||
794 |
boolean isCjkLocale = (Locale.CHINESE.getLanguage().equals(language) || |
|
795 |
Locale.JAPANESE.getLanguage().equals(language) || |
|
796 |
Locale.KOREAN.getLanguage().equals(language)); |
|
797 |
boolean isGnome = "gnome".equals(desktop); |
|
798 |
boolean isLocal = SwingUtilities2.isLocalDisplay(); |
|
799 |
||
800 |
boolean setAA = isLocal && (!isGnome || !isCjkLocale); |
|
801 |
||
802 |
Object aaTextInfo = SwingUtilities2.AATextInfo.getAATextInfo(setAA); |
|
803 |
return aaTextInfo; |
|
804 |
} |
|
805 |
||
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
806 |
private static ReferenceQueue<LookAndFeel> queue = new ReferenceQueue<LookAndFeel>(); |
2 | 807 |
|
808 |
private static void flushUnreferenced() { |
|
809 |
AATextListener aatl; |
|
810 |
while ((aatl = (AATextListener) queue.poll()) != null) { |
|
811 |
aatl.dispose(); |
|
812 |
} |
|
813 |
} |
|
814 |
||
815 |
private static class AATextListener |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
816 |
extends WeakReference<LookAndFeel> implements PropertyChangeListener { |
2 | 817 |
private String key = SunToolkit.DESKTOPFONTHINTS; |
818 |
||
819 |
AATextListener(LookAndFeel laf) { |
|
820 |
super(laf, queue); |
|
821 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
|
822 |
tk.addPropertyChangeListener(key, this); |
|
823 |
} |
|
824 |
||
2658 | 825 |
@Override |
2 | 826 |
public void propertyChange(PropertyChangeEvent pce) { |
827 |
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
|
828 |
if (defaults.getBoolean("Synth.doNotSetTextAA")) { |
|
829 |
dispose(); |
|
830 |
return; |
|
831 |
} |
|
832 |
||
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
833 |
LookAndFeel laf = get(); |
2 | 834 |
if (laf == null || laf != UIManager.getLookAndFeel()) { |
835 |
dispose(); |
|
836 |
return; |
|
837 |
} |
|
838 |
||
839 |
Object aaTextInfo = getAATextInfo(); |
|
840 |
defaults.put(SwingUtilities2.AA_TEXT_PROPERTY_KEY, aaTextInfo); |
|
841 |
||
842 |
updateUI(); |
|
843 |
} |
|
844 |
||
845 |
void dispose() { |
|
846 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
|
847 |
tk.removePropertyChangeListener(key, this); |
|
848 |
} |
|
849 |
||
850 |
/** |
|
851 |
* Updates the UI of the passed in window and all its children. |
|
852 |
*/ |
|
853 |
private static void updateWindowUI(Window window) { |
|
854 |
updateStyles(window); |
|
855 |
Window ownedWins[] = window.getOwnedWindows(); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
856 |
for (Window w : ownedWins) { |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
857 |
updateWindowUI(w); |
2 | 858 |
} |
859 |
} |
|
860 |
||
861 |
/** |
|
862 |
* Updates the UIs of all the known Frames. |
|
863 |
*/ |
|
864 |
private static void updateAllUIs() { |
|
865 |
Frame appFrames[] = Frame.getFrames(); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
866 |
for (Frame frame : appFrames) { |
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
867 |
updateWindowUI(frame); |
2 | 868 |
} |
869 |
} |
|
870 |
||
871 |
/** |
|
872 |
* Indicates if an updateUI call is pending. |
|
873 |
*/ |
|
874 |
private static boolean updatePending; |
|
875 |
||
876 |
/** |
|
877 |
* Sets whether or not an updateUI call is pending. |
|
878 |
*/ |
|
879 |
private static synchronized void setUpdatePending(boolean update) { |
|
880 |
updatePending = update; |
|
881 |
} |
|
882 |
||
883 |
/** |
|
884 |
* Returns true if a UI update is pending. |
|
885 |
*/ |
|
886 |
private static synchronized boolean isUpdatePending() { |
|
887 |
return updatePending; |
|
888 |
} |
|
889 |
||
890 |
protected void updateUI() { |
|
891 |
if (!isUpdatePending()) { |
|
892 |
setUpdatePending(true); |
|
893 |
Runnable uiUpdater = new Runnable() { |
|
2658 | 894 |
@Override |
2 | 895 |
public void run() { |
896 |
updateAllUIs(); |
|
897 |
setUpdatePending(false); |
|
898 |
} |
|
899 |
}; |
|
900 |
SwingUtilities.invokeLater(uiUpdater); |
|
901 |
} |
|
902 |
} |
|
903 |
} |
|
904 |
||
905 |
private void writeObject(java.io.ObjectOutputStream out) |
|
906 |
throws IOException { |
|
907 |
throw new NotSerializableException(this.getClass().getName()); |
|
908 |
} |
|
909 |
||
910 |
private class Handler implements PropertyChangeListener { |
|
2658 | 911 |
@Override |
2 | 912 |
public void propertyChange(PropertyChangeEvent evt) { |
913 |
String propertyName = evt.getPropertyName(); |
|
914 |
Object newValue = evt.getNewValue(); |
|
915 |
Object oldValue = evt.getOldValue(); |
|
916 |
||
917 |
if ("focusOwner" == propertyName) { |
|
918 |
if (oldValue instanceof JComponent) { |
|
919 |
repaintIfBackgroundsDiffer((JComponent)oldValue); |
|
920 |
||
921 |
} |
|
922 |
||
923 |
if (newValue instanceof JComponent) { |
|
924 |
repaintIfBackgroundsDiffer((JComponent)newValue); |
|
925 |
} |
|
926 |
} |
|
927 |
else if ("managingFocus" == propertyName) { |
|
928 |
// De-register listener on old keyboard focus manager and |
|
929 |
// register it on the new one. |
|
930 |
KeyboardFocusManager manager = |
|
931 |
(KeyboardFocusManager)evt.getSource(); |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
932 |
if (newValue.equals(Boolean.FALSE)) { |
2 | 933 |
manager.removePropertyChangeListener(_handler); |
934 |
} |
|
935 |
else { |
|
936 |
manager.addPropertyChangeListener(_handler); |
|
937 |
} |
|
938 |
} |
|
939 |
} |
|
940 |
||
941 |
/** |
|
942 |
* This is a support method that will check if the background colors of |
|
943 |
* the specified component differ between focused and unfocused states. |
|
944 |
* If the color differ the component will then repaint itself. |
|
945 |
* |
|
946 |
* @comp the component to check |
|
947 |
*/ |
|
948 |
private void repaintIfBackgroundsDiffer(JComponent comp) { |
|
949 |
ComponentUI ui = (ComponentUI)comp.getClientProperty( |
|
950 |
SwingUtilities2.COMPONENT_UI_PROPERTY_KEY); |
|
951 |
if (ui instanceof SynthUI) { |
|
952 |
SynthUI synthUI = (SynthUI)ui; |
|
953 |
SynthContext context = synthUI.getContext(comp); |
|
954 |
SynthStyle style = context.getStyle(); |
|
955 |
int state = context.getComponentState(); |
|
956 |
||
957 |
// Get the current background color. |
|
958 |
Color currBG = style.getColor(context, ColorType.BACKGROUND); |
|
959 |
||
960 |
// Get the last background color. |
|
961 |
state ^= SynthConstants.FOCUSED; |
|
962 |
context.setComponentState(state); |
|
963 |
Color lastBG = style.getColor(context, ColorType.BACKGROUND); |
|
964 |
||
965 |
// Reset the component state back to original. |
|
966 |
state ^= SynthConstants.FOCUSED; |
|
967 |
context.setComponentState(state); |
|
968 |
||
969 |
// Repaint the component if the backgrounds differed. |
|
970 |
if (currBG != null && !currBG.equals(lastBG)) { |
|
971 |
comp.repaint(); |
|
972 |
} |
|
973 |
context.dispose(); |
|
974 |
} |
|
975 |
} |
|
976 |
} |
|
977 |
} |