📄 webbrowser.java
字号:
listener.statusTextChange(e);
break;
case WebBrowserEvent.WEBBROWSER_CLOSE:
listener.windowClose(e);
break;
}
}
}
/**
* Adds a <code>WebBrowserEvent</code> listener to the listener list.
* If listener is null, no exception is thrown and no action is performed.
*
* @param listener the WebBrowser event listener.
*/
public synchronized void addWebBrowserListener(
WebBrowserListener listener) {
if (! webBrowserListeners.contains(listener)) {
webBrowserListeners.addElement(listener);
}
}
/**
* Removes a <code>WebBrowserEvent</code> listener from the listener list.
* If listener is null, no exception is thrown and no action is performed.
* If the listener is not in the listener list, no listener is removed.
*
* @param listener the WebBrowser event listener.
*/
public synchronized void removeWebBrowserListener(
WebBrowserListener listener) {
if (listener == null)
return;
webBrowserListeners.removeElement(listener);
}
/**
* Returns an array of all the registered WebBrowser listeners.
*
* @return all of this component's <code>WebBrowserListener</code>s or an
* empty array if no component listeners are currently registered.
* @since 0.9
*/
public WebBrowserListener[] getWebBrowserListeners() {
return (WebBrowserListener[])webBrowserListeners.toArray();
}
/**
* Returns the URL of the resource that is currently being displayed.
*
* @return the current URL being display, or <code>null</code> if no URL is
* currently displayed or the WebBrowser is not yet initialized.
*/
public URL getURL() {
eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_GETURL);
if (waitForResult() == true) {
try {
return new URL(eventThread.eventRetString);
}
catch (Exception e) {
}
}
return null;
}
/**
* Sets the loaded page to be a blank page.
*
*/
public void setURL() {
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_NAVIGATE, "about:blank");
}
/**
* Navigates to a resource identified by an URL using HTTP GET method.
*
* @param url the URL to navigate.
*/
public void setURL(URL url) {
setURL(url, null);
}
/**
* Navigates to a resource identified by an URL using HTTP POST method.
*
* @param url the URL to navigate.
* @param postData data to send to the server during the HTTP POST
* transaction.
*/
public void setURL(URL url, String postData) {
if (postData == null) {
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_NAVIGATE, url.toString());
}
else {
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_NAVIGATE_POST, url.toString());
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_NAVIGATE_POSTDATA, postData);
}
}
/**
* Navigates backward one item in the history list.
*/
public void back() {
eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_GOBACK);
}
/**
* Navigates forward one item in the history list.
*/
public void forward() {
eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_GOFORWARD);
}
/**
* Reloads the URL that is currently displayed in the WebBrowser component.
*/
public void refresh() {
eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_REFRESH);
}
/**
* Stops any page loading and rendering activities.
*/
public void stop() {
eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_STOP);
}
/**
* Sets new HTML content.
*
* @param htmlContent the HTML content to set.
* @since 0.9
*/
public void setContent(String htmlContent) {
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_SETCONTENT, htmlContent);
}
/**
* Returns the HTML content of a document, opened in a browser.
*
* @return the HTML content of a document, opened in a browser.
* @since 0.9
*/
public String getContent() {
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_GETCONTENT);
if (waitForResult() == true) {
try {
return eventThread.eventRetString;
}
catch (Exception e) {
}
}
return null;
}
/**
* Executes specified JavaScript code in a currently opened document.
* This should not be called until after a documentComplete event is
* fired in <code>WebBrowserListener</code>.
*
* @return the result of JavaScript execution, if there is any.
* @since 0.9
*/
public String executeScript(java.lang.String javaScript) {
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_EXECUTESCRIPT,
javaScript);
if (waitForResult() == true) {
try {
return eventThread.eventRetString;
}
catch (Exception e) {
}
}
return null;
}
/**
* Enables or disables debug message output. Debug message out is disabled
* initially by default. Calls it via reflection when necessary.
*
* @param b if <code>true</true>, debug message output is enabled;
* otherwise debug message output is disabled.
*/
public static void setDebug(boolean b) {
WebBrowserUtil.enableDebugMessages(b);
}
/**
* Returns a <code>Status</code> object, which indicates the status of this
* <code>WebBrowser</code> object.
*
* @deprecated The <code>WebBrowser.Status</code> inner class is deprecated
* as of release 0.9 of JDIC. Its APIs have been moved to this
* <code>Browser</code> class. This API is no longer used, and
* will be removed in a future release.
* @see Status
*/
public Status getStatus() {
return new Status(this);
}
/**
* Returns the name of the embedded browser's native binary, which runs as
* a standalone native process.
*
* @deprecated As of release 0.9 of JDIC. This method was unnecessarily
* exposed and will be removed in a future release.
*/
public static String getBrowserBinary () {
return WebBrowserUtil.getEmbedBinaryName();
}
/**
* Checks whether this <code>WebBrowser</code> object is initialized
* successfully.
*
* @return <code>true</code> if the <code>WebBrowser</code> object is
* initialized successfully; otherwise, <code>false</code>.
*/
public boolean isInitialized() {
return isInitialized;
}
/**
* Checks whether this <code>WebBrowser</code> object's back command
* is enabled.
*
* @return <code>true</code> if the WebBrowser can navigate to the
* previous session history item, and <code>false</code> otherwise.
* @see #back
*/
public boolean isBackEnabled() {
return isBackEnabled;
}
/**
* Checks whether this <code>WebBrowser</code> object's forward command
* is enabled.
*
* @return <code>true</code> if the WebBrowser can navigate to the
* next session history item, and <code>false</code> otherwise.
* @see #forward
*/
public boolean isForwardEnabled() {
return isForwardEnabled;
}
/**
* Called before a navigation occurs.
* <p>
* A subclass can override this method to block the navigation or allow it
* to proceed.
*
* @param url the URL to navigate to.
* @return <code>false</code> will block the navigation from starting;
* <code>true</code> otherwise. By default, it returns <code>
* true</code>.
*/
protected boolean willOpenURL(URL url) {
if (null == url)
return true;
WebBrowserUtil.trace("URL = " + url.toString());
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
security.checkConnect(url.getHost(), url.getPort());
}
catch (AccessControlException e){
return false;
}
}
return true;
}
/**
* Called when a new window is to be created for displaying a resource.
* <p>
* A subclass can override this method to block the creation of a new
* window or allow it to proceed.
*
* @return <code>false</code> will block the creation of a new window;
* <code>true</code> otherwise. By default, it returns <code>
* true</code>.
*/
protected boolean willOpenWindow() {
return true;
}
int getInstanceNum() {
return instanceNum;
}
/**
* Waits for a result returned from the native embedded browser.
* <p>
* This method is called by methods requiring a return value, such as
* getURL, getContent, executeScript.
*/
private boolean waitForResult() {
if (! isInitialized) {
WebBrowserUtil.trace("You can't call this method before " +
"WebBrowser is initialized!");
return false;
}
boolean ret = false;
synchronized(this) {
try {
wait();
ret = true;
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
return ret;
}
int getNativeWindow() {
// The java.home property value is required to load jawt.dll on Windows.
return nativeGetWindow(System.getProperty("java.home"));
}
/* native functions */
private native int nativeGetWindow(String javaHome);
/**
* An inner class which is used for retrieving the WebBrowser's properties,
* such as the initialization status, back and forward status.
*
* @deprecated As of release 0.9 of JDIC. Its APIs have been moved to
* <code>org.jdesktop.jdic.browser.WebBrowser</code> class.
* Will be removed in a future release.
*/
public static class Status {
WebBrowser webBrowser;
Status (WebBrowser curWebBrowser) {
webBrowser = curWebBrowser;
}
/**
* Checks whether this <code>WebBrowser</code> object is initialized
* successfully.
*
* @deprecated As of release 0.9 of JDIC, replaced by
* <code>WebBrowser.isInitialized()</code>.
* @return <code>true</code> if the <code>WebBrowser</code> object is
* initialized successfully; otherwise, <code>false</code>.
*/
public boolean isInitialized() {
return webBrowser.isInitialized();
}
/**
* Checks whether this <code>WebBrowser</code> object's back command
* is enabled.
*
* @deprecated As of release 0.9 of JDIC, replaced by
* <code>WebBrowser.isBackEnabled()</code>.
* @return <code>true</code> if the WebBrowser can navigate to the
* previous session history item, and <code>false</code>
* otherwise.
* @see #back
*/
public boolean isBackEnabled() {
return webBrowser.isBackEnabled();
}
/**
* Checks whether this <code>WebBrowser</code> object's forward command
* is enabled.
*
* @deprecated As of release 0.9 of JDIC, replaced by
* <code>WebBrowser.isForwardEnabled()</code>.
* @return <code>true</code> if the WebBrowser can navigate to the
* next session history item, and <code>false</code> otherwise.
* @see #forward
*/
public boolean isForwardEnabled() {
return webBrowser.isForwardEnabled();
}
}
class MyFocusListener implements FocusListener {
public void focusGained(FocusEvent e) {
WebBrowserUtil.trace("\nMyFocusListener: focusGained\n");
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_FOCUSGAINED);
}
public void focusLost(FocusEvent e) {
WebBrowserUtil.trace("\nMyFocusListener: focusLost\n");
eventThread.fireNativeEvent(instanceNum,
NativeEventData.EVENT_FOCUSLOST);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -