1 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 ; 30 import java.io.Serializable ; 31 import java.lang.reflect.Constructor ; 32 import java.util.*; 33 34 41 public class LookAndFeel implements Serializable { 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 .class); 47 wrappers.put(Character.TYPE, Character .class); 48 wrappers.put(Byte.TYPE, Byte .class); 49 wrappers.put(Short.TYPE, Short .class); 50 wrappers.put(Integer.TYPE, Integer .class); 51 wrappers.put(Long.TYPE, Long .class); 52 wrappers.put(Float.TYPE, Float .class); 53 wrappers.put(Double.TYPE, Double .class); 54 } 55 56 protected Properties properties; 57 58 private static final Map finalResources = Collections.synchronizedMap(new HashMap()); 59 60 65 public LookAndFeel(Properties properties) { 66 this.properties = properties; 67 } 68 69 73 public String getName() { 74 return properties.getProperty("lookandfeel.name"); 75 } 76 77 81 public String getDescription() { 82 return properties.getProperty("lookandfeel.description"); 83 } 84 85 94 public CGDefaults createDefaults() { 95 return new ResourceFactory(); 96 } 97 98 104 public static Object makeCG(String className) { 105 Object result = finalResources.get(className); 106 if (result == null) { 107 try { 108 Class cgClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); 109 result = cgClass.newInstance(); 110 finalResources.put(className, result); 111 } catch (Exception ex) { 112 log.fatal(null, ex); 113 } 114 } 115 return result; 116 } 117 118 123 public static Color makeColor(String colorString) { 124 if (colorString != null) { 125 try { 126 return Color.decode(colorString.trim()); 127 } catch (Exception ex) { 128 ex.printStackTrace(); 129 return null; 130 } 131 } 132 return null; 133 } 134 135 140 public static SDimension makeDimension(String 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 159 public static SIcon makeIcon(String 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 174 public static CSSAttributeSet makeAttributeSet(String string) { 175 CSSAttributeSet attributes = new CSSAttributeSet(); 176 StringTokenizer tokens = new StringTokenizer(string, ";"); 177 while (tokens.hasMoreTokens()) { 178 String 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 193 public static Resource makeResource(String 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 207 public static StyleSheet makeStyleSheet(String resourceName) { 208 try { 209 CSSStyleSheet result = new CSSStyleSheet(); 210 InputStream in = LookAndFeel.class.getClassLoader().getResourceAsStream(resourceName); 211 result.read(in); 212 in.close(); 213 return result; 214 } catch (Exception e) { 215 log.warn("Exception", e); 216 } 217 return null; 218 } 219 220 228 public static Object makeObject(String value, Class clazz) { 229 Object result; 230 try { 231 if (value.startsWith("new ")) { 232 int bracket = value.indexOf("("); 233 String 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 ) wrappers.get(clazz); 239 Constructor constructor = clazz.getConstructor(new Class []{String .class}); 240 result = constructor.newInstance(new Object []{value}); 241 } 242 } catch (NoSuchMethodException e) { 243 log.fatal(value + " : " + clazz.getName() 244 + " doesn't have a single String arg constructor", e); 245 result = null; 246 } catch (Exception e) { 247 log.error(e.getClass().getName() + " : " + value, e); 248 result = null; 249 } 250 return result; 251 } 252 253 259 public String 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 get(Object key, Class type) { 271 Object value = get(key); 272 if (value != null) 273 return value; 274 275 String property; 276 if (key instanceof Class ) { 277 Class clazz = (Class ) 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 |