001: /**
002: * $RCSfile: VAStepFactory.java,v $
003: * @creation 15/03/00
004: * @modification $Date: 2005/05/14 10:14:17 $
005: */package com.memoire.vainstall;
006:
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.io.InputStream;
010: import java.lang.reflect.Constructor;
011: import java.lang.reflect.InvocationTargetException;
012: import java.net.URL;
013: import java.net.URLClassLoader;
014: import java.util.Properties;
015:
016: import javax.swing.LookAndFeel;
017: import javax.swing.UIManager;
018: import javax.swing.UnsupportedLookAndFeelException;
019:
020: /**
021: * @version $Id: VAStepFactory.java,v 1.8 2005/05/14 10:14:17 deniger Exp $
022: * @author Axel von Arnim
023: */
024:
025: public abstract class VAStepFactory {
026: private static VAStepFactory UNIQUE_UI = null;
027:
028: private AbstractInstall setup_;
029:
030: private VAWizardInterface wizard_;
031:
032: private static boolean testGraphicUi(String uiId) {
033: boolean res = true;
034: if (("graphic".equals(uiId)) || ("xtra".equals(uiId))) {
035: try {
036: java.awt.Toolkit.getDefaultToolkit();
037: } catch (Error ex) {
038: res = false;
039: }
040: }
041: return res;
042: }
043:
044: private static boolean testTextUi(String uiId, AbstractInstall setup) {
045: boolean res = true;
046: if ("ansi".equals(uiId)) {
047: res = !(setup.IS_WIN || setup.IS_MAC);
048: }
049: return res;
050: }
051:
052: public static void setLnf(String uiId, Properties _vaProps,
053: Class c, File tmpDir) {
054: if ('g' == uiId.charAt(0)) {
055: String lnfJar = _vaProps.getProperty("vainstall.lnf.jar",
056: null);
057: String lnfClass = _vaProps.getProperty(
058: "vainstall.lnf.class", null);
059: if (lnfClass == null)
060: return;
061: Class clazz = null;
062: if (lnfJar != null) {
063: InputStream in = c.getResourceAsStream("resources/"
064: + lnfJar);
065: File dest = null;
066: try {
067: dest = tmpDir != null ? File.createTempFile("lnf",
068: ".jar", tmpDir) : File.createTempFile(
069: "lnf", ".jar");
070: dest.deleteOnExit();
071: FileOutputStream out = new FileOutputStream(dest);
072: byte[] buffer = new byte[100000];
073: while (true) {
074: synchronized (buffer) {
075:
076: int amountRead = in.read(buffer);
077: if (amountRead == -1) {
078: break;
079: }
080: out.write(buffer, 0, amountRead);
081: }
082: }
083: if (dest != null) {
084: URLClassLoader cl = new URLClassLoader(
085: new URL[] { dest.toURL() });
086: clazz = cl.loadClass(lnfClass);
087: //important
088: UIManager.put("ClassLoader", cl);
089:
090: }
091: } catch (Exception _e) {
092: if (VAGlobals.DEBUG)
093: _e.printStackTrace();
094: return;
095: }
096: } else if ("[SYSTEM_LNF]".equals(lnfClass)) {
097: try {
098: String systemLnfClass = UIManager
099: .getSystemLookAndFeelClassName();
100: if (systemLnfClass.equals(UIManager
101: .getCrossPlatformLookAndFeelClassName())
102: && System.getProperty("os.name").indexOf(
103: "Linux") != -1) {
104: systemLnfClass = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
105: }
106: if (VAGlobals.DEBUG) {
107: System.out
108: .println("System lnf "
109: + UIManager
110: .getSystemLookAndFeelClassName());
111: }
112: clazz = Class.forName(systemLnfClass);
113: } catch (ClassNotFoundException _e) {
114: if (VAGlobals.DEBUG)
115: _e.printStackTrace();
116: return;
117: }
118: } else {
119: try {
120: clazz = Class.forName(lnfClass);
121: } catch (ClassNotFoundException _e) {
122: if (VAGlobals.DEBUG)
123: _e.printStackTrace();
124: return;
125:
126: }
127: }
128: if (clazz != null)
129: if (VAGlobals.DEBUG) {
130: System.out.println("used lnf " + clazz.getName());
131: }
132: try {
133: UIManager.setLookAndFeel((LookAndFeel) clazz
134: .newInstance());
135: } catch (Exception _e1) {
136: if (VAGlobals.DEBUG)
137: _e1.printStackTrace();
138: }
139: }
140: }
141:
142: public static VAStepFactory createUI(String uiId,
143: AbstractInstall setup) {
144: if (UNIQUE_UI != null)
145: return UNIQUE_UI;
146: if ((uiId == null) || (uiId.length() == 0)) {
147: System.err.println("No UI specified: trying graphic UI");
148: return createUI("graphic", setup);
149: }
150: if (!testGraphicUi(uiId)) {
151: System.err
152: .println("No graphic display found. Switching to ansi mode");
153: return createUI("ansi", setup);
154: }
155: if (!testTextUi(uiId, setup)) {
156: System.err
157: .println("Ansi not supported. Switching to text mode");
158: return createUI("text", setup);
159: }
160: String className = "com.memoire.vainstall." + uiId.charAt(0)
161: + "ui.VA" + Character.toUpperCase(uiId.charAt(0))
162: + uiId.substring(1) + "UI";
163: try {
164: Class clazz = Class.forName(className);
165: Constructor constructor = clazz.getConstructor(null);
166: Object o = constructor.newInstance(null);
167: UNIQUE_UI = (VAStepFactory) o;
168: } catch (ClassNotFoundException cnfe) {
169: System.err.println(cnfe + ": " + className);
170: if ("graphic".equals(uiId)) {
171: return createUI("ansi", setup);
172: }
173: if ("ansi".equals(uiId)) {
174: return createUI("text", setup);
175: }
176: if ("text".equals(uiId)) {
177: setup.cancel();
178: return null;
179: } else {
180: System.err.println("trying graphic UI");
181: return createUI("graphic", setup);
182: }
183: } catch (ClassCastException cce) {
184: System.err.println(cce);
185: System.err.println("This class is not a VAStepFactory: "
186: + className);
187: setup.cancel();
188: } catch (NoSuchMethodException nsme) {
189: System.err.println(nsme);
190: System.err
191: .println("Please implement a void constructor for "
192: + className);
193: setup.cancel();
194: } catch (SecurityException se) {
195: System.err.println(se);
196: setup.cancel();
197: } catch (InstantiationException ie) {
198: System.err.println(ie);
199: System.err
200: .println("Please don't make this class abstract: "
201: + className);
202: setup.cancel();
203: } catch (IllegalAccessException iace) {
204: System.err.println(iace);
205: System.err
206: .println("Please set public access to constructor for "
207: + className);
208: setup.cancel();
209: } catch (IllegalArgumentException iare) {
210: System.err.println(iare);
211: System.err.println("Please set no args to constructor for "
212: + className);
213: setup.cancel();
214: } catch (InvocationTargetException ite) {
215: System.err.println(ite);
216: System.err.println("Exception in constructor for "
217: + className);
218: ite.getTargetException().printStackTrace();
219: setup.cancel();
220: }
221:
222: if (UNIQUE_UI != null) {
223: UNIQUE_UI.setup_ = setup;
224: UNIQUE_UI.initUI();
225: }
226: return UNIQUE_UI;
227: }
228:
229: public abstract void initUI();
230:
231: public void activateUI() {
232: if (wizard_ == null) {
233: showError(new NullPointerException(
234: "Null wizard! Please create one or overload VAStepFactory::activateUI()"));
235: setup_.cancel();
236: }
237: wizard_.show();
238: }
239:
240: public void quitUI() {
241: if (wizard_ != null) {
242: wizard_.dispose(false);
243: }
244: }
245:
246: public abstract VAStep createSetupLanguageStep();
247:
248: public abstract VAStep createWelcomeStep();
249:
250: public abstract VAStep createLicenseStep();
251:
252: public abstract VAStep createReadmeStep();
253:
254: public abstract VAStep createLicenseKeyStep();
255:
256: public abstract VAStep createUpgradeStep();
257:
258: public abstract VAStep createDirectoryStep();
259:
260: public abstract VAStep createInstallStep();
261:
262: public abstract VAStep createShortcutStep();
263:
264: public abstract VAStep createEndStep();
265:
266: public void setActionEnabled(int actions) {
267: if (wizard_ == null) {
268: showError(new NullPointerException(
269: "Null wizard! Please create one or overload VAStepFactory::setActionEnabled()"));
270: setup_.cancel();
271: }
272: wizard_.setActionEnabled(actions);
273: }
274:
275: public void showFatalError(Throwable t) {
276: t.printStackTrace();
277: }
278:
279: public void showError(Throwable t) {
280: System.err.println(t);
281: }
282:
283: public void uiSleep(long millis) {
284: }
285:
286: protected AbstractInstall getInstaller() {
287: return setup_;
288: }
289:
290: protected VAWizardInterface getWizard() {
291: return wizard_;
292: }
293:
294: protected void setWizard(VAWizardInterface w) {
295: wizard_ = w;
296: }
297: }
|