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.session.Session; 19 import org.wings.session.SessionManager; 20 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 24 public abstract class LookAndFeelFactory { 25 private final transient static Log log = LogFactory.getLog(LookAndFeelFactory.class); 26 27 private static String DEFAULT_LOOKANDFEEL_FACTORY = "org.wings.plaf.LookAndFeelFactory$Default"; 28 29 private static LookAndFeelFactory factory; 30 31 public static void setLookAndFeelFactory(LookAndFeelFactory factory) { 32 LookAndFeelFactory.factory = factory; 33 } 34 35 38 public static LookAndFeelFactory getLookAndFeelFactory() { 39 if (factory == null) { 40 synchronized (LookAndFeelFactory.class) { 41 if (factory == null) { 42 String className = (String ) SessionManager.getSession().getProperty("wings.lookandfeel.factory"); 43 if (className == null) 44 className = DEFAULT_LOOKANDFEEL_FACTORY; 45 46 try { 47 Class factoryClass = null; 48 try { 49 factoryClass = Class.forName(className, true, 50 Thread.currentThread() 51 .getContextClassLoader()); 52 } catch (ClassNotFoundException e) { 53 factoryClass = Class.forName(className); 56 } 57 factory = (LookAndFeelFactory) factoryClass.newInstance(); 58 } catch (Exception e) { 59 log.fatal("could not load wings.lookandfeel.factory: " + 60 className, e); 61 throw new RuntimeException ("could not load" + 62 " wings.lookandfeel.factory: " + 63 className + 64 "(" + e.getMessage() + ")"); 65 } 66 } 67 } 68 } 69 return factory; 70 } 71 72 public abstract LookAndFeel create() throws IOException ; 73 74 static class Default extends LookAndFeelFactory { 75 private static String DEFAULT_LOOKANDFEEL = "org.wings.plaf.css.CSSLookAndFeel"; 76 private static HashMap lafs = new HashMap (); 77 78 79 public LookAndFeel create() 80 throws IOException { 81 LookAndFeel laf = (LookAndFeel)lafs.get(SessionManager.getSession().getUserAgent().getBrowserType()); 82 if (laf == null) { 83 synchronized (Default.class) { 84 laf = (LookAndFeel)lafs.get(SessionManager.getSession().getUserAgent().getBrowserType()); 85 if (laf == null) { 86 Session session = SessionManager.getSession(); 87 String lafName = (String ) session.getProperty("wings.lookandfeel.default"); 88 if (lafName == null) 89 lafName = DEFAULT_LOOKANDFEEL; 90 try { 91 Class lafClass = Class.forName(lafName, true, Thread.currentThread().getContextClassLoader()); 92 laf = (LookAndFeel) lafClass.newInstance(); 93 } catch (Exception e) { 94 log.fatal("create", e); 95 throw new IOException (e.getMessage()); 96 } 97 lafs.put(SessionManager.getSession().getUserAgent().getBrowserType(), laf); 98 } 99 } 100 } 101 return laf; 102 } 103 } 104 } 105 | Popular Tags |