Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / UIManager.java
blobe1ee28b3f1a54deb96e2cc5972f140ca3cd9f0cf
1 /* UIManager.java --
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.Font;
44 import java.awt.Insets;
45 import java.beans.PropertyChangeListener;
46 import java.beans.PropertyChangeSupport;
47 import java.io.Serializable;
48 import java.util.Locale;
50 import javax.swing.border.Border;
51 import javax.swing.plaf.ComponentUI;
52 import javax.swing.plaf.metal.MetalLookAndFeel;
54 /**
55 * Manages the current {@link LookAndFeel} and any auxiliary {@link LookAndFeel}
56 * instances.
58 public class UIManager implements Serializable
60 /**
61 * Represents the basic information about a {@link LookAndFeel} (LAF), so
62 * that a list of installed LAFs can be presented without actually loading
63 * the LAF class(es).
65 public static class LookAndFeelInfo
67 String name, clazz;
69 /**
70 * Creates a new instance.
72 * @param name the look and feel name.
73 * @param clazz the look and feel class name.
75 public LookAndFeelInfo(String name,
76 String clazz)
78 this.name = name;
79 this.clazz = clazz;
82 /**
83 * Returns the name of the look and feel.
85 * @return The name of the look and feel.
87 public String getName()
89 return name;
92 /**
93 * Returns the fully qualified class name for the {@link LookAndFeel}.
95 * @return The fully qualified class name for the {@link LookAndFeel}.
97 public String getClassName()
99 return clazz;
103 * Returns a String representation of the LookAndFeelInfo object.
105 * @return a String representation of the LookAndFeelInfo object
107 public String toString()
109 StringBuffer s = new StringBuffer();
110 s.append(getClass().getName());
111 s.append('[');
112 s.append(getName());
113 s.append(' ');
114 s.append(getClassName());
115 s.append(']');
116 return s.toString();
120 private static final long serialVersionUID = -5547433830339189365L;
122 /** The installed look and feel(s). */
123 static LookAndFeelInfo [] installed = {
124 new LookAndFeelInfo("Metal", "javax.swing.plaf.metal.MetalLookAndFeel"),
125 new LookAndFeelInfo("GNU", "gnu.javax.swing.plaf.gnu.GNULookAndFeel")
128 /** The installed auxiliary look and feels. */
129 static LookAndFeel[] auxLookAndFeels;
131 /** The current look and feel. */
132 static LookAndFeel currentLookAndFeel;
134 static UIDefaults currentUIDefaults;
137 * UIDefaults set by the user.
139 static UIDefaults userUIDefaults;
141 /** Property change listener mechanism. */
142 static PropertyChangeSupport listeners
143 = new PropertyChangeSupport(UIManager.class);
145 static
147 String defaultlaf = System.getProperty("swing.defaultlaf");
148 try {
149 if (defaultlaf != null)
151 Class lafClass = Class.forName(defaultlaf);
152 LookAndFeel laf = (LookAndFeel) lafClass.newInstance();
153 setLookAndFeel(laf);
155 else
157 setLookAndFeel(new MetalLookAndFeel());
160 catch (Exception ex)
162 System.err.println("cannot initialize Look and Feel: " + defaultlaf);
163 System.err.println("error: " + ex.toString());
164 System.err.println("falling back to Metal Look and Feel");
167 setLookAndFeel(new MetalLookAndFeel());
169 catch (Exception ex2)
171 throw (Error) new AssertionError("There must be no problem installing"
172 + " the MetalLookAndFeel.")
173 .initCause(ex2);
179 * Creates a new instance of the <code>UIManager</code>. There is no need
180 * to construct an instance of this class, since all methods are static.
182 public UIManager()
184 // Do nothing here.
188 * Add a <code>PropertyChangeListener</code> to the listener list.
190 * @param listener the listener to add
192 public static void addPropertyChangeListener(PropertyChangeListener listener)
194 listeners.addPropertyChangeListener(listener);
198 * Remove a <code>PropertyChangeListener</code> from the listener list.
200 * @param listener the listener to remove
202 public static void removePropertyChangeListener(PropertyChangeListener
203 listener)
205 listeners.removePropertyChangeListener(listener);
209 * Returns an array of all added <code>PropertyChangeListener</code> objects.
211 * @return an array of listeners
213 * @since 1.4
215 public static PropertyChangeListener[] getPropertyChangeListeners()
217 return listeners.getPropertyChangeListeners();
221 * Add a {@link LookAndFeel} to the list of auxiliary look and feels.
223 * @param laf the auxiliary look and feel (<code>null</code> not permitted).
225 * @throws NullPointerException if <code>laf</code> is <code>null</code>.
227 * @see #getAuxiliaryLookAndFeels()
229 public static void addAuxiliaryLookAndFeel(LookAndFeel laf)
231 if (laf == null)
232 throw new NullPointerException("Null 'laf' argument.");
233 if (auxLookAndFeels == null)
235 auxLookAndFeels = new LookAndFeel[1];
236 auxLookAndFeels[0] = laf;
237 return;
240 LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length + 1];
241 System.arraycopy(auxLookAndFeels, 0, temp, 0, auxLookAndFeels.length);
242 auxLookAndFeels = temp;
243 auxLookAndFeels[auxLookAndFeels.length - 1] = laf;
247 * Removes a {@link LookAndFeel} (LAF) from the list of auxiliary LAFs.
249 * @param laf the LAF to remove.
251 * @return <code>true</code> if the LAF was removed, and <code>false</code>
252 * otherwise.
254 public static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)
256 if (auxLookAndFeels == null)
257 return false;
258 int count = auxLookAndFeels.length;
259 if (count == 1 && auxLookAndFeels[0] == laf)
261 auxLookAndFeels = null;
262 return true;
264 for (int i = 0; i < count; i++)
266 if (auxLookAndFeels[i] == laf)
268 LookAndFeel[] temp = new LookAndFeel[auxLookAndFeels.length - 1];
269 if (i == 0)
271 System.arraycopy(auxLookAndFeels, 1, temp, 0, count - 1);
273 else if (i == count - 1)
275 System.arraycopy(auxLookAndFeels, 0, temp, 0, count - 1);
277 else
279 System.arraycopy(auxLookAndFeels, 0, temp, 0, i);
280 System.arraycopy(auxLookAndFeels, i + 1, temp, i,
281 count - i - 1);
283 auxLookAndFeels = temp;
284 return true;
287 return false;
291 * Returns an array (possibly <code>null</code>) containing the auxiliary
292 * {@link LookAndFeel}s that are in use. These are used by the
293 * {@link javax.swing.plaf.multi.MultiLookAndFeel} class.
295 * @return The auxiliary look and feels (possibly <code>null</code>).
297 * @see #addAuxiliaryLookAndFeel(LookAndFeel)
299 public static LookAndFeel[] getAuxiliaryLookAndFeels()
301 return auxLookAndFeels;
305 * Returns an object from the {@link UIDefaults} table for the current
306 * {@link LookAndFeel}.
308 * @param key the key.
310 * @return The object.
312 public static Object get(Object key)
314 Object val = null;
315 if (userUIDefaults != null)
316 val = userUIDefaults.get(key);
317 if (val == null)
318 val = getLookAndFeelDefaults().get(key);
319 return val;
323 * Returns an object from the {@link UIDefaults} table for the current
324 * {@link LookAndFeel}.
326 * @param key the key.
328 * @return The object.
330 public static Object get(Object key, Locale locale)
332 Object val = null;
333 if (userUIDefaults != null)
334 val = userUIDefaults.get(key, locale);
335 if (val == null)
336 val = getLookAndFeelDefaults().get(key, locale);
337 return val;
341 * Returns a boolean value from the defaults table,
342 * <code>false</code> if key is not present.
344 * @since 1.4
346 public static boolean getBoolean(Object key)
348 Boolean value = (Boolean) get(key);
349 return value != null ? value.booleanValue() : false;
353 * Returns a boolean value from the defaults table,
354 * <code>false</code> if key is not present.
356 * @since 1.4
358 public static boolean getBoolean(Object key, Locale locale)
360 Boolean value = (Boolean) get(key, locale);
361 return value != null ? value.booleanValue() : false;
365 * Returns a border from the defaults table.
367 public static Border getBorder(Object key)
369 return (Border) get(key);
373 * Returns a border from the defaults table.
375 * @since 1.4
377 public static Border getBorder(Object key, Locale locale)
379 return (Border) get(key, locale);
383 * Returns a drawing color from the defaults table.
385 public static Color getColor(Object key)
387 return (Color) get(key);
391 * Returns a drawing color from the defaults table.
393 public static Color getColor(Object key, Locale locale)
395 return (Color) get(key);
399 * The fully qualified class name of the cross platform (Metal) look and feel.
400 * This string can be passed to Class.forName()
402 * @return <code>"javax.swing.plaf.metal.MetalLookAndFeel"</code>
404 public static String getCrossPlatformLookAndFeelClassName()
406 return "javax.swing.plaf.metal.MetalLookAndFeel";
410 * Returns the default values for this look and feel.
412 * @return The {@link UIDefaults} for the current {@link LookAndFeel}.
414 public static UIDefaults getDefaults()
416 return currentUIDefaults;
420 * Returns a dimension from the defaults table.
422 public static Dimension getDimension(Object key)
424 return (Dimension) get(key);
428 * Returns a dimension from the defaults table.
430 public static Dimension getDimension(Object key, Locale locale)
432 return (Dimension) get(key, locale);
436 * Retrieves a font from the defaults table of the current
437 * LookAndFeel.
439 * @param key an Object that specifies the font. Typically,
440 * this is a String such as
441 * <code>TitledBorder.font</code>.
443 public static Font getFont(Object key)
445 return (Font) get(key);
449 * Retrieves a font from the defaults table of the current
450 * LookAndFeel.
452 * @param key an Object that specifies the font. Typically,
453 * this is a String such as
454 * <code>TitledBorder.font</code>.
456 public static Font getFont(Object key, Locale locale)
458 return (Font) get(key ,locale);
462 * Returns an Icon from the defaults table.
464 public static Icon getIcon(Object key)
466 return (Icon) get(key);
470 * Returns an Icon from the defaults table.
472 public static Icon getIcon(Object key, Locale locale)
474 return (Icon) get(key, locale);
478 * Returns an Insets object from the defaults table.
480 public static Insets getInsets(Object key)
482 Object o = get(key);
483 if (o instanceof Insets)
484 return (Insets) o;
485 else
486 return null;
490 * Returns an Insets object from the defaults table.
492 public static Insets getInsets(Object key, Locale locale)
494 Object o = get(key, locale);
495 if (o instanceof Insets)
496 return (Insets) o;
497 else
498 return null;
502 * Returns an array containing information about the {@link LookAndFeel}s
503 * that are installed.
505 * @return A list of the look and feels that are available (installed).
507 public static LookAndFeelInfo[] getInstalledLookAndFeels()
509 return installed;
512 public static int getInt(Object key)
514 Integer x = (Integer) get(key);
515 if (x == null)
516 return 0;
517 return x.intValue();
520 public static int getInt(Object key, Locale locale)
522 Integer x = (Integer) get(key, locale);
523 if (x == null)
524 return 0;
525 return x.intValue();
529 * Returns the current look and feel (which may be <code>null</code>).
531 * @return The current look and feel.
533 * @see #setLookAndFeel(LookAndFeel)
535 public static LookAndFeel getLookAndFeel()
537 return currentLookAndFeel;
541 * Returns the <code>UIDefaults</code> table of the currently active
542 * look and feel.
544 * @return The {@link UIDefaults} for the current {@link LookAndFeel}.
546 public static UIDefaults getLookAndFeelDefaults()
548 return currentUIDefaults;
552 * Returns a string from the defaults table.
554 public static String getString(Object key)
556 return (String) get(key);
560 * Returns a string from the defaults table.
562 public static String getString(Object key, Locale locale)
564 return (String) get(key, locale);
568 * Returns the name of the {@link LookAndFeel} class that implements the
569 * native systems look and feel if there is one, otherwise the name
570 * of the default cross platform LookAndFeel class.
572 * @return The fully qualified class name for the system look and feel.
574 * @see #getCrossPlatformLookAndFeelClassName()
576 public static String getSystemLookAndFeelClassName()
578 return getCrossPlatformLookAndFeelClassName();
582 * Returns UI delegate from the current {@link LookAndFeel} that renders the
583 * target component.
585 * @param target the target component.
587 public static ComponentUI getUI(JComponent target)
589 ComponentUI ui = null;
590 if (userUIDefaults != null
591 && userUIDefaults.get(target.getUIClassID()) != null)
592 ui = userUIDefaults.getUI(target);
593 if (ui == null)
594 ui = currentUIDefaults.getUI(target);
595 return ui;
599 * Creates a new look and feel and adds it to the current array.
601 * @param name the look and feel name.
602 * @param className the fully qualified name of the class that implements the
603 * look and feel.
605 public static void installLookAndFeel(String name, String className)
607 installLookAndFeel(new LookAndFeelInfo(name, className));
611 * Adds the specified look and feel to the current array and then calls
612 * setInstalledLookAndFeels(javax.swing.UIManager.LookAndFeelInfo[]).
614 public static void installLookAndFeel(LookAndFeelInfo info)
616 LookAndFeelInfo[] newInstalled = new LookAndFeelInfo[installed.length + 1];
617 System.arraycopy(installed, 0, newInstalled, 0, installed.length);
618 newInstalled[newInstalled.length - 1] = info;
619 setInstalledLookAndFeels(newInstalled);
623 * Stores an object in the defaults table.
625 public static Object put(Object key, Object value)
627 Object old = get(key);
628 if (userUIDefaults == null)
629 userUIDefaults = new UIDefaults();
630 userUIDefaults.put(key, value);
631 return old;
635 * Replaces the current array of installed LookAndFeelInfos.
637 public static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)
639 installed = infos;
643 * Sets the current {@link LookAndFeel}.
645 * @param newLookAndFeel the new look and feel (<code>null</code> permitted).
647 * @throws UnsupportedLookAndFeelException if the look and feel is not
648 * supported on the current platform.
650 * @see LookAndFeel#isSupportedLookAndFeel()
652 public static void setLookAndFeel(LookAndFeel newLookAndFeel)
653 throws UnsupportedLookAndFeelException
655 if (newLookAndFeel != null && ! newLookAndFeel.isSupportedLookAndFeel())
656 throw new UnsupportedLookAndFeelException(newLookAndFeel.getName());
657 LookAndFeel oldLookAndFeel = currentLookAndFeel;
658 if (oldLookAndFeel != null)
659 oldLookAndFeel.uninitialize();
661 // Set the current default look and feel using a LookAndFeel object.
662 currentLookAndFeel = newLookAndFeel;
663 if (newLookAndFeel != null)
665 newLookAndFeel.initialize();
666 currentUIDefaults = newLookAndFeel.getDefaults();
668 else
670 currentUIDefaults = null;
672 listeners.firePropertyChange("lookAndFeel", oldLookAndFeel, newLookAndFeel);
673 //revalidate();
674 //repaint();
678 * Set the current default look and feel using a class name.
680 * @param className the look and feel class name.
682 * @throws UnsupportedLookAndFeelException if the look and feel is not
683 * supported on the current platform.
685 * @see LookAndFeel#isSupportedLookAndFeel()
687 public static void setLookAndFeel(String className)
688 throws ClassNotFoundException, InstantiationException, IllegalAccessException,
689 UnsupportedLookAndFeelException
691 Class c = Class.forName(className);
692 LookAndFeel a = (LookAndFeel) c.newInstance(); // throws class-cast-exception
693 setLookAndFeel(a);