View Javadoc

    1   package baseCode.gui;
    2
    3   import java.awt.BorderLayout;
    4   import java.awt.Color;
    5   import java.awt.Dimension;
    6   import java.awt.event.ActionEvent;
    7   import java.awt.event.ActionListener;
    8   import java.io.IOException;
    9   import java.net.URL;
    10
    11  import javax.swing.JEditorPane;
    12  import javax.swing.JFrame;
    13  import javax.swing.JLabel;
    14  import javax.swing.JPanel;
    15  import javax.swing.JScrollPane;
    16  import javax.swing.JTextField;
    17  import javax.swing.event.HyperlinkEvent;
    18  import javax.swing.event.HyperlinkListener;
    19
    20  /***
    21   * @author Will Braynen;
    22   */
    23  public class JWebBrowser extends JFrame implements HyperlinkListener,
    24        ActionListener {
    25
    26     private JTextField m_urlField;
    27     private JEditorPane m_htmlPane;
    28     private String m_initialURL;
    29
    30     public static void main( String[] args ) {
    31        if ( args.length == 0 )
    32           new JWebBrowser( "http://microarray.genomecenter.columbia.edu/ermineJ" );
    33        else
    34           new JWebBrowser( args[0] );
    35     }
    36
    37     public JWebBrowser( String initialURL ) {
    38        super( "Web Browser" );
    39        m_initialURL = initialURL;
    40
    41        JPanel topPanel = new JPanel();
    42        topPanel.setBackground( Color.lightGray );
    43        JLabel urlLabel = new JLabel( "URL:" );
    44        m_urlField = new JTextField( 30 );
    45        m_urlField.setText( initialURL );
    46        m_urlField.addActionListener( this );
    47        topPanel.add( urlLabel );
    48        topPanel.add( m_urlField );
    49        getContentPane().add( topPanel, BorderLayout.NORTH );
    50
    51        try {
    52           m_htmlPane = new JEditorPane( initialURL );
    53           m_htmlPane.setEditable( false );
    54           m_htmlPane.addHyperlinkListener( this );
    55           JScrollPane scrollPane = new JScrollPane( m_htmlPane );
    56           getContentPane().add( scrollPane, BorderLayout.CENTER );
    57        } catch ( IOException e ) {
    58           GuiUtil.error( "Can't build HTML pane for " + initialURL, e );
    59        }
    60
    61        Dimension screenSize = getToolkit().getScreenSize();
    62        int width = screenSize.width * 8 / 10;
    63        int height = screenSize.height * 8 / 10;
    64        setBounds( width / 8, height / 8, width, height );
    65        setVisible( true );
    66     }
    67
    68     public void actionPerformed( ActionEvent event ) {
    69        String url;
    70        if ( event.getSource() == m_urlField )
    71           url = m_urlField.getText();
    72        else
    73           // Clicked "home" button instead of entering URL (if we had a home button)
    74           url = m_initialURL;
    75        try {
    76           m_htmlPane.setPage( new URL( url ) );
    77           m_urlField.setText( url );
    78        } catch ( IOException e ) {
    79           GuiUtil.error( "Can't follow link to " + url, e );
    80        }
    81     }
    82
    83     public void hyperlinkUpdate( HyperlinkEvent event ) {
    84        if ( event.getEventType() == HyperlinkEvent.EventType.ACTIVATED ) {
    85           try {
    86              m_htmlPane.setPage( event.getURL() );
    87              m_urlField.setText( event.getURL().toExternalForm() );
    88           } catch ( IOException e ) {
    89              GuiUtil.error( "Can't follow link to "
    90                    + event.getURL().toExternalForm(), e );
    91           }
    92        }
    93     }
    94  }
    95