1 20 21 package org.jdesktop.jdic.browser; 22 23 import java.util.Vector ; 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 import java.awt.*; 27 import java.awt.event.*; 28 import java.security.*; 29 30 import org.jdesktop.jdic.init.JdicInitException; 31 import org.jdesktop.jdic.init.JdicManager; 32 import org.jdesktop.jdic.browser.internal.WebBrowserUtil; 33 34 56 public class WebBrowser extends Canvas 57 { 58 private MyFocusListener focusListener = new MyFocusListener(); 59 private static NativeEventThread eventThread; 62 private Vector webBrowserListeners = new Vector (); 63 private int instanceNum; 64 private static int lastInstanceNum = 0; 65 66 private boolean isInitialized = false; 68 private boolean isBackEnabled = false; 69 private boolean isForwardEnabled = false; 70 private String initFailureMessage = "WebBrowser is not initialized."; 71 72 static { 73 try { 77 JdicManager jm = JdicManager.getManager(); 78 jm.initShareNative(); 79 jm.initBrowserNative(); 80 } catch (JdicInitException e){ 81 e.printStackTrace(); 82 } 83 eventThread = new NativeEventThread(); 84 85 AccessController.doPrivileged( 86 new PrivilegedAction() { 87 public Object run() { 88 System.loadLibrary("jdic"); 89 return null; } 91 } 92 ); 93 } 94 95 void setInitialized(boolean b) { 96 isInitialized = b; 97 } 98 99 void setInitFailureMessage(String msg) { 100 initFailureMessage = msg; 101 } 102 103 106 public WebBrowser() { 107 this(null); 108 } 109 110 113 public WebBrowser(URL url) { 114 synchronized(this) { 115 instanceNum = lastInstanceNum; 116 lastInstanceNum++; 117 } 118 eventThread.attachWebBrowser(this); 119 120 if (0 == instanceNum) { 121 eventThread.start(); 122 eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_INIT); 123 } 124 125 if (null != url) { 126 setURL(url); 127 } 128 129 setFocusable(true); 130 addFocusListener(focusListener); 131 } 132 133 138 public void addNotify() { 139 boolean flag = super.isDisplayable(); 140 super.addNotify(); 141 if(!flag) { 142 eventThread.fireNativeEvent(instanceNum, 143 NativeEventData.EVENT_CREATEWINDOW); 144 } 145 } 146 147 154 public void removeNotify() { 155 Thread disposeThread = new Thread () { 156 public void run() { 157 synchronized( WebBrowser.this ){ 158 eventThread.fireNativeEvent(instanceNum, 159 NativeEventData.EVENT_DESTROYWINDOW); 160 try { 161 WebBrowser.this.wait(); 165 } catch (InterruptedException e) { 166 } 167 } 168 WebBrowser.super.removeNotify(); 169 setInitialized(false); 170 } 171 }; 172 173 disposeThread.start(); 174 } 175 176 186 public void setBounds(int x, int y, int width, int height) { 187 super.setBounds(x, y, width, height); 188 eventThread.fireNativeEvent(instanceNum, 189 NativeEventData.EVENT_SET_BOUNDS, 190 new Rectangle(x, y, width, height)); 191 } 192 193 197 void dispatchWebBrowserEvent(WebBrowserEvent e) { 198 int eid = e.getID(); 199 200 WebBrowserUtil.trace("Got event from NativeEventThread " + eid); 201 202 String msg = "@" + instanceNum + "," + eid + ","; 207 if (WebBrowserEvent.WEBBROWSER_BEFORE_NAVIGATE == eid) { 208 URL url = null; 209 try { 210 url = new URL (e.getData()); 211 } catch (MalformedURLException ex1) { 212 try { 213 if (!WebBrowserUtil.isDefaultBrowserMozilla()) { 215 url = new URL ("file:/" + e.getData()); 216 } 217 } catch (MalformedURLException ex2) { 218 } 219 } 220 221 msg += willOpenURL(url) ? "0" : "1"; 222 eventThread.messenger.sendMessage(msg); 223 return; 224 } 225 else if (WebBrowserEvent.WEBBROWSER_BEFORE_NEWWINDOW == eid) { 226 msg += willOpenWindow() ? "0" : "1"; 227 eventThread.messenger.sendMessage(msg); 228 return; 229 } 230 else if (WebBrowserEvent.WEBBROWSER_COMMAND_STATE_CHANGE == eid) { 231 String data = e.getData(); 232 if (data.startsWith("forward")) { 233 isForwardEnabled = data.substring(8).equals("1"); 234 WebBrowserUtil.trace("Forward State changed = " 235 + isForwardEnabled); 236 } 237 else if (data.startsWith("back")) { 238 isBackEnabled = data.substring(5).equals("1"); 239 WebBrowserUtil.trace("Back State changed = " + isBackEnabled); 240 } 241 return; 242 } 243 else if (WebBrowserEvent.WEBBROWSER_FOCUS_REQUEST == eid) { 244 WebBrowserUtil.trace("Got event from brower: Focus request."); 245 requestFocus(); 246 return; 247 } 248 else if (WebBrowserEvent.WEBBROWSER_DESTROYWINDOW_SUCC == eid){ 249 WebBrowserUtil.trace("Got event from brower: Destory window " + 250 "succeeds."); 251 synchronized(this){ 252 this.notify(); 254 } 255 } 256 257 int size; 259 Vector tl; 260 synchronized (this) { 261 size = webBrowserListeners.size(); 262 if (size == 0) { 263 return; 264 } 265 266 tl = (Vector ) webBrowserListeners.clone(); 267 } 268 269 for (int i = 0; i < size; ++i) { 270 WebBrowserListener listener = (WebBrowserListener) tl.elementAt(i); 271 switch (eid) { 272 case WebBrowserEvent.WEBBROWSER_DOWNLOAD_STARTED: 273 listener.downloadStarted(e); 274 break; 275 case WebBrowserEvent.WEBBROWSER_DOWNLOAD_COMPLETED: 276 listener.downloadCompleted(e); 277 break; 278 case WebBrowserEvent.WEBBROWSER_DOWNLOAD_PROGRESS: 279 listener.downloadProgress(e); 280 break; 281 case WebBrowserEvent.WEBBROWSER_DOWNLOAD_ERROR: 282 listener.downloadError(e); 283 break; 284 case WebBrowserEvent.WEBBROWSER_DOCUMENT_COMPLETED: 285 listener.documentCompleted(e); 286 break; 287 case WebBrowserEvent.WEBBROWSER_TITLE_CHANGE: 288 listener.titleChange(e); 289 break; 290 case WebBrowserEvent.WEBBROWSER_STATUSTEXT_CHANGE: 291 listener.statusTextChange(e); 292 break; 293 } 294 } 295 } 296 297 303 public synchronized void addWebBrowserListener( 304 WebBrowserListener listener) { 305 if (! webBrowserListeners.contains(listener)) { 306 webBrowserListeners.addElement(listener); 307 } 308 } 309 310 317 public synchronized void removeWebBrowserListener( 318 WebBrowserListener listener) { 319 320 if (listener == null) 321 return; 322 323 webBrowserListeners.removeElement(listener); 324 } 325 326 333 public WebBrowserListener[] getWebBrowserListeners() { 334 return (WebBrowserListener[])webBrowserListeners.toArray(); 335 } 336 337 343 public URL getURL() { 344 eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_GETURL); 345 346 if (waitForResult() == true) { 347 try { 348 return new URL (eventThread.eventRetString); 349 } 350 catch (Exception e) { 351 } 352 } 353 return null; 354 } 355 356 360 public void setURL() { 361 eventThread.fireNativeEvent(instanceNum, 362 NativeEventData.EVENT_NAVIGATE, "about:blank"); 363 } 364 365 370 public void setURL(URL url) { 371 setURL(url, null); 372 } 373 374 381 public void setURL(URL url, String postData) { 382 if (postData == null) { 383 eventThread.fireNativeEvent(instanceNum, 384 NativeEventData.EVENT_NAVIGATE, url.toString()); 385 } 386 else { 387 eventThread.fireNativeEvent(instanceNum, 388 NativeEventData.EVENT_NAVIGATE_POST, url.toString()); 389 eventThread.fireNativeEvent(instanceNum, 390 NativeEventData.EVENT_NAVIGATE_POSTDATA, postData); 391 } 392 } 393 394 397 public void back() { 398 eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_GOBACK); 399 } 400 401 404 public void forward() { 405 eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_GOFORWARD); 406 } 407 408 411 public void refresh() { 412 eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_REFRESH); 413 } 414 415 418 public void stop() { 419 eventThread.fireNativeEvent(instanceNum, NativeEventData.EVENT_STOP); 420 } 421 422 428 public void setContent(String htmlContent) { 429 eventThread.fireNativeEvent(instanceNum, 430 NativeEventData.EVENT_SETCONTENT, htmlContent); 431 } 432 433 439 public String getContent() { 440 eventThread.fireNativeEvent(instanceNum, 441 NativeEventData.EVENT_GETCONTENT); 442 443 if (waitForResult() == true) { 444 try { 445 return eventThread.eventRetString; 446 } 447 catch (Exception e) { 448 } 449 } 450 return null; 451 } 452 453 461 public String executeScript(java.lang.String javaScript) { 462 eventThread.fireNativeEvent(instanceNum, 463 NativeEventData.EVENT_EXECUTESCRIPT, 464 javaScript); 465 466 if (waitForResult() == true) { 467 try { 468 return eventThread.eventRetString; 469 } 470 catch (Exception e) { 471 } 472 } 473 return null; 474 } 475 476 483 public static void setDebug(boolean b) { 484 WebBrowserUtil.enableDebugMessages(b); 485 } 486 487 497 public Status getStatus() { 498 return new Status(this); 499 } 500 501 508 public static String getBrowserBinary () { 509 return WebBrowserUtil.getEmbedBinaryName(); 510 } 511 512 519 public boolean isInitialized() { 520 return isInitialized; 521 } 522 523 531 public boolean isBackEnabled() { 532 return isBackEnabled; 533 } 534 535 543 public boolean isForwardEnabled() { 544 return isForwardEnabled; 545 } 546 547 558 protected boolean willOpenURL(URL url) { 559 if (null == url) 560 return true; 561 562 WebBrowserUtil.trace("URL = " + url.toString()); 563 SecurityManager security = System.getSecurityManager(); 564 if (security != null) { 565 try { 566 security.checkConnect(url.getHost(), url.getPort()); 567 } 568 catch (AccessControlException e){ 569 return false; 570 } 571 } 572 return true; 573 } 574 575 585 protected boolean willOpenWindow() { 586 return true; 587 } 588 589 int getInstanceNum() { 590 return instanceNum; 591 } 592 593 599 private boolean waitForResult() { 600 if (! isInitialized) { 601 WebBrowserUtil.trace("You can't call this method before " + 602 "WebBrowser is initialized!"); 603 return false; 604 } 605 606 boolean ret = false; 607 synchronized(this) { 608 try { 609 wait(); 610 ret = true; 611 } 612 catch (Exception e) { 613 System.out.println(e.getMessage()); 614 } 615 } 616 return ret; 617 } 618 619 int getNativeWindow() { 620 return nativeGetWindow(System.getProperty("java.home")); 622 } 623 624 625 private native int nativeGetWindow(String javaHome); 626 627 635 public static class Status { 636 WebBrowser webBrowser; 637 638 Status (WebBrowser curWebBrowser) { 639 webBrowser = curWebBrowser; 640 } 641 642 651 public boolean isInitialized() { 652 return webBrowser.isInitialized(); 653 } 654 655 666 public boolean isBackEnabled() { 667 return webBrowser.isBackEnabled(); 668 } 669 670 680 public boolean isForwardEnabled() { 681 return webBrowser.isForwardEnabled(); 682 } 683 } 684 685 class MyFocusListener implements FocusListener { 686 public void focusGained(FocusEvent e) { 687 WebBrowserUtil.trace("\nMyFocusListener: focusGained\n"); 688 eventThread.fireNativeEvent(instanceNum, 689 NativeEventData.EVENT_FOCUSGAINED); 690 } 691 692 public void focusLost(FocusEvent e) { 693 WebBrowserUtil.trace("\nMyFocusListener: focusLost\n"); 694 eventThread.fireNativeEvent(instanceNum, 695 NativeEventData.EVENT_FOCUSLOST); 696 } 697 } 698 } 699 | Popular Tags |