001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013:
014: package org.wings.plaf;
015:
016: import java.awt.Color;
017: import java.io.InputStream;
018: import java.lang.reflect.Constructor;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Properties;
022: import java.util.StringTokenizer;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.wings.Resource;
027: import org.wings.SDimension;
028: import org.wings.SIcon;
029: import org.wings.SResourceIcon;
030: import org.wings.resource.ClassPathResource;
031: import org.wings.style.CSSAttributeSet;
032: import org.wings.style.CSSProperty;
033: import org.wings.style.CSSStyleSheet;
034: import org.wings.style.StyleSheet;
035:
036: public class ResourceFactory {
037: private final transient static Log log = LogFactory
038: .getLog(ResourceFactory.class);
039:
040: private static final Map WRAPPERS = new HashMap();
041: static {
042: WRAPPERS.put(Boolean.TYPE, Boolean.class);
043: WRAPPERS.put(Character.TYPE, Character.class);
044: WRAPPERS.put(Byte.TYPE, Byte.class);
045: WRAPPERS.put(Short.TYPE, Short.class);
046: WRAPPERS.put(Integer.TYPE, Integer.class);
047: WRAPPERS.put(Long.TYPE, Long.class);
048: WRAPPERS.put(Float.TYPE, Float.class);
049: WRAPPERS.put(Double.TYPE, Double.class);
050: }
051:
052: private final Properties properties;
053:
054: public ResourceFactory(Properties properties) {
055: this .properties = properties;
056: }
057:
058: public Object get(Object key, Class type) {
059: String property;
060: if (key instanceof Class) {
061: Class clazz = (Class) key;
062: do {
063: property = properties.getProperty(clazz.getName());
064: clazz = clazz.getSuperclass();
065: } while (property == null && clazz != null);
066: } else
067: property = properties.getProperty(key.toString());
068:
069: if (property == null)
070: return null;
071: if (ComponentCG.class.isAssignableFrom(type))
072: return makeComponentCG(property);
073: else if (LayoutCG.class.isAssignableFrom(type))
074: return makeLayoutCG(property);
075: else if (type.isAssignableFrom(SIcon.class))
076: return makeIcon(property);
077: else if (type.isAssignableFrom(Resource.class))
078: return makeResource(property);
079: else if (type.isAssignableFrom(CSSAttributeSet.class))
080: return makeAttributeSet(property);
081: else if (type.isAssignableFrom(StyleSheet.class))
082: return makeStyleSheet(property);
083: else if (type.isAssignableFrom(Color.class))
084: return makeColor(property);
085: else if (type.isAssignableFrom(SDimension.class))
086: return makeDimension(property);
087: else if (type.isAssignableFrom(Boolean.class))
088: return makeBoolean(property);
089: else if (type.isAssignableFrom(Class.class))
090: return makeClass(property);
091: else
092: return makeObject(property, type);
093: }
094:
095: private static Map instances = new HashMap();
096:
097: private static Object sharedInstance(String className) {
098: Object cg = instances.get(className);
099: if (cg == null) {
100: try {
101: Class cgClass = Class.forName(className, true, Thread
102: .currentThread().getContextClassLoader());
103: cg = cgClass.newInstance();
104: instances.put(className, cg);
105: } catch (Exception ex) {
106: throw new RuntimeException(ex);
107: }
108: }
109: return cg;
110: }
111:
112: /**
113: * Create a CG instance.
114: *
115: * @param className the full qualified class name of the CG
116: * @return a new CG instance
117: */
118: public static ComponentCG makeComponentCG(String className) {
119: return (ComponentCG) sharedInstance(className);
120: }
121:
122: /**
123: * Create a CG instance.
124: *
125: * @param className the full qualified class name of the CG
126: * @return a new CG instance
127: */
128: public static LayoutCG makeLayoutCG(String className) {
129: return (LayoutCG) sharedInstance(className);
130: }
131:
132: /**
133: * Utility method that creates an java.awt.Color from a html color hex string
134: *
135: * @return the create color
136: */
137: public static Color makeColor(String colorString) {
138: if (colorString != null) {
139: try {
140: return Color.decode(colorString.trim());
141: } catch (Exception ex) {
142: throw new RuntimeException(ex);
143: }
144: }
145: return null;
146: }
147:
148: /**
149: * Utility method that creates a dimension from a dimension string separated by comma
150: *
151: * @return the create color
152: */
153: public static SDimension makeDimension(String dimensionString) {
154: if (dimensionString != null) {
155: int commaIndex = dimensionString.indexOf(',');
156: if (commaIndex > 0) {
157: return new SDimension(dimensionString.substring(0,
158: commaIndex), dimensionString
159: .substring(commaIndex + 1));
160: }
161: }
162: return null;
163: }
164:
165: /**
166: * Utility method that creates an Icon from a resource
167: * located relative to the given base class. Uses the ClassLoader
168: * of the LookAndFeel
169: *
170: * @param fileName of the image file
171: * @return a newly allocated Icon
172: */
173: public static SIcon makeIcon(String fileName) {
174: return new SResourceIcon(fileName);
175: }
176:
177: /**
178: * Utility method that creates a Boolean from a String
179: * Uses the ClassLoader of the LookAndFeel.
180: *
181: * @param bool The Boolean as String
182: * @return a newly allocated Icon
183: */
184: public static Boolean makeBoolean(String bool) {
185: return Boolean.valueOf(bool);
186: }
187:
188: /**
189: * Utility method that creates an CSSPropertySet from a String
190: *
191: * @param string attributes string
192: * @return a newly allocated CSSPropertySet
193: */
194: public static CSSAttributeSet makeAttributeSet(String string) {
195: CSSAttributeSet attributes = new CSSAttributeSet();
196: StringTokenizer tokens = new StringTokenizer(string, ";");
197: while (tokens.hasMoreTokens()) {
198: String token = tokens.nextToken();
199: int pos = token.indexOf(":");
200: if (pos >= 0) {
201: attributes.put(
202: new CSSProperty(token.substring(0, pos)), token
203: .substring(pos + 1));
204: }
205: }
206: return attributes;
207: }
208:
209: /**
210: * Utility method that creates a styleSheet from a string
211: *
212: * @param resourceName styleSheet as a string
213: * @return the styleSheet
214: */
215: public static Resource makeResource(String resourceName) {
216: return new ClassPathResource(resourceName);
217: }
218:
219: /**
220: * Utility method that creates a stylesheet object from a resource
221: *
222: * @return the styleSheet
223: */
224: public static StyleSheet makeStyleSheet(String resourceName) {
225: try {
226: CSSStyleSheet result = new CSSStyleSheet();
227: InputStream in = Thread.currentThread()
228: .getContextClassLoader().getResourceAsStream(
229: resourceName);
230: result.read(in);
231: in.close();
232: return result;
233: } catch (Exception e) {
234: throw new RuntimeException(e);
235: }
236: }
237:
238: /**
239: * Utility method that creates a Class from a resource
240: * located realtive to the given base class. Uses the ClassLoader
241: * of the LookAndFeel
242: *
243: * @param className name of the class
244: * @return a class instance
245: */
246: public static Class makeClass(String className) {
247: try {
248: return Class.forName(className, true, Thread
249: .currentThread().getContextClassLoader());
250: } catch (ClassNotFoundException e) {
251: throw new RuntimeException(e);
252: }
253: }
254:
255: /**
256: * Utility method that creates an Object of class <code>clazz</code>
257: * using the single String arg constructor.
258: *
259: * @param value object as a string
260: * @param clazz class of the object
261: * @return the object
262: */
263: public static Object makeObject(String value, Class clazz) {
264: Object result;
265: try {
266: if (value.startsWith("new ")) {
267: int bracket = value.indexOf("(");
268: String name = value.substring("new ".length(), bracket);
269: clazz = Class.forName(name, true, Thread
270: .currentThread().getContextClassLoader());
271: result = clazz.newInstance();
272: } else {
273: if (clazz.isPrimitive())
274: clazz = (Class) WRAPPERS.get(clazz);
275: Constructor constructor = clazz
276: .getConstructor(new Class[] { String.class });
277: result = constructor
278: .newInstance(new Object[] { value });
279: }
280: } catch (Exception e) {
281: throw new RuntimeException(e);
282: }
283: return result;
284: }
285: }
|