HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Designing a Web Browser"
Volume: 4 Issue: 4, p. 54

	

Listing 1.
 
import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.plaf.metal.*; 
import com.sun.java.swing.plaf.motif.*; 
import com.sun.java.swing.plaf.windows.*; 
import ice.iblite.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.StringTokenizer; 
import java.beans.*; 
public class SimpleBrowser extends JFrame implements 
PropertyChangeListener, WindowListener { 

  Browser iceBrowser; 
  ControlPanel control = new ControlPanel(); 
  LinkPanel link = new LinkPanel(); 
  LocationPanel location = new LocationPanel(); 

  class ControlButton extends JButton { 
    ControlButton() { 
      setBorderPainted(false); 
      setFocusPainted(false); 
      setHorizontalTextPosition(SwingConstants.CENTER); 
      setVerticalTextPosition(SwingConstants.BOTTOM); 
    } 
  } 

  class MouseEventListener implements MouseListener { 
    JButton button; 
    MouseEventListener(JButton button) { 
      this.button = button; 
    } 
    public void mouseClicked(MouseEvent ev) { 
    } 
    public void mouseEntered(MouseEvent ev) { 
      button.setBorderPainted(true); 
    } 
    public void mouseExited(MouseEvent ev) { 
      button.setBorderPainted(false); 
    } 
    public void mousePressed(MouseEvent ev) { 
    } 
    public void mouseReleased(MouseEvent ev) { 
    } 
  } 

  class OpenListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      String result; 
      result = JOptionPane.showInputDialog("Enter the 
URL or specify the local file you would like to open : "); 
      if (result != null) { 
        iceBrowser.gotoLocation(result); 
      } 
    } 
  } 

  class BackListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      iceBrowser.goBack(); 
    } 
  } 

  class ForwardListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      iceBrowser.goForward(); 
    } 
  } 

  class ReloadListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      iceBrowser.reload(); 
    } 
  } 

  class ExitListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      System.exit(0); 
    } 
  } 

  class ControlPanel extends JPanel { 

    private JLabel status; 
    private boolean idle = false; 
    private ControlButton back; 
    private ControlButton forward; 

    ControlPanel() { 

      setBorder(new EtchedBorder()); 
      GridBagLayout gridbag = new GridBagLayout(); 
      setLayout(gridbag); 
      GridBagConstraints c = new GridBagConstraints(); 

      JPanel panel1 = new JPanel(); 
      panel1.setLayout(new FlowLayout 
(java.awt.FlowLayout.LEFT,0,0)); 

      ControlButton open = new ControlButton(); 
      open.setIcon(new ImageIcon("images/OpenArrow.gif")); 
      open.setRolloverIcon(new ImageIcon 
("images/BrightOpenArrow.gif")); 
      open.setText("Open"); 
      open.addActionListener(new OpenListener()); 
      open.addMouseListener(new MouseEventListener(open)); 
      open.setToolTipText("Open a document"); 

      back = new ControlButton(); 
      back.setIcon(new ImageIcon("images/Left.gif")); 
      back.setRolloverIcon(new ImageIcon 
("images/BrightLeft.gif")); 
      back.setDisabledIcon(new ImageIcon 
("images/DarkLeft.gif")); 
      back.setText("Back"); 
      back.setEnabled(false); 
      back.addActionListener(new BackListener()); 
      back.addMouseListener(new MouseEventListener(back)); 
      back.setToolTipText("Go to previous page"); 

      forward = new ControlButton(); 
      forward.setIcon(new ImageIcon("images/Right.gif")); 
      forward.setRolloverIcon(new ImageIcon 
("images/BrightRight.gif")); 
      forward.setDisabledIcon(new ImageIcon 
("images/DarkRight.gif")); 
      forward.setText("Forward"); 
      forward.setEnabled(false); 
      forward.addActionListener(new ForwardListener()); 
      forward.addMouseListener 
(new MouseEventListener(forward)); 
      forward.setToolTipText("Go to next page"); 

      ControlButton reload = new ControlButton(); 
      reload.setIcon(new ImageIcon("images/RotCCDown.gif")); 
      reload.setRolloverIcon(new ImageIcon 
("images/BrightRotCCDown.gif")); 
      reload.setText("Reload"); 
      reload.setToolTipText("Reload this page from the server"); 
      reload.addActionListener(new ReloadListener()); 
      reload.addMouseListener(new MouseEventListener(reload)); 
      reload.setToolTipText("Reload this page from the server"); 

      ControlButton exit = new ControlButton(); 
      exit.setIcon(new ImageIcon("images/Exit.gif")); 
      exit.setRolloverIcon(new ImageIcon("images/BrightExit.gif")); 
      exit.setText("Exit"); 
      exit.addActionListener(new ExitListener()); 
      exit.addMouseListener(new MouseEventListener(exit)); 
      exit.setToolTipText("Exit from the browser"); 

      panel1.add(open); 
      panel1.add(back); 
      panel1.add(forward); 
      panel1.add(reload); 
      panel1.add(exit); 

      status = new JLabel(); 
      status.setIcon(new ImageIcon("images/fdbk-anim.gif")); 
      status.setBorder(new BevelBorder 
(javax.swing.border.BevelBorder.RAISED)); 

      JPanel panel2 = new JPanel(); 
      panel2.setLayout(new FlowLayout 
(java.awt.FlowLayout.RIGHT,0,0)); 
      panel2.add(status); 

      c.weightx = 1.0; 
      c.anchor = GridBagConstraints.WEST; 
      c.fill = GridBagConstraints.NONE; 
      c.gridx = 1; c.gridy = 1; 
      gridbag.setConstraints(panel1, c); 
      add(panel1); 

      c.anchor = GridBagConstraints.EAST; 
      c.fill = GridBagConstraints.NONE; 
      c.gridx = 2; c.gridy = 1; 
      c.insets = new Insets(0,0,0,10); 
      gridbag.setConstraints(panel2, c); 
      add(panel2); 

    } 

    void setBusy() { 
      status.setIcon(new ImageIcon("images/fdbk-anim.gif")); 
      idle = false; 
    } 

    void setIdle() { 
      if (!(idle)) { 
        status.setIcon(new ImageIcon("images/fdbk-rest.gif")); 
        idle = true; 
      } 
    } 

    void setState() { 
      int s = iceBrowser.getBackHistory().size(); 
      if (s == 0) { 
        back.setEnabled(false); 
        back.setToolTipText("Go to previous page"); 
      } else { 
        back.setEnabled(true); 
        back.setToolTipText((String)iceBrowser.getBackHistory() 
.elementAt(s-1)); 
      } 
      if (iceBrowser.getForwardHistory().size() == 0) { 
        forward.setEnabled(false); 
        forward.setToolTipText("Go to next page"); 
      } else { 
        forward.setEnabled(true); 
        forward.setToolTipText 
((String)iceBrowser.getForwardHistory() 
.elementAt(0)); 
      } 
    } 

  } 

  class LinkPanel extends JPanel { 

    JProgressBar progressBar; 
    JLabel label; 
    JPanel panel1; 

    LinkPanel() { 
      setBorder(new EtchedBorder()); 
      GridBagLayout gridbag = new GridBagLayout(); 
      setLayout(gridbag); 
      GridBagConstraints c = new GridBagConstraints(); 

      label = new JLabel(" "); 
      progressBar = new JProgressBar(); 
      progressBar.setMinimum(0); 

      panel1 = new JPanel(); 
      panel1.setLayout(gridbag); 
      panel1.add(label); 
      c.weightx = 1.0; 
      c.anchor = GridBagConstraints.WEST; 
      c.fill = GridBagConstraints.BOTH; 
      c.gridx = 1; c.gridy = 1; 
      gridbag.setConstraints(label, c); 
      panel1.add(label); 
      c.insets = new Insets(5,5,5,5); 
      gridbag.setConstraints(panel1, c); 

      JPanel panel2 = new JPanel(); 
      panel2.setLayout(new FlowLayout 
(java.awt.FlowLayout.RIGHT,0,0)); 
      panel2.add(progressBar); 
      c.weightx = 0.0; 
      c.anchor = GridBagConstraints.EAST; 
      c.fill = GridBagConstraints.NONE; 
      c.gridx = 2; c.gridy = 1; 
      gridbag.setConstraints(panel2, c); 

      add(panel1); 
      add(panel2); 

    } 

    void setLabel(String s) { 
      label.setText(s); 
    } 

    void setProgressBarMax(int v) { 
      progressBar.setMaximum(v); 
    } 

    void setProgressBarValue(int v) { 
      progressBar.setValue(v); 
    } 

  } 

  class LocationPanel extends JPanel implements KeyListener { 

    JTextField textField; 

    LocationPanel() { 
      setBorder(new EtchedBorder()); 

      GridBagLayout gridbag = new GridBagLayout(); 
      setLayout(gridbag); 
      GridBagConstraints c = new GridBagConstraints(); 
      c.anchor = GridBagConstraints.WEST; 
      c.insets = new Insets(4,4,4,4); 

      JLabel label = new JLabel("Location: "); 
      label.setIcon(new ImageIcon("images/World.gif")); 
      textField = new JTextField(); 
      textField.addKeyListener(this); 

      c.fill = GridBagConstraints.NONE; 
      c.gridx = 1; c.gridy = 1; 
      gridbag.setConstraints(label, c); 

      c.fill = GridBagConstraints.BOTH; 
      c.gridx = 2; c.gridy = 1; 
      c.weightx = 1.0; 
      gridbag.setConstraints(textField, c); 
  

      add(label); 
      add(textField); 
    } 

    public void keyTyped(KeyEvent ev) { 
    } 

    public void keyPressed(KeyEvent ev) { 
      if (ev.getKeyCode() == KeyEvent.VK_ENTER) 
        iceBrowser.gotoLocation(textField.getText()); 
    } 

    public void keyReleased(KeyEvent ev) { 
    } 

    void setText(String s) { 
      textField.setText(s); 
    } 

  } 

  public void propertyChange(PropertyChangeEvent ev) { 
    int max = 0; 
    int currentMax = 0; 
    int current = 0; 
    String currentLabel = "Document : Done"; 
    String prop = ev.getPropertyName(); 
    System.out.println(prop); 
    control.setState(); 
    if (prop.equals("statusString")) { 
      String s = (String)ev.getNewValue(); 
      if (s.equals("")) { 
        setCursor(new Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
        link.setLabel(currentLabel); 
      } else { 
        setCursor(new Cursor(java.awt.Cursor.HAND_CURSOR)); 
        link.setLabel(s); 
      } 
    } else if (prop.equals("currentLocation")) { 
      System.out.println((String)ev.getNewValue()); 
    } else if (prop.equals("documentBase")) { 
      location.setText((String)ev.getNewValue()); 
    } else if (prop.equals("documentTitle")) { 
      setTitle((String)ev.getNewValue()); 
      control.setBusy(); 
      link.setProgressBarValue(0); 
    } else if (prop.equals("parsingProgress")) { 
      String status = iceBrowser.getParsingProgress(); 
      StringTokenizer st = new StringTokenizer(status, " "); 
      String tokens[] = new String[3]; 
      int i = 0; 
      while (st.hasMoreTokens()) { 
        tokens[i] = st.nextToken(); 
        System.out.println(tokens[i]); 
        i++; 
      } 
      //if (!(tokens[2].equals("-1"))) { 
      //  current = java.lang.Integer.valueOf(tokens[1]) 
.intValue(); 
      //  currentMax = java.lang.Integer.valueOf(tokens[2]) 
.intValue(); 
      // v1.405 seems to return "charsRead totalChars frameName" 
instead of 
      // "frameName charsRead totalChars" 
      if (!(tokens[1].equals("-1"))) { 
        current = java.lang.Integer.valueOf(tokens[0]) 
.intValue(); 
        currentMax = java.lang.Integer.valueOf(tokens[1]) 
.intValue(); 
        if (currentMax == current) { 
          currentLabel = "Document : Done"; 
          control.setIdle(); 
        } else { 
          currentLabel = "Reading"; 
        } 
        link.setLabel(currentLabel); 
        if (max != currentMax) { 
          max = currentMax; 
          link.setProgressBarMax(max); 
        } 
        link.setProgressBarValue(current); 
      } 
    } 
  } 

  public SimpleBrowser() { 

    setSize(800, 500); 
    addWindowListener(this); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    panel.add("Center", control); 
    panel.add("South", location); 

    iceBrowser = new Browser(); 
    iceBrowser.addPropertyChangeListener(this); 

    getContentPane().add("North", panel); 
    getContentPane().add("Center", iceBrowser); 
    getContentPane().add("South", link); 

    iceBrowser.gotoLocation("http://www.javasoft.com/"); 

  } 

  public void windowClosing( WindowEvent event ) { dispose(); } 
  public void windowOpened( WindowEvent event ) {} 
  public void windowIconified( WindowEvent event ) {} 
  public void windowDeiconified( WindowEvent event ) {} 
  public void windowClosed( WindowEvent event ) { System.exit(0);} 
  public void windowActivated( WindowEvent event ) {} 
  public void windowDeactivated( WindowEvent event ) {} 

  public static void main(String args[]) { 
    String lnfName; 
    if (new MotifLookAndFeel().isNativeLookAndFeel()) { 
      lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; 
    } else if (new WindowsLookAndFeel().isNativeLookAndFeel()) { 
      lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 
    } else { 
      lnfName = "javax.swing.plaf.metal.MetalLookAndFeel"; 
    } 
    try { 
      System.out.println(lnfName); 
      UIManager.setLookAndFeel(lnfName); 
    } catch (Exception ex) { 
      ex.printStackTrace(); 
    } 
    new SimpleBrowser().show(); 
  } 

} 

  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.