View Javadoc

    1   package baseCode.gui;
    2
    3   import java.awt.BorderLayout;
    4   import java.awt.Dimension;
    5   import java.awt.FlowLayout;
    6   import java.awt.Point;
    7   import java.awt.event.ActionEvent;
    8   import java.util.Vector;
    9
    10  import javax.swing.BorderFactory;
    11  import javax.swing.JButton;
    12  import javax.swing.JDialog;
    13  import javax.swing.JFrame;
    14  import javax.swing.JLabel;
    15  import javax.swing.JPanel;
    16  import javax.swing.SwingConstants;
    17
    18  import baseCode.util.StatusViewer;
    19
    20  /***
    21   *
    22   *
    23   *
    24   * <hr>
    25   * <p>Copyright (c) 2004-2005 Columbia University
    26   * @author pavlidis
    27   * @author Homin Lee
    28   * @version $Id: Wizard.java,v 1.10 2005/03/21 18:01:03 pavlidis Exp $
    29   */
    30  public abstract class Wizard extends JDialog {
    31     protected JPanel mainPanel;
    32     protected FlowLayout flowlayout1 = new FlowLayout();
    33     protected JPanel BottomPanel = new JPanel();
    34     protected JPanel BottomPanelWrap = new JPanel();
    35     protected JLabel jLabelStatus = new JLabel();
    36     protected JPanel jPanelStatus = new JPanel();
    37
    38     protected JButton nextButton = new JButton();
    39     protected JButton backButton = new JButton();
    40     protected JButton cancelButton = new JButton();
    41     protected JButton finishButton = new JButton();
    42
    43     Vector steps = new Vector();
    44     protected JFrame callingframe;
    45     private StatusViewer statusMessenger;
    46
    47     public Wizard( JFrame callingframe, int width, int height ) {
    48        //enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    49        this.callingframe = callingframe;
    50        setModal( true );
    51        jbInit( width, height );
    52     }
    53
    54     //Component initialization
    55     private void jbInit( int width, int height ) {
    56        setResizable( true );
    57        mainPanel = ( JPanel ) this.getContentPane();
    58        mainPanel.setPreferredSize( new Dimension( width, height ) );
    59        mainPanel.setLayout( new BorderLayout() );
    60
    61        // holds the buttons and the status bar.
    62        BottomPanelWrap.setLayout( new BorderLayout() );
    63
    64        //bottom buttons/////////////////////////////////////////////////////////
    65        BottomPanel.setPreferredSize( new Dimension( width, 40 ) );
    66        nextButton.setText( "Next >" );
    67        nextButton
    68              .addActionListener( new Wizard_nextButton_actionAdapter( this ) );
    69        nextButton.setMnemonic( 'n' );
    70        backButton.setText( "< Back" );
    71        backButton
    72              .addActionListener( new Wizard_backButton_actionAdapter( this ) );
    73        backButton.setEnabled( false );
    74        backButton.setMnemonic( 'b' );
    75        cancelButton.setText( "Cancel" );
    76        cancelButton.addActionListener( new Wizard_cancelButton_actionAdapter(
    77              this ) );
    78        cancelButton.setMnemonic( 'c' );
    79        finishButton.setText( "Finish" );
    80        finishButton.setMnemonic( 'f' );
    81        finishButton.addActionListener( new Wizard_finishButton_actionAdapter(
    82              this ) );
    83        BottomPanel.add( cancelButton, null );
    84        BottomPanel.add( backButton, null );
    85        BottomPanel.add( nextButton, null );
    86        BottomPanel.add( finishButton, null );
    87
    88        // status bar
    89        jPanelStatus.setBorder( BorderFactory.createEtchedBorder() );
    90        jLabelStatus.setFont( new java.awt.Font( "Dialog", 0, 11 ) );
    91        jLabelStatus.setPreferredSize( new Dimension( width - 40, 19 ) );
    92        jLabelStatus.setHorizontalAlignment( SwingConstants.LEFT );
    93        jPanelStatus.add( jLabelStatus, null );
    94        statusMessenger = new StatusJlabel( jLabelStatus );
    95
    96        BottomPanelWrap.add( BottomPanel, BorderLayout.NORTH );
    97        BottomPanelWrap.add( jPanelStatus, BorderLayout.SOUTH );
    98
    99        mainPanel.add( BottomPanelWrap, BorderLayout.SOUTH );
    100
    101    }
    102
    103    /***
    104     * Print a message to the status bar.
    105     *
    106     * @param a
    107     */
    108    public void showStatus( String a ) {
    109       statusMessenger.setStatus( a );
    110    }
    111
    112    /***
    113     * Print an error message to the status bar.
    114     *
    115     * @param a
    116     */
    117    public void showError( String a ) {
    118       statusMessenger.setError( a );
    119    }
    120
    121    /***
    122     * Make the status bar empty.
    123     */
    124    public void clearStatus() {
    125       statusMessenger.setStatus( "" );
    126    }
    127
    128    protected void addStep( int step, WizardStep panel ) {
    129       steps.add( step - 1, panel );
    130       if ( step == 1 )
    131             mainPanel.add( ( JPanel ) steps.get( 0 ), BorderLayout.CENTER );
    132    }
    133
    134    public void showWizard() {
    135       Dimension dlgSize = getPreferredSize();
    136       Dimension frmSize = callingframe.getSize();
    137       Point loc = callingframe.getLocation();
    138       setLocation( ( frmSize.width - dlgSize.width ) / 2 + loc.x,
    139             ( frmSize.height - dlgSize.height ) / 2 + loc.y );
    140       pack();
    141       nextButton.requestFocusInWindow();
    142       show();
    143    }
    144
    145    /***
    146     * Define what happens when the 'next' button is pressed
    147     *
    148     * @param e
    149     */
    150    protected abstract void nextButton_actionPerformed( ActionEvent e );
    151
    152    /***
    153     * Define what happens when the 'back' button is pressed
    154     *
    155     * @param e
    156     */
    157    protected abstract void backButton_actionPerformed( ActionEvent e );
    158
    159    /***
    160     * Define what happens when the 'cancel' button is pressed.
    161     *
    162     * @param e
    163     */
    164    protected abstract void cancelButton_actionPerformed( ActionEvent e );
    165
    166    /***
    167     * Define what happens when the 'finish' button is pressed.
    168     *
    169     * @param e
    170     */
    171    protected abstract void finishButton_actionPerformed( ActionEvent e );
    172
    173    /***
    174     * Disable the "finish" button, indicating the user has some steps to do yet.
    175     */
    176    public void setFinishDisabled() {
    177       this.finishButton.setEnabled( false );
    178    }
    179
    180    /***
    181     * Enable the "finish" button, indicating the user can get out of the wizard at this stage.
    182     */
    183    public void setFinishEnabled() {
    184       this.finishButton.setEnabled( true );
    185    }
    186 }
    187
    188 class Wizard_nextButton_actionAdapter implements java.awt.event.ActionListener {
    189    Wizard adaptee;
    190
    191    Wizard_nextButton_actionAdapter( Wizard adaptee ) {
    192       this.adaptee = adaptee;
    193    }
    194
    195    public void actionPerformed( ActionEvent e ) {
    196       adaptee.nextButton_actionPerformed( e );
    197    }
    198 }
    199
    200 class Wizard_backButton_actionAdapter implements java.awt.event.ActionListener {
    201    Wizard adaptee;
    202
    203    Wizard_backButton_actionAdapter( Wizard adaptee ) {
    204       this.adaptee = adaptee;
    205    }
    206
    207    public void actionPerformed( ActionEvent e ) {
    208       adaptee.backButton_actionPerformed( e );
    209    }
    210 }
    211
    212 class Wizard_cancelButton_actionAdapter implements
    213       java.awt.event.ActionListener {
    214    Wizard adaptee;
    215
    216    Wizard_cancelButton_actionAdapter( Wizard adaptee ) {
    217       this.adaptee = adaptee;
    218    }
    219
    220    public void actionPerformed( ActionEvent e ) {
    221       adaptee.cancelButton_actionPerformed( e );
    222    }
    223 }
    224
    225 class Wizard_finishButton_actionAdapter implements
    226       java.awt.event.ActionListener {
    227    Wizard adaptee;
    228
    229    Wizard_finishButton_actionAdapter( Wizard adaptee ) {
    230       this.adaptee = adaptee;
    231    }
    232
    233    public void actionPerformed( ActionEvent e ) {
    234       adaptee.finishButton_actionPerformed( e );
    235    }
    236 }
    237