1 7 8 package javax.swing; 9 10 import java.awt.Font ; 11 import java.awt.event.InputEvent ; 12 import java.awt.event.KeyEvent ; 13 import java.awt.Color ; 14 import java.awt.Component ; 15 import java.awt.SystemColor ; 16 import java.awt.Toolkit ; 17 18 import javax.swing.text.*; 19 import javax.swing.border.*; 20 import javax.swing.plaf.*; 21 22 import java.net.URL ; 23 import com.sun.java.swing.SwingUtilities2; 24 25 import java.util.StringTokenizer ; 26 27 28 36 public abstract class LookAndFeel 37 { 38 39 52 public static void installColors(JComponent c, 53 String defaultBgName, 54 String defaultFgName) 55 { 56 Color bg = c.getBackground(); 57 if (bg == null || bg instanceof UIResource) { 58 c.setBackground(UIManager.getColor(defaultBgName)); 59 } 60 61 Color fg = c.getForeground(); 62 if (fg == null || fg instanceof UIResource) { 63 c.setForeground(UIManager.getColor(defaultFgName)); 64 } 65 } 66 67 68 83 public static void installColorsAndFont(JComponent c, 84 String defaultBgName, 85 String defaultFgName, 86 String defaultFontName) { 87 Font f = c.getFont(); 88 if (f == null || f instanceof UIResource) { 89 c.setFont(UIManager.getFont(defaultFontName)); 90 } 91 92 installColors(c, defaultBgName, defaultFgName); 93 } 94 95 96 103 public static void installBorder(JComponent c, String defaultBorderName) { 104 Border b = c.getBorder(); 105 if (b == null || b instanceof UIResource) { 106 c.setBorder(UIManager.getBorder(defaultBorderName)); 107 } 108 } 109 110 111 117 public static void uninstallBorder(JComponent c) { 118 if (c.getBorder() instanceof UIResource) { 119 c.setBorder(null); 120 } 121 } 122 123 142 public static void installProperty(JComponent c, 143 String propertyName, Object propertyValue) { 144 c.setUIProperty(propertyName, propertyValue); 145 } 146 147 170 public static JTextComponent.KeyBinding[] makeKeyBindings(Object [] keyBindingList) 171 { 172 JTextComponent.KeyBinding[] rv = new JTextComponent.KeyBinding[keyBindingList.length / 2]; 173 174 for(int i = 0; i < keyBindingList.length; i += 2) { 175 KeyStroke keystroke = (keyBindingList[i] instanceof KeyStroke ) 176 ? (KeyStroke )keyBindingList[i] 177 : KeyStroke.getKeyStroke((String )keyBindingList[i]); 178 String action = (String )keyBindingList[i+1]; 179 rv[i / 2] = new JTextComponent.KeyBinding(keystroke, action); 180 } 181 182 return rv; 183 } 184 185 195 public static InputMap makeInputMap(Object [] keys) { 196 InputMap retMap = new InputMapUIResource(); 197 loadKeyBindings(retMap, keys); 198 return retMap; 199 } 200 201 211 public static ComponentInputMap makeComponentInputMap(JComponent c, 212 Object [] keys) { 213 ComponentInputMap retMap = new ComponentInputMapUIResource(c); 214 loadKeyBindings(retMap, keys); 215 return retMap; 216 } 217 218 219 231 public static void loadKeyBindings(InputMap retMap, Object [] keys) { 232 if (keys != null) { 233 for (int counter = 0, maxCounter = keys.length; 234 counter < maxCounter; counter++) { 235 Object keyStrokeO = keys[counter++]; 236 KeyStroke ks = (keyStrokeO instanceof KeyStroke ) ? 237 (KeyStroke )keyStrokeO : 238 KeyStroke.getKeyStroke((String )keyStrokeO); 239 retMap.put(ks, keys[counter]); 240 } 241 } 242 } 243 244 249 public static Object makeIcon(final Class <?> baseClass, final String gifFile) { 250 return SwingUtilities2.makeIcon(baseClass, baseClass, gifFile); 251 } 252 253 266 public void provideErrorFeedback(Component component) { 267 Toolkit toolkit = null; 268 if (component != null) { 269 toolkit = component.getToolkit(); 270 } else { 271 toolkit = Toolkit.getDefaultToolkit(); 272 } 273 toolkit.beep(); 274 } 276 288 public static Object getDesktopPropertyValue(String systemPropertyName, Object fallbackValue) { 289 Object value = Toolkit.getDefaultToolkit().getDesktopProperty(systemPropertyName); 290 if (value == null) { 291 return fallbackValue; 292 } else if (value instanceof Color ) { 293 return new ColorUIResource((Color )value); 294 } else if (value instanceof Font ) { 295 return new FontUIResource((Font )value); 296 } 297 return value; 298 } 299 300 318 public Icon getDisabledIcon(JComponent component, Icon icon) { 319 if (icon instanceof ImageIcon ) { 320 return new IconUIResource(new ImageIcon (GrayFilter. 321 createDisabledImage(((ImageIcon )icon).getImage()))); 322 } 323 return null; 324 } 325 326 346 public Icon getDisabledSelectedIcon(JComponent component, Icon icon) { 347 return getDisabledIcon(component, icon); 348 } 349 350 359 public abstract String getName(); 360 361 362 371 public abstract String getID(); 372 373 374 379 public abstract String getDescription(); 380 381 382 397 public boolean getSupportsWindowDecorations() { 398 return false; 399 } 400 401 407 public abstract boolean isNativeLookAndFeel(); 408 409 410 418 public abstract boolean isSupportedLookAndFeel(); 419 420 421 432 public void initialize() { 433 } 434 435 436 443 public void uninitialize() { 444 } 445 446 455 public UIDefaults getDefaults() { 456 return null; 457 } 458 459 465 public String toString() { 466 return "[" + getDescription() + " - " + getClass().getName() + "]"; 467 } 468 } 469 | Popular Tags |