KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > LookAndFeelUtil


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.net.URLClassLoader JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Properties JavaDoc;
18 import java.util.Vector JavaDoc;
19 import java.util.jar.JarEntry JavaDoc;
20 import java.util.jar.JarFile JavaDoc;
21
22 import javax.swing.LookAndFeel JavaDoc;
23 import javax.swing.UIManager JavaDoc;
24 import javax.swing.UnsupportedLookAndFeelException JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28 /**
29  * Description of the Class
30  *
31  * @author Laurent Etiemble
32  * @version $Revision: 1.2 $
33  * @todo Javadoc to complete
34  */

35 public class LookAndFeelUtil
36 {
37    /** Description of the Field */
38    private static Logger logger = Logger.getLogger(LookAndFeelUtil.class);
39    /** Description of the Field */
40    private static Collection JavaDoc plafs = new Vector JavaDoc();
41
42
43    /** Constructor for the AboutServiceProvider object */
44    private LookAndFeelUtil() { }
45
46
47    /** Description of the Method */
48    public static void setLookAndFeel()
49    {
50       try
51       {
52          logger.debug("Searching for Pluggable Look and Feel classes");
53
54          // Try to load Look And Feels through the property file
55
InputStream JavaDoc stream = LookAndFeelUtil.class.getResourceAsStream("/plaf.properties");
56          if (stream != null)
57          {
58             Properties JavaDoc props = new Properties JavaDoc();
59             props.load(stream);
60             stream.close();
61
62             String JavaDoc name = props.getProperty("plaf.class");
63             if (name != null)
64             {
65                loadClass(name);
66             }
67          }
68
69          // Find list of Look And Feels through the Class Loader
70
//
71
// The trick is to search the class that ends with "LookAndFeel"
72
// and to verify that they inherit the javax.swing.LookAndFeel class
73
// It may be defeated if the Look And Feel is not named like this
74
//
75
URL JavaDoc[] urls = ((URLClassLoader JavaDoc) Thread.currentThread().getContextClassLoader()).getURLs();
76
77          for (int i = 0; i < urls.length; i++)
78          {
79             if (urls[i].getFile().endsWith(".jar"))
80             {
81                JarFile JavaDoc file = new JarFile JavaDoc(new File JavaDoc(urls[i].getFile()));
82                Enumeration JavaDoc entries = file.entries();
83                while (entries.hasMoreElements())
84                {
85                   JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
86                   if (!entry.isDirectory())
87                   {
88                      String JavaDoc name = entry.getName();
89                      // If it ends with LookAndFeel, it may be a Look And Feel
90
if (name.endsWith("LookAndFeel.class"))
91                      {
92                         name = name.substring(0, name.length() - ".class".length());
93                         name = name.replace('/', '.');
94                         logger.debug("Found Look and Feel class : " + name);
95
96                         loadClass(name);
97                      }
98                   }
99                }
100             }
101          }
102
103          // If there are some Look And Feel, set it up
104
for (Iterator JavaDoc iterator = plafs.iterator(); iterator.hasNext(); )
105          {
106             LookAndFeel JavaDoc plaf = (LookAndFeel JavaDoc) iterator.next();
107             try
108             {
109                UIManager.setLookAndFeel(plaf);
110                logger.debug("LookAndFeelService " + plaf.getName() + " setup");
111                break;
112             }
113             catch (UnsupportedLookAndFeelException JavaDoc ulafe)
114             {
115                logger.warn("Look And Feel not supported (" + ulafe.getMessage() + ")");
116             }
117          }
118       }
119       catch (ClassCastException JavaDoc cce)
120       {
121          logger.warn("Context ClassLoader must be an URLClassLoader (" + cce.getMessage() + ")");
122       }
123       catch (IOException JavaDoc ioe)
124       {
125          logger.warn("Cannot create a JarFile (" + ioe.getMessage() + ")");
126       }
127    }
128
129
130    /**
131     * Description of the Method
132     *
133     * @param name Description of the Parameter
134     */

135    private static void loadClass(String JavaDoc name)
136    {
137       // Load the class and add it
138
try
139       {
140          Class JavaDoc plafClass = Thread.currentThread().getContextClassLoader().loadClass(name);
141          if (LookAndFeel JavaDoc.class.isAssignableFrom(plafClass))
142          {
143             LookAndFeel JavaDoc plaf = (LookAndFeel JavaDoc) plafClass.newInstance();
144             plafs.add(plaf);
145             logger.debug("Look and Feel added : " + plaf.getName());
146          }
147       }
148       catch (ClassNotFoundException JavaDoc cnfe)
149       {
150          logger.warn("The Look And Feel class " + name + " was not found");
151       }
152       catch (InstantiationException JavaDoc ie)
153       {
154          logger.warn("Error while creating Look And Feel");
155       }
156       catch (IllegalAccessException JavaDoc iae)
157       {
158          logger.warn("Error while accessing Look And Feel");
159       }
160    }
161 }
162
Popular Tags