import javax.swing.text.html.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
@author
public class Browser {
@param jep@param url
protected static void setPage(JEditorPane jep, String url){
try {
jep.setPage(url);
}
catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
class backButtonListener implements ActionListener {
protected JEditorPane jep;
protected JLabel label;
protected JButton backButton;
protected Vector history;
public backButtonListener(JEditorPane jep, JButton backButton, Vector history, JLabel label){
this.jep = jep;
this.backButton = backButton;
this.history = history;
this.label = label;
}
@param e
public void actionPerformed(ActionEvent e){
try{
String curl = (String)history.lastElement();
history.removeElement(curl);
curl = (String)history.lastElement();
System.out.println("Back to " + curl);
setPage(jep,curl);
label.setText("<html><b>URL:</b> "+ curl);
if (history.size() == 1)
backButton.setEnabled(false);
}
catch (Exception ex){
System.out.println("Exception " + ex);
}
}
}
class LinkFollower implements HyperlinkListener {
protected JEditorPane jep;
protected JLabel label;
protected JButton backButton;
protected Vector history;
public LinkFollower(JEditorPane jep, JButton backButton, Vector history, JLabel label){
this.jep = jep;
this.backButton = backButton;
this.history = history;
this.label = label;
}
@param evt
public void hyperlinkUpdate(HyperlinkEvent evt){
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
try {
String currentURL = evt.getURL().toString();
history.add(currentURL);
backButton.setEnabled(true);
System.out.println("Going to " + currentURL);
setPage(jep,currentURL);
label.setText("<html><b>URL:</b> "+ currentURL);
}
catch (Exception e) {
System.out.println("ERROR: Trouble fetching url");
}
}
}
}
@param initialPage
public Browser(String initialPage){
Vector history = new Vector();
history.add(initialPage);
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
setPage(jep, initialPage);
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Simple Web Browser");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
JLabel label = new JLabel("<html><b>URL:</b> "+ initialPage);
JButton backButton = new JButton ("Back");
backButton.setActionCommand("back");
backButton.setToolTipText("Go to previous page");
backButton.setEnabled(false);
backButton.addActionListener(new backButtonListener(jep, backButton, history, label));
JButton exitButton = new JButton ("Exit");
exitButton.setActionCommand("exit");
exitButton.setToolTipText("Quit this application");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JToolBar toolBar = new JToolBar();
toolBar.add(backButton);
toolBar.add(exitButton);
jep.addHyperlinkListener(new LinkFollower(jep, backButton, history, label));
JPanel contentPane = (JPanel)f.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(400, 100));
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(label, BorderLayout.SOUTH);
f.pack();
f.setSize(640, 360);
f.setVisible(true);
}
public static void main(String[] args) {
String initialPage = new String("http://www.cse.sc.edu");
if (args.length > 0) initialPage = args[0];
Browser b = new Browser(initialPage);
}
}