1 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 35 public class LookAndFeelUtil 36 { 37 38 private static Logger logger = Logger.getLogger(LookAndFeelUtil.class); 39 40 private static Collection plafs = new Vector (); 41 42 43 44 private LookAndFeelUtil() { } 45 46 47 48 public static void setLookAndFeel() 49 { 50 try 51 { 52 logger.debug("Searching for Pluggable Look and Feel classes"); 53 54 InputStream stream = LookAndFeelUtil.class.getResourceAsStream("/plaf.properties"); 56 if (stream != null) 57 { 58 Properties props = new Properties (); 59 props.load(stream); 60 stream.close(); 61 62 String name = props.getProperty("plaf.class"); 63 if (name != null) 64 { 65 loadClass(name); 66 } 67 } 68 69 URL [] urls = ((URLClassLoader ) 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 file = new JarFile (new File (urls[i].getFile())); 82 Enumeration entries = file.entries(); 83 while (entries.hasMoreElements()) 84 { 85 JarEntry entry = (JarEntry ) entries.nextElement(); 86 if (!entry.isDirectory()) 87 { 88 String name = entry.getName(); 89 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 for (Iterator iterator = plafs.iterator(); iterator.hasNext(); ) 105 { 106 LookAndFeel plaf = (LookAndFeel ) iterator.next(); 107 try 108 { 109 UIManager.setLookAndFeel(plaf); 110 logger.debug("LookAndFeelService " + plaf.getName() + " setup"); 111 break; 112 } 113 catch (UnsupportedLookAndFeelException ulafe) 114 { 115 logger.warn("Look And Feel not supported (" + ulafe.getMessage() + ")"); 116 } 117 } 118 } 119 catch (ClassCastException cce) 120 { 121 logger.warn("Context ClassLoader must be an URLClassLoader (" + cce.getMessage() + ")"); 122 } 123 catch (IOException ioe) 124 { 125 logger.warn("Cannot create a JarFile (" + ioe.getMessage() + ")"); 126 } 127 } 128 129 130 135 private static void loadClass(String name) 136 { 137 try 139 { 140 Class plafClass = Thread.currentThread().getContextClassLoader().loadClass(name); 141 if (LookAndFeel .class.isAssignableFrom(plafClass)) 142 { 143 LookAndFeel plaf = (LookAndFeel ) plafClass.newInstance(); 144 plafs.add(plaf); 145 logger.debug("Look and Feel added : " + plaf.getName()); 146 } 147 } 148 catch (ClassNotFoundException cnfe) 149 { 150 logger.warn("The Look And Feel class " + name + " was not found"); 151 } 152 catch (InstantiationException ie) 153 { 154 logger.warn("Error while creating Look And Feel"); 155 } 156 catch (IllegalAccessException iae) 157 { 158 logger.warn("Error while accessing Look And Feel"); 159 } 160 } 161 } 162 | Popular Tags |