41 |
import java.awt.Dimension; |
import java.awt.Dimension; |
42 |
import java.awt.Font; |
import java.awt.Font; |
43 |
import java.awt.Insets; |
import java.awt.Insets; |
44 |
|
import java.beans.PropertyChangeEvent; |
45 |
import java.beans.PropertyChangeListener; |
import java.beans.PropertyChangeListener; |
46 |
|
import java.lang.reflect.Method; |
47 |
|
import java.lang.reflect.Constructor; |
48 |
import java.util.Hashtable; |
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; |
import java.util.Locale; |
54 |
|
import java.util.Set; |
55 |
|
import java.util.HashSet; |
56 |
|
import java.util.MissingResourceException; |
57 |
|
import java.util.ResourceBundle; |
58 |
import javax.swing.border.Border; |
import javax.swing.border.Border; |
59 |
import javax.swing.plaf.ComponentUI; |
import javax.swing.plaf.ComponentUI; |
60 |
|
|
67 |
*/ |
*/ |
68 |
public class UIDefaults extends Hashtable |
public class UIDefaults extends Hashtable |
69 |
{ |
{ |
70 |
|
|
71 |
|
LinkedList bundles; |
72 |
|
Set listeners; |
73 |
|
Locale defaultLocale; |
74 |
|
|
75 |
interface ActiveValue |
interface ActiveValue |
76 |
{ |
{ |
77 |
Object createValue(UIDefaults table); |
Object createValue(UIDefaults table); |
79 |
|
|
80 |
public static class LazyInputMap implements LazyValue |
public static class LazyInputMap implements LazyValue |
81 |
{ |
{ |
82 |
|
Object[] bind; |
83 |
public LazyInputMap(Object[] bindings) |
public LazyInputMap(Object[] bindings) |
84 |
{ |
{ |
85 |
|
bind = bindings; |
86 |
} |
} |
87 |
public Object createValue(UIDefaults table) |
public Object createValue(UIDefaults table) |
88 |
{ |
{ |
89 |
throw new Error("not implemented"); |
InputMap im = new InputMap (); |
90 |
|
for (int i = 0; 2*i+1 < bind.length; ++i) |
91 |
|
{ |
92 |
|
im.put (KeyStroke.getKeyStroke ((String) bind[2*i]), |
93 |
|
bind[2*i+1]); |
94 |
|
} |
95 |
|
return im; |
96 |
} |
} |
97 |
} // class LazyInputMap |
} // class LazyInputMap |
98 |
|
|
103 |
|
|
104 |
public static class ProxyLazyValue implements LazyValue |
public static class ProxyLazyValue implements LazyValue |
105 |
{ |
{ |
106 |
|
LazyValue inner; |
107 |
public ProxyLazyValue(String s) |
public ProxyLazyValue(String s) |
108 |
{ |
{ |
109 |
throw new Error("not implemented"); |
final String className = s; |
110 |
|
inner = new LazyValue () |
111 |
|
{ |
112 |
|
public Object createValue (UIDefaults table) |
113 |
|
{ |
114 |
|
try |
115 |
|
{ |
116 |
|
return Class |
117 |
|
.forName (className) |
118 |
|
.getConstructor (new Class[] {}) |
119 |
|
.newInstance (new Object[] {}); |
120 |
|
} |
121 |
|
catch (Exception e) |
122 |
|
{ |
123 |
|
return null; |
124 |
|
} |
125 |
|
} |
126 |
|
}; |
127 |
} |
} |
128 |
|
|
129 |
public ProxyLazyValue(String c, String m) |
public ProxyLazyValue(String c, String m) |
130 |
{ |
{ |
131 |
throw new Error("not implemented"); |
final String className = c; |
132 |
|
final String methodName = m; |
133 |
|
inner = new LazyValue () |
134 |
|
{ |
135 |
|
public Object createValue (UIDefaults table) |
136 |
|
{ |
137 |
|
try |
138 |
|
{ |
139 |
|
return Class |
140 |
|
.forName (className) |
141 |
|
.getMethod (methodName, new Class[] {}) |
142 |
|
.invoke (null, new Object[] {}); |
143 |
|
} |
144 |
|
catch (Exception e) |
145 |
|
{ |
146 |
|
return null; |
147 |
|
} |
148 |
|
} |
149 |
|
}; |
150 |
|
} |
151 |
|
|
152 |
|
public ProxyLazyValue (String c, Object[] os) |
153 |
|
{ |
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) |
158 |
|
{ |
159 |
|
clss[i] = objs[i].getClass (); |
160 |
|
} |
161 |
|
inner = new LazyValue () |
162 |
|
{ |
163 |
|
public Object createValue (UIDefaults table) |
164 |
|
{ |
165 |
|
try |
166 |
|
{ |
167 |
|
return Class |
168 |
|
.forName (className) |
169 |
|
.getConstructor (clss) |
170 |
|
.newInstance (objs); |
171 |
} |
} |
172 |
public ProxyLazyValue(String c, Object[] o) |
catch (Exception e) |
173 |
{ |
{ |
174 |
throw new Error("not implemented"); |
return null; |
175 |
|
} |
176 |
|
} |
177 |
|
}; |
178 |
} |
} |
179 |
public ProxyLazyValue(String c, String m, Object[] o) |
|
180 |
|
public ProxyLazyValue (String c, String m, Object[] os) |
181 |
|
{ |
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) |
187 |
{ |
{ |
188 |
throw new Error("not implemented"); |
clss[i] = objs[i].getClass (); |
189 |
} |
} |
190 |
|
inner = new LazyValue () |
191 |
|
{ |
192 |
public Object createValue(UIDefaults table) |
public Object createValue(UIDefaults table) |
193 |
{ |
{ |
194 |
throw new Error("not implemented"); |
try |
195 |
|
{ |
196 |
|
return Class |
197 |
|
.forName (className) |
198 |
|
.getMethod (methodName, clss) |
199 |
|
.invoke (null, objs); |
200 |
|
} |
201 |
|
catch (Exception e) |
202 |
|
{ |
203 |
|
return null; |
204 |
|
} |
205 |
|
} |
206 |
|
}; |
207 |
|
} |
208 |
|
|
209 |
|
public Object createValue (UIDefaults table) |
210 |
|
{ |
211 |
|
return inner.createValue (table); |
212 |
} |
} |
213 |
} // class ProxyLazyValue |
} // class ProxyLazyValue |
214 |
|
|
216 |
|
|
217 |
public UIDefaults() |
public UIDefaults() |
218 |
{ |
{ |
219 |
|
bundles = new LinkedList (); |
220 |
|
listeners = new HashSet (); |
221 |
|
defaultLocale = Locale.getDefault (); |
222 |
} |
} |
223 |
|
|
224 |
public UIDefaults(Object[] entries) |
public UIDefaults(Object[] entries) |
225 |
{ |
{ |
226 |
// XXX |
bundles = new LinkedList (); |
227 |
|
listeners = new HashSet (); |
228 |
|
defaultLocale = Locale.getDefault (); |
229 |
|
|
230 |
|
for (int i = 0; (2*i+1) < entries.length; ++i) |
231 |
|
{ |
232 |
|
put (entries[2*i], entries[2*i+1]); |
233 |
|
} |
234 |
} |
} |
235 |
|
|
236 |
public Object get(Object key) |
public Object get(Object key) |
237 |
{ |
{ |
238 |
// XXX Obey 1.4 specs |
return this.get (key, getDefaultLocale ()); |
|
return super.get(key); |
|
239 |
} |
} |
240 |
|
|
241 |
public Object get(Object key, Locale l) |
public Object get (Object key, Locale loc) |
242 |
{ |
{ |
243 |
throw new Error("not implemented"); |
Object obj = null; |
244 |
|
|
245 |
|
if (super.containsKey (key)) |
246 |
|
{ |
247 |
|
obj = super.get (key); |
248 |
|
} |
249 |
|
else if (key instanceof String) |
250 |
|
{ |
251 |
|
String keyString = (String) key; |
252 |
|
ListIterator i = bundles.listIterator (0); |
253 |
|
while (i.hasNext ()) |
254 |
|
{ |
255 |
|
String bundle_name = (String) i.next (); |
256 |
|
ResourceBundle res = |
257 |
|
ResourceBundle.getBundle (bundle_name, loc); |
258 |
|
if (res != null) |
259 |
|
{ |
260 |
|
try |
261 |
|
{ |
262 |
|
obj = res.getObject (keyString); |
263 |
|
break; |
264 |
|
} |
265 |
|
catch (MissingResourceException me) |
266 |
|
{ |
267 |
|
// continue, this bundle has no such key |
268 |
|
} |
269 |
|
} |
270 |
|
} |
271 |
|
} |
272 |
|
|
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. |
276 |
|
|
277 |
|
if (obj == null) |
278 |
|
return null; |
279 |
|
|
280 |
|
if (obj instanceof LazyValue) |
281 |
|
{ |
282 |
|
Object resolved = ((LazyValue)obj).createValue (this); |
283 |
|
super.remove (key); |
284 |
|
super.put (key, resolved); |
285 |
|
return resolved; |
286 |
|
} |
287 |
|
else if (obj instanceof ActiveValue) |
288 |
|
{ |
289 |
|
return ((ActiveValue)obj).createValue (this); |
290 |
|
} |
291 |
|
|
292 |
|
return obj; |
293 |
} |
} |
294 |
|
|
295 |
public Object put(Object key, Object value) |
public Object put(Object key, Object value) |
296 |
{ |
{ |
297 |
throw new Error("not implemented"); |
Object old = super.put (key, value); |
298 |
|
if (key instanceof String && old != value) |
299 |
|
firePropertyChange ((String) key, old, value); |
300 |
|
return old; |
301 |
} |
} |
302 |
|
|
303 |
public void putDefaults(Object[] list) |
public void putDefaults(Object[] entries) |
304 |
|
{ |
305 |
|
for (int i = 0; (2*i+1) < entries.length; ++i) |
306 |
{ |
{ |
307 |
throw new Error("not implemented"); |
super.put (entries[2*i], entries[2*i+1]); |
308 |
|
} |
309 |
|
firePropertyChange ("UIDefaults", null, null); |
310 |
} |
} |
311 |
|
|
312 |
public Font getFont(Object key) |
public Font getFont(Object key) |
417 |
|
|
418 |
public Class getUIClass(String id, ClassLoader loader) |
public Class getUIClass(String id, ClassLoader loader) |
419 |
{ |
{ |
420 |
throw new Error("not implemented"); |
String className = (String) get (id); |
421 |
|
if (className == null) |
422 |
|
return null; |
423 |
|
try |
424 |
|
{ |
425 |
|
if (loader != null) |
426 |
|
return loader.loadClass (className); |
427 |
|
return Class.forName (className); |
428 |
|
} |
429 |
|
catch (Exception e) |
430 |
|
{ |
431 |
|
return null; |
432 |
|
} |
433 |
} |
} |
434 |
|
|
435 |
public Class getUIClass(String id) |
public Class getUIClass(String id) |
436 |
{ |
{ |
437 |
throw new Error("not implemented"); |
return getUIClass (id, null); |
438 |
} |
} |
439 |
|
|
440 |
protected void getUIError(String msg) |
protected void getUIError(String msg) |
441 |
{ |
{ |
442 |
// Does nothing unless overridden. |
System.err.println ("UIDefaults.getUIError: " + msg); |
443 |
|
} |
444 |
|
|
445 |
|
public ComponentUI getUI(JComponent target) |
446 |
|
{ |
447 |
|
String classId = target.getUIClassID (); |
448 |
|
Class cls = getUIClass (classId); |
449 |
|
if (cls == null) |
450 |
|
{ |
451 |
|
getUIError ("failed to locate UI class:" + classId); |
452 |
|
return null; |
453 |
|
} |
454 |
|
|
455 |
|
Method factory; |
456 |
|
|
457 |
|
try |
458 |
|
{ |
459 |
|
factory = cls.getMethod ("createUI", new Class[] { JComponent.class } ); |
460 |
|
} |
461 |
|
catch (NoSuchMethodException nme) |
462 |
|
{ |
463 |
|
getUIError ("failed to locate createUI method on " + cls.toString ()); |
464 |
|
return null; |
465 |
|
} |
466 |
|
|
467 |
|
try |
468 |
|
{ |
469 |
|
return (ComponentUI) factory.invoke (null, new Object[] { target }); |
470 |
} |
} |
471 |
|
catch (java.lang.reflect.InvocationTargetException ite) |
472 |
|
{ |
473 |
|
getUIError ("InvocationTargetException ("+ ite.getTargetException() |
474 |
|
+") calling createUI(...) on " + cls.toString ()); |
475 |
|
return null; |
476 |
|
|
477 |
public ComponentUI getUI(JComponent a) |
} |
478 |
|
catch (Exception e) |
479 |
{ |
{ |
480 |
String pp = a.getUIClassID(); |
getUIError ("exception calling createUI(...) on " + cls.toString ()); |
481 |
ComponentUI p = (ComponentUI) get(pp); |
return null; |
482 |
if (p == null) |
} |
|
getUIError("failed to locate UI:" + pp); |
|
|
return p; |
|
483 |
} |
} |
484 |
|
|
485 |
void addPropertyChangeListener(PropertyChangeListener l) |
void addPropertyChangeListener(PropertyChangeListener listener) |
486 |
{ |
{ |
487 |
throw new Error("not implemented"); |
listeners.add (listener); |
488 |
} |
} |
489 |
|
|
490 |
void removePropertyChangeListener(PropertyChangeListener l) |
void removePropertyChangeListener(PropertyChangeListener listener) |
491 |
{ |
{ |
492 |
throw new Error("not implemented"); |
listeners.remove (listener); |
493 |
} |
} |
494 |
|
|
495 |
public PropertyChangeListener[] getPropertyChangeListeners() |
public PropertyChangeListener[] getPropertyChangeListeners() |
496 |
{ |
{ |
497 |
throw new Error("not implemented"); |
return (PropertyChangeListener[]) listeners.toArray (); |
498 |
} |
} |
499 |
|
|
500 |
protected void firePropertyChange(String property, Object o, Object n) |
protected void firePropertyChange(String property, Object o, Object n) |
501 |
{ |
{ |
502 |
throw new Error("not implemented"); |
Iterator i = listeners.iterator (); |
503 |
|
PropertyChangeEvent pce = new PropertyChangeEvent (this, property, o, n); |
504 |
|
while (i.hasNext ()) |
505 |
|
{ |
506 |
|
PropertyChangeListener pcl = (PropertyChangeListener) i.next (); |
507 |
|
pcl.propertyChange (pce); |
508 |
|
} |
509 |
} |
} |
510 |
|
|
511 |
void addResourceBundle(String name) |
void addResourceBundle(String name) |
512 |
{ |
{ |
513 |
throw new Error("not implemented"); |
bundles.addFirst (name); |
514 |
} |
} |
515 |
|
|
516 |
void removeResourceBundle(String name) |
void removeResourceBundle(String name) |
517 |
{ |
{ |
518 |
throw new Error("not implemented"); |
bundles.remove (name); |
519 |
} |
} |
520 |
|
|
521 |
void setDefaultLocale(Locale l) |
void setDefaultLocale(Locale loc) |
522 |
{ |
{ |
523 |
throw new Error("not implemented"); |
defaultLocale = loc; |
524 |
} |
} |
525 |
|
|
526 |
public Locale getDefaultLocale() |
public Locale getDefaultLocale() |
527 |
{ |
{ |
528 |
throw new Error("not implemented"); |
return defaultLocale; |
529 |
} |
} |
530 |
} // class UIDefaults |
} // class UIDefaults |