KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: LookAndFeelFactory.java,v 1.8 2005/03/03 13:18:24 neurolabs 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.session.Session;
19 import org.wings.session.SessionManager;
20
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 public abstract class LookAndFeelFactory {
25     private final transient static Log log = LogFactory.getLog(LookAndFeelFactory.class);
26
27     private static String JavaDoc 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     /**
36      * Get the lool and feel factory.
37      */

38     public static LookAndFeelFactory getLookAndFeelFactory() {
39         if (factory == null) {
40             synchronized (LookAndFeelFactory.class) {
41                 if (factory == null) {
42                     String JavaDoc className = (String JavaDoc) SessionManager.getSession().getProperty("wings.lookandfeel.factory");
43                     if (className == null)
44                         className = DEFAULT_LOOKANDFEEL_FACTORY;
45
46                     try {
47                         Class JavaDoc factoryClass = null;
48                         try {
49                             factoryClass = Class.forName(className, true,
50                                     Thread.currentThread()
51                                     .getContextClassLoader());
52                         } catch (ClassNotFoundException JavaDoc e) {
53                             // fallback, in case the servlet container fails to set the
54
// context class loader.
55
factoryClass = Class.forName(className);
56                         }
57                         factory = (LookAndFeelFactory) factoryClass.newInstance();
58                     } catch (Exception JavaDoc e) {
59                         log.fatal("could not load wings.lookandfeel.factory: " +
60                                 className, e);
61                         throw new RuntimeException JavaDoc("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 JavaDoc;
73
74     static class Default extends LookAndFeelFactory {
75         private static String JavaDoc DEFAULT_LOOKANDFEEL = "org.wings.plaf.css.CSSLookAndFeel";
76         private static HashMap JavaDoc lafs = new HashMap JavaDoc();
77         
78
79         public LookAndFeel create()
80                 throws IOException JavaDoc {
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 JavaDoc lafName = (String JavaDoc) session.getProperty("wings.lookandfeel.default");
88                         if (lafName == null)
89                             lafName = DEFAULT_LOOKANDFEEL;
90                         try {
91                             Class JavaDoc lafClass = Class.forName(lafName, true, Thread.currentThread().getContextClassLoader());
92                             laf = (LookAndFeel) lafClass.newInstance();
93                         } catch (Exception JavaDoc e) {
94                             log.fatal("create", e);
95                             throw new IOException JavaDoc(e.getMessage());
96                         }
97                         lafs.put(SessionManager.getSession().getUserAgent().getBrowserType(), laf);
98                     }
99                 }
100             }
101             return laf;
102         }
103     }
104 }
105
Popular Tags