1 6 7 package com.memoire.vainstall; 8 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.io.InputStream ; 12 import java.lang.reflect.Constructor ; 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 import java.net.URLClassLoader ; 16 import java.util.Properties ; 17 18 import javax.swing.LookAndFeel ; 19 import javax.swing.UIManager ; 20 import javax.swing.UnsupportedLookAndFeelException ; 21 22 26 27 public abstract class VAStepFactory { 28 private static VAStepFactory UNIQUE_UI = null; 29 30 private AbstractInstall setup_; 31 32 private VAWizardInterface wizard_; 33 34 private static boolean testGraphicUi(String uiId) { 35 boolean res = true; 36 if (("graphic".equals(uiId)) || ("xtra".equals(uiId))) { 37 try { 38 java.awt.Toolkit.getDefaultToolkit(); 39 } catch (Error ex) { 40 res = false; 41 } 42 } 43 return res; 44 } 45 46 private static boolean testTextUi(String uiId, AbstractInstall setup) { 47 boolean res = true; 48 if ("ansi".equals(uiId)) { 49 res = !(setup.IS_WIN || setup.IS_MAC); 50 } 51 return res; 52 } 53 54 55 public static void setLnf(String uiId, Properties _vaProps, Class c, 56 File tmpDir) { 57 if ('g' == uiId.charAt(0)) { 58 String lnfJar = _vaProps.getProperty("vainstall.lnf.jar", null); 59 String lnfClass = _vaProps.getProperty("vainstall.lnf.class", null); 60 if (lnfClass == null) 61 return; 62 Class clazz = null; 63 if(lnfJar!=null){ 64 InputStream in = c.getResourceAsStream("resources/" + lnfJar); 65 File dest = null; 66 try { 67 dest = tmpDir != null ? File.createTempFile("lnf", ".jar", 68 tmpDir) : File.createTempFile("lnf", ".jar"); 69 dest.deleteOnExit(); 70 FileOutputStream out = new FileOutputStream (dest); 71 byte[] buffer = new byte[100000]; 72 while (true) { 73 synchronized (buffer) { 74 75 int amountRead = in.read(buffer); 76 if (amountRead == -1) { 77 break; 78 } 79 out.write(buffer, 0, amountRead); 80 } 81 } 82 if (dest != null) { 83 URLClassLoader cl = new URLClassLoader (new URL [] { dest 84 .toURL() }); 85 clazz = cl.loadClass(lnfClass); 86 UIManager.put("ClassLoader", cl); 88 89 } 90 } catch (Exception _e) { 91 if (VAGlobals.DEBUG) 92 _e.printStackTrace(); 93 return; 94 } 95 } 96 else if("[SYSTEM_LNF]".equals(lnfClass)){ 97 try { 98 String systemLnfClass=UIManager.getSystemLookAndFeelClassName(); 99 if(systemLnfClass.equals(UIManager.getCrossPlatformLookAndFeelClassName()) && 100 System.getProperty("os.name").indexOf("Linux")!=-1){ 101 systemLnfClass="com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; 102 } 103 if (VAGlobals.DEBUG){ 104 System.out.println("System lnf "+UIManager.getSystemLookAndFeelClassName()); 105 } 106 clazz=Class.forName(systemLnfClass); 107 } catch (ClassNotFoundException _e) { 108 if (VAGlobals.DEBUG) 109 _e.printStackTrace(); 110 return; 111 } 112 } 113 else{ 114 try { 115 clazz=Class.forName(lnfClass); 116 } catch (ClassNotFoundException _e) { 117 if (VAGlobals.DEBUG) 118 _e.printStackTrace(); 119 return; 120 121 } 122 } 123 if (clazz != null) 124 if (VAGlobals.DEBUG){ 125 System.out.println("used lnf "+clazz.getName()); 126 } 127 try { 128 UIManager.setLookAndFeel((LookAndFeel ) clazz.newInstance()); 129 } catch (Exception _e1) { 130 if (VAGlobals.DEBUG) 131 _e1.printStackTrace(); 132 } 133 } 134 } 135 136 public static VAStepFactory createUI(String uiId, AbstractInstall setup) { 137 if (UNIQUE_UI != null) 138 return UNIQUE_UI; 139 if ((uiId == null) || (uiId.length() == 0)) { 140 System.err.println("No UI specified: trying graphic UI"); 141 return createUI("graphic", setup); 142 } 143 if (!testGraphicUi(uiId)) { 144 System.err 145 .println("No graphic display found. Switching to ansi mode"); 146 return createUI("ansi", setup); 147 } 148 if (!testTextUi(uiId, setup)) { 149 System.err.println("Ansi not supported. Switching to text mode"); 150 return createUI("text", setup); 151 } 152 String className = "com.memoire.vainstall." + uiId.charAt(0) + "ui.VA" 153 + Character.toUpperCase(uiId.charAt(0)) + uiId.substring(1) 154 + "UI"; 155 try { 156 Class clazz = Class.forName(className); 157 Constructor constructor = clazz.getConstructor(null); 158 Object o = constructor.newInstance(null); 159 UNIQUE_UI = (VAStepFactory) o; 160 } catch (ClassNotFoundException cnfe) { 161 System.err.println(cnfe + ": " + className); 162 if ("graphic".equals(uiId)) { 163 return createUI("ansi", setup); 164 } 165 if ("ansi".equals(uiId)) { 166 return createUI("text", setup); 167 } 168 if ("text".equals(uiId)) { 169 setup.cancel(); 170 return null; 171 } else { 172 System.err.println("trying graphic UI"); 173 return createUI("graphic", setup); 174 } 175 } catch (ClassCastException cce) { 176 System.err.println(cce); 177 System.err.println("This class is not a VAStepFactory: " 178 + className); 179 setup.cancel(); 180 } catch (NoSuchMethodException nsme) { 181 System.err.println(nsme); 182 System.err.println("Please implement a void constructor for " 183 + className); 184 setup.cancel(); 185 } catch (SecurityException se) { 186 System.err.println(se); 187 setup.cancel(); 188 } catch (InstantiationException ie) { 189 System.err.println(ie); 190 System.err.println("Please don't make this class abstract: " 191 + className); 192 setup.cancel(); 193 } catch (IllegalAccessException iace) { 194 System.err.println(iace); 195 System.err.println("Please set public access to constructor for " 196 + className); 197 setup.cancel(); 198 } catch (IllegalArgumentException iare) { 199 System.err.println(iare); 200 System.err.println("Please set no args to constructor for " 201 + className); 202 setup.cancel(); 203 } catch (InvocationTargetException ite) { 204 System.err.println(ite); 205 System.err.println("Exception in constructor for " + className); 206 ite.getTargetException().printStackTrace(); 207 setup.cancel(); 208 } 209 210 if (UNIQUE_UI != null) { 211 UNIQUE_UI.setup_ = setup; 212 UNIQUE_UI.initUI(); 213 } 214 return UNIQUE_UI; 215 } 216 217 public abstract void initUI(); 218 219 public void activateUI() { 220 if (wizard_ == null) { 221 showError(new NullPointerException ( 222 "Null wizard! Please create one or overload VAStepFactory::activateUI()")); 223 setup_.cancel(); 224 } 225 wizard_.show(); 226 } 227 228 public void quitUI() { 229 if (wizard_ != null) { 230 wizard_.dispose(false); 231 } 232 } 233 234 public abstract VAStep createSetupLanguageStep(); 235 236 public abstract VAStep createWelcomeStep(); 237 238 public abstract VAStep createLicenseStep(); 239 240 public abstract VAStep createReadmeStep(); 241 242 public abstract VAStep createLicenseKeyStep(); 243 244 public abstract VAStep createUpgradeStep(); 245 246 public abstract VAStep createDirectoryStep(); 247 248 public abstract VAStep createInstallStep(); 249 250 public abstract VAStep createShortcutStep(); 251 252 public abstract VAStep createEndStep(); 253 254 public void setActionEnabled(int actions) { 255 if (wizard_ == null) { 256 showError(new NullPointerException ( 257 "Null wizard! Please create one or overload VAStepFactory::setActionEnabled()")); 258 setup_.cancel(); 259 } 260 wizard_.setActionEnabled(actions); 261 } 262 263 public void showFatalError(Throwable t) { 264 t.printStackTrace(); 265 } 266 267 public void showError(Throwable t) { 268 System.err.println(t); 269 } 270 271 public void uiSleep(long millis) { 272 } 273 274 protected AbstractInstall getInstaller() { 275 return setup_; 276 } 277 278 protected VAWizardInterface getWizard() { 279 return wizard_; 280 } 281 282 protected void setWizard(VAWizardInterface w) { 283 wizard_ = w; 284 } 285 }
| Popular Tags
|