Java Reference
In-Depth Information
Example 3−8: HTMLWriter.java (continued)
// string of JavaScript to the web browser
window = (JSObject)
main_window.eval("self.open(''," +
"'HTMLWriter" + window_num++ + "'," +
"'menubar,status,resizable,scrollbars," +
"width=" + width + ",height=" + height + "')");
// Obtain the Document object of this new window, and open it.
document = (JSObject) window.getMember("document");
document.call("open", null);
}
/**
* This is the write() method required for all Writer subclasses.
* Writer defines all its other write() methods in terms of this one.
**/
public void write(char[] buf, int offset, int length) {
// If no window or document, do nothing. This occurs if the stream
// has been closed, or if the code is not running in Navigator.
if ((window == null) || (document == null)) return;
// If the window has been closed by the user, do nothing
if (((Boolean)window.getMember("closed")).booleanValue()) return;
// Otherwise, create a string from the specified bytes
String s = new String(buf, offset, length);
// And pass it to the JS document.write() method to output the HTML
document.call("write", new String[] { s });
}
/**
* There is no general way to force JavaScript to flush all pending output,
* so this method does nothing. To flush, output a <P> tag or some other
* HTML tag that forces a line break in the output.
**/
public void flush() {}
/**
* When the stream is closed, close the JavaScript Document object
* (But don't close the window yet.)
**/
public void close() { document.call("close", null); document = null; }
/**
* If the browser window is still open, close it.
* This method is unique to HTMLWriter.
**/
public void closeWindow() {
if (document != null) close();
if (!((Boolean)window.getMember("closed")).booleanValue())
window.call("close", null);
window = null;
}
/** A finalizer method to close the window in case we forget. */
public void finalize() { closeWindow(); }
/**
* This nested class is an applet that demonstrates the use of HTMLWriter.
* It reads the contents of the URL specified in its url parameter and
Search WWH ::




Custom Search