KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > updater > UpdaterFrame


1     /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.updater;
21
22 import java.awt.*;
23 import java.util.Locale JavaDoc;
24 import javax.swing.*;
25 import java.io.*;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.net.URL JavaDoc;
28
29
30 import javax.swing.border.LineBorder JavaDoc;
31
32 /**
33  *
34  * @author phrebejk, akemr, Jiri Rechtacek
35  * @version
36  */

37 public class UpdaterFrame extends javax.swing.JPanel JavaDoc {
38
39     int xxx = 0xFFFFFF;
40
41     /** Operating system is Windows x */
42     public static final int OS_WIN = 1;
43     /** Operating system is Solaris. */
44     public static final int OS_SOLARIS = 8;
45     /** Operating system is Linux. */
46     public static final int OS_LINUX = 16;
47     /** Operating system is OS/2. */
48     public static final int OS_OS2 = 1024;
49     /** Operating system is unknown. */
50     public static final int OS_OTHER = 65536;
51     
52     private static final String JavaDoc SPLASH_PATH = "org/netbeans/updater/resources/updatersplash"; // NOI18N
53
private static final String JavaDoc 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     /** For external running Updater without GUI */
66     private static boolean noSplash = false;
67     
68     /** Creates new form UpdaterFrame */
69     public UpdaterFrame() {
70         initComponents ();
71         
72         if ( addBorder() )
73             setBorder(new LineBorder JavaDoc(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 JavaDoc getMainWindowTitle() {
90         return Localization.getBrandedString("UpdaterFrame.Form.title");
91     }
92     
93     /** This method is called from within the constructor to
94      * initialize the form.
95      * WARNING: Do NOT modify this code. The content of this method is
96      * always regenerated by the FormEditor.
97      */

98     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
99
private void initComponents() {
100         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
101
102         jLabel3 = new javax.swing.JLabel JavaDoc();
103         jTextArea1 = new javax.swing.JTextArea JavaDoc();
104         textLabel = new javax.swing.JLabel JavaDoc();
105         progressBar = new javax.swing.JProgressBar JavaDoc();
106
107         setLayout(new java.awt.GridBagLayout JavaDoc());
108
109         setBackground(stringToColor("UpdaterFrame.Background", new Color(6, 4, 100)));
110         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(0, 0, 0, 0)));
111         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
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 JavaDoc(8, 14, 0, 14);
127         add(jTextArea1, gridBagConstraints);
128
129         textLabel.setFont(new java.awt.Font JavaDoc("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 JavaDoc();
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 JavaDoc(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 JavaDoc();
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 JavaDoc(5, 14, 10, 14);
146         add(progressBar, gridBagConstraints);
147
148     }
149     // </editor-fold>//GEN-END:initComponents
150

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             // paint the background color.
159
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             // only some systems supports non-frame windows
174
splashWindow = new SplashWindow();
175         } else {
176             splashWindow = (Window)new SplashFrame();
177         }
178
179         // show splash
180
javax.swing.SwingUtilities.invokeLater(
181                     new Runnable JavaDoc() {
182                         public void run() {
183                             splashWindow.setVisible(true);
184                             splashWindow.toFront ();
185                         }
186                     });
187     }
188     
189     /**
190     * @param args the command line arguments
191     */

192     public static void main (String JavaDoc 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 JavaDoc listener, String JavaDoc brandingToken, boolean dontShowSplash) {
209         noSplash = dontShowSplash;
210         runFromIDE (files, listener, brandingToken);
211     }
212
213     public static void runFromIDE (File[] files, PropertyChangeListener JavaDoc listener, String JavaDoc 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 JavaDoc startFromIDE (File[] files, PropertyChangeListener JavaDoc listener, String JavaDoc 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         /*
246         ClassRunner runner = new ClassRunner();
247         runner.start();
248         */

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     /** Tells the Localization class whether we are running from IDE or not
261      */

262     boolean isFromIde() {
263         return fromIDE;
264     }
265
266
267     // Variables declaration - do not modify//GEN-BEGIN:variables
268
private javax.swing.JLabel JavaDoc jLabel3;
269     private javax.swing.JTextArea JavaDoc jTextArea1;
270     private javax.swing.JProgressBar JavaDoc progressBar;
271     private javax.swing.JLabel JavaDoc textLabel;
272     // End of variables declaration//GEN-END:variables
273

274
275     static void setLabel( final String JavaDoc text ) {
276         
277         if (noSplash) return;
278
279         final javax.swing.JLabel JavaDoc label = panel.textLabel;
280
281         javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
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 JavaDoc progressBar = panel.progressBar;
297         final boolean bb = bigBounds;
298
299         javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
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 JavaDoc progressBar = panel.progressBar;
313         final boolean bb = bigBounds;
314
315         javax.swing.SwingUtilities.invokeLater( new Runnable JavaDoc() {
316                                                     public void run() {
317                                                         progressBar.setValue( bb ? (int)(value / 1024) : (int)value );
318                                                     }
319                                                 });
320     }
321     
322     /** Get the operating system on which the IDE is running.
323     * @return one of the <code>OS_*</code> constants (such as {@link #OS_WINNT})
324     */

325     public static final int getOperatingSystem () {
326         int operatingSystem = -1;
327         String JavaDoc osName = System.getProperty ("os.name");
328         if ( osName != null && osName.startsWith("Windows")) // NOI18N
329
operatingSystem = OS_WIN;
330         else if ("Solaris".equals (osName)) // NOI18N
331
operatingSystem = OS_SOLARIS;
332         else if (osName.startsWith ("SunOS")) // NOI18N
333
operatingSystem = OS_SOLARIS;
334         else if ("Linux".equals (osName)) // NOI18N
335
operatingSystem = OS_LINUX;
336         else if ("OS/2".equals (osName)) // NOI18N
337
operatingSystem = OS_OS2;
338         else
339             operatingSystem = OS_OTHER;
340         return operatingSystem;
341     }
342     
343     /** Getter for property fromIDE.
344      * @return Value of property fromIDE.
345      */

346     static boolean isFromIDE() {
347         return fromIDE;
348     }
349
350     static UpdaterFrame getUpdaterFrame() {
351         return panel;
352     }
353     
354     static private Color stringToColor( String JavaDoc key, Color defcolor ) {
355         try {
356             String JavaDoc str = Localization.getBrandedString( key + "_R" ); // NOI18N
357
int re = Integer.parseInt( str );
358             
359             str = Localization.getBrandedString( key + "_G" ); // NOI18N
360
int gr = Integer.parseInt( str );
361             
362             str = Localization.getBrandedString( key + "_B" ); // NOI18N
363
int bl = Integer.parseInt( str );
364             
365             return new Color ( re, gr, bl );
366         } catch ( Exception JavaDoc e ) {
367             return defcolor;
368         }
369     }
370     
371     static private Dimension stringToDimension (String JavaDoc key, Dimension defaultSize) {
372         try {
373             String JavaDoc str = Localization.getBrandedString( key + "_X" ); // NOI18N
374
int x = Integer.parseInt( str );
375             
376             str = Localization.getBrandedString( key + "_Y" ); // NOI18N
377
int y = Integer.parseInt( str );
378             
379             return new Dimension (x, y);
380         } catch ( Exception JavaDoc e ) {
381             return defaultSize;
382         }
383     }
384     
385     static private boolean addBorder() {
386         return "true".equals( Localization.getBrandedString( "UpdaterFrame.hasBorder" ) ); // NOI18N
387
}
388     
389     static private boolean isGradient () {
390         return "true".equals (Localization.getBrandedString ("UpdaterFrame.isGradient")); // NOI18N
391
}
392     
393     
394     private void loadSplash() {
395         URL JavaDoc lookup = Localization.getBrandedResource( SPLASH_PATH, ".gif" ); // NOI18N
396
if ( lookup != null )
397             jLabel3.setIcon( new ImageIcon( lookup ) );
398     }
399         
400     static class SplashFrame extends JFrame {
401         
402         /** Creates a new SplashFrame */
403         public SplashFrame () {
404             super (getMainWindowTitle ());
405             setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
406             setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
407             // add splash component
408
getContentPane().add(panel);
409             center(this);
410         }
411         
412         public java.awt.Dimension JavaDoc getPreferredSize () {
413             return stringToDimension("UpdaterFrame.Splash.PreferredSize", new Dimension (400, 280));
414         }
415     }
416     
417     
418     static class SplashWindow extends Window {
419         /** Creates a new SplashWindow */
420         public SplashWindow () {
421             super(new Frame());
422             // add splash component
423
setLayout (new java.awt.BorderLayout JavaDoc ());
424             add(panel, java.awt.BorderLayout.CENTER);
425             center(this);
426         }
427
428         public java.awt.Dimension JavaDoc getPreferredSize () {
429             return stringToDimension("UpdaterFrame.Splash.PreferredSize", new Dimension (400, 280));
430         }
431     }
432     
433     // copied from core/CLIOptions
434

435     private static boolean isOption (String JavaDoc value, String JavaDoc 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 JavaDoc[] args) {
447         // let's go through the command line
448
for (int i = 0; i < args.length; i++) {
449             if (args[i] == null) {
450                 continue;
451             }
452             if (isOption (args[i], "nosplash")) { // NOI18N
453
UpdaterFrame.noSplash = true;
454             } else if (isOption (args[i], "locale")) { // NOI18N
455
args[i] = null;
456                 String JavaDoc localeParam = args[++i];
457                 String JavaDoc language;
458                 String JavaDoc country = ""; // NOI18N
459
String JavaDoc variant = ""; // NOI18N
460
int index1 = localeParam.indexOf(":"); // NOI18N
461
if (index1 == -1)
462                     language = localeParam;
463                 else {
464                     language = localeParam.substring(0, index1);
465                     int index2 = localeParam.indexOf(":", index1+1); // NOI18N
466
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 JavaDoc(language, country, variant));
474             } else if (isOption (args[i], "branding")) { // NOI18N
475
args[i] = null;
476                 if (++i == args.length) {
477                     System.err.println("Option --branding requires one argument.");
478                     return 2;
479                 }
480                 String JavaDoc branding = args[i];
481                 if (branding.equals("-")) branding = null; // NOI18N
482
try {
483                     Localization.setBranding(branding);
484                 } catch (IllegalArgumentException JavaDoc iae) {
485                     iae.printStackTrace();
486                     return 1;
487                 }
488             }
489         }
490         
491         return 0;
492     }
493     
494 }
495
Popular Tags