1 19 20 package org.netbeans.updater; 21 22 import java.awt.*; 23 import java.util.Locale ; 24 import javax.swing.*; 25 import java.io.*; 26 import java.beans.PropertyChangeListener ; 27 import java.net.URL ; 28 29 30 import javax.swing.border.LineBorder ; 31 32 37 public class UpdaterFrame extends javax.swing.JPanel { 38 39 int xxx = 0xFFFFFF; 40 41 42 public static final int OS_WIN = 1; 43 44 public static final int OS_SOLARIS = 8; 45 46 public static final int OS_LINUX = 16; 47 48 public static final int OS_OS2 = 1024; 49 50 public static final int OS_OTHER = 65536; 51 52 private static final String SPLASH_PATH = "org/netbeans/updater/resources/updatersplash"; private static final String UNDERLINE = "_"; 54 55 private static UpdaterFrame panel; 56 57 private static boolean bigBounds = false; 58 59 private static ModuleUpdater mu = null; 60 61 private static boolean fromIDE = false; 62 63 private static Window splashWindow; 64 65 66 private static boolean noSplash = false; 67 68 69 public UpdaterFrame() { 70 initComponents (); 71 72 if ( addBorder() ) 73 setBorder(new LineBorder (stringToColor ("UpdaterFrame.LineBorder.Color", new Color(0, 0, 0)))); 74 75 loadSplash(); 76 } 77 78 static final void center(Window c) { 79 c.pack(); 80 81 GraphicsConfiguration gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 82 Rectangle bounds = gconf.getBounds(); 83 Dimension dialogSize = c.getSize(); 84 85 c.setLocation(bounds.x + (bounds.width - dialogSize.width) / 2, 86 bounds.y + (bounds.height - dialogSize.height) / 2); 87 } 88 89 static String getMainWindowTitle() { 90 return Localization.getBrandedString("UpdaterFrame.Form.title"); 91 } 92 93 98 private void initComponents() { 100 java.awt.GridBagConstraints gridBagConstraints; 101 102 jLabel3 = new javax.swing.JLabel (); 103 jTextArea1 = new javax.swing.JTextArea (); 104 textLabel = new javax.swing.JLabel (); 105 progressBar = new javax.swing.JProgressBar (); 106 107 setLayout(new java.awt.GridBagLayout ()); 108 109 setBackground(stringToColor("UpdaterFrame.Background", new Color(6, 4, 100))); 110 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (0, 0, 0, 0))); 111 gridBagConstraints = new java.awt.GridBagConstraints (); 112 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 113 add(jLabel3, gridBagConstraints); 114 115 jTextArea1.setBackground(stringToColor("UpdaterFrame.TextBackground", new Color (213, 204, 187))); 116 jTextArea1.setEditable(false); 117 jTextArea1.setForeground(stringToColor("UpdaterFrame.TextForeground", java.awt.Color.white)); 118 jTextArea1.setText(Localization.getBrandedString( "UpdaterFrame.jTextArea1.text" )); 119 jTextArea1.setDisabledTextColor(stringToColor("UpdaterFrame.DisabledTextColor", java.awt.Color.white)); 120 jTextArea1.setOpaque(false); 121 gridBagConstraints = new java.awt.GridBagConstraints (); 122 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 123 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 124 gridBagConstraints.weightx = 1.0; 125 gridBagConstraints.weighty = 1.0; 126 gridBagConstraints.insets = new java.awt.Insets (8, 14, 0, 14); 127 add(jTextArea1, gridBagConstraints); 128 129 textLabel.setFont(new java.awt.Font ("Dialog", 1, 11)); 130 textLabel.setForeground(stringToColor("UpdaterFrame.TextForeground", java.awt.Color.white)); 131 textLabel.setText(Localization.getBrandedString("UpdaterFrame.textLabel.text")); 132 gridBagConstraints = new java.awt.GridBagConstraints (); 133 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 134 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 135 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 136 gridBagConstraints.insets = new java.awt.Insets (8, 14, 8, 14); 137 add(textLabel, gridBagConstraints); 138 139 progressBar.setPreferredSize(stringToDimension("UpdaterFrame.ProgressBar.PreferredSize", new Dimension (300, 20))); 140 progressBar.setRequestFocusEnabled(false); 141 gridBagConstraints = new java.awt.GridBagConstraints (); 142 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 143 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 144 gridBagConstraints.weightx = 1.0; 145 gridBagConstraints.insets = new java.awt.Insets (5, 14, 10, 14); 146 add(progressBar, gridBagConstraints); 147 148 } 149 151 protected void paintComponent (Graphics g) { 152 if (isGradient ()) { 153 Color outerColor = stringToColor ("UpdaterFrame.outerColor", new Color(230, 242, 234)); 154 Color centerColor = stringToColor ("UpdaterFrame.centerColor", Color.WHITE); 155 int w = getWidth(); 156 int h = getHeight(); 157 158 Graphics2D g2d = (Graphics2D) g; 160 g2d.setPaint(new GradientPaint(0, 0, outerColor, w/2, 0, centerColor, true)); 161 g2d.fillRect(0, 0, w, h); 162 } else { 163 super.paintComponent (g); 164 } 165 } 166 167 private static void showSplash () { 168 169 if ((getOperatingSystem () == OS_WIN) || 170 (getOperatingSystem () == OS_SOLARIS) || 171 (getOperatingSystem () == OS_OS2) || 172 (getOperatingSystem () == OS_LINUX)) { 173 splashWindow = new SplashWindow(); 175 } else { 176 splashWindow = (Window)new SplashFrame(); 177 } 178 179 javax.swing.SwingUtilities.invokeLater( 181 new Runnable () { 182 public void run() { 183 splashWindow.setVisible(true); 184 splashWindow.toFront (); 185 } 186 }); 187 } 188 189 192 public static void main (String args[]) { 193 if (args.length > 0) { 194 cli (args); 195 } 196 197 panel = new UpdaterFrame (); 198 199 if (!noSplash) { 200 showSplash (); 201 } 202 203 mu = new ModuleUpdater(); 204 mu.start(); 205 206 } 207 208 public static void runFromIDE (File[] files, PropertyChangeListener listener, String brandingToken, boolean dontShowSplash) { 209 noSplash = dontShowSplash; 210 runFromIDE (files, listener, brandingToken); 211 } 212 213 public static void runFromIDE (File[] files, PropertyChangeListener listener, String brandingToken) { 214 fromIDE = true; 215 Localization.setBranding (brandingToken); 216 panel = new UpdaterFrame (); 217 panel.addPropertyChangeListener( listener ); 218 if (! noSplash) { 219 showSplash(); 220 } 221 222 mu = new ModuleUpdater(); 223 mu.setInstallOnly (files); 224 mu.start(); 225 } 226 227 public static Thread startFromIDE (File[] files, PropertyChangeListener listener, String brandingToken) { 228 noSplash = true; 229 fromIDE = true; 230 Localization.setBranding (brandingToken); 231 panel = new UpdaterFrame (); 232 panel.addPropertyChangeListener( listener ); 233 if (! noSplash) { 234 showSplash(); 235 } 236 237 mu = new ModuleUpdater(); 238 mu.setInstallOnly (files); 239 mu.start(); 240 return mu; 241 } 242 243 void unpackingFinished() { 244 runningFinished(); 245 249 } 250 251 void runningFinished() { 252 if ( fromIDE ) { 253 firePropertyChange("FINISHED", null, null); 254 if (splashWindow != null) splashWindow.dispose(); 255 } 256 else 257 System.exit( 0 ); 258 } 259 260 262 boolean isFromIde() { 263 return fromIDE; 264 } 265 266 267 private javax.swing.JLabel jLabel3; 269 private javax.swing.JTextArea jTextArea1; 270 private javax.swing.JProgressBar progressBar; 271 private javax.swing.JLabel textLabel; 272 274 275 static void setLabel( final String text ) { 276 277 if (noSplash) return; 278 279 final javax.swing.JLabel label = panel.textLabel; 280 281 javax.swing.SwingUtilities.invokeLater( new Runnable () { 282 public void run() { 283 label.setText( text ); 284 } 285 }); 286 287 } 288 289 290 static void setProgressRange( final long min, final long max ) { 291 292 if (noSplash) return; 293 294 bigBounds = max > 0xFFFF; 295 296 final javax.swing.JProgressBar progressBar = panel.progressBar; 297 final boolean bb = bigBounds; 298 299 javax.swing.SwingUtilities.invokeLater( new Runnable () { 300 public void run() { 301 progressBar.setMinimum( bb ? (int)(min / 1024) : (int)min ); 302 progressBar.setMaximum( bb ? (int)(max / 1024) : (int)max ); 303 } 304 }); 305 306 } 307 308 static void setProgressValue( final long value ) { 309 310 if (noSplash) return; 311 312 final javax.swing.JProgressBar progressBar = panel.progressBar; 313 final boolean bb = bigBounds; 314 315 javax.swing.SwingUtilities.invokeLater( new Runnable () { 316 public void run() { 317 progressBar.setValue( bb ? (int)(value / 1024) : (int)value ); 318 } 319 }); 320 } 321 322 325 public static final int getOperatingSystem () { 326 int operatingSystem = -1; 327 String osName = System.getProperty ("os.name"); 328 if ( osName != null && osName.startsWith("Windows")) operatingSystem = OS_WIN; 330 else if ("Solaris".equals (osName)) operatingSystem = OS_SOLARIS; 332 else if (osName.startsWith ("SunOS")) operatingSystem = OS_SOLARIS; 334 else if ("Linux".equals (osName)) operatingSystem = OS_LINUX; 336 else if ("OS/2".equals (osName)) operatingSystem = OS_OS2; 338 else 339 operatingSystem = OS_OTHER; 340 return operatingSystem; 341 } 342 343 346 static boolean isFromIDE() { 347 return fromIDE; 348 } 349 350 static UpdaterFrame getUpdaterFrame() { 351 return panel; 352 } 353 354 static private Color stringToColor( String key, Color defcolor ) { 355 try { 356 String str = Localization.getBrandedString( key + "_R" ); int re = Integer.parseInt( str ); 358 359 str = Localization.getBrandedString( key + "_G" ); int gr = Integer.parseInt( str ); 361 362 str = Localization.getBrandedString( key + "_B" ); int bl = Integer.parseInt( str ); 364 365 return new Color ( re, gr, bl ); 366 } catch ( Exception e ) { 367 return defcolor; 368 } 369 } 370 371 static private Dimension stringToDimension (String key, Dimension defaultSize) { 372 try { 373 String str = Localization.getBrandedString( key + "_X" ); int x = Integer.parseInt( str ); 375 376 str = Localization.getBrandedString( key + "_Y" ); int y = Integer.parseInt( str ); 378 379 return new Dimension (x, y); 380 } catch ( Exception e ) { 381 return defaultSize; 382 } 383 } 384 385 static private boolean addBorder() { 386 return "true".equals( Localization.getBrandedString( "UpdaterFrame.hasBorder" ) ); } 388 389 static private boolean isGradient () { 390 return "true".equals (Localization.getBrandedString ("UpdaterFrame.isGradient")); } 392 393 394 private void loadSplash() { 395 URL lookup = Localization.getBrandedResource( SPLASH_PATH, ".gif" ); if ( lookup != null ) 397 jLabel3.setIcon( new ImageIcon( lookup ) ); 398 } 399 400 static class SplashFrame extends JFrame { 401 402 403 public SplashFrame () { 404 super (getMainWindowTitle ()); 405 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 406 setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR)); 407 getContentPane().add(panel); 409 center(this); 410 } 411 412 public java.awt.Dimension getPreferredSize () { 413 return stringToDimension("UpdaterFrame.Splash.PreferredSize", new Dimension (400, 280)); 414 } 415 } 416 417 418 static class SplashWindow extends Window { 419 420 public SplashWindow () { 421 super(new Frame()); 422 setLayout (new java.awt.BorderLayout ()); 424 add(panel, java.awt.BorderLayout.CENTER); 425 center(this); 426 } 427 428 public java.awt.Dimension getPreferredSize () { 429 return stringToDimension("UpdaterFrame.Splash.PreferredSize", new Dimension (400, 280)); 430 } 431 } 432 433 435 private static boolean isOption (String value, String optionName) { 436 if (value == null) return false; 437 438 if (value.startsWith ("--")) { 439 return value.substring (2).equals (optionName); 440 } else if (value.startsWith ("-")) { 441 return value.substring (1).equals (optionName); 442 } 443 return false; 444 } 445 446 private static int cli(String [] args) { 447 for (int i = 0; i < args.length; i++) { 449 if (args[i] == null) { 450 continue; 451 } 452 if (isOption (args[i], "nosplash")) { UpdaterFrame.noSplash = true; 454 } else if (isOption (args[i], "locale")) { args[i] = null; 456 String localeParam = args[++i]; 457 String language; 458 String country = ""; String variant = ""; int index1 = localeParam.indexOf(":"); if (index1 == -1) 462 language = localeParam; 463 else { 464 language = localeParam.substring(0, index1); 465 int index2 = localeParam.indexOf(":", index1+1); if (index2 != -1) { 467 country = localeParam.substring(index1+1, index2); 468 variant = localeParam.substring(index2+1); 469 } 470 else 471 country = localeParam.substring(index1+1); 472 } 473 Locale.setDefault(new Locale (language, country, variant)); 474 } else if (isOption (args[i], "branding")) { args[i] = null; 476 if (++i == args.length) { 477 System.err.println("Option --branding requires one argument."); 478 return 2; 479 } 480 String branding = args[i]; 481 if (branding.equals("-")) branding = null; try { 483 Localization.setBranding(branding); 484 } catch (IllegalArgumentException iae) { 485 iae.printStackTrace(); 486 return 1; 487 } 488 } 489 } 490 491 return 0; 492 } 493 494 } 495 | Popular Tags |