2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
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;
|
|
27 |
|
|
28 |
import java.awt.Font;
|
|
29 |
import java.awt.event.InputEvent;
|
|
30 |
import java.awt.event.KeyEvent;
|
|
31 |
import java.awt.Color;
|
|
32 |
import java.awt.Component;
|
|
33 |
import java.awt.SystemColor;
|
|
34 |
import java.awt.Toolkit;
|
|
35 |
|
|
36 |
import javax.swing.text.*;
|
|
37 |
import javax.swing.border.*;
|
|
38 |
import javax.swing.plaf.*;
|
|
39 |
|
|
40 |
import java.net.URL;
|
|
41 |
import sun.swing.SwingUtilities2;
|
|
42 |
import sun.swing.DefaultLayoutStyle;
|
|
43 |
import sun.swing.ImageIconUIResource;
|
|
44 |
|
|
45 |
import java.util.StringTokenizer;
|
|
46 |
|
|
47 |
|
|
48 |
/**
|
|
49 |
* {@code LookAndFeel}, as the name implies, encapsulates a look and
|
|
50 |
* feel. Beyond installing a look and feel most developers never need to
|
|
51 |
* interact directly with {@code LookAndFeel}. In general only developers
|
|
52 |
* creating a custom look and feel need to concern themselves with this class.
|
|
53 |
* <p>
|
|
54 |
* Swing is built upon the foundation that each {@code JComponent}
|
|
55 |
* subclass has an implementation of a specific {@code ComponentUI}
|
|
56 |
* subclass. The {@code ComponentUI} is often referred to as "the ui",
|
|
57 |
* "component ui", or "look and feel delegate". The {@code ComponentUI}
|
|
58 |
* subclass is responsible for providing the look and feel specific
|
|
59 |
* functionality of the component. For example, {@code JTree} requires
|
|
60 |
* an implementation of the {@code ComponentUI} subclass {@code
|
|
61 |
* TreeUI}. The implementation of the specific {@code
|
|
62 |
* ComponentUI} subclass is provided by the {@code LookAndFeel}. Each
|
|
63 |
* {@code JComponent} subclass identifies the {@code ComponentUI}
|
|
64 |
* subclass it requires by way of the {@code JComponent} method {@code
|
|
65 |
* getUIClassID}.
|
|
66 |
* <p>
|
|
67 |
* Each {@code LookAndFeel} implementation must provide
|
|
68 |
* an implementation of the appropriate {@code ComponentUI} subclass by
|
|
69 |
* specifying a value for each of Swing's ui class ids in the {@code
|
|
70 |
* UIDefaults} object returned from {@code getDefaults}. For example,
|
|
71 |
* {@code BasicLookAndFeel} uses {@code BasicTreeUI} as the concrete
|
|
72 |
* implementation for {@code TreeUI}. This is accomplished by {@code
|
|
73 |
* BasicLookAndFeel} providing the key-value pair {@code
|
|
74 |
* "TreeUI"-"javax.swing.plaf.basic.BasicTreeUI"}, in the
|
|
75 |
* {@code UIDefaults} returned from {@code getDefaults}. Refer to
|
|
76 |
* {@link UIDefaults#getUI(JComponent)} for defails on how the implementation
|
|
77 |
* of the {@code ComponentUI} subclass is obtained.
|
|
78 |
* <p>
|
|
79 |
* When a {@code LookAndFeel} is installed the {@code UIManager} does
|
|
80 |
* not check that an entry exists for all ui class ids. As such,
|
|
81 |
* random exceptions will occur if the current look and feel has not
|
|
82 |
* provided a value for a particular ui class id and an instance of
|
|
83 |
* the {@code JComponent} subclass is created.
|
|
84 |
*
|
|
85 |
* <h2>Recommendations for Look and Feels</h2>
|
|
86 |
*
|
|
87 |
* As noted in {@code UIManager} each {@code LookAndFeel} has the opportunity
|
|
88 |
* to provide a set of defaults that are layered in with developer and
|
|
89 |
* system defaults. Some of Swing's components require the look and feel
|
|
90 |
* to provide a specific set of defaults. These are documented in the
|
|
91 |
* classes that require the specific default.
|
|
92 |
*
|
|
93 |
* <h3><a name="#defaultRecommendation">ComponentUIs and defaults</a></h2>
|
|
94 |
*
|
|
95 |
* All {@code ComponentUIs} typically need to set various properties
|
|
96 |
* on the {@code JComponent} the {@code ComponentUI} is providing the
|
|
97 |
* look and feel for. This is typically done when the {@code
|
|
98 |
* ComponentUI} is installed on the {@code JComponent}. Setting a
|
|
99 |
* property should only be done if the developer has not set the
|
|
100 |
* property. For non-primitive values it is recommended that the
|
|
101 |
* {@code ComponentUI} only change the property on the {@code
|
|
102 |
* JComponent} if the current value is {@code null} or implements
|
|
103 |
* {@code UIResource}. If the current value is {@code null} or
|
|
104 |
* implements {@code UIResource} it indicates the property has not
|
|
105 |
* been set by the developer, and the ui is free to change it. For
|
|
106 |
* example, {@code BasicButtonUI.installDefaults} only changes the
|
|
107 |
* font on the {@code JButton} if the return value from {@code
|
|
108 |
* button.getFont()} is {@code null} or implements {@code
|
|
109 |
* UIResource}. On the other hand if {@code button.getFont()} returned
|
|
110 |
* a {@code non-null} value that did not implement {@code UIResource}
|
|
111 |
* then {@code BasicButtonUI.installDefaults} would not change the
|
|
112 |
* {@code JButton}'s font.
|
|
113 |
* <p>
|
|
114 |
* For primitive values, such as {@code opaque}, the method {@code
|
|
115 |
* installProperty} should be invoked. {@code installProperty} only changes
|
|
116 |
* the correspoding property if the value has not been changed by the
|
|
117 |
* developer.
|
|
118 |
* <p>
|
|
119 |
* {@code ComponentUI} implementations should use the various install methods
|
|
120 |
* provided by this class as they handle the necessary checking and install
|
|
121 |
* the property using the recommended guidelines.
|
|
122 |
* <p>
|
|
123 |
* <h3><a name="exceptions"></a>Exceptions</h3>
|
|
124 |
*
|
|
125 |
* All of the install methods provided by {@code LookAndFeel} need to
|
|
126 |
* access the defaults if the value of the property being changed is
|
|
127 |
* {@code null} or a {@code UIResource}. For example, installing the
|
|
128 |
* font does the following:
|
|
129 |
* <pre>
|
|
130 |
* JComponent c;
|
|
131 |
* Font font = c.getFont();
|
|
132 |
* if (font == null || (font instanceof UIResource)) {
|
|
133 |
* c.setFont(UIManager.getFont("fontKey"));
|
|
134 |
* }
|
|
135 |
* </pre>
|
|
136 |
* If the font is {@code null} or a {@code UIResource}, the
|
|
137 |
* defaults table is queried with the key {@code fontKey}. All of
|
|
138 |
* {@code UIDefault's} get methods throw a {@code
|
|
139 |
* NullPointerException} if passed in {@code null}. As such, unless
|
|
140 |
* otherwise noted each of the various install methods of {@code
|
|
141 |
* LookAndFeel} throw a {@code NullPointerException} if the current
|
|
142 |
* value is {@code null} or a {@code UIResource} and the supplied
|
|
143 |
* defaults key is {@code null}. In addition, unless otherwise specified
|
|
144 |
* all of the {@code install} methods throw a {@code NullPointerException} if
|
|
145 |
* a {@code null} component is passed in.
|
|
146 |
*
|
|
147 |
* @author Tom Ball
|
|
148 |
* @author Hans Muller
|
|
149 |
*/
|
|
150 |
public abstract class LookAndFeel
|
|
151 |
{
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Convenience method for setting a component's foreground
|
|
155 |
* and background color properties with values from the
|
|
156 |
* defaults. The properties are only set if the current
|
|
157 |
* value is either {@code null} or a {@code UIResource}.
|
|
158 |
*
|
|
159 |
* @param c component to set the colors on
|
|
160 |
* @param defaultBgName key for the background
|
|
161 |
* @param defaultFgName key for the foreground
|
|
162 |
*
|
|
163 |
* @see #installColorsAndFont
|
|
164 |
* @see UIManager#getColor
|
|
165 |
* @throws NullPointerException as described in
|
|
166 |
* <a href="#exceptions">exceptions</a>
|
|
167 |
*/
|
|
168 |
public static void installColors(JComponent c,
|
|
169 |
String defaultBgName,
|
|
170 |
String defaultFgName)
|
|
171 |
{
|
|
172 |
Color bg = c.getBackground();
|
|
173 |
if (bg == null || bg instanceof UIResource) {
|
|
174 |
c.setBackground(UIManager.getColor(defaultBgName));
|
|
175 |
}
|
|
176 |
|
|
177 |
Color fg = c.getForeground();
|
|
178 |
if (fg == null || fg instanceof UIResource) {
|
|
179 |
c.setForeground(UIManager.getColor(defaultFgName));
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
|
|
184 |
/**
|
|
185 |
* Convenience method for setting a component's foreground,
|
|
186 |
* background and font properties with values from the
|
|
187 |
* defaults. The properties are only set if the current
|
|
188 |
* value is either {@code null} or a {@code UIResource}.
|
|
189 |
*
|
|
190 |
* @param c component set to the colors and font on
|
|
191 |
* @param defaultBgName key for the background
|
|
192 |
* @param defaultFgName key for the foreground
|
|
193 |
* @param defaultFontName key for the font
|
|
194 |
* @throws NullPointerException as described in
|
|
195 |
* <a href="#exceptions">exceptions</a>
|
|
196 |
*
|
|
197 |
* @see #installColors
|
|
198 |
* @see UIManager#getColor
|
|
199 |
* @see UIManager#getFont
|
|
200 |
*/
|
|
201 |
public static void installColorsAndFont(JComponent c,
|
|
202 |
String defaultBgName,
|
|
203 |
String defaultFgName,
|
|
204 |
String defaultFontName) {
|
|
205 |
Font f = c.getFont();
|
|
206 |
if (f == null || f instanceof UIResource) {
|
|
207 |
c.setFont(UIManager.getFont(defaultFontName));
|
|
208 |
}
|
|
209 |
|
|
210 |
installColors(c, defaultBgName, defaultFgName);
|
|
211 |
}
|
|
212 |
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Convenience method for setting a component's border property with
|
|
216 |
* a value from the defaults. The border is only set if the border is
|
|
217 |
* {@code null} or an instance of {@code UIResource}.
|
|
218 |
*
|
|
219 |
* @param c component to set the border on
|
|
220 |
* @param defaultBorderName key specifying the border
|
|
221 |
* @throws NullPointerException as described in
|
|
222 |
* <a href="#exceptions">exceptions</a>
|
|
223 |
*/
|
|
224 |
public static void installBorder(JComponent c, String defaultBorderName) {
|
|
225 |
Border b = c.getBorder();
|
|
226 |
if (b == null || b instanceof UIResource) {
|
|
227 |
c.setBorder(UIManager.getBorder(defaultBorderName));
|
|
228 |
}
|
|
229 |
}
|
|
230 |
|
|
231 |
|
|
232 |
/**
|
|
233 |
* Convenience method for uninstalling a border. If the border of
|
|
234 |
* the component is a {@code UIResource}, it is set to {@code
|
|
235 |
* null}.
|
|
236 |
*
|
|
237 |
* @param c component to uninstall the border on
|
|
238 |
* @throws NullPointerException if {@code c} is {@code null}
|
|
239 |
*/
|
|
240 |
public static void uninstallBorder(JComponent c) {
|
|
241 |
if (c.getBorder() instanceof UIResource) {
|
|
242 |
c.setBorder(null);
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
/**
|
|
247 |
* Convenience method for installing a property with the specified name
|
|
248 |
* and value on a component if that property has not already been set
|
|
249 |
* by the developer. This method is intended to be used by
|
|
250 |
* ui delegate instances that need to specify a default value for a
|
|
251 |
* property of primitive type (boolean, int, ..), but do not wish
|
|
252 |
* to override a value set by the client. Since primitive property
|
|
253 |
* values cannot be wrapped with the {@code UIResource} marker, this method
|
|
254 |
* uses private state to determine whether the property has been set
|
|
255 |
* by the client.
|
|
256 |
*
|
|
257 |
* @throws IllegalArgumentException if the specified property is not
|
|
258 |
* one which can be set using this method
|
|
259 |
* @throws ClassCastException if the property value has not been set
|
|
260 |
* by the developer and the type does not match the property's type
|
|
261 |
* @throws NullPointerException if {@code c} is {@code null}, or the
|
|
262 |
* named property has not been set by the developer and
|
|
263 |
* {@code propertyValue} is {@code null}
|
|
264 |
* @param c target component to set the property on
|
|
265 |
* @param propertyName name of the property to set
|
|
266 |
* @param propertyValue value of the property
|
|
267 |
* @since 1.5
|
|
268 |
*/
|
|
269 |
public static void installProperty(JComponent c,
|
|
270 |
String propertyName, Object propertyValue) {
|
|
271 |
// this is a special case because the JPasswordField's ancestor heirarchy
|
|
272 |
// includes a class outside of javax.swing, thus we cannot call setUIProperty
|
|
273 |
// directly.
|
|
274 |
if (c instanceof JPasswordField) {
|
|
275 |
if (!((JPasswordField)c).customSetUIProperty(propertyName, propertyValue)) {
|
|
276 |
c.setUIProperty(propertyName, propertyValue);
|
|
277 |
}
|
|
278 |
} else {
|
|
279 |
c.setUIProperty(propertyName, propertyValue);
|
|
280 |
}
|
|
281 |
}
|
|
282 |
|
|
283 |
/**
|
|
284 |
* Convenience method for building an array of {@code
|
|
285 |
* KeyBindings}. While this method is not deprecated, developers
|
|
286 |
* should instead use {@code ActionMap} and {@code InputMap} for
|
|
287 |
* supplying key bindings.
|
|
288 |
* <p>
|
|
289 |
* This method returns an array of {@code KeyBindings}, one for each
|
|
290 |
* alternating {@code key-action} pair in {@code keyBindingList}.
|
|
291 |
* A {@code key} can either be a {@code String} in the format
|
|
292 |
* specified by the <code>KeyStroke.getKeyStroke</code> method, or
|
|
293 |
* a {@code KeyStroke}. The {@code action} part of the pair is a
|
|
294 |
* {@code String} that corresponds to the name of the {@code
|
|
295 |
* Action}.
|
|
296 |
* <p>
|
|
297 |
* The following example illustrates creating a {@code KeyBinding} array
|
|
298 |
* from six alternating {@code key-action} pairs:
|
|
299 |
* <pre>
|
|
300 |
* JTextComponent.KeyBinding[] multilineBindings = makeKeyBindings( new Object[] {
|
|
301 |
* "UP", DefaultEditorKit.upAction,
|
|
302 |
* "DOWN", DefaultEditorKit.downAction,
|
|
303 |
* "PAGE_UP", DefaultEditorKit.pageUpAction,
|
|
304 |
* "PAGE_DOWN", DefaultEditorKit.pageDownAction,
|
|
305 |
* "ENTER", DefaultEditorKit.insertBreakAction,
|
|
306 |
* "TAB", DefaultEditorKit.insertTabAction
|
|
307 |
* });
|
|
308 |
* </pre>
|
|
309 |
* If {@code keyBindingList's} length is odd, the last element is
|
|
310 |
* ignored.
|
|
311 |
* <p>
|
|
312 |
* Supplying a {@code null} value for either the {@code key} or
|
|
313 |
* {@code action} part of the {@code key-action} pair results in
|
|
314 |
* creating a {@code KeyBinding} with the corresponding value
|
|
315 |
* {@code null}. As other parts of Swing's expect {@code non-null} values
|
|
316 |
* in a {@code KeyBinding}, you should avoid supplying {@code null} as
|
|
317 |
* either the {@code key} or {@code action} part of the {@code key-action}
|
|
318 |
* pair.
|
|
319 |
*
|
|
320 |
* @param keyBindingList an array of {@code key-action} pairs
|
|
321 |
* @return an array of {@code KeyBindings}
|
|
322 |
* @throws NullPointerException if {@code keyBindingList} is {@code null}
|
|
323 |
* @throws ClassCastException if the {@code key} part of the pair is
|
|
324 |
* not a {@code KeyStroke} or {@code String}, or the
|
|
325 |
* {@code action} part of the pair is not a {@code String}
|
|
326 |
* @see ActionMap
|
|
327 |
* @see InputMap
|
|
328 |
* @see KeyStroke#getKeyStroke
|
|
329 |
*/
|
|
330 |
public static JTextComponent.KeyBinding[] makeKeyBindings(Object[] keyBindingList)
|
|
331 |
{
|
|
332 |
JTextComponent.KeyBinding[] rv = new JTextComponent.KeyBinding[keyBindingList.length / 2];
|
|
333 |
|
|
334 |
for(int i = 0; i < keyBindingList.length; i += 2) {
|
|
335 |
KeyStroke keystroke = (keyBindingList[i] instanceof KeyStroke)
|
|
336 |
? (KeyStroke)keyBindingList[i]
|
|
337 |
: KeyStroke.getKeyStroke((String)keyBindingList[i]);
|
|
338 |
String action = (String)keyBindingList[i+1];
|
|
339 |
rv[i / 2] = new JTextComponent.KeyBinding(keystroke, action);
|
|
340 |
}
|
|
341 |
|
|
342 |
return rv;
|
|
343 |
}
|
|
344 |
|
|
345 |
/**
|
|
346 |
* Creates a {@code InputMapUIResource} from <code>keys</code>. This is
|
|
347 |
* a convenience method for creating a new {@code InputMapUIResource},
|
|
348 |
* invoking {@code loadKeyBindings(map, keys)}, and returning the
|
|
349 |
* {@code InputMapUIResource}.
|
|
350 |
*
|
|
351 |
* @param keys alternating pairs of {@code keystroke-action key}
|
|
352 |
* pairs as described in {@link #loadKeyBindings}
|
|
353 |
* @return newly created and populated {@code InputMapUIResource}
|
|
354 |
* @see #loadKeyBindings
|
|
355 |
*
|
|
356 |
* @since 1.3
|
|
357 |
*/
|
|
358 |
public static InputMap makeInputMap(Object[] keys) {
|
|
359 |
InputMap retMap = new InputMapUIResource();
|
|
360 |
loadKeyBindings(retMap, keys);
|
|
361 |
return retMap;
|
|
362 |
}
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Creates a {@code ComponentInputMapUIResource} from
|
|
366 |
* <code>keys</code>. This is a convenience method for creating a
|
|
367 |
* new {@code ComponentInputMapUIResource}, invoking {@code
|
|
368 |
* loadKeyBindings(map, keys)}, and returning the {@code
|
|
369 |
* ComponentInputMapUIResource}.
|
|
370 |
*
|
|
371 |
* @param c component to create the {@code ComponentInputMapUIResource}
|
|
372 |
* with
|
|
373 |
* @param keys alternating pairs of {@code keystroke-action key}
|
|
374 |
* pairs as described in {@link #loadKeyBindings}
|
|
375 |
* @return newly created and populated {@code InputMapUIResource}
|
|
376 |
* @throws IllegalArgumentException if {@code c} is {@code null}
|
|
377 |
*
|
|
378 |
* @see #loadKeyBindings
|
|
379 |
* @see ComponentInputMapUIResource
|
|
380 |
*
|
|
381 |
* @since 1.3
|
|
382 |
*/
|
|
383 |
public static ComponentInputMap makeComponentInputMap(JComponent c,
|
|
384 |
Object[] keys) {
|
|
385 |
ComponentInputMap retMap = new ComponentInputMapUIResource(c);
|
|
386 |
loadKeyBindings(retMap, keys);
|
|
387 |
return retMap;
|
|
388 |
}
|
|
389 |
|
|
390 |
|
|
391 |
/**
|
|
392 |
* Populates an {@code InputMap} with the specified bindings.
|
|
393 |
* The bindings are supplied as a list of alternating
|
|
394 |
* {@code keystroke-action key} pairs. The {@code keystroke} is either
|
|
395 |
* an instance of {@code KeyStroke}, or a {@code String}
|
|
396 |
* that identifies the {@code KeyStroke} for the binding. Refer
|
|
397 |
* to {@code KeyStroke.getKeyStroke(String)} for the specific
|
|
398 |
* format. The {@code action key} part of the pair is the key
|
|
399 |
* registered in the {@code InputMap} for the {@code KeyStroke}.
|
|
400 |
* <p>
|
|
401 |
* The following illustrates loading an {@code InputMap} with two
|
|
402 |
* {@code key-action} pairs:
|
|
403 |
* <pre>
|
|
404 |
* LookAndFeel.loadKeyBindings(inputMap, new Object[] {
|
|
405 |
* "control X", "cut",
|
|
406 |
* "control V", "paste"
|
|
407 |
* });
|
|
408 |
* </pre>
|
|
409 |
* <p>
|
|
410 |
* Supplying a {@code null} list of bindings ({@code keys}) does not
|
|
411 |
* change {@code retMap} in any way.
|
|
412 |
* <p>
|
|
413 |
* Specifying a {@code null} {@code action key} results in
|
|
414 |
* removing the {@code keystroke's} entry from the {@code InputMap}.
|
|
415 |
* A {@code null} {@code keystroke} is ignored.
|
|
416 |
*
|
|
417 |
* @param retMap {@code InputMap} to add the {@code key-action}
|
|
418 |
* pairs to
|
|
419 |
* @param keys bindings to add to {@code retMap}
|
|
420 |
* @throws NullPointerException if {@code keys} is
|
|
421 |
* {@code non-null}, not empty, and {@code retMap} is
|
|
422 |
* {@code null}
|
|
423 |
*
|
|
424 |
* @see KeyStroke#getKeyStroke(String)
|
|
425 |
* @see InputMap
|
|
426 |
*
|
|
427 |
* @since 1.3
|
|
428 |
*/
|
|
429 |
public static void loadKeyBindings(InputMap retMap, Object[] keys) {
|
|
430 |
if (keys != null) {
|
|
431 |
for (int counter = 0, maxCounter = keys.length;
|
|
432 |
counter < maxCounter; counter++) {
|
|
433 |
Object keyStrokeO = keys[counter++];
|
|
434 |
KeyStroke ks = (keyStrokeO instanceof KeyStroke) ?
|
|
435 |
(KeyStroke)keyStrokeO :
|
|
436 |
KeyStroke.getKeyStroke((String)keyStrokeO);
|
|
437 |
retMap.put(ks, keys[counter]);
|
|
438 |
}
|
|
439 |
}
|
|
440 |
}
|
|
441 |
|
|
442 |
/**
|
|
443 |
* Creates and returns a {@code UIDefault.LazyValue} that loads an
|
|
444 |
* image. The returned value is an implementation of {@code
|
|
445 |
* UIDefaults.LazyValue}. When {@code createValue} is invoked on
|
|
446 |
* the returned object, the image is loaded. If the image is {@code
|
|
447 |
* non-null}, it is then wrapped in an {@code Icon} that implements {@code
|
|
448 |
* UIResource}. The image is loaded using {@code
|
|
449 |
* Class.getResourceAsStream(gifFile)}.
|
|
450 |
* <p>
|
|
451 |
* This method does not check the arguments in any way. It is
|
|
452 |
* strongly recommended that {@code non-null} values are supplied else
|
|
453 |
* exceptions may occur when {@code createValue} is invoked on the
|
|
454 |
* returned object.
|
|
455 |
*
|
|
456 |
* @param baseClass {@code Class} used to load the resource
|
|
457 |
* @param gifFile path to the image to load
|
|
458 |
* @return a {@code UIDefaults.LazyValue}; when resolved the
|
|
459 |
* {@code LazyValue} loads the specified image
|
|
460 |
* @see UIDefaults.LazyValue
|
|
461 |
* @see Icon
|
|
462 |
* @see Class#getResourceAsStream(String)
|
|
463 |
*/
|
|
464 |
public static Object makeIcon(final Class<?> baseClass, final String gifFile) {
|
|
465 |
return SwingUtilities2.makeIcon(baseClass, baseClass, gifFile);
|
|
466 |
}
|
|
467 |
|
|
468 |
/**
|
|
469 |
* Returns the <code>LayoutStyle</code> for this look
|
|
470 |
* and feel. This never returns {@code null}.
|
|
471 |
* <p>
|
|
472 |
* You generally don't use the <code>LayoutStyle</code> from
|
|
473 |
* the look and feel, instead use the <code>LayoutStyle</code>
|
|
474 |
* method <code>getInstance</code>.
|
|
475 |
*
|
|
476 |
* @see LayoutStyle#getInstance
|
|
477 |
* @return the <code>LayoutStyle</code> for this look and feel
|
|
478 |
* @since 1.6
|
|
479 |
*/
|
|
480 |
public LayoutStyle getLayoutStyle() {
|
|
481 |
return DefaultLayoutStyle.getInstance();
|
|
482 |
}
|
|
483 |
|
|
484 |
/**
|
|
485 |
* Invoked when the user attempts an invalid operation,
|
|
486 |
* such as pasting into an uneditable <code>JTextField</code>
|
|
487 |
* that has focus. The default implementation beeps. Subclasses
|
|
488 |
* that wish different behavior should override this and provide
|
|
489 |
* the additional feedback.
|
|
490 |
*
|
|
491 |
* @param component the <code>Component</code> the error occurred in,
|
|
492 |
* may be <code>null</code>
|
|
493 |
* indicating the error condition is not directly
|
|
494 |
* associated with a <code>Component</code>
|
|
495 |
* @since 1.4
|
|
496 |
*/
|
|
497 |
public void provideErrorFeedback(Component component) {
|
|
498 |
Toolkit toolkit = null;
|
|
499 |
if (component != null) {
|
|
500 |
toolkit = component.getToolkit();
|
|
501 |
} else {
|
|
502 |
toolkit = Toolkit.getDefaultToolkit();
|
|
503 |
}
|
|
504 |
toolkit.beep();
|
|
505 |
} // provideErrorFeedback()
|
|
506 |
|
|
507 |
/**
|
|
508 |
* Returns the value of the specified system desktop property by
|
|
509 |
* invoking <code>Toolkit.getDefaultToolkit().getDesktopProperty()</code>.
|
|
510 |
* If the value of the specified property is {@code null},
|
|
511 |
* {@code fallbackValue} is returned.
|
|
512 |
*
|
|
513 |
* @param systemPropertyName the name of the system desktop property being queried
|
|
514 |
* @param fallbackValue the object to be returned as the value if the system value is null
|
|
515 |
* @return the current value of the desktop property
|
|
516 |
*
|
|
517 |
* @see java.awt.Toolkit#getDesktopProperty
|
|
518 |
*
|
|
519 |
* @since 1.4
|
|
520 |
*/
|
|
521 |
public static Object getDesktopPropertyValue(String systemPropertyName, Object fallbackValue) {
|
|
522 |
Object value = Toolkit.getDefaultToolkit().getDesktopProperty(systemPropertyName);
|
|
523 |
if (value == null) {
|
|
524 |
return fallbackValue;
|
|
525 |
} else if (value instanceof Color) {
|
|
526 |
return new ColorUIResource((Color)value);
|
|
527 |
} else if (value instanceof Font) {
|
|
528 |
return new FontUIResource((Font)value);
|
|
529 |
}
|
|
530 |
return value;
|
|
531 |
}
|
|
532 |
|
|
533 |
/**
|
|
534 |
* Returns an <code>Icon</code> with a disabled appearance.
|
|
535 |
* This method is used to generate a disabled <code>Icon</code> when
|
|
536 |
* one has not been specified. For example, if you create a
|
|
537 |
* <code>JButton</code> and only specify an <code>Icon</code> via
|
|
538 |
* <code>setIcon</code> this method will be called to generate the
|
|
539 |
* disabled <code>Icon</code>. If {@code null} is passed as
|
|
540 |
* <code>icon</code> this method returns {@code null}.
|
|
541 |
* <p>
|
|
542 |
* Some look and feels might not render the disabled {@code Icon}, in which
|
|
543 |
* case they will ignore this.
|
|
544 |
*
|
|
545 |
* @param component {@code JComponent} that will display the {@code Icon},
|
|
546 |
* may be {@code null}
|
|
547 |
* @param icon {@code Icon} to generate the disabled icon from
|
|
548 |
* @return disabled {@code Icon}, or {@code null} if a suitable
|
|
549 |
* {@code Icon} can not be generated
|
|
550 |
* @since 1.5
|
|
551 |
*/
|
|
552 |
public Icon getDisabledIcon(JComponent component, Icon icon) {
|
|
553 |
if (icon instanceof ImageIcon) {
|
|
554 |
return new ImageIconUIResource(GrayFilter.
|
|
555 |
createDisabledImage(((ImageIcon)icon).getImage()));
|
|
556 |
}
|
|
557 |
return null;
|
|
558 |
}
|
|
559 |
|
|
560 |
/**
|
|
561 |
* Returns an <code>Icon</code> for use by disabled
|
|
562 |
* components that are also selected. This method is used to generate an
|
|
563 |
* <code>Icon</code> for components that are in both the disabled and
|
|
564 |
* selected states but do not have a specific <code>Icon</code> for this
|
|
565 |
* state. For example, if you create a <code>JButton</code> and only
|
|
566 |
* specify an <code>Icon</code> via <code>setIcon</code> this method
|
|
567 |
* will be called to generate the disabled and selected
|
|
568 |
* <code>Icon</code>. If {@code null} is passed as <code>icon</code> this
|
|
569 |
* methods returns {@code null}.
|
|
570 |
* <p>
|
|
571 |
* Some look and feels might not render the disabled and selected
|
|
572 |
* {@code Icon}, in which case they will ignore this.
|
|
573 |
*
|
|
574 |
* @param component {@code JComponent} that will display the {@code Icon},
|
|
575 |
* may be {@code null}
|
|
576 |
* @param icon {@code Icon} to generate disabled and selected icon from
|
|
577 |
* @return disabled and selected icon, or {@code null} if a suitable
|
|
578 |
* {@code Icon} can not be generated.
|
|
579 |
* @since 1.5
|
|
580 |
*/
|
|
581 |
public Icon getDisabledSelectedIcon(JComponent component, Icon icon) {
|
|
582 |
return getDisabledIcon(component, icon);
|
|
583 |
}
|
|
584 |
|
|
585 |
/**
|
|
586 |
* Return a short string that identifies this look and feel, e.g.
|
|
587 |
* "CDE/Motif". This string should be appropriate for a menu item.
|
|
588 |
* Distinct look and feels should have different names, e.g.
|
|
589 |
* a subclass of MotifLookAndFeel that changes the way a few components
|
|
590 |
* are rendered should be called "CDE/Motif My Way"; something
|
|
591 |
* that would be useful to a user trying to select a L&F from a list
|
|
592 |
* of names.
|
|
593 |
*
|
|
594 |
* @return short identifier for the look and feel
|
|
595 |
*/
|
|
596 |
public abstract String getName();
|
|
597 |
|
|
598 |
|
|
599 |
/**
|
|
600 |
* Return a string that identifies this look and feel. This string
|
|
601 |
* will be used by applications/services that want to recognize
|
|
602 |
* well known look and feel implementations. Presently
|
|
603 |
* the well known names are "Motif", "Windows", "Mac", "Metal". Note
|
|
604 |
* that a LookAndFeel derived from a well known superclass
|
|
605 |
* that doesn't make any fundamental changes to the look or feel
|
|
606 |
* shouldn't override this method.
|
|
607 |
*
|
|
608 |
* @return identifier for the look and feel
|
|
609 |
*/
|
|
610 |
public abstract String getID();
|
|
611 |
|
|
612 |
|
|
613 |
/**
|
|
614 |
* Return a one line description of this look and feel implementation,
|
|
615 |
* e.g. "The CDE/Motif Look and Feel". This string is intended for
|
|
616 |
* the user, e.g. in the title of a window or in a ToolTip message.
|
|
617 |
*
|
|
618 |
* @return short description for the look and feel
|
|
619 |
*/
|
|
620 |
public abstract String getDescription();
|
|
621 |
|
|
622 |
|
|
623 |
/**
|
|
624 |
* Returns {@code true} if the <code>LookAndFeel</code> returned
|
|
625 |
* <code>RootPaneUI</code> instances support providing {@code Window}
|
|
626 |
* decorations in a <code>JRootPane</code>.
|
|
627 |
* <p>
|
|
628 |
* The default implementation returns {@code false}, subclasses that
|
|
629 |
* support {@code Window} decorations should override this and return
|
|
630 |
* {@code true}.
|
|
631 |
*
|
|
632 |
* @return {@code true} if the {@code RootPaneUI} instances created by
|
|
633 |
* this look and feel support client side decorations
|
|
634 |
* @see JDialog#setDefaultLookAndFeelDecorated
|
|
635 |
* @see JFrame#setDefaultLookAndFeelDecorated
|
|
636 |
* @see JRootPane#setWindowDecorationStyle
|
|
637 |
* @since 1.4
|
|
638 |
*/
|
|
639 |
public boolean getSupportsWindowDecorations() {
|
|
640 |
return false;
|
|
641 |
}
|
|
642 |
|
|
643 |
/**
|
|
644 |
* If the underlying platform has a "native" look and feel, and
|
|
645 |
* this is an implementation of it, return {@code true}. For
|
|
646 |
* example, when the underlying platform is Solaris running CDE
|
|
647 |
* a CDE/Motif look and feel implementation would return {@code
|
|
648 |
* true}.
|
|
649 |
*
|
|
650 |
* @return {@code true} if this look and feel represents the underlying
|
|
651 |
* platform look and feel
|
|
652 |
*/
|
|
653 |
public abstract boolean isNativeLookAndFeel();
|
|
654 |
|
|
655 |
|
|
656 |
/**
|
|
657 |
* Return {@code true} if the underlying platform supports and or permits
|
|
658 |
* this look and feel. This method returns {@code false} if the look
|
|
659 |
* and feel depends on special resources or legal agreements that
|
|
660 |
* aren't defined for the current platform.
|
|
661 |
*
|
|
662 |
*
|
|
663 |
* @return {@code true} if this is a supported look and feel
|
|
664 |
* @see UIManager#setLookAndFeel
|
|
665 |
*/
|
|
666 |
public abstract boolean isSupportedLookAndFeel();
|
|
667 |
|
|
668 |
|
|
669 |
/**
|
|
670 |
* Initializes the look and feel. While this method is public,
|
|
671 |
* it should only be invoked by the {@code UIManager} when a
|
|
672 |
* look and feel is installed as the current look and feel. This
|
|
673 |
* method is invoked before the {@code UIManager} invokes
|
|
674 |
* {@code getDefaults}. This method is intended to perform any
|
|
675 |
* initialization for the look and feel. Subclasses
|
|
676 |
* should do any one-time setup they need here, rather than
|
|
677 |
* in a static initializer, because look and feel class objects
|
|
678 |
* may be loaded just to discover that {@code isSupportedLookAndFeel()}
|
|
679 |
* returns {@code false}.
|
|
680 |
*
|
|
681 |
* @see #uninitialize
|
|
682 |
* @see UIManager#setLookAndFeel
|
|
683 |
*/
|
|
684 |
public void initialize() {
|
|
685 |
}
|
|
686 |
|
|
687 |
|
|
688 |
/**
|
|
689 |
* Uninitializes the look and feel. While this method is public,
|
|
690 |
* it should only be invoked by the {@code UIManager} when
|
|
691 |
* the look and feel is uninstalled. For example,
|
|
692 |
* {@code UIManager.setLookAndFeel} invokes this when the look and
|
|
693 |
* feel is changed.
|
|
694 |
* <p>
|
|
695 |
* Subclasses may choose to free up some resources here.
|
|
696 |
*
|
|
697 |
* @see #initialize
|
|
698 |
* @see UIManager#setLookAndFeel
|
|
699 |
*/
|
|
700 |
public void uninitialize() {
|
|
701 |
}
|
|
702 |
|
|
703 |
/**
|
|
704 |
* Returns the look and feel defaults. While this method is public,
|
|
705 |
* it should only be invoked by the {@code UIManager} when the
|
|
706 |
* look and feel is set as the current look and feel and after
|
|
707 |
* {@code initialize} has been invoked.
|
|
708 |
*
|
|
709 |
* @return the look and feel defaults
|
|
710 |
* @see #initialize
|
|
711 |
* @see #uninitialize
|
|
712 |
* @see UIManager#setLookAndFeel
|
|
713 |
*/
|
|
714 |
public UIDefaults getDefaults() {
|
|
715 |
return null;
|
|
716 |
}
|
|
717 |
|
|
718 |
/**
|
|
719 |
* Returns a string that displays and identifies this
|
|
720 |
* object's properties.
|
|
721 |
*
|
|
722 |
* @return a String representation of this object
|
|
723 |
*/
|
|
724 |
public String toString() {
|
|
725 |
return "[" + getDescription() + " - " + getClass().getName() + "]";
|
|
726 |
}
|
|
727 |
}
|