KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > LookAndFeel


1 /*
2  * $Id: LookAndFeel.java,v 1.12 2005/05/27 09:17:34 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.Resource;
19 import org.wings.SDimension;
20 import org.wings.SIcon;
21 import org.wings.SResourceIcon;
22 import org.wings.resource.ClasspathResource;
23 import org.wings.style.CSSAttributeSet;
24 import org.wings.style.CSSStyleSheet;
25 import org.wings.style.StyleSheet;
26 import org.wings.style.CSSProperty;
27
28 import java.awt.*;
29 import java.io.InputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.lang.reflect.Constructor JavaDoc;
32 import java.util.*;
33
34 /**
35  * A Look-and-Feel consists of a bunch of CGs and resource properties.
36  * wingS provides a pluggable look-and-feel (laf or plaf) concept similar to that of Swing.
37  * A certain plaf implementation normally adresses a specific browser platform.
38  *
39  * @see org.wings.plaf.ComponentCG
40  */

41 public class LookAndFeel implements Serializable JavaDoc {
42     private final transient static Log log = LogFactory.getLog(LookAndFeel.class);
43
44     private static Map wrappers = new HashMap();
45     static {
46         wrappers.put(Boolean.TYPE, Boolean JavaDoc.class);
47         wrappers.put(Character.TYPE, Character JavaDoc.class);
48         wrappers.put(Byte.TYPE, Byte JavaDoc.class);
49         wrappers.put(Short.TYPE, Short JavaDoc.class);
50         wrappers.put(Integer.TYPE, Integer JavaDoc.class);
51         wrappers.put(Long.TYPE, Long JavaDoc.class);
52         wrappers.put(Float.TYPE, Float JavaDoc.class);
53         wrappers.put(Double.TYPE, Double JavaDoc.class);
54     }
55
56     protected Properties properties;
57
58     private static final Map finalResources = Collections.synchronizedMap(new HashMap());
59
60     /**
61      * Instantiate a laf using the war's classLoader.
62      *
63      * @param properties the configuration of the laf
64      */

65     public LookAndFeel(Properties properties) {
66         this.properties = properties;
67     }
68
69     /**
70      * Return a unique string that identifies this look and feel, e.g.
71      * "konqueror"
72      */

73     public String JavaDoc getName() {
74         return properties.getProperty("lookandfeel.name");
75     }
76
77     /**
78      * Return a one line description of this look and feel implementation,
79      * e.g. "Optimized for KDE's Konqueror Browser".
80      */

81     public String JavaDoc getDescription() {
82         return properties.getProperty("lookandfeel.description");
83     }
84
85     /**
86      * create a fresh CGDefaults map. One defaults map per Session is generated
87      * in its CGManager. It is necessary to create a fresh defaults map, since
88      * it caches values that might be modified within the sessions. A prominent
89      * example of changed values per sessions are the CG's themselves:
90      * CG-properties might be changed per session...
91      *
92      * @return the laf's defaults
93      */

94     public CGDefaults createDefaults() {
95         return new ResourceFactory();
96     }
97
98     /**
99      * Create a CG instance.
100      *
101      * @param className the full qualified class name of the CG
102      * @return a new CG instance
103      */

104     public static Object JavaDoc makeCG(String JavaDoc className) {
105         Object JavaDoc result = finalResources.get(className);
106         if (result == null) {
107             try {
108                 Class JavaDoc cgClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
109                 result = cgClass.newInstance();
110                 finalResources.put(className, result);
111             } catch (Exception JavaDoc ex) {
112                 log.fatal(null, ex);
113             }
114         }
115         return result;
116     }
117
118     /**
119      * Utility method that creates an java.awt.Color from a html color hex string
120      *
121      * @return the create color
122      */

123     public static Color makeColor(String JavaDoc colorString) {
124         if (colorString != null) {
125             try {
126                 return Color.decode(colorString.trim());
127             } catch (Exception JavaDoc ex) {
128                 ex.printStackTrace();
129                 return null;
130             }
131         }
132         return null;
133     }
134
135     /**
136      * Utility method that creates an java.awt.Color from a html color hex string
137      *
138      * @return the create color
139      */

140     public static SDimension makeDimension(String JavaDoc dimensionString) {
141         if (dimensionString != null) {
142             int commaIndex = dimensionString.indexOf(',');
143             if (commaIndex > 0) {
144                 return new SDimension(dimensionString.substring(0, commaIndex),
145                         dimensionString.substring(commaIndex + 1));
146             }
147         }
148         return null;
149     }
150
151     /**
152      * Utility method that creates an Icon from a resource
153      * located realtive to the given base class. Uses the ClassLoader
154      * of the LookAndFeel
155      *
156      * @param fileName of the image file
157      * @return a newly allocated Icon
158      */

159     public static SIcon makeIcon(String JavaDoc fileName) {
160         SIcon result = (SIcon) finalResources.get(fileName);
161         if (result == null) {
162             result = new SResourceIcon(fileName);
163             finalResources.put(fileName, result);
164         }
165         return result;
166     }
167
168     /**
169      * Utility method that creates an CSSPropertySet from a String
170      *
171      * @param string attributes string
172      * @return a newly allocated CSSPropertySet
173      */

174     public static CSSAttributeSet makeAttributeSet(String JavaDoc string) {
175         CSSAttributeSet attributes = new CSSAttributeSet();
176         StringTokenizer tokens = new StringTokenizer(string, ";");
177         while (tokens.hasMoreTokens()) {
178             String JavaDoc token = tokens.nextToken();
179             int pos = token.indexOf(":");
180             if (pos >= 0) {
181                 attributes.put(new CSSProperty(token.substring(0, pos)), token.substring(pos + 1));
182             }
183         }
184         return attributes;
185     }
186
187     /**
188      * Utility method that creates a styleSheet from a string
189      *
190      * @param resourceName styleSheet as a string
191      * @return the styleSheet
192      */

193     public static Resource makeResource(String JavaDoc resourceName) {
194         Resource result = (Resource) finalResources.get(resourceName);
195         if (result == null) {
196             result = new ClasspathResource(resourceName);
197             finalResources.put(resourceName, result);
198         }
199         return result;
200     }
201
202     /**
203      * Utility method that creates a stylesheet object from a resource
204      *
205      * @return the styleSheet
206      */

207     public static StyleSheet makeStyleSheet(String JavaDoc resourceName) {
208         try {
209             CSSStyleSheet result = new CSSStyleSheet();
210             InputStream JavaDoc in = LookAndFeel.class.getClassLoader().getResourceAsStream(resourceName);
211             result.read(in);
212             in.close();
213             return result;
214         } catch (Exception JavaDoc e) {
215             log.warn("Exception", e);
216         }
217         return null;
218     }
219
220     /**
221      * Utility method that creates an Object of class <code>clazz</code>
222      * using the single String arg constructor.
223      *
224      * @param value object as a string
225      * @param clazz class of the object
226      * @return the object
227      */

228     public static Object JavaDoc makeObject(String JavaDoc value, Class JavaDoc clazz) {
229         Object JavaDoc result;
230         try {
231             if (value.startsWith("new ")) {
232                 int bracket = value.indexOf("(");
233                 String JavaDoc name = value.substring("new ".length(), bracket);
234                 clazz = Class.forName(name, true, Thread.currentThread().getContextClassLoader());
235                 result = clazz.newInstance();
236             } else {
237                 if (clazz.isPrimitive())
238                     clazz = (Class JavaDoc) wrappers.get(clazz);
239                 Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
240                 result = constructor.newInstance(new Object JavaDoc[]{value});
241             }
242         } catch (NoSuchMethodException JavaDoc e) {
243             log.fatal(value + " : " + clazz.getName()
244                     + " doesn't have a single String arg constructor", e);
245             result = null;
246         } catch (Exception JavaDoc e) {
247             log.error(e.getClass().getName() + " : " + value, e);
248             result = null;
249         }
250         return result;
251     }
252
253     /**
254      * Returns a string that displays and identifies this
255      * object's properties.
256      *
257      * @return a String representation of this object
258      */

259     public String JavaDoc toString() {
260         return "[" + getDescription() + " - " + getClass().getName() + "]";
261     }
262
263
264     class ResourceFactory extends CGDefaults {
265
266         public ResourceFactory() {
267             super(null);
268         }
269
270         public Object JavaDoc get(Object JavaDoc key, Class JavaDoc type) {
271             Object JavaDoc value = get(key);
272             if (value != null)
273                 return value;
274
275             String JavaDoc property;
276             if (key instanceof Class JavaDoc) {
277                 Class JavaDoc clazz = (Class JavaDoc) key;
278                 do {
279                     property = properties.getProperty(clazz.getName());
280                     clazz = clazz.getSuperclass();
281                 } while (property == null && clazz != null);
282             } else
283                 property = properties.getProperty(key.toString());
284
285             if (property == null) {
286                 put(key, null);
287                 return null;
288             }
289
290             if (ComponentCG.class.isAssignableFrom(type) || LayoutCG.class.isAssignableFrom(type) || PrefixAndSuffixDelegate.class.isAssignableFrom(type))
291                 value = makeCG(property);
292             else if (type.isAssignableFrom(SIcon.class))
293                 value = makeIcon(property);
294             else if (type.isAssignableFrom(Resource.class))
295                 value = makeResource(property);
296             else if (type.isAssignableFrom(CSSAttributeSet.class))
297                 value = makeAttributeSet(property);
298             else if (type.isAssignableFrom(StyleSheet.class))
299                 value = makeStyleSheet(property);
300             else if (type.isAssignableFrom(Color.class))
301                 value = makeColor(property);
302             else if (type.isAssignableFrom(SDimension.class))
303                 value = makeDimension(property);
304             else
305                 value = makeObject(property, type);
306
307             put(key, value);
308             return value;
309         }
310     }
311 }
312
313
314
Popular Tags