1 15 16 18 36 package ixenon.free.install; 37 38 import java.io.*; 39 import java.util.*; 40 41 import java.awt.*; 42 import java.awt.event.*; 43 import javax.swing.JComponent ; 44 import javax.swing.JProgressBar ; 45 import javax.swing.SwingConstants ; 46 import javax.swing.SwingUtilities ; 47 48 import ixenon.free.swing.*; 50 51 63 public class InstallUtilities implements InstallConstants { 64 65 66 public final static int NOMINAL_BUFFER_SIZE=8192; 67 public final static String PRESERVE_SUFFIX=".preserved"; 68 69 73 public static String installModeToString( int installMode ) 74 { 75 switch (installMode) { 76 case BASIC_INSTALLATION: return BASIC_INSTALLATION_STRING; 77 case CUSTOM_INSTALLATION: return CUSTOM_INSTALLATION_STRING; 78 case COMPACT_INSTALLATION: return COMPACT_INSTALLATION_STRING; 79 } 80 return ""; 81 } 82 83 92 public static boolean copyFile( File sourceFile, File targetFile ) 93 { 94 return copyFile( sourceFile, targetFile, false ); 95 } 96 97 113 public static boolean copyFile( File sourceFile, File targetFile, 114 boolean preserve ) 115 { 116 boolean bfDone=true; 117 FileInputStream fin = null; 118 FileOutputStream fout = null; 119 120 if ( sourceFile.equals(targetFile)) 121 return (false); 123 if ( sourceFile.isDirectory() ) 124 return (false); if ( targetFile.isDirectory() ) 126 return (false); 128 if ( targetFile.exists() ) { 130 if (preserve && 131 sourceFile.length() != targetFile.length() && 132 sourceFile.lastModified() != targetFile.lastModified() ) { 133 File preserveFile = new File( targetFile.getPath()+PRESERVE_SUFFIX ); 134 if (preserveFile.exists()) 136 preserveFile.delete(); 137 targetFile.renameTo(preserveFile); 139 } 140 else 141 targetFile.delete(); 143 } 144 145 try { 146 fin = new FileInputStream( sourceFile ); 147 fout = new FileOutputStream( targetFile ); 148 byte buffer[] = new byte[NOMINAL_BUFFER_SIZE]; 149 int n; 150 while ( (n = fin.read( buffer )) > -1 ) 151 fout.write( buffer, 0, n ); 152 } 153 catch (IOException e) { 154 System.err.println("IO Exception:" + e.getMessage() ); 155 bfDone = false; 156 } 157 finally { 158 if (fin != null) 159 try { fin.close(); } catch (Exception e) { ; } 160 if (fout != null) 161 try { fout.close(); } catch (Exception e) { ; } 162 } 163 164 return (bfDone); 165 } 166 167 177 public static boolean copyFile( 178 JProgressBar pbar, File sourceFile, File targetFile ) 179 { 180 return copyFile( pbar, sourceFile, targetFile, false ); 181 } 182 183 201 public static boolean copyFile( 202 JProgressBar pbar, File sourceFile, File targetFile, boolean preserve ) 203 { 204 boolean bfDone=true; 205 FileInputStream fin = null; 206 FileOutputStream fout = null; 207 208 if ( sourceFile.equals(targetFile)) 209 return (false); 211 if ( sourceFile.isDirectory() ) 212 return (false); if ( targetFile.isDirectory() ) 214 return (false); 216 if ( targetFile.exists() ) { 218 if (preserve && 219 sourceFile.length() != targetFile.length() && 220 sourceFile.lastModified() != targetFile.lastModified() ) { 221 File preserveFile = new File( targetFile.getPath()+PRESERVE_SUFFIX ); 222 if (preserveFile.exists()) 224 preserveFile.delete(); 225 targetFile.renameTo(preserveFile); 227 } 228 else 229 targetFile.delete(); 231 } 232 233 SwingUtilities.invokeLater( new ResetProgressBar( pbar, 0, 100, 0 ) ); 234 235 try { 236 int percentage, total_bytes=0; 237 long fileLength = sourceFile.length(); 238 239 fin = new FileInputStream( sourceFile ); 240 fout = new FileOutputStream( targetFile ); 241 byte buffer[] = new byte[NOMINAL_BUFFER_SIZE]; 242 int n; 243 while ( (n = fin.read( buffer )) > -1 ) { 244 fout.write( buffer, 0, n ); 245 total_bytes += n; 246 percentage = (int)( 100.0 * ((double)total_bytes) / fileLength ); 247 SwingUtilities.invokeLater( new UpdateProgressBar( pbar, percentage ) ); 248 try { Thread.sleep(5); } catch (InterruptedException ie) { } 249 } 250 251 SwingUtilities.invokeLater( new UpdateProgressBar( pbar, 100 ) ); 253 } 254 catch (IOException e) { 255 System.err.println("IO Exception:" + e.getMessage() ); 256 bfDone = false; 257 } 258 finally { 259 if (fin != null) 260 try { fin.close(); } catch (Exception e) { ; } 261 if (fout != null) 262 try { fout.close(); } catch (Exception e) { ; } 263 264 } 265 266 return (bfDone); 267 } 268 269 279 public static boolean setFilePermissions( String filename, int perm ) 280 { 281 return setFilePermissions( new File(filename), perm ); 282 } 283 284 294 public static boolean setFilePermissions( File file, int perm ) 295 { 296 boolean done=true; 297 String command; 298 String osname = System.getProperty("os.name"); 299 if ( osname.startsWith( "Windows 95" ) || 300 osname.startsWith( "Windows 98" ) || 301 osname.startsWith("Windows 2000") || 302 osname.startsWith("Windows NT") || 303 osname.startsWith("OS/2") ) { 304 switch (perm) { 306 case INST_EXE_PERMS: 307 return true; 310 case INST_RDWR_PERMS: 311 command="ATTRIB -R \""+file.getAbsolutePath()+"\""; 312 break; 313 case INST_RDONLY_PERMS: 314 default: 315 command="ATTRIB +R \""+file.getAbsolutePath()+"\""; 316 break; 317 } 318 } 319 else { 320 switch (perm) { 321 case INST_EXE_PERMS: 322 command="/bin/chmod 755 "+file.getAbsolutePath(); 323 break; 324 case INST_RDWR_PERMS: 325 command="/bin/chmod 644 "+file.getAbsolutePath(); 326 break; 327 case INST_RDONLY_PERMS: 328 default: 329 command="/bin/chmod 444 "+file.getAbsolutePath(); 330 break; 331 } 332 } 333 334 try { 335 Process p = Runtime.getRuntime().exec(command); 336 p.waitFor(); 337 done = ( p.exitValue() == 0 ? true : false ); 339 340 } catch (IOException e) { 341 System.err.println("IO error: " + e); 342 done = false; 343 } catch (InterruptedException e1) { 344 System.err.println("Exception: " + e1.getMessage()); 345 done = false; 346 } 347 348 349 return (done); 350 } 351 352 357 public static String convertUnixPathToNTPath( String unixpath ) 358 { 359 StringBuffer ntpath = new StringBuffer (); 360 361 for (int j=0; j<unixpath.length(); ++j) { 362 char ch = unixpath.charAt(j); 363 if (ch == '/' ) 364 ch = '\\'; 365 ntpath.append(ch); 366 } 367 368 return ntpath.toString(); 369 } 370 371 372 377 public static String convertNTPathToUnixPath( String ntpath ) 378 { 379 StringBuffer unixpath = new StringBuffer (); 380 381 for (int j=0; j<ntpath.length(); ++j) { 382 char ch = ntpath.charAt(j); 383 if (ch == '\\' ) 384 ch = '/'; 385 unixpath.append(ch); 386 } 387 388 return unixpath.toString(); 389 } 390 391 392 404 public static String normalizePathname( String pathname ) 405 { 406 String fileSep = System.getProperty("file.separator"); 407 if ( fileSep.equals("/") ) 408 return convertNTPathToUnixPath( pathname ); 409 else if ( fileSep.equals("\\") ) 410 return convertUnixPathToNTPath( pathname ); 411 412 return (pathname); 413 } 414 415 421 public static File createNormalizedFile( String path ) 422 { 423 return new File( normalizePathname( path )); 424 } 425 426 433 public static File createNormalizedFile( String prefix, String path ) 434 { 435 File file = new File( normalizePathname( prefix ) ); 436 return createNormalizedFile( file, path ); 437 } 438 439 446 public static File createNormalizedFile( File prefix, String path ) 447 { 448 String normPath = normalizePathname( path ); 449 return new File( prefix, normPath ); 450 } 451 452 457 public static String getBackslashifiedPath( String path ) 458 { 459 StringBuffer protpath = new StringBuffer (); 460 461 for (int j=0; j<path.length(); ++j) { 462 char ch = path.charAt(j); 463 if (ch == '\\' ) 464 protpath.append(ch); 465 protpath.append(ch); 466 } 467 468 return protpath.toString(); 469 } 470 471 475 public static void centerComponentOnScreen( Component comp ) 476 { 477 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 478 Dimension size = new Dimension(comp.getSize()); 479 480 int x = (screen.width-size.width)/2; 481 int y = (screen.height-size.height)/2; 482 comp.setBounds(x, y, size.width, size.height); 483 } 484 485 496 public static void centerHiresComponentOnScreen( 497 Component comp, Dimension hiresBest ) 498 { 499 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 500 Dimension size = new Dimension(comp.getSize()); 501 502 if (screen.width > 1100 ) { 503 size.width = 1024; 505 size.height = 695; 506 } 507 int x = (screen.width-size.width)/2; 508 int y = (screen.height-size.height)/2; 509 comp.setBounds(x, y, size.width, size.height); 510 } 511 512 } 513 514 | Popular Tags |