Methods Summary |
---|
void | animate()Display the next frame. Called by the animator timer
String frame = animationFrames[animationFrame++]; // Get next frame
messageLine.setText(animationMessage + " " + frame); // Update msgline
animationFrame = animationFrame % animationFrames.length;
|
public void | back()Go back to the previously displayed page.
if (currentHistoryPage > 0) // go back, if we can
visit((URL)history.get(--currentHistoryPage));
// Enable or disable actions as appropriate
backAction.setEnabled((currentHistoryPage > 0));
forwardAction.setEnabled((currentHistoryPage < history.size()-1));
|
public void | close()Close this browser window. If this was the only open window,
and exitWhenLastBrowserClosed is true, then exit the VM
this.setVisible(false); // Hide the window
this.dispose(); // Destroy the window
synchronized(WebBrowser.class) { // Synchronize for thread-safety
WebBrowser.numBrowserWindows--; // There is one window fewer now
if ((numBrowserWindows==0) && exitWhenLastWindowClosed)
System.exit(0); // Exit if it was the last one
}
|
public void | displayPage(java.net.URL url)Ask the browser to display the specified URL, and put it in the
history list.
if (visit(url)) { // go to the specified url, and if we succeed:
history.add(url); // Add the url to the history list
int numentries = history.size();
if (numentries > MAX_HISTORY+10) { // Trim history when too large
history = history.subList(numentries-MAX_HISTORY, numentries);
numentries = MAX_HISTORY;
}
currentHistoryPage = numentries-1; // Set current history page
// If we can go back, then enable the Back action
if (currentHistoryPage > 0) backAction.setEnabled(true);
}
|
public void | displayPage(java.lang.String href)Like displayPage(URL), but takes a string instead
try {
displayPage(new URL(href));
}
catch (MalformedURLException ex) {
messageLine.setText("Bad URL: " + href);
}
|
public void | exit(boolean confirm)Exit the VM. If confirm is true, ask the user if they are sure.
Note that showConfirmDialog() displays a dialog, waits for the user,
and returns the user's response (i.e. the button the user selected).
if (!confirm ||
(JOptionPane.showConfirmDialog(this, // dialog parent
/* message to display */ "Are you sure you want to quit?",
/* dialog title */ "Really Quit?",
/* dialog buttons */ JOptionPane.YES_NO_OPTION) ==
JOptionPane.YES_OPTION)) // If Yes button was clicked
System.exit(0);
|
public void | forward()Go forward to the next page in the history list
if (currentHistoryPage < history.size()-1) // go forward, if we can
visit((URL)history.get(++currentHistoryPage));
// Enable or disable actions as appropriate
backAction.setEnabled((currentHistoryPage > 0));
forwardAction.setEnabled((currentHistoryPage < history.size()-1));
|
public java.lang.String | getHome() return home;
|
public void | home()Display the page specified by the "home" property displayPage(getHome());
|
public void | hyperlinkUpdate(javax.swing.event.HyperlinkEvent e)This method implements HyperlinkListener. It is invoked when the user
clicks on a hyperlink, or move the mouse onto or off of a link
HyperlinkEvent.EventType type = e.getEventType(); // what happened?
if (type == HyperlinkEvent.EventType.ACTIVATED) { // Click!
displayPage(e.getURL()); // Follow the link; display new page
}
else if (type == HyperlinkEvent.EventType.ENTERED) { // Mouse over!
// When mouse goes over a link, display it in the message line
messageLine.setText(e.getURL().toString());
}
else if (type == HyperlinkEvent.EventType.EXITED) { // Mouse out!
messageLine.setText(" "); // Clear the message line
}
|
public static void | main(java.lang.String[] args)A simple main() method that allows the WebBrowser class to be used
as a stand-alone application.
// End the program when there are no more open browser windows
WebBrowser.setExitWhenLastWindowClosed(true);
WebBrowser browser = new WebBrowser(); // Create a browser window
browser.setSize(800, 600); // Set its size
browser.setVisible(true); // Make it visible.
// Tell the browser what to display. This method is defined below.
browser.displayPage((args.length > 0) ? args[0] : browser.getHome());
|
public void | newBrowser()Open a new browser window
WebBrowser b = new WebBrowser();
b.setSize(this.getWidth(), this.getHeight());
b.setVisible(true);
|
public void | openPage()Allow the user to choose a local file, and display it
// Lazy creation: don't create the JFileChooser until it is needed
if (fileChooser == null) {
fileChooser = new JFileChooser();
// This javax.swing.filechooser.FileFilter displays only HTML files
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
String fn = f.getName();
if (fn.endsWith(".html") || fn.endsWith(".htm"))
return true;
else return false;
}
public String getDescription() { return "HTML Files"; }
};
fileChooser.setFileFilter(filter);
fileChooser.addChoosableFileFilter(filter);
}
// Ask the user to choose a file.
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// If they didn't click "Cancel", then try to display the file.
File selectedFile = fileChooser.getSelectedFile();
String url = "file://" + selectedFile.getAbsolutePath();
displayPage(url);
}
|
public void | propertyChange(java.beans.PropertyChangeEvent e)This method implements java.beans.PropertyChangeListener. It is
invoked whenever a bound property changes in the JEditorPane object.
The property we are interested in is the "page" property, because it
tells us when a page has finished loading.
if (e.getPropertyName().equals("page")) // If the page property changed
stopAnimation(); // Then stop the loading... animation
|
public void | reload()Reload the current page in the history list
if (currentHistoryPage != -1) {
// We can't reload the current document, so display a blank page
textPane.setDocument(new javax.swing.text.html.HTMLDocument());
// Now re-visit the current URL
visit((URL)history.get(currentHistoryPage));
}
|
public static void | setExitWhenLastWindowClosed(boolean b)Set the static property that controls the behavior of close()
exitWhenLastWindowClosed = b;
|
public void | setHome(java.lang.String home)These are accessor methods for the home property. this.home = home;
|
void | startAnimation(java.lang.String msg)Start the animation. Called by the visit() method.
animationMessage = msg; // Save the message to display
animationFrame = 0; // Start with frame 0 of the animation
animator.start(); // Tell the timer to start firing.
|
void | stopAnimation()Stop the animation. Called by propertyChanged() method.
animator.stop(); // Tell the timer to stop firing events
messageLine.setText(" "); // Clear the message line
|
boolean | visit(java.net.URL url)This internal method attempts to load and display the specified URL.
It is called from various places throughout the class.
try {
String href = url.toString();
// Start animating. Animation is stopped in propertyChanged()
startAnimation("Loading " + href + "...");
textPane.setPage(url); // Load and display the URL
this.setTitle(href); // Display URL in window titlebar
urlField.setText(href); // Display URL in text input field
return true; // Return success
}
catch (IOException ex) { // If page loading fails
stopAnimation();
messageLine.setText("Can't load page: " + ex.getMessage());
return false; // Return failure
}
|