Clover coverage report - AdWT - 1.0.0
Coverage timestamp: sam. déc. 27 2003 15:14:10 CET
file stats: LOC: 162   Methods: 3
NCLOC: 113   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LookAndFeelUtil.java 0% 0% 0% 0%
coverage
 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;
 10   
 import java.io.IOException;
 11   
 import java.io.InputStream;
 12   
 import java.net.URL;
 13   
 import java.net.URLClassLoader;
 14   
 import java.util.Collection;
 15   
 import java.util.Enumeration;
 16   
 import java.util.Iterator;
 17   
 import java.util.Properties;
 18   
 import java.util.Vector;
 19   
 import java.util.jar.JarEntry;
 20   
 import java.util.jar.JarFile;
 21   
 
 22   
 import javax.swing.LookAndFeel;
 23   
 import javax.swing.UIManager;
 24   
 import javax.swing.UnsupportedLookAndFeelException;
 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 plafs = new Vector();
 41   
 
 42   
 
 43   
    /** Constructor for the AboutServiceProvider object */
 44  0
    private LookAndFeelUtil() { }
 45   
 
 46   
 
 47   
    /** Description of the Method */
 48  0
    public static void setLookAndFeel()
 49   
    {
 50  0
       try
 51   
       {
 52  0
          logger.debug("Searching for Pluggable Look and Feel classes");
 53   
 
 54   
          // Try to load Look And Feels through the property file
 55  0
          InputStream stream = LookAndFeelUtil.class.getResourceAsStream("/plaf.properties");
 56  0
          if (stream != null)
 57   
          {
 58  0
             Properties props = new Properties();
 59  0
             props.load(stream);
 60  0
             stream.close();
 61   
 
 62  0
             String name = props.getProperty("plaf.class");
 63  0
             if (name != null)
 64   
             {
 65  0
                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  0
          URL[] urls = ((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURLs();
 76   
 
 77  0
          for (int i = 0; i < urls.length; i++)
 78   
          {
 79  0
             if (urls[i].getFile().endsWith(".jar"))
 80   
             {
 81  0
                JarFile file = new JarFile(new File(urls[i].getFile()));
 82  0
                Enumeration entries = file.entries();
 83  0
                while (entries.hasMoreElements())
 84   
                {
 85  0
                   JarEntry entry = (JarEntry) entries.nextElement();
 86  0
                   if (!entry.isDirectory())
 87   
                   {
 88  0
                      String name = entry.getName();
 89   
                      // If it ends with LookAndFeel, it may be a Look And Feel
 90  0
                      if (name.endsWith("LookAndFeel.class"))
 91   
                      {
 92  0
                         name = name.substring(0, name.length() - ".class".length());
 93  0
                         name = name.replace('/', '.');
 94  0
                         logger.debug("Found Look and Feel class : " + name);
 95   
 
 96  0
                         loadClass(name);
 97   
                      }
 98   
                   }
 99   
                }
 100   
             }
 101   
          }
 102   
 
 103   
          // If there are some Look And Feel, set it up
 104  0
          for (Iterator iterator = plafs.iterator(); iterator.hasNext(); )
 105   
          {
 106  0
             LookAndFeel plaf = (LookAndFeel) iterator.next();
 107  0
             try
 108   
             {
 109  0
                UIManager.setLookAndFeel(plaf);
 110  0
                logger.debug("LookAndFeelService " + plaf.getName() + " setup");
 111  0
                break;
 112   
             }
 113   
             catch (UnsupportedLookAndFeelException ulafe)
 114   
             {
 115  0
                logger.warn("Look And Feel not supported (" + ulafe.getMessage() + ")");
 116   
             }
 117   
          }
 118   
       }
 119   
       catch (ClassCastException cce)
 120   
       {
 121  0
          logger.warn("Context ClassLoader must be an URLClassLoader (" + cce.getMessage() + ")");
 122   
       }
 123   
       catch (IOException ioe)
 124   
       {
 125  0
          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  0
    private static void loadClass(String name)
 136   
    {
 137   
       // Load the class and add it
 138  0
       try
 139   
       {
 140  0
          Class plafClass = Thread.currentThread().getContextClassLoader().loadClass(name);
 141  0
          if (LookAndFeel.class.isAssignableFrom(plafClass))
 142   
          {
 143  0
             LookAndFeel plaf = (LookAndFeel) plafClass.newInstance();
 144  0
             plafs.add(plaf);
 145  0
             logger.debug("Look and Feel added : " + plaf.getName());
 146   
          }
 147   
       }
 148   
       catch (ClassNotFoundException cnfe)
 149   
       {
 150  0
          logger.warn("The Look And Feel class " + name + " was not found");
 151   
       }
 152   
       catch (InstantiationException ie)
 153   
       {
 154  0
          logger.warn("Error while creating Look And Feel");
 155   
       }
 156   
       catch (IllegalAccessException iae)
 157   
       {
 158  0
          logger.warn("Error while accessing Look And Feel");
 159   
       }
 160   
    }
 161   
 }
 162