1 /* UIDefaults.java -- database for all settings and interface bindings.
2 Copyright (C) 2002 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
.lang
.reflect
.Method
;
47 import java
.lang
.reflect
.Constructor
;
48 import java
.util
.Hashtable
;
49 import java
.util
.Iterator
;
50 import java
.util
.List
;
51 import java
.util
.ListIterator
;
52 import java
.util
.LinkedList
;
53 import java
.util
.Locale
;
55 import java
.util
.HashSet
;
56 import java
.util
.MissingResourceException
;
57 import java
.util
.ResourceBundle
;
58 import javax
.swing
.border
.Border
;
59 import javax
.swing
.plaf
.ComponentUI
;
62 * UIDefaults is a database where all settings and interface bindings are
63 * stored into. An PLAF implementation fills one of these (see for example
64 * plaf/basic/BasicDefaults.java) with "JButton" -> new BasicButtonUI().
66 * @author Ronald Veldema (rveldema@cs.vu.nl)
68 public class UIDefaults
extends Hashtable
77 Object
createValue(UIDefaults table
);
78 } // interface ActiveValue
80 public static class LazyInputMap
implements LazyValue
83 public LazyInputMap(Object
[] bindings
)
87 public Object
createValue(UIDefaults table
)
89 InputMap im
= new InputMap ();
90 for (int i
= 0; 2*i
+1 < bind
.length
; ++i
)
92 im
.put (KeyStroke
.getKeyStroke ((String
) bind
[2*i
]),
97 } // class LazyInputMap
101 Object
createValue(UIDefaults table
);
102 } // interface LazyValue
104 public static class ProxyLazyValue
implements LazyValue
107 public ProxyLazyValue(String s
)
109 final String className
= s
;
110 inner
= new LazyValue ()
112 public Object
createValue (UIDefaults table
)
118 .getConstructor (new Class
[] {})
119 .newInstance (new Object
[] {});
129 public ProxyLazyValue(String c
, String m
)
131 final String className
= c
;
132 final String methodName
= m
;
133 inner
= new LazyValue ()
135 public Object
createValue (UIDefaults table
)
141 .getMethod (methodName
, new Class
[] {})
142 .invoke (null, new Object
[] {});
152 public ProxyLazyValue (String c
, Object
[] os
)
154 final String className
= c
;
155 final Object
[] objs
= os
;
156 final Class
[] clss
= new Class
[objs
.length
];
157 for (int i
= 0; i
< objs
.length
; ++i
)
159 clss
[i
] = objs
[i
].getClass ();
161 inner
= new LazyValue ()
163 public Object
createValue (UIDefaults table
)
169 .getConstructor (clss
)
180 public ProxyLazyValue (String c
, String m
, Object
[] os
)
182 final String className
= c
;
183 final String methodName
= m
;
184 final Object
[] objs
= os
;
185 final Class
[] clss
= new Class
[objs
.length
];
186 for (int i
= 0; i
< objs
.length
; ++i
)
188 clss
[i
] = objs
[i
].getClass ();
190 inner
= new LazyValue ()
192 public Object
createValue(UIDefaults table
)
198 .getMethod (methodName
, clss
)
199 .invoke (null, objs
);
209 public Object
createValue (UIDefaults table
)
211 return inner
.createValue (table
);
213 } // class ProxyLazyValue
215 private static final long serialVersionUID
= 7341222528856548117L;
219 bundles
= new LinkedList ();
220 listeners
= new HashSet ();
221 defaultLocale
= Locale
.getDefault ();
224 public UIDefaults(Object
[] entries
)
226 bundles
= new LinkedList ();
227 listeners
= new HashSet ();
228 defaultLocale
= Locale
.getDefault ();
230 for (int i
= 0; (2*i
+1) < entries
.length
; ++i
)
232 put (entries
[2*i
], entries
[2*i
+1]);
236 public Object
get(Object key
)
238 return this.get (key
, getDefaultLocale ());
241 public Object
get (Object key
, Locale loc
)
245 if (super.containsKey (key
))
247 obj
= super.get (key
);
249 else if (key
instanceof String
)
251 String keyString
= (String
) key
;
252 ListIterator i
= bundles
.listIterator (0);
255 String bundle_name
= (String
) i
.next ();
257 ResourceBundle
.getBundle (bundle_name
, loc
);
262 obj
= res
.getObject (keyString
);
265 catch (MissingResourceException me
)
267 // continue, this bundle has no such key
273 // now we've found the object, resolve it.
274 // nb: LazyValues aren't supported in resource bundles, so it's correct
275 // to insert their results in the locale-less hashtable.
280 if (obj
instanceof LazyValue
)
282 Object resolved
= ((LazyValue
)obj
).createValue (this);
284 super.put (key
, resolved
);
287 else if (obj
instanceof ActiveValue
)
289 return ((ActiveValue
)obj
).createValue (this);
295 public Object
put(Object key
, Object value
)
297 Object old
= super.put (key
, value
);
298 if (key
instanceof String
&& old
!= value
)
299 firePropertyChange ((String
) key
, old
, value
);
303 public void putDefaults(Object
[] entries
)
305 for (int i
= 0; (2*i
+1) < entries
.length
; ++i
)
307 super.put (entries
[2*i
], entries
[2*i
+1]);
309 firePropertyChange ("UIDefaults", null, null);
312 public Font
getFont(Object key
)
315 return o
instanceof Font ?
(Font
) o
: null;
318 public Font
getFont(Object key
, Locale l
)
320 Object o
= get(key
, l
);
321 return o
instanceof Font ?
(Font
) o
: null;
324 public Color
getColor(Object key
)
327 return o
instanceof Color ?
(Color
) o
: null;
330 public Color
getColor(Object key
, Locale l
)
332 Object o
= get(key
, l
);
333 return o
instanceof Color ?
(Color
) o
: null;
336 public Icon
getIcon(Object key
)
339 return o
instanceof Icon ?
(Icon
) o
: null;
342 public Icon
getIcon(Object key
, Locale l
)
344 Object o
= get(key
, l
);
345 return o
instanceof Icon ?
(Icon
) o
: null;
348 public Border
getBorder(Object key
)
351 return o
instanceof Border ?
(Border
) o
: null;
354 public Border
getBorder(Object key
, Locale l
)
356 Object o
= get(key
, l
);
357 return o
instanceof Border ?
(Border
) o
: null;
360 public String
getString(Object key
)
363 return o
instanceof String ?
(String
) o
: null;
366 public String
getString(Object key
, Locale l
)
368 Object o
= get(key
, l
);
369 return o
instanceof String ?
(String
) o
: null;
372 int getInt(Object key
)
375 return o
instanceof Integer ?
((Integer
) o
).intValue() : 0;
378 int getInt(Object key
, Locale l
)
380 Object o
= get(key
, l
);
381 return o
instanceof Integer ?
((Integer
) o
).intValue() : 0;
384 public boolean getBoolean(Object key
)
386 return Boolean
.TRUE
.equals(get(key
));
389 public boolean getBoolean(Object key
, Locale l
)
391 return Boolean
.TRUE
.equals(get(key
, l
));
394 public Insets
getInsets(Object key
)
397 return o
instanceof Insets ?
(Insets
) o
: null;
400 public Insets
getInsets(Object key
, Locale l
)
402 Object o
= get(key
, l
);
403 return o
instanceof Insets ?
(Insets
) o
: null;
406 public Dimension
getDimension(Object key
)
409 return o
instanceof Dimension ?
(Dimension
) o
: null;
412 public Dimension
getDimension(Object key
, Locale l
)
414 Object o
= get(key
, l
);
415 return o
instanceof Dimension ?
(Dimension
) o
: null;
418 public Class
getUIClass(String id
, ClassLoader loader
)
420 String className
= (String
) get (id
);
421 if (className
== null)
426 return loader
.loadClass (className
);
427 return Class
.forName (className
);
435 public Class
getUIClass(String id
)
437 return getUIClass (id
, null);
440 protected void getUIError(String msg
)
442 System
.err
.println ("UIDefaults.getUIError: " + msg
);
445 public ComponentUI
getUI(JComponent target
)
447 String classId
= target
.getUIClassID ();
448 Class cls
= getUIClass (classId
);
451 getUIError ("failed to locate UI class:" + classId
);
459 factory
= cls
.getMethod ("createUI", new Class
[] { JComponent
.class } );
461 catch (NoSuchMethodException nme
)
463 getUIError ("failed to locate createUI method on " + cls
.toString ());
469 return (ComponentUI
) factory
.invoke (null, new Object
[] { target
});
471 catch (java
.lang
.reflect
.InvocationTargetException ite
)
473 getUIError ("InvocationTargetException ("+ ite
.getTargetException()
474 +") calling createUI(...) on " + cls
.toString ());
480 getUIError ("exception calling createUI(...) on " + cls
.toString ());
485 void addPropertyChangeListener(PropertyChangeListener listener
)
487 listeners
.add (listener
);
490 void removePropertyChangeListener(PropertyChangeListener listener
)
492 listeners
.remove (listener
);
495 public PropertyChangeListener
[] getPropertyChangeListeners()
497 return (PropertyChangeListener
[]) listeners
.toArray ();
500 protected void firePropertyChange(String property
, Object o
, Object n
)
502 Iterator i
= listeners
.iterator ();
503 PropertyChangeEvent pce
= new PropertyChangeEvent (this, property
, o
, n
);
506 PropertyChangeListener pcl
= (PropertyChangeListener
) i
.next ();
507 pcl
.propertyChange (pce
);
511 void addResourceBundle(String name
)
513 bundles
.addFirst (name
);
516 void removeResourceBundle(String name
)
518 bundles
.remove (name
);
521 void setDefaultLocale(Locale loc
)
526 public Locale
getDefaultLocale()
528 return defaultLocale
;
530 } // class UIDefaults