OSDN Git Service

* javax/naming/CompoundName.java (CompoundName): Don't check for
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / UIDefaults.java
1 /* UIDefaults.java -- database for all settings and interface bindings.
2    Copyright (C) 2002, 2004  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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
19 02111-1307 USA.
20
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.
25
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. */
37
38 package javax.swing;
39
40 import java.awt.Color;
41 import java.awt.Dimension;
42 import java.awt.Font;
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;
54 import java.util.Set;
55
56 import javax.swing.border.Border;
57 import javax.swing.plaf.ComponentUI;
58
59 /**
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().
63  *
64  * @author Ronald Veldema (rveldema@cs.vu.nl)
65  */
66 public class UIDefaults extends Hashtable
67 {
68   private LinkedList bundles;
69   private Locale defaultLocale;
70   private PropertyChangeSupport propertyChangeSupport;
71
72   public interface ActiveValue
73   {
74     Object createValue(UIDefaults table);
75   }
76
77   public static class LazyInputMap implements LazyValue
78   {
79     Object[] bind;
80     public LazyInputMap(Object[] bindings)
81     {
82       bind = bindings;
83     }
84     public Object createValue(UIDefaults table)
85     {
86       InputMap im = new InputMap ();
87       for (int i = 0; 2*i+1 < bind.length; ++i)
88         {
89           im.put (KeyStroke.getKeyStroke ((String) bind[2*i]),
90                   bind[2*i+1]);
91         }
92       return im;
93     }
94   }
95
96   public interface LazyValue
97   {
98     Object createValue(UIDefaults table);
99   }
100
101   public static class ProxyLazyValue implements LazyValue
102   {
103     LazyValue inner;
104     public ProxyLazyValue(String s)
105     {
106       final String className = s;
107       inner = new LazyValue ()
108         { 
109           public Object createValue (UIDefaults table) 
110           {
111             try
112               {
113                 return Class
114                   .forName (className)
115                   .getConstructor (new Class[] {})
116                   .newInstance (new Object[] {});
117               }
118             catch (Exception e)
119               {
120                 return null;
121               }
122           }
123         };
124     }
125
126     public ProxyLazyValue(String c, String m)
127     {
128       final String className = c;
129       final String methodName = m;
130       inner = new LazyValue ()
131         { 
132           public Object createValue (UIDefaults table) 
133           {
134             try 
135               {                
136                 return Class
137                   .forName (className)
138                   .getMethod (methodName, new Class[] {})
139                   .invoke (null, new Object[] {});
140               }
141             catch (Exception e)
142               {
143                 return null;
144               }
145           }
146         };
147     }
148     
149     public ProxyLazyValue (String c, Object[] os)
150     {
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)
155         {
156           clss[i] = objs[i].getClass ();
157         }      
158       inner = new LazyValue ()
159         { 
160           public Object createValue (UIDefaults table) 
161           {            
162             try
163               {
164                 return Class
165                   .forName (className)
166                   .getConstructor (clss)
167                   .newInstance (objs);
168     }
169             catch (Exception e)
170     {
171                 return null;
172               }
173           }
174         };
175     }
176
177     public ProxyLazyValue (String c, String m, Object[] os)
178     {
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)
184     {
185           clss[i] = objs[i].getClass ();
186     }
187       inner = new LazyValue ()
188         { 
189     public Object createValue(UIDefaults table)
190     {
191             try 
192               {
193                 return Class
194                   .forName (className)
195                   .getMethod (methodName, clss)
196                   .invoke (null, objs);
197               }
198             catch (Exception e)
199               {
200                 return null;
201               }
202           }
203         };
204     }
205     
206     public Object createValue (UIDefaults table)
207     {
208       return inner.createValue (table);
209     }
210   }
211
212   private static final long serialVersionUID = 7341222528856548117L;
213
214   public UIDefaults()
215   {
216     bundles = new LinkedList ();
217     defaultLocale = Locale.getDefault ();
218     propertyChangeSupport = new PropertyChangeSupport(this);
219   }
220
221   public UIDefaults(Object[] entries)
222   {
223     this();
224
225     for (int i = 0; (2*i+1) < entries.length; ++i)
226         put (entries[2*i], entries[2*i+1]);
227       }
228
229   public Object get(Object key)
230   {
231     return this.get (key, getDefaultLocale ());
232   }
233
234   public Object get (Object key, Locale loc)
235   {
236     Object obj = null;
237
238     if (super.containsKey (key))
239       {
240         obj = super.get (key);
241       }
242     else if (key instanceof String)
243       {
244         String keyString = (String) key;
245         ListIterator i = bundles.listIterator (0);
246         while (i.hasNext ())
247   {
248             String bundle_name = (String) i.next ();
249             ResourceBundle res =
250               ResourceBundle.getBundle (bundle_name, loc);
251             if (res != null)
252               {
253                 try 
254                   {                    
255                     obj = res.getObject (keyString);
256                     break;
257                   }
258                 catch (MissingResourceException me)
259                   {
260                     // continue, this bundle has no such key
261                   }
262               }
263           }
264       }
265
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.
269
270     if (obj == null)
271       return null;
272
273     if (obj instanceof LazyValue)
274       {
275         Object resolved = ((LazyValue)obj).createValue (this);
276         super.remove (key);
277         super.put (key, resolved);
278         return resolved;
279       }
280     else if (obj instanceof ActiveValue)
281       {
282         return ((ActiveValue)obj).createValue (this);
283       }    
284
285     return obj;
286   }
287
288   public Object put(Object key, Object value)
289   {
290     Object old = super.put (key, value);
291     if (key instanceof String && old != value)
292       firePropertyChange ((String) key, old, value);
293     return old;
294   }
295
296   public void putDefaults(Object[] entries)
297   {
298     for (int i = 0; (2*i+1) < entries.length; ++i)
299   {
300         super.put (entries[2*i], entries[2*i+1]);
301       }
302     firePropertyChange ("UIDefaults", null, null);
303   }
304
305   public Font getFont(Object key)
306   {
307     Object o = get(key);
308     return o instanceof Font ? (Font) o : null;
309   }
310
311   public Font getFont(Object key, Locale l)
312   {
313     Object o = get(key, l);
314     return o instanceof Font ? (Font) o : null;
315   }
316
317   public Color getColor(Object key)
318   {
319     Object o = get(key);
320     return o instanceof Color ? (Color) o : null;
321   }
322
323   public Color getColor(Object key, Locale l)
324   {
325     Object o = get(key, l);
326     return o instanceof Color ? (Color) o : null;
327   }
328
329   public Icon getIcon(Object key)
330   {
331     Object o = get(key);
332     return o instanceof Icon ? (Icon) o : null;
333   }
334
335   public Icon getIcon(Object key, Locale l)
336   {
337     Object o = get(key, l);
338     return o instanceof Icon ? (Icon) o : null;
339   }
340
341   public Border getBorder(Object key)
342   {
343     Object o = get(key);
344     return o instanceof Border ? (Border) o : null;
345   }
346
347   public Border getBorder(Object key, Locale l)
348   {
349     Object o = get(key, l);
350     return o instanceof Border ? (Border) o : null;
351   }
352
353   public String getString(Object key)
354   {
355     Object o = get(key);
356     return o instanceof String ? (String) o : null;
357   }
358
359   public String getString(Object key, Locale l)
360   {
361     Object o = get(key, l);
362     return o instanceof String ? (String) o : null;
363   }
364
365   public int getInt(Object key)
366   {
367     Object o = get(key);
368     return o instanceof Integer ? ((Integer) o).intValue() : 0;
369   }
370
371   public int getInt(Object key, Locale l)
372   {
373     Object o = get(key, l);
374     return o instanceof Integer ? ((Integer) o).intValue() : 0;
375   }
376
377   public boolean getBoolean(Object key)
378   {
379     return Boolean.TRUE.equals(get(key));
380   }
381
382   public boolean getBoolean(Object key, Locale l)
383   {
384     return Boolean.TRUE.equals(get(key, l));
385   }
386
387   public Insets getInsets(Object key) 
388   {
389     Object o = get(key);
390     return o instanceof Insets ? (Insets) o : null;
391   }
392
393   public Insets getInsets(Object key, Locale l) 
394   {
395     Object o = get(key, l);
396     return o instanceof Insets ? (Insets) o : null;
397   }
398
399   public Dimension getDimension(Object key) 
400   {
401     Object o = get(key);
402     return o instanceof Dimension ? (Dimension) o : null;
403   }
404
405   public Dimension getDimension(Object key, Locale l) 
406   {
407     Object o = get(key, l);
408     return o instanceof Dimension ? (Dimension) o : null;
409   }
410
411   public Class getUIClass(String id, ClassLoader loader)
412   {
413     String className = (String) get (id);
414     if (className == null)
415       return null;
416     try 
417       {
418         if (loader != null)
419           return loader.loadClass (className);    
420         return Class.forName (className);
421       }
422     catch (Exception e)
423       {
424         return null;
425       }
426   }
427
428   public Class getUIClass(String id)
429   {
430     return getUIClass (id, null);
431   }
432
433   protected void getUIError(String msg)
434   {
435     System.err.println ("UIDefaults.getUIError: " + msg);
436   }
437
438   public ComponentUI getUI(JComponent target)
439   {
440     String classId = target.getUIClassID ();
441     Class cls = getUIClass (classId);
442     if (cls == null)
443       {
444         getUIError ("failed to locate UI class:" + classId);
445         return null;
446       }
447
448     Method factory;
449
450     try 
451       {
452         factory = cls.getMethod ("createUI", new Class[] { JComponent.class } );
453       }
454     catch (NoSuchMethodException nme)
455       {
456         getUIError ("failed to locate createUI method on " + cls.toString ());
457         return null;
458   }
459
460     try
461   {
462         return (ComponentUI) factory.invoke (null, new Object[] { target });
463   }
464     catch (java.lang.reflect.InvocationTargetException ite)
465         {
466         getUIError ("InvocationTargetException ("+ ite.getTargetException() 
467                     +") calling createUI(...) on " + cls.toString ());
468         return null;        
469         }
470     catch (Exception e)
471   {
472         getUIError ("exception calling createUI(...) on " + cls.toString ());
473         return null;        
474       }
475   }
476
477   public void addPropertyChangeListener(PropertyChangeListener listener)
478   {
479     propertyChangeSupport.addPropertyChangeListener(listener);
480   }
481
482   void removePropertyChangeListener(PropertyChangeListener listener)
483   {
484     propertyChangeSupport.removePropertyChangeListener(listener);
485   }
486
487   public PropertyChangeListener[] getPropertyChangeListeners()
488   {
489     return propertyChangeSupport.getPropertyChangeListeners();
490   }
491
492   protected void firePropertyChange(String property,
493                                     Object oldValue, Object newValue)
494   {
495     propertyChangeSupport.firePropertyChange(property, oldValue, newValue);
496   }
497
498   public void addResourceBundle(String name)
499   {
500     bundles.addFirst (name);
501   }
502
503   public void removeResourceBundle(String name)
504   {
505     bundles.remove (name);
506   }
507
508   public void setDefaultLocale(Locale loc)
509   {
510     defaultLocale = loc;
511   }
512
513   public Locale getDefaultLocale()
514   {
515     return defaultLocale;
516   }
517 }