alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.updater;

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.beans.PropertyChangeListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author  phrebejk, akemr
 * @version 
 */
public class UpdaterFrame extends javax.swing.JPanel {

    /** Operating system is Windows x */
    public static final int OS_WIN = 1;
    /** Operating system is Solaris. */
    public static final int OS_SOLARIS = 8;
    /** Operating system is Linux. */
    public static final int OS_LINUX = 16;
    /** Operating system is OS/2. */
    public static final int OS_OS2 = 1024;
    /** Operating system is unknown. */
    public static final int OS_OTHER = 65536;
    
    private static final String SPLASH_PATH = "org/netbeans/updater/resources/updatersplash"; // NOI18N
    private static final String UNDERLINE = "_";

    private static UpdaterFrame panel;

    private static boolean bigBounds = false;

    private static ModuleUpdater mu = null;
    
    private static boolean fromIDE = false;
    
    private static Window splashWindow;
    
    /** For external running Updater without GUI */
    private static boolean nongui = false;
    
    /** Creates new form UpdaterFrame */
    public UpdaterFrame() {
        initComponents ();
        
        if ( addBorder() )
            setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        
        loadSplash();
    }
    
    static final void center(Window c) {
        c.pack();

        GraphicsConfiguration gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        Rectangle bounds = gconf.getBounds();
        Dimension dialogSize = c.getSize();
        
        c.setLocation(bounds.x + (bounds.width - dialogSize.width) / 2,
                    bounds.y + (bounds.height - dialogSize.height) / 2);
    }

    static String getMainWindowTitle() {
        return Localization.getBrandedString("UpdaterFrame.Form.title");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel3 = new javax.swing.JLabel();
        jTextArea1 = new javax.swing.JTextArea();
        textLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        setLayout(new java.awt.GridBagLayout());

        setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(0, 0, 0, 0)));
        setBackground(stringToColor("UpdaterFrame.TextBackground", new Color(0, 33, 173)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        add(jLabel3, gridBagConstraints);

        jTextArea1.setBackground(new java.awt.Color(213, 204, 187));
        jTextArea1.setEditable(false);
        jTextArea1.setForeground(stringToColor("UpdaterFrame.TextForeground", java.awt.Color.white));
        jTextArea1.setText(Localization.getBrandedString( "UpdaterFrame.jTextArea1.text" ));
        jTextArea1.setDisabledTextColor(java.awt.Color.white);
        jTextArea1.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 14, 0, 14);
        add(jTextArea1, gridBagConstraints);

        textLabel.setFont(new java.awt.Font("Dialog", 1, 11));
        textLabel.setForeground(stringToColor("UpdaterFrame.TextForeground", java.awt.Color.white));
        textLabel.setText(Localization.getBrandedString("UpdaterFrame.textLabel.text"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 14, 8, 14);
        add(textLabel, gridBagConstraints);

        progressBar.setPreferredSize(new java.awt.Dimension(300, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 14, 10, 14);
        add(progressBar, gridBagConstraints);

    }//GEN-END:initComponents

    private static void showSplash () {
        
        if ((getOperatingSystem () == OS_WIN) ||
            (getOperatingSystem () == OS_SOLARIS) ||
            (getOperatingSystem () == OS_OS2) ||
            (getOperatingSystem () == OS_LINUX)) {
            // only some systems supports non-frame windows
            splashWindow = new SplashWindow();
        } else {        
            splashWindow = (Window)new SplashFrame();
        }

        // show splash
        javax.swing.SwingUtilities.invokeLater( 
                    new Runnable() {
                        public void run() {
                            splashWindow.show ();
                            splashWindow.toFront ();
                        }
                    });
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
        if ( args.length > 0 && args[0].equals( "-nodisplay" ) )  // NOI18N
            nongui = true;
        else {
            panel = new UpdaterFrame ();
            showSplash();
        }

        mu = new ModuleUpdater();
        mu.start();

    }

    public static void runFromIDE ( File[] files, PropertyChangeListener listener ) {

        fromIDE = true;        
        panel = new UpdaterFrame ();
        panel.addPropertyChangeListener( listener );
        showSplash();
        
        mu = new ModuleUpdater();
        mu.setInstallOnly (files);
        mu.start();
    }

    void unpackingFinished() {
        runningFinished();
        /*
        ClassRunner runner = new ClassRunner(); 
        runner.start();
        */
    }

    void runningFinished() {
        if ( fromIDE ) {
            firePropertyChange("FINISHED", null, null);
            splashWindow.dispose();
        }
        else
            System.exit( 0 );
    }
    
    /** Tells the Localization class whether we are running from IDE or not
     */    
    boolean isFromIde() {
        return fromIDE;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel textLabel;
    // End of variables declaration//GEN-END:variables


    static void setLabel( final String text ) {
        
        if ( nongui ) return;

        final javax.swing.JLabel label = panel.textLabel;

        javax.swing.SwingUtilities.invokeLater( new Runnable() {
                                                    public void run() {
                                                        label.setText( text );
                                                    }
                                                });

    }


    static void setProgressRange( final long min, final long max ) {
        
        if ( nongui ) return;

        bigBounds = max > 0xFFFF;

        final javax.swing.JProgressBar progressBar = panel.progressBar;
        final boolean bb = bigBounds;

        javax.swing.SwingUtilities.invokeLater( new Runnable() {
                                                    public void run() {
                                                        progressBar.setMinimum( bb ? (int)(min / 1024) : (int)min );
                                                        progressBar.setMaximum( bb ? (int)(max / 1024) : (int)max );
                                                    }
                                                });

    }

    static void setProgressValue( final long value ) {
        
        if ( nongui ) return;

        final javax.swing.JProgressBar progressBar = panel.progressBar;
        final boolean bb = bigBounds;

        javax.swing.SwingUtilities.invokeLater( new Runnable() {
                                                    public void run() {
                                                        progressBar.setValue( bb ? (int)(value / 1024) : (int)value );
                                                    }
                                                });
    }
    
    /** Get the operating system on which the IDE is running.
    * @return one of the OS_* constants (such as {@link #OS_WINNT})
    */
    public static final int getOperatingSystem () {
        int operatingSystem = -1;
        String osName = System.getProperty ("os.name");
        if ( osName != null && osName.startsWith("Windows")) // NOI18N
            operatingSystem = OS_WIN;
        else if ("Solaris".equals (osName)) // NOI18N
            operatingSystem = OS_SOLARIS;
        else if (osName.startsWith ("SunOS")) // NOI18N
            operatingSystem = OS_SOLARIS;
        else if ("Linux".equals (osName)) // NOI18N
            operatingSystem = OS_LINUX;
        else if ("OS/2".equals (osName)) // NOI18N
            operatingSystem = OS_OS2;
        else
            operatingSystem = OS_OTHER;
        return operatingSystem;
    }
    
    /** Getter for property fromIDE.
     * @return Value of property fromIDE.
     */
    static boolean isFromIDE() {
        return fromIDE;
    }

    static UpdaterFrame getUpdaterFrame() {
        return panel;
    }
    
    private Color stringToColor( String key, Color defcolor ) {
        try {
            String str = Localization.getBrandedString( key + "_R" );  // NOI18N
            int re = Integer.parseInt( str );
            
            str = Localization.getBrandedString( key + "_G" );  // NOI18N
            int gr = Integer.parseInt( str );
            
            str = Localization.getBrandedString( key + "_B" );  // NOI18N
            int bl = Integer.parseInt( str );
            
            return new Color ( re, gr, bl );
        } catch ( Exception e ) {
            return defcolor;
        }
    }
    
    private boolean addBorder() {
        return "true".equals( Localization.getBrandedString( "UpdaterFrame.hasBorder" ) ); // NOI18N
    }
    
    
    private void loadSplash() {
        URL lookup = Localization.getBrandedResource( SPLASH_PATH, ".gif" ); // NOI18N
        if ( lookup != null )
            jLabel3.setIcon( new ImageIcon( lookup ) );
    }
        
    static class SplashFrame extends JFrame {
        
        /** Creates a new SplashFrame */
        public SplashFrame () {
            super (getMainWindowTitle ());
            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
            // add splash component
            getContentPane().add(panel);
            center(this);
        }
        
        public java.awt.Dimension getPreferredSize () {
            return new java.awt.Dimension (400, 280);
        }
    }
    
    static class SplashWindow extends Window {
        /** Creates a new SplashWindow */
        public SplashWindow () {
            super(new Frame());
            // add splash component
            setLayout (new java.awt.BorderLayout ());
            add(panel, java.awt.BorderLayout.CENTER);
            center(this);
        }

        public java.awt.Dimension getPreferredSize () {
            return new java.awt.Dimension (400, 280);
        }        
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.