1 19 20 package org.netbeans.modules.webclient; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.beans.*; 25 import java.net.*; 26 import java.util.*; 27 import javax.swing.SwingUtilities ; 28 29 import org.openide.*; 30 import org.openide.awt.*; 31 32 import org.mozilla.util.*; 33 import org.mozilla.webclient.*; 34 35 import org.openide.util.NbBundle; 36 import org.openide.util.RequestProcessor; 37 38 43 class WebclientBrowserImpl extends HtmlBrowser.Impl { 44 45 private static boolean debug = true; 46 47 49 50 private WebclientBrowser factory; 51 52 53 private BrowserControl browserControl; 54 55 private WebclientBrowserComponent browser; 56 57 58 private WindowControl winCtrl; 59 60 private Navigation navigation; 61 62 private History browserHistory; 63 64 65 private EventRegistration eventRegistration; 66 67 68 private URL url; 69 70 private PropertyChangeSupport pcs; 71 72 73 private String statusMessage = ""; 75 76 private String title = ""; 78 79 81 84 public WebclientBrowserImpl (WebclientBrowser fact) { 85 pcs = new PropertyChangeSupport (this); 86 this.factory = fact; 87 88 try { 90 System.loadLibrary("jawt"); 93 if (factory.getAppData () != null) 94 BrowserControlFactory.setAppData (factory.getAppData ().getAbsolutePath ()); 95 else 96 throw new IllegalStateException (NbBundle.getMessage(WebclientBrowserImpl.class,"ERR_appData_path_is_not_set")); 97 98 browserControl = BrowserControlFactory.newBrowserControl(); 99 100 winCtrl = (WindowControl) 101 browserControl.queryInterface(BrowserControl.WINDOW_CONTROL_NAME); 102 if (debug) System.out.println("NativeWebShell="+winCtrl.getNativeWebShell()); 104 browser = new WebclientBrowserComponent (this); 105 106 } 107 catch(Exception e) { 108 ErrorManager.getDefault().annotate(e, NbBundle.getMessage(WebclientBrowserImpl.class,"ERR_Cannot_init_impl")); 109 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 110 } 111 } 112 113 public void initialize () { 114 browser.setVisible(true); 115 if (navigation == null ) { 116 try { 117 for (int i=0; i<20; i++) { 119 if (debug) System.out.println("Checking ("+i+") NativeWebShell="+winCtrl.getNativeWebShell()); if (winCtrl.getNativeWebShell() != -1) 121 break; 122 try { 123 Thread.currentThread().sleep(1000); 124 } 125 catch (InterruptedException e) { } 127 } 128 if (winCtrl.getNativeWebShell() == -1) 129 throw new IllegalStateException ("Cannot get native web shell"); 131 navigation = (Navigation) 132 browserControl.queryInterface(BrowserControl.NAVIGATION_NAME); 133 CurrentPage currentPage = (CurrentPage) 134 browserControl.queryInterface(BrowserControl.CURRENT_PAGE_NAME); 135 browserHistory = (History) 136 browserControl.queryInterface(BrowserControl.HISTORY_NAME); 137 138 eventRegistration = (EventRegistration) 139 browserControl.queryInterface(BrowserControl.EVENT_REGISTRATION_NAME); 140 eventRegistration.addDocumentLoadListener (new DocumentLoadListener () { 141 public void eventDispatched(WebclientEvent event) 142 { 143 String old = statusMessage; 144 int eventType = (int)event.getType(); 145 146 if (eventType == (int)DocumentLoadEvent.START_DOCUMENT_LOAD_EVENT_MASK) { 147 setStatusMessage (NbBundle.getMessage(WebclientBrowserImpl.class,"MSG_Loading")); 148 Object o = event.getEventData(); 149 if (debug) System.out.println("start evt data: "+o+" class "+((o!=null)?o.getClass().getName():"null")); 150 if (o != null) { 151 if (o instanceof String ) { 152 try { 153 url = new URL ((String )o); 154 pcs.firePropertyChange (PROP_URL, null, null); 155 } 156 catch (MalformedURLException ex) { 157 } 159 } 160 else if (o instanceof URL) { 161 url = (URL)o; 162 pcs.firePropertyChange (PROP_URL, null, null); 163 } 164 } 165 } 166 else if (eventType == (int)DocumentLoadEvent.END_DOCUMENT_LOAD_EVENT_MASK) { 167 setStatusMessage (NbBundle.getMessage(WebclientBrowserImpl.class,"MSG_Done")); 168 pcs.firePropertyChange (null, null, null); 169 if (debug) System.out.println("end evt data: "+event.getEventData()); 170 } 171 else if (eventType == (int)DocumentLoadEvent.START_URL_LOAD_EVENT_MASK 172 || eventType == (int)DocumentLoadEvent.END_URL_LOAD_EVENT_MASK 173 || eventType == (int)DocumentLoadEvent.FETCH_INTERRUPT_EVENT_MASK 174 || eventType == (int)DocumentLoadEvent.PROGRESS_URL_LOAD_EVENT_MASK 175 || eventType == (int)DocumentLoadEvent.STATUS_URL_LOAD_EVENT_MASK 176 || eventType == (int)DocumentLoadEvent.UNKNOWN_CONTENT_EVENT_MASK) { 177 if (event.getEventData () instanceof String ) 178 setStatusMessage ((String )event.getEventData ()); 179 } 180 } 181 }); 182 183 } 184 catch(Exception e) { 185 ErrorManager.getDefault().annotate(e, NbBundle.getMessage(WebclientBrowserImpl.class,"ERR_Cannot_init_impl")); 186 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 187 } 188 } 189 190 } 191 192 195 public void destroy () { 196 208 } 209 210 211 213 219 public java.awt.Component getComponent () { 220 return null; 221 } 222 223 226 public void reloadDocument () { 227 initialize (); 228 navigation.refresh (Navigation.LOAD_FORCE_RELOAD); 229 } 230 231 234 public void stopLoading () { 235 initialize (); 236 navigation.stop(); 237 } 238 239 244 public void setURL (URL url) { 245 if (SwingUtilities.isEventDispatchThread ()) { 246 final URL newUrl = url; 247 RequestProcessor.getDefault(). post ( 248 new Runnable () { 249 public void run () { 250 WebclientBrowserImpl.this.setURL (newUrl); 251 } 252 }); 253 return; 254 } 255 256 try { 257 initialize (); 258 URL old = getURL (); 259 260 if (isInternalProtocol (url.getProtocol ())) { 262 url = URLUtil.createExternalURL(url); 263 } 264 265 if ((old != null) && old.equals (url)) { 266 navigation.refresh (Navigation.LOAD_FORCE_RELOAD); 267 } else { 268 WindowControl winCtrl = (WindowControl) 269 browserControl.queryInterface(BrowserControl.WINDOW_CONTROL_NAME); 270 this.url = url; 271 navigation.loadURL ( url.toString ()); 272 pcs.firePropertyChange (PROP_URL, old, url); 273 } 274 } 275 catch (Exception e) { 276 e.printStackTrace(); 277 } 278 } 279 280 285 public URL getURL () { 286 return url; 287 } 288 289 294 public String getStatusMessage () { 295 return statusMessage; 296 } 297 298 299 void setStatusMessage (String msg) { 300 StatusDisplayer.getDefault ().setStatusText (msg); 302 String old = statusMessage; 303 statusMessage = msg; 304 pcs.firePropertyChange (PROP_STATUS_MESSAGE, old, statusMessage); 305 } 306 307 310 public String getTitle () { 311 return title; 312 } 313 314 317 public boolean isForward () { 318 if (url == null) return false; 319 initialize (); 320 return browserHistory.canForward (); 321 } 322 323 325 public void forward () { 326 initialize (); 327 browserHistory.forward(); 328 } 329 330 333 public boolean isBackward () { 334 if (url == null) return false; 335 initialize (); 336 return browserHistory.canBack (); 337 } 338 339 341 public void backward () { 342 initialize (); 343 browserHistory.back(); 344 } 345 346 349 public boolean isHistory () { 350 if (url == null) return false; 351 initialize (); 352 return (browserHistory.getHistoryLength() > 0); 353 } 354 355 357 public void showHistory () { 358 } 360 361 366 public void addPropertyChangeListener (PropertyChangeListener l) { 367 pcs.addPropertyChangeListener (l); 368 } 369 370 375 public void removePropertyChangeListener (PropertyChangeListener l) { 376 pcs.removePropertyChangeListener (l); 377 } 378 379 380 382 383 386 public BrowserControl getBrowserControl () { 387 return browserControl; 388 } 389 390 397 protected final static boolean isInternalProtocol (String protocol) { 398 if (protocol.startsWith ("nb")) return true; 400 401 return false; 402 } 403 } 404 | Popular Tags |