View Javadoc

    1   package baseCode.gui;
    2
    3   import java.awt.BorderLayout;
    4   import java.awt.Color;
    5   import java.awt.Container;
    6   import java.awt.Dimension;
    7   import java.awt.Point;
    8   import java.awt.event.ActionEvent;
    9   import java.awt.event.MouseEvent;
    10  import java.awt.event.MouseListener;
    11  import java.io.IOException;
    12
    13  import javax.swing.JButton;
    14  import javax.swing.JDialog;
    15  import javax.swing.JEditorPane;
    16  import javax.swing.JFrame;
    17  import javax.swing.JLabel;
    18  import javax.swing.JPanel;
    19  import javax.swing.event.HyperlinkEvent;
    20  import javax.swing.event.HyperlinkListener;
    21
    22  import baseCode.util.BrowserLauncher;
    23
    24  /***
    25   * <p>
    26   * Title:
    27   * </p>
    28   * <p>
    29   * Description:
    30   * </p>
    31   * <p>
    32   * Copyright: Copyright (c) 2003
    33   * </p>
    34   * <p>
    35   * Company:
    36   * </p>
    37   *
    38   * @author Homin Lee
    39   * @version $Id: AppDialog.java,v 1.7 2004/07/27 03:18:58 pavlidis Exp $
    40   */
    41
    42  public abstract class AppDialog extends JDialog {
    43     /***
    44      * <hr>
    45      * <p>
    46      * Copyright (c) 2004 Columbia University
    47      *
    48      * @author pavlidis
    49      * @version $Id: AppDialog.java,v 1.7 2004/07/27 03:18:58 pavlidis Exp $
    50      */
    51
    52     JPanel mainPanel;
    53     BorderLayout borderLayout1 = new BorderLayout();
    54     JPanel contentPanel = new JPanel();
    55     JPanel bottomPanel = new JPanel();
    56     protected JButton actionButton = new JButton();
    57     protected JButton cancelButton = new JButton();
    58     protected JButton helpButton = new JButton();
    59
    60     protected Container callingframe;
    61
    62     public AppDialog() {
    63
    64     }
    65
    66     public AppDialog( JFrame callingframe, int width, int height ) {
    67        this.callingframe = callingframe;
    68        setModal( true );
    69        jbInit( width, height );
    70     }
    71
    72     private void jbInit( int width, int height ) {
    73        setResizable( true );
    74        mainPanel = ( JPanel ) this.getContentPane();
    75        mainPanel.setPreferredSize( new Dimension( width, height ) );
    76        mainPanel.setLayout( borderLayout1 );
    77
    78        contentPanel.setPreferredSize( new Dimension( width, height - 40 ) );
    79        BorderLayout borderLayout4 = new BorderLayout();
    80        contentPanel.setLayout( borderLayout4 );
    81
    82        bottomPanel.setPreferredSize( new Dimension( width, 40 ) );
    83        cancelButton.setText( "Cancel" );
    84        cancelButton.setMnemonic( 'c' );
    85        cancelButton.addActionListener( new AppDialog_cancelButton_actionAdapter(
    86              this ) );
    87        actionButton.addActionListener( new AppDialog_actionButton_actionAdapter(
    88              this ) );
    89
    90        helpButton.addActionListener( new AppDialog_helpButton_actionAdapter(
    91              this ) );
    92        helpButton.setText( "Help" );
    93
    94        bottomPanel.add( helpButton, null );
    95        bottomPanel.add( cancelButton, null );
    96        bottomPanel.add( actionButton, null );
    97        mainPanel.add( contentPanel, BorderLayout.CENTER );
    98        mainPanel.add( bottomPanel, BorderLayout.SOUTH );
    99     }
    100
    101    public void showDialog() {
    102       Dimension dlgSize = getPreferredSize();
    103       Dimension frmSize = callingframe.getSize();
    104       Point loc = callingframe.getLocation();
    105       setLocation( ( frmSize.width - dlgSize.width ) / 2 + loc.x,
    106             ( frmSize.height - dlgSize.height ) / 2 + loc.y );
    107       pack();
    108       actionButton.requestFocusInWindow();
    109       show();
    110    }
    111
    112    // helper to respond to links.
    113    class LinkFollower implements HyperlinkListener {
    114
    115       /*
    116        * (non-Javadoc)
    117        *
    118        * @see javax.swing.event.HyperlinkListener#hyperlinkUpdate(javax.swing.event.HyperlinkEvent)
    119        */
    120       public void hyperlinkUpdate( HyperlinkEvent e ) {
    121          if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
    122             try {
    123                BrowserLauncher.openURL( e.getURL().toExternalForm() );
    124             } catch ( IOException e1 ) {
    125                GuiUtil.error( "Could not open link" );
    126             }
    127          }
    128       }
    129    }
    130
    131    // Slightly specialized editor pane.
    132    class HelpEditorPane extends JEditorPane {
    133
    134       HelpEditorPane( String text ) {
    135          super();
    136          this.setEditable( false );
    137          this.setContentType( "text/html" );
    138          this.setText( text );
    139          this.addHyperlinkListener( new LinkFollower() );
    140       }
    141
    142    }
    143
    144    // @todo why using spaces for layout?
    145    protected void addHelp( String text ) {
    146
    147       HelpEditorPane helpArea = null;
    148
    149       helpArea = new HelpEditorPane( text );
    150       JLabel jLabel1 = new JLabel( "      " );
    151       JLabel jLabel2 = new JLabel( " " );
    152       JLabel jLabel3 = new JLabel( " " );
    153       JLabel jLabel4 = new JLabel( "      " );
    154       BorderLayout borderLayout2 = new BorderLayout();
    155       JPanel labelPanel = new JPanel();
    156       labelPanel.setBackground( Color.WHITE );
    157       labelPanel.setLayout( borderLayout2 );
    158       labelPanel.add( helpArea, BorderLayout.CENTER );
    159       labelPanel.add( jLabel1, BorderLayout.WEST );
    160       labelPanel.add( jLabel2, BorderLayout.NORTH );
    161       labelPanel.add( jLabel3, BorderLayout.SOUTH );
    162       labelPanel.add( jLabel4, BorderLayout.EAST );
    163       contentPanel.add( labelPanel, BorderLayout.NORTH );
    164
    165       helpArea.addMouseListener( new AppDialog_mouselistener_actionAdapter(
    166             this ) );
    167
    168    }
    169
    170    protected void addMain( JPanel panel ) {
    171       contentPanel.add( panel, BorderLayout.CENTER );
    172    }
    173
    174    protected void setActionButtonText( String val ) {
    175       actionButton.setText( val );
    176    }
    177
    178    protected void setCancelButtonText( String val ) {
    179       cancelButton.setText( val );
    180    }
    181
    182    protected void setHelpButtonText( String val ) {
    183       helpButton.setText( val );
    184    }
    185
    186    protected abstract void cancelButton_actionPerformed( ActionEvent e );
    187
    188    protected abstract void actionButton_actionPerformed( ActionEvent e );
    189
    190    protected abstract void helpButton_actionPerformed( ActionEvent e );
    191
    192    /***
    193     * @param e
    194     */
    195    public void mouseButton_actionPerformed( MouseEvent e ) {
    196       // TODO Auto-generated method stub
    197    }
    198
    199 }
    200
    201 class AppDialog_helpButton_actionAdapter implements
    202       java.awt.event.ActionListener {
    203    AppDialog adaptee;
    204
    205    AppDialog_helpButton_actionAdapter( AppDialog adaptee ) {
    206       this.adaptee = adaptee;
    207    }
    208
    209    public void actionPerformed( ActionEvent e ) {
    210       adaptee.helpButton_actionPerformed( e );
    211    }
    212 }
    213
    214 class AppDialog_cancelButton_actionAdapter implements
    215       java.awt.event.ActionListener {
    216    AppDialog adaptee;
    217
    218    AppDialog_cancelButton_actionAdapter( AppDialog adaptee ) {
    219       this.adaptee = adaptee;
    220    }
    221
    222    public void actionPerformed( ActionEvent e ) {
    223       adaptee.cancelButton_actionPerformed( e );
    224    }
    225 }
    226
    227 class AppDialog_actionButton_actionAdapter implements
    228       java.awt.event.ActionListener {
    229    AppDialog adaptee;
    230
    231    AppDialog_actionButton_actionAdapter( AppDialog adaptee ) {
    232       this.adaptee = adaptee;
    233    }
    234
    235    public void actionPerformed( ActionEvent e ) {
    236       adaptee.actionButton_actionPerformed( e );
    237    }
    238 }
    239
    240 class AppDialog_mouselistener_actionAdapter implements MouseListener {
    241
    242    AppDialog adaptee;
    243
    244    /***
    245     * @param adaptee
    246     */
    247    public AppDialog_mouselistener_actionAdapter( AppDialog adaptee ) {
    248       this.adaptee = adaptee;
    249    }
    250
    251    /*
    252     * (non-Javadoc)
    253     *
    254     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
    255     */
    256    public void mouseClicked( MouseEvent e ) {
    257       adaptee.mouseButton_actionPerformed( e );
    258    }
    259
    260    /*
    261     * (non-Javadoc)
    262     *
    263     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
    264     */
    265    public void mouseEntered( MouseEvent e ) {
    266    }
    267
    268    /*
    269     * (non-Javadoc)
    270     *
    271     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
    272     */
    273    public void mouseExited( MouseEvent e ) {
    274    }
    275
    276    /*
    277     * (non-Javadoc)
    278     *
    279     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
    280     */
    281    public void mousePressed( MouseEvent e ) {
    282    }
    283
    284    /*
    285     * (non-Javadoc)
    286     *
    287     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
    288     */
    289    public void mouseReleased( MouseEvent e ) {
    290    }
    291
    292 }