38 |
|
|
39 |
package javax.swing; |
package javax.swing; |
40 |
|
|
41 |
|
import java.awt.Color; |
42 |
import java.awt.Component; |
import java.awt.Component; |
43 |
|
import java.awt.Font; |
44 |
import java.awt.Toolkit; |
import java.awt.Toolkit; |
45 |
|
import java.net.URL; |
46 |
|
|
47 |
|
import javax.swing.border.Border; |
48 |
|
import javax.swing.plaf.ComponentInputMapUIResource; |
49 |
|
import javax.swing.plaf.IconUIResource; |
50 |
|
import javax.swing.plaf.InputMapUIResource; |
51 |
|
import javax.swing.plaf.UIResource; |
52 |
import javax.swing.text.JTextComponent; |
import javax.swing.text.JTextComponent; |
53 |
|
|
54 |
public abstract class LookAndFeel |
public abstract class LookAndFeel |
112 |
*/ |
*/ |
113 |
public void initialize() |
public void initialize() |
114 |
{ |
{ |
115 |
|
// We do nothing here. This method is meant to be overridden by |
116 |
|
// LookAndFeel implementations. |
117 |
} |
} |
118 |
|
|
119 |
/** |
/** |
123 |
*/ |
*/ |
124 |
public static void installBorder(JComponent c, String defaultBorderName) |
public static void installBorder(JComponent c, String defaultBorderName) |
125 |
{ |
{ |
126 |
|
Border b = c.getBorder(); |
127 |
|
if (b == null || b instanceof UIResource) |
128 |
|
c.setBorder(UIManager.getBorder(defaultBorderName)); |
129 |
} |
} |
130 |
|
|
131 |
/** |
/** |
132 |
* Convenience method for initializing a component's foreground and |
* Convenience method for initializing a component's foreground and |
133 |
* background color properties with values from the current defaults table. |
* background color properties with values from the current defaults table. |
134 |
*/ |
*/ |
135 |
public static void installColors(JComponent c, String defaultBgName, String defaultFgName) |
public static void installColors(JComponent c, String defaultBgName, |
136 |
|
String defaultFgName) |
137 |
{ |
{ |
138 |
|
// Install background. |
139 |
|
Color bg = c.getBackground(); |
140 |
|
if (bg == null || bg instanceof UIResource) |
141 |
|
c.setBackground(UIManager.getColor(defaultBgName)); |
142 |
|
|
143 |
|
// Install foreground. |
144 |
|
Color fg = c.getForeground(); |
145 |
|
if (fg == null || fg instanceof UIResource) |
146 |
|
c.setForeground(UIManager.getColor(defaultFgName)); |
147 |
} |
} |
148 |
|
|
149 |
/** |
/** |
151 |
* and font properties with values from the current defaults table. |
* and font properties with values from the current defaults table. |
152 |
*/ |
*/ |
153 |
public static void installColorsAndFont(JComponent component, |
public static void installColorsAndFont(JComponent component, |
154 |
String defaultBgName, |
String defaultBgName, |
155 |
String defaultFgName, |
String defaultFgName, |
156 |
String defaultFontName) |
String defaultFontName) |
157 |
{ |
{ |
158 |
|
// Install colors. |
159 |
|
installColors(component, defaultBgName, defaultFgName); |
160 |
|
// Install font. |
161 |
|
Font f = component.getFont(); |
162 |
|
if (f == null || f instanceof UIResource) |
163 |
|
component.setFont(UIManager.getFont(defaultFontName)); |
164 |
} |
} |
165 |
|
|
166 |
/** |
/** |
185 |
public abstract boolean isSupportedLookAndFeel(); |
public abstract boolean isSupportedLookAndFeel(); |
186 |
|
|
187 |
/** |
/** |
188 |
* Loads the bindings in keys into retMap. |
* Loads the bindings in keys into retMap. Does not remove existing entries |
189 |
|
* from retMap. <code>keys</code> describes the InputMap, every even indexed |
190 |
|
* item is either a KeyStroke or a String representing a KeyStroke and every |
191 |
|
* odd indexed item is the Object associated with that KeyStroke in an |
192 |
|
* ActionMap. |
193 |
|
* |
194 |
|
* @param retMap the InputMap into which we load bindings |
195 |
|
* @param keys the Object array describing the InputMap as above |
196 |
*/ |
*/ |
197 |
public static void loadKeyBindings(InputMap retMap, Object[] keys) |
public static void loadKeyBindings(InputMap retMap, Object[] keys) |
198 |
{ |
{ |
199 |
|
if (keys == null) |
200 |
|
return; |
201 |
|
for (int i = 0; i < keys.length - 1; i+= 2) |
202 |
|
{ |
203 |
|
Object key = keys[i]; |
204 |
|
KeyStroke keyStroke; |
205 |
|
if (key instanceof KeyStroke) |
206 |
|
keyStroke = (KeyStroke)key; |
207 |
|
else |
208 |
|
keyStroke = KeyStroke.getKeyStroke((String)key); |
209 |
|
retMap.put(keyStroke, keys[i+1]); |
210 |
|
} |
211 |
} |
} |
212 |
|
|
213 |
/** |
/** |
214 |
* Creates a ComponentInputMap from keys. |
* Creates a ComponentInputMap from keys. |
215 |
|
* <code>keys</code> describes the InputMap, every even indexed |
216 |
|
* item is either a KeyStroke or a String representing a KeyStroke and every |
217 |
|
* odd indexed item is the Object associated with that KeyStroke in an |
218 |
|
* ActionMap. |
219 |
|
* |
220 |
|
* @param c the JComponent associated with the ComponentInputMap |
221 |
|
* @param keys the Object array describing the InputMap as above |
222 |
*/ |
*/ |
223 |
public static ComponentInputMap makeComponentInputMap(JComponent c, |
public static ComponentInputMap makeComponentInputMap(JComponent c, |
224 |
Object[] keys) |
Object[] keys) |
225 |
{ |
{ |
226 |
return null; |
ComponentInputMap retMap = new ComponentInputMapUIResource(c); |
227 |
|
loadKeyBindings(retMap, keys); |
228 |
|
return retMap; |
229 |
} |
} |
230 |
|
|
231 |
/** |
/** |
234 |
*/ |
*/ |
235 |
public static Object makeIcon(Class baseClass, String gifFile) |
public static Object makeIcon(Class baseClass, String gifFile) |
236 |
{ |
{ |
237 |
return null; |
final URL file = baseClass.getResource(gifFile); |
238 |
|
return new UIDefaults.LazyValue() |
239 |
|
{ |
240 |
|
public Object createValue(UIDefaults table) |
241 |
|
{ |
242 |
|
return new IconUIResource(new ImageIcon(file)); |
243 |
|
} |
244 |
|
}; |
245 |
} |
} |
246 |
|
|
247 |
/** |
/** |
248 |
* Creates a InputMap from keys. |
* Creates a InputMap from keys. |
249 |
|
* <code>keys</code> describes the InputMap, every even indexed |
250 |
|
* item is either a KeyStroke or a String representing a KeyStroke and every |
251 |
|
* odd indexed item is the Object associated with that KeyStroke in an |
252 |
|
* ActionMap. |
253 |
|
* |
254 |
|
* @param keys the Object array describing the InputMap as above |
255 |
*/ |
*/ |
256 |
public static InputMap makeInputMap(Object[] keys) |
public static InputMap makeInputMap(Object[] keys) |
257 |
{ |
{ |
258 |
return null; |
InputMap retMap = new InputMapUIResource(); |
259 |
|
loadKeyBindings(retMap, keys); |
260 |
|
return retMap; |
261 |
} |
} |
262 |
|
|
263 |
/** |
/** |
264 |
* Convenience method for building lists of KeyBindings. |
* Convenience method for building lists of KeyBindings. |
265 |
|
* <code>keyBindingList</code> is an array of KeyStroke-Action pairs where |
266 |
|
* even indexed elements are KeyStrokes or Strings representing KeyStrokes |
267 |
|
* and odd indexed elements are the associated Actions. |
268 |
|
* |
269 |
|
* @param keyBindingList the array of KeyStroke-Action pairs |
270 |
|
* @return a JTextComponent.KeyBinding array |
271 |
*/ |
*/ |
272 |
public static JTextComponent.KeyBinding[] makeKeyBindings(Object[] keyBindingList) |
public static JTextComponent.KeyBinding[] makeKeyBindings(Object[] keyBindingList) |
273 |
{ |
{ |
274 |
return null; |
JTextComponent.KeyBinding[] retBindings = |
275 |
|
new JTextComponent.KeyBinding[keyBindingList.length / 2]; |
276 |
|
for (int i = 0; i < keyBindingList.length - 1; i+= 2) |
277 |
|
{ |
278 |
|
KeyStroke stroke; |
279 |
|
if (keyBindingList[i] instanceof KeyStroke) |
280 |
|
stroke = (KeyStroke)keyBindingList[i]; |
281 |
|
else |
282 |
|
stroke = KeyStroke.getKeyStroke((String)keyBindingList[i]); |
283 |
|
retBindings[i/2] = new JTextComponent.KeyBinding(stroke, (String)keyBindingList[i+1]); |
284 |
|
} |
285 |
|
return retBindings; |
286 |
} |
} |
287 |
|
|
288 |
/** |
/** |
313 |
*/ |
*/ |
314 |
public void uninitialize() |
public void uninitialize() |
315 |
{ |
{ |
316 |
|
// We do nothing here. This method is meant to be overridden by |
317 |
|
// LookAndFeel implementations. |
318 |
} |
} |
319 |
|
|
320 |
/** |
/** |
323 |
*/ |
*/ |
324 |
public static void uninstallBorder(JComponent c) |
public static void uninstallBorder(JComponent c) |
325 |
{ |
{ |
326 |
|
if (c.getBorder() instanceof UIResource) |
327 |
|
c.setBorder(null); |
328 |
} |
} |
329 |
} |
} |