001: /*
002: * LnFLoader.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.lnf;
013:
014: import java.io.File;
015: import java.net.MalformedURLException;
016: import java.net.URL;
017: import java.net.URLClassLoader;
018: import javax.swing.LookAndFeel;
019: import javax.swing.UIManager;
020: import workbench.WbManager;
021: import workbench.resource.Settings;
022: import workbench.util.StringUtil;
023:
024: /**
025: * A class to manage Look and feels that can be loaded at runtime.
026: * @author support@sql-workbench.net
027: */
028: public class LnFLoader {
029: private LnFDefinition lnfDef;
030: private String[] liblist;
031:
032: public LnFLoader(LnFDefinition definition)
033: throws ClassNotFoundException, MalformedURLException,
034: InstantiationException, IllegalAccessException {
035: this .lnfDef = definition;
036: if (!lnfDef.isBuiltInLnF()) {
037: String libList = definition.getLibrary();
038: if (libList != null) {
039: if (libList.indexOf(StringUtil.getPathSeparator()) > -1) {
040: this .liblist = libList.split(StringUtil
041: .getPathSeparator());
042: } else if (libList
043: .indexOf(LnFDefinition.LNF_PATH_SEPARATOR) > -1) {
044: this .liblist = libList.split(StringUtil
045: .getPathSeparator());
046: } else {
047: // Assuming a single library
048: this .liblist = new String[] { libList };
049: }
050: }
051: }
052: }
053:
054: public boolean isAvailable() {
055: if (this .lnfDef.isBuiltInLnF())
056: return true;
057: try {
058: ClassLoader loader = createLoader();
059: String resName = this .lnfDef.getClassName().replace('.',
060: '/')
061: + ".class";
062: URL cl = loader.getResource(resName);
063: return (cl != null);
064: } catch (Exception e) {
065: return false;
066: }
067: }
068:
069: private ClassLoader createLoader() throws MalformedURLException {
070: if (this .liblist != null) {
071: URL[] url = new URL[this .liblist.length];
072: for (int i = 0; i < this .liblist.length; i++) {
073: String fname = Settings.getInstance().replaceLibDirKey(
074: liblist[i]);
075: File f = new File(fname);
076: if (!f.isAbsolute()) {
077: f = new File(WbManager.getInstance().getJarPath(),
078: this .liblist[i]);
079: }
080:
081: url[i] = f.toURL();
082: }
083: ClassLoader loader = new URLClassLoader(url, this
084: .getClass().getClassLoader());
085: return loader;
086: } else {
087: return null;
088: }
089: }
090:
091: public Class loadClass() throws ClassNotFoundException {
092: Class lnfClass = null;
093: try {
094: ClassLoader loader = createLoader();
095: if (loader != null) {
096: // Tell the LNF class which classloader to use!
097: // This is important otherwise, the LnF will no
098: // initialize correctly
099: UIManager.getDefaults().put("ClassLoader", loader);
100: Thread.currentThread().setContextClassLoader(loader);
101: lnfClass = loader.loadClass(this .lnfDef.getClassName());
102: } else {
103: // If no library is specified we assume the class
104: // is available through the system classloader
105: // My tests showed that the property is not set initially
106: // so I assume this means "use system classloader"
107: UIManager.getDefaults().put("ClassLoader", null);
108: lnfClass = Class.forName(this .lnfDef.getClassName());
109: }
110: } catch (Exception e) {
111: throw new ClassNotFoundException("Could not load class "
112: + this .lnfDef.getClassName(), e);
113: }
114: return lnfClass;
115: }
116:
117: public LookAndFeel getLookAndFeel() throws ClassNotFoundException {
118: try {
119: Class lnf = loadClass();
120: return (LookAndFeel) lnf.newInstance();
121: } catch (Exception e) {
122: throw new ClassNotFoundException("Could not load class "
123: + this.lnfDef.getClassName(), e);
124: }
125: }
126: }
|