| 1 21 22 package com.izforge.izpack.panels; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.io.BufferedReader ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 32 import com.izforge.izpack.gui.IzPanelLayout; 33 import com.izforge.izpack.installer.InstallData; 34 import com.izforge.izpack.installer.InstallerFrame; 35 import com.izforge.izpack.installer.IzPanel; 36 import com.izforge.izpack.installer.ResourceNotFoundException; 37 import com.izforge.izpack.util.AbstractUIHandler; 38 import com.izforge.izpack.util.Debug; 39 import com.izforge.izpack.util.IoHelper; 40 import com.izforge.izpack.util.OsVersion; 41 import com.izforge.izpack.util.VariableSubstitutor; 42 43 49 public class PathInputPanel extends IzPanel implements ActionListener  50 { 51 52 55 private static final long serialVersionUID = 3257566217698292531L; 56 57 58 protected boolean mustExist = false; 59 60 61 protected String [] existFiles = null; 62 63 64 66 protected PathSelectionPanel pathSelectionPanel; 67 68 protected String emptyTargetMsg; 69 70 protected String warnMsg; 71 72 protected static String defaultInstallDir = null; 73 74 80 public PathInputPanel(InstallerFrame parent, InstallData idata) 81 { 82 super(parent, idata, new IzPanelLayout()); 83 emptyTargetMsg = getI18nStringForClass("empty_target", "TargetPanel"); 85 warnMsg = getI18nStringForClass("warn", "TargetPanel"); 86 87 String introText = getI18nStringForClass("extendedIntro", "PathInputPanel"); 88 if (introText == null || introText.endsWith("extendedIntro") 89 || introText.indexOf('$') > -1 ) 90 { 91 introText = getI18nStringForClass("intro", "PathInputPanel"); 92 if (introText == null || introText.endsWith("intro")) 93 introText = ""; 94 } 95 add(createMultiLineLabel(introText)); 98 add(createLabel("info", "TargetPanel", "open", 101 LEFT, true), NEXT_LINE); 102 pathSelectionPanel = new PathSelectionPanel(this, idata); 104 add(pathSelectionPanel, NEXT_LINE); 105 createLayoutBottom(); 106 getLayoutHelper().completeLayout(); 107 } 108 112 public void createLayoutBottom() 113 { 114 } 116 117 122 public void actionPerformed(ActionEvent e) 123 { 124 Object source = e.getSource(); 125 if (source == pathSelectionPanel.getPathInputField()) 126 { 127 parent.navigateNext(); 128 } 129 130 } 131 132 137 public boolean isValidated() 138 { 139 String chosenPath = pathSelectionPanel.getPath(); 140 boolean ok = true; 141 142 if (chosenPath.length() == 0) 144 { 145 if (isMustExist()) 146 { 147 emitError(parent.langpack.getString("installer.error"), parent.langpack 148 .getString("PathInputPanel.required")); 149 return false; 150 } 151 ok = emitWarning(parent.langpack.getString("installer.warning"), emptyTargetMsg); 152 } 153 if (!ok) return ok; 154 155 File path = new File (chosenPath).getAbsoluteFile(); 157 chosenPath = path.toString(); 158 pathSelectionPanel.setPath(chosenPath); 159 if (isMustExist()) 160 { 161 if (!path.exists()) 162 { 163 emitError(parent.langpack.getString("installer.error"), parent.langpack 164 .getString(getI18nStringForClass("required", "PathInputPanel"))); 165 return false; 166 } 167 if (!pathIsValid()) 168 { 169 emitError(parent.langpack.getString("installer.error"), parent.langpack 170 .getString(getI18nStringForClass("notValid", "PathInputPanel"))); 171 return false; 172 } 173 } 174 else 175 { 176 if (!isWriteable()) 178 { 179 emitError(parent.langpack.getString("installer.error"), getI18nStringForClass( 180 "notwritable", "TargetPanel")); 181 return false; 182 } 183 if (path.exists()) 186 { 187 int res = askQuestion(parent.langpack.getString("installer.warning"), warnMsg, 188 AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_YES); 189 ok = res == AbstractUIHandler.ANSWER_YES; 190 } 191 else 192 { 193 ok = this.emitNotificationFeedback(getI18nStringForClass("createdir", "TargetPanel") + "\n" 194 + chosenPath); 195 196 } 197 } 198 return ok; 199 } 200 201 208 protected boolean pathIsValid() 209 { 210 if (existFiles == null) return true; 211 for (int i = 0; i < existFiles.length; ++i) 212 { 213 File path = new File (pathSelectionPanel.getPath(), existFiles[i]).getAbsoluteFile(); 214 if (!path.exists()) return false; 215 } 216 return true; 217 } 218 219 224 public boolean isMustExist() 225 { 226 return mustExist; 227 } 228 229 234 public void setMustExist(boolean b) 235 { 236 mustExist = b; 237 } 238 239 244 public String [] getExistFiles() 245 { 246 return existFiles; 247 } 248 249 254 public void setExistFiles(String [] strings) 255 { 256 existFiles = strings; 257 } 258 259 274 public static void loadDefaultInstallDir(InstallerFrame parentFrame, InstallData idata) 275 { 276 if (getDefaultInstallDir() != null) return; 278 BufferedReader br = null; 279 try 280 { 281 InputStream in = null; 282 283 if (OsVersion.IS_WINDOWS) 284 { 285 try 286 { 287 in = parentFrame.getResource("TargetPanel.dir.windows"); 288 } 289 catch (ResourceNotFoundException rnfe) 290 {} } 292 else if (OsVersion.IS_OSX) 293 { 294 try 295 { 296 in = parentFrame.getResource("TargetPanel.dir.macosx"); 297 } 298 catch (ResourceNotFoundException rnfe) 299 {} } 301 else 302 { 303 String os = System.getProperty("os.name"); 304 os = os.replace(' ', '_'); os = os.toLowerCase(); try 309 { 310 in = parentFrame.getResource("TargetPanel.dir.".concat(os)); 311 } 312 catch (ResourceNotFoundException rnfe) 313 {} 314 if (in == null) 316 { 317 try 318 { 319 in = parentFrame.getResource("TargetPanel.dir.unix"); 320 } 321 catch (ResourceNotFoundException eee) 322 {} 323 } 324 325 } 326 327 if (in == null) 330 { 331 try 332 { 333 in = parentFrame.getResource("TargetPanel.dir"); 334 } 335 catch (ResourceNotFoundException eee) 336 {} 337 } 338 339 if (in != null) 340 { 341 InputStreamReader isr = new InputStreamReader (in); 343 br = new BufferedReader (isr); 344 String line; 345 while ((line = br.readLine()) != null) 346 { 347 line = line.trim(); 348 if (!"".equals(line)) break; 350 } 351 defaultInstallDir = line; 352 VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables()); 353 defaultInstallDir = vs.substitute(defaultInstallDir, null); 354 } 355 } 356 catch (Exception e) 357 { 358 e.printStackTrace(); 360 defaultInstallDir = null; 361 } 363 finally 364 { 365 try 366 { 367 if (br != null) br.close(); 368 } 369 catch (IOException ignored) 370 {} 371 } 372 } 373 374 379 public boolean isWriteable() 380 { 381 File existParent = IoHelper.existingParent(new File (pathSelectionPanel.getPath())); 382 if (existParent == null) return false; 383 if (OsVersion.IS_WINDOWS) 387 { 388 File tmpFile; 389 try 390 { 391 tmpFile = File.createTempFile("izWrTe", ".tmp", existParent); 392 tmpFile.deleteOnExit(); 393 } 394 catch (IOException e) 395 { 396 Debug.trace(e.toString()); 397 return false; 398 } 399 return true; 400 } 401 return existParent.canWrite(); 402 } 403 404 409 public static String getDefaultInstallDir() 410 { 411 return defaultInstallDir; 412 } 413 414 419 public static void setDefaultInstallDir(String string) 420 { 421 defaultInstallDir = string; 422 } 423 424 } 425 | Popular Tags |