-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSimpleWebBrowser.java
137 lines (118 loc) · 3.93 KB
/
SimpleWebBrowser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.net.URL;
/**
* Defines a simple web browser that can load web pages from
* URLs specified by the user in a "Location" box. Almost all
* the functionality is provided automatically by the JEditorPane
* class. The program loads web pages synchronously and can hang
* indefinitely while trying to load a page. See
* SimpleWebBrowserWithThread for an asynchronous version.
* This class can be run as a standalone application.
*/
public class SimpleWebBrowser extends JPanel {
/**
* The main routine simply opens a window that shows a SimpleWebBrowser panel.
*/
public static void main(String[] args) {
JFrame window = new JFrame("SimpleWebBrowser");
SimpleWebBrowser content = new SimpleWebBrowser();
window.setContentPane(content);
window.setSize(600,500);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
/**
* The pane in which documents are displayed.
*/
private JEditorPane editPane;
/**
* An input box where the user enters the URL of a document
* to be loaded into the edit pane. A valid URL string has
* to contain the substring "://". If the string in the box
* does not contain this substring, then "http://" is
* prepended to the string.
*/
private JTextField locationInput;
/**
* Defines a listener that responds when the user clicks on
* a link in the document.
*/
private class LinkListener implements HyperlinkListener {
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
loadURL(evt.getURL());
}
}
}
/**
* Defines a listener that loads a new page when the user
* clicks the "Go" button or presses return in the location
* input box.
*/
private class GoListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
URL url;
try {
String location = locationInput.getText().trim();
if (location.length() == 0)
throw new Exception();
if (! location.contains("://"))
location = "http://" + location;
url = new URL(location);
}
catch (Exception e) {
JOptionPane.showMessageDialog(SimpleWebBrowser.this,
"The Location input box does not\ncontain a legal URL.");
return;
}
loadURL(url);
locationInput.selectAll();
locationInput.requestFocus();
}
}
/**
* Construct a panel that contains a JEditorPane in a JScrollPane,
* with a tool bar that has a Location input box and a Go button.
*/
public SimpleWebBrowser() {
setBackground(Color.BLACK);
setLayout(new BorderLayout(1,1));
setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
editPane = new JEditorPane();
editPane.setEditable(false);
editPane.addHyperlinkListener(new LinkListener());
add(new JScrollPane(editPane),BorderLayout.CENTER);
JToolBar toolbar = new JToolBar();
toolbar.setFloatable(false);
add(toolbar,BorderLayout.NORTH);
ActionListener goListener = new GoListener();
locationInput = new JTextField("math.hws.edu/javanotes/index.html", 40);
locationInput.addActionListener(goListener);
JButton goButton = new JButton(" Go ");
goButton.addActionListener(goListener);
toolbar.add( new JLabel(" Location: "));
toolbar.add(locationInput);
toolbar.addSeparator(new Dimension(5,0));
toolbar.add(goButton);
}
/**
* Loads the document at the specified URL into the edit pane.
*/
private void loadURL(URL url) {
try {
editPane.setPage(url);
}
catch (Exception e) {
editPane.setContentType("text/plain");
editPane.setText( "Sorry, the requested document was not found\n"
+"or cannot be displayed.\n\nError:" + e);
}
}
}