import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
// The Mini Web Browser.
public class MiniBrowser
extends JFrame {
// These are the buttons for iterating through the page list.
private JButton backButton, forwardButton;
// Page location text field.
// Editor pane for displaying pages.
// Browser's list of pages that have been visited.
// Constructor for Mini Web Browser.
public MiniBrowser()
{
// Set application title.
super("WebBorwser - created by Ikram");
// Set window size.
setSize(640, 480);
// Handle closing events.
actionExit();
}
});
// Set up file menu.
actionExit();
}
});
fileMenu.add(fileExitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// Set up button panel.
backButton =
new JButton("< Back");
actionBack();
}
});
backButton.setEnabled(false);
buttonPanel.add(backButton);
forwardButton =
new JButton("Forward >");
actionForward();
}
});
forwardButton.setEnabled(false);
buttonPanel.add(forwardButton);
locationTextField.
addKeyListener(new KeyAdapter() {if (e.
getKeyCode() ==
KeyEvent.
VK_ENTER) { actionGo();
}
}
});
buttonPanel.add(locationTextField);
actionGo();
}
});
buttonPanel.add(goButton);
// Set up page display.
displayEditorPane.setContentType("text/html");
displayEditorPane.setEditable(false);
displayEditorPane.addHyperlinkListener(this);
getContentPane
().
add(new JScrollPane(displayEditorPane
),
}
// Exit this program.
private void actionExit() {
}
// Go back to the page viewed before the current page.
private void actionBack() {
URL currentUrl = displayEditorPane.
getPage();
int pageIndex = pageList.indexOf(currentUrl.toString());
try {
showPage(
new URL((String) pageList.
get(pageIndex -
1)),
false);
}
}
// Go forward to the page viewed after the current page.
private void actionForward() {
URL currentUrl = displayEditorPane.
getPage();
int pageIndex = pageList.indexOf(currentUrl.toString());
try {
showPage(
new URL((String) pageList.
get(pageIndex +
1)),
false);
}
}
// Load and show the page specified in the location text field.
private void actionGo() {
URL verifiedUrl = verifyUrl
(locationTextField.
getText());
if (verifiedUrl != null) {
showPage(verifiedUrl, true);
} else {
showError("Invalid URL");
}
}
// Show dialog box with error message.
private void showError
(String errorMessage
) { }
// Verify URL format.
// Only allow HTTP URLs.
if (!url.toLowerCase().startsWith("http://"))
return null;
// Verify format of URL.
try {
verifiedUrl =
new URL(url
);
return null;
}
return verifiedUrl;
}
/* Show the specified page and add it to
the page list if specified. */
private void showPage
(URL pageUrl,
boolean addToList
) {
// Show hour glass cursor while crawling is under way.
try {
// Get URL of page currently being displayed.
URL currentUrl = displayEditorPane.
getPage();
// Load and display specified page.
displayEditorPane.setPage(pageUrl);
// Get URL of new page being displayed.
URL newUrl = displayEditorPane.
getPage();
// Add page to list if specified.
if (addToList) {
int listSize = pageList.size();
if (listSize > 0) {
int pageIndex =
pageList.indexOf(currentUrl.toString());
if (pageIndex < listSize - 1) {
for (int i = listSize - 1; i > pageIndex; i--) {
pageList.remove(i);
}
}
}
pageList.add(newUrl.toString());
}
// Update location text field with URL of current page.
locationTextField.setText(newUrl.toString());
// Update buttons based on the page being displayed.
updateButtons();
}
{
// Show error messsage.
showError("Unable to load page");
}
finally
{
// Return to default cursor.
setCursor
(Cursor.
getDefaultCursor());
}
}
/* Update back and forward buttons based on
the page being displayed. */
private void updateButtons() {
if (pageList.size() < 2) {
backButton.setEnabled(false);
forwardButton.setEnabled(false);
} else {
URL currentUrl = displayEditorPane.
getPage();
int pageIndex = pageList.indexOf(currentUrl.toString());
backButton.setEnabled(pageIndex > 0);
forwardButton.setEnabled(
pageIndex < (pageList.size() - 1));
}
}
// Handle hyperlink's being clicked.
document.processHTMLFrameHyperlinkEvent(linkEvent);
} else {
showPage(event.getURL(), true);
}
}
}
// Run the Mini Browser.
public static void main
(String[] args
) { MiniBrowser browser = new MiniBrowser();
browser.show();
}
}