1 /* UIDefaults.java -- database for all settings and interface bindings.
2 Copyright (C) 2002, 2004 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)
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
40 import java.awt.Color;
41 import java.awt.Dimension;
43 import java.awt.Insets;
44 import java.beans.PropertyChangeEvent;
45 import java.beans.PropertyChangeListener;
46 import java.beans.PropertyChangeSupport;
47 import java.lang.reflect.Method;
48 import java.util.Hashtable;
49 import java.util.LinkedList;
50 import java.util.ListIterator;
51 import java.util.Locale;
52 import java.util.MissingResourceException;
53 import java.util.ResourceBundle;
56 import javax.swing.border.Border;
57 import javax.swing.plaf.ComponentUI;
60 * UIDefaults is a database where all settings and interface bindings are
61 * stored into. An PLAF implementation fills one of these (see for example
62 * plaf/basic/BasicLookAndFeel.java) with "ButtonUI" -> new BasicButtonUI().
64 * @author Ronald Veldema (rveldema@cs.vu.nl)
66 public class UIDefaults extends Hashtable
68 private LinkedList bundles;
69 private Locale defaultLocale;
70 private PropertyChangeSupport propertyChangeSupport;
72 public interface ActiveValue
74 Object createValue(UIDefaults table);
77 public static class LazyInputMap implements LazyValue
80 public LazyInputMap(Object[] bindings)
84 public Object createValue(UIDefaults table)
86 InputMap im = new InputMap ();
87 for (int i = 0; 2*i+1 < bind.length; ++i)
89 im.put (KeyStroke.getKeyStroke ((String) bind[2*i]),
96 public interface LazyValue
98 Object createValue(UIDefaults table);
101 public static class ProxyLazyValue implements LazyValue
104 public ProxyLazyValue(String s)
106 final String className = s;
107 inner = new LazyValue ()
109 public Object createValue (UIDefaults table)
115 .getConstructor (new Class[] {})
116 .newInstance (new Object[] {});
126 public ProxyLazyValue(String c, String m)
128 final String className = c;
129 final String methodName = m;
130 inner = new LazyValue ()
132 public Object createValue (UIDefaults table)
138 .getMethod (methodName, new Class[] {})
139 .invoke (null, new Object[] {});
149 public ProxyLazyValue (String c, Object[] os)
151 final String className = c;
152 final Object[] objs = os;
153 final Class[] clss = new Class[objs.length];
154 for (int i = 0; i < objs.length; ++i)
156 clss[i] = objs[i].getClass ();
158 inner = new LazyValue ()
160 public Object createValue (UIDefaults table)
166 .getConstructor (clss)
177 public ProxyLazyValue (String c, String m, Object[] os)
179 final String className = c;
180 final String methodName = m;
181 final Object[] objs = os;
182 final Class[] clss = new Class[objs.length];
183 for (int i = 0; i < objs.length; ++i)
185 clss[i] = objs[i].getClass ();
187 inner = new LazyValue ()
189 public Object createValue(UIDefaults table)
195 .getMethod (methodName, clss)
196 .invoke (null, objs);
206 public Object createValue (UIDefaults table)
208 return inner.createValue (table);
212 private static final long serialVersionUID = 7341222528856548117L;
216 bundles = new LinkedList ();
217 defaultLocale = Locale.getDefault ();
218 propertyChangeSupport = new PropertyChangeSupport(this);
221 public UIDefaults(Object[] entries)
225 for (int i = 0; (2*i+1) < entries.length; ++i)
226 put (entries[2*i], entries[2*i+1]);
229 public Object get(Object key)
231 return this.get (key, getDefaultLocale ());
234 public Object get (Object key, Locale loc)
238 if (super.containsKey (key))
240 obj = super.get (key);
242 else if (key instanceof String)
244 String keyString = (String) key;
245 ListIterator i = bundles.listIterator (0);
248 String bundle_name = (String) i.next ();
250 ResourceBundle.getBundle (bundle_name, loc);
255 obj = res.getObject (keyString);
258 catch (MissingResourceException me)
260 // continue, this bundle has no such key
266 // now we've found the object, resolve it.
267 // nb: LazyValues aren't supported in resource bundles, so it's correct
268 // to insert their results in the locale-less hashtable.
273 if (obj instanceof LazyValue)
275 Object resolved = ((LazyValue)obj).createValue (this);
277 super.put (key, resolved);
280 else if (obj instanceof ActiveValue)
282 return ((ActiveValue)obj).createValue (this);
288 public Object put(Object key, Object value)
290 Object old = super.put (key, value);
291 if (key instanceof String && old != value)
292 firePropertyChange ((String) key, old, value);
296 public void putDefaults(Object[] entries)
298 for (int i = 0; (2*i+1) < entries.length; ++i)
300 super.put (entries[2*i], entries[2*i+1]);
302 firePropertyChange ("UIDefaults", null, null);
305 public Font getFont(Object key)
308 return o instanceof Font ? (Font) o : null;
311 public Font getFont(Object key, Locale l)
313 Object o = get(key, l);
314 return o instanceof Font ? (Font) o : null;
317 public Color getColor(Object key)
320 return o instanceof Color ? (Color) o : null;
323 public Color getColor(Object key, Locale l)
325 Object o = get(key, l);
326 return o instanceof Color ? (Color) o : null;
329 public Icon getIcon(Object key)
332 return o instanceof Icon ? (Icon) o : null;
335 public Icon getIcon(Object key, Locale l)
337 Object o = get(key, l);
338 return o instanceof Icon ? (Icon) o : null;
341 public Border getBorder(Object key)
344 return o instanceof Border ? (Border) o : null;
347 public Border getBorder(Object key, Locale l)
349 Object o = get(key, l);
350 return o instanceof Border ? (Border) o : null;
353 public String getString(Object key)
356 return o instanceof String ? (String) o : null;
359 public String getString(Object key, Locale l)
361 Object o = get(key, l);
362 return o instanceof String ? (String) o : null;
365 public int getInt(Object key)
368 return o instanceof Integer ? ((Integer) o).intValue() : 0;
371 public int getInt(Object key, Locale l)
373 Object o = get(key, l);
374 return o instanceof Integer ? ((Integer) o).intValue() : 0;
377 public boolean getBoolean(Object key)
379 return Boolean.TRUE.equals(get(key));
382 public boolean getBoolean(Object key, Locale l)
384 return Boolean.TRUE.equals(get(key, l));
387 public Insets getInsets(Object key)
390 return o instanceof Insets ? (Insets) o : null;
393 public Insets getInsets(Object key, Locale l)
395 Object o = get(key, l);
396 return o instanceof Insets ? (Insets) o : null;
399 public Dimension getDimension(Object key)
402 return o instanceof Dimension ? (Dimension) o : null;
405 public Dimension getDimension(Object key, Locale l)
407 Object o = get(key, l);
408 return o instanceof Dimension ? (Dimension) o : null;
411 public Class getUIClass(String id, ClassLoader loader)
413 String className = (String) get (id);
414 if (className == null)
419 return loader.loadClass (className);
420 return Class.forName (className);
428 public Class getUIClass(String id)
430 return getUIClass (id, null);
433 protected void getUIError(String msg)
435 System.err.println ("UIDefaults.getUIError: " + msg);
438 public ComponentUI getUI(JComponent target)
440 String classId = target.getUIClassID ();
441 Class cls = getUIClass (classId);
444 getUIError ("failed to locate UI class:" + classId);
452 factory = cls.getMethod ("createUI", new Class[] { JComponent.class } );
454 catch (NoSuchMethodException nme)
456 getUIError ("failed to locate createUI method on " + cls.toString ());
462 return (ComponentUI) factory.invoke (null, new Object[] { target });
464 catch (java.lang.reflect.InvocationTargetException ite)
466 getUIError ("InvocationTargetException ("+ ite.getTargetException()
467 +") calling createUI(...) on " + cls.toString ());
472 getUIError ("exception calling createUI(...) on " + cls.toString ());
477 public void addPropertyChangeListener(PropertyChangeListener listener)
479 propertyChangeSupport.addPropertyChangeListener(listener);
482 void removePropertyChangeListener(PropertyChangeListener listener)
484 propertyChangeSupport.removePropertyChangeListener(listener);
487 public PropertyChangeListener[] getPropertyChangeListeners()
489 return propertyChangeSupport.getPropertyChangeListeners();
492 protected void firePropertyChange(String property,
493 Object oldValue, Object newValue)
495 propertyChangeSupport.firePropertyChange(property, oldValue, newValue);
498 public void addResourceBundle(String name)
500 bundles.addFirst (name);
503 public void removeResourceBundle(String name)
505 bundles.remove (name);
508 public void setDefaultLocale(Locale loc)
513 public Locale getDefaultLocale()
515 return defaultLocale;