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: package org.wings.plaf;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.session.Browser;
018: import org.wings.session.Session;
019: import org.wings.session.SessionManager;
020:
021: import java.io.IOException;
022: import java.util.HashMap;
023:
024: public abstract class LookAndFeelFactory {
025: private final transient static Log log = LogFactory
026: .getLog(LookAndFeelFactory.class);
027:
028: private static final String DEFAULT_LOOKANDFEEL_FACTORY = "org.wings.plaf.LookAndFeelFactory$Default";
029: private static final String DEFAULT_LOOKANDFEEL = "org.wings.plaf.css.CSSLookAndFeel";
030:
031: private static LookAndFeelFactory factory;
032:
033: static {
034: final String className = (String) SessionManager.getSession()
035: .getProperty("wings.lookandfeel.factory",
036: DEFAULT_LOOKANDFEEL_FACTORY);
037:
038: try {
039: Class factoryClass = null;
040: try {
041: factoryClass = Class.forName(className, true, Thread
042: .currentThread().getContextClassLoader());
043: } catch (ClassNotFoundException e) {
044: // fallback, in case the servlet container fails to set the
045: // context class loader.
046: factoryClass = Class.forName(className);
047: }
048: factory = (LookAndFeelFactory) factoryClass.newInstance();
049: } catch (Exception e) {
050: log.fatal("could not load wings.lookandfeel.factory: "
051: + className, e);
052: throw new RuntimeException(
053: "could not load wings.lookandfeel.factory: "
054: + className + e.getMessage());
055: }
056: }
057:
058: public static void setLookAndFeelFactory(LookAndFeelFactory factory) {
059: LookAndFeelFactory.factory = factory;
060: }
061:
062: /**
063: * Get the lool and feel factory.
064: */
065: public static LookAndFeelFactory getLookAndFeelFactory() {
066: return factory;
067: }
068:
069: public abstract LookAndFeel create() throws IOException;
070:
071: static class Default extends LookAndFeelFactory {
072: private final HashMap<String, LookAndFeel> lafs = new HashMap<String, LookAndFeel>();
073:
074: @Override
075: public LookAndFeel create() throws IOException {
076: Session session = SessionManager.getSession();
077: Browser userAgent = session.getUserAgent();
078: final String lookupKey = userAgent.getBrowserType()
079: .getShortName()
080: + Integer.toString(userAgent.getMajorVersion());
081:
082: LookAndFeel laf = lafs.get(lookupKey);
083: if (laf == null) {
084: synchronized (Default.class) {
085: laf = lafs.get(lookupKey);
086: if (laf == null) {
087: String lafName = (String) session
088: .getProperty("wings.lookandfeel.default");
089: if (lafName == null)
090: lafName = DEFAULT_LOOKANDFEEL;
091: try {
092: Class lafClass = Class.forName(lafName,
093: true, Thread.currentThread()
094: .getContextClassLoader());
095: laf = (LookAndFeel) lafClass.newInstance();
096: } catch (Exception e) {
097: log.fatal("create", e);
098: throw new IOException(e.getMessage());
099: }
100: lafs.put(lookupKey, laf);
101: }
102: }
103: }
104: return laf;
105: }
106: }
107: }
|