KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > webclient > WebclientBrowserImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
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 /**
39  * Real implementation of Mozilla browser functionality needed by IDE.
40  *
41  * @author Radim.Kubacki@sun.com
42  */

43 class WebclientBrowserImpl extends HtmlBrowser.Impl {
44
45     private static boolean debug = true;
46     
47     // variables .................................................................
48

49     /** browser factory */
50     private WebclientBrowser factory;
51
52     /** browser controller */
53     private BrowserControl browserControl;
54     /** browser visual component */
55     private WebclientBrowserComponent browser;
56     
57     /** WindowControl for embedding */
58     private WindowControl winCtrl;
59     /** browser navigation interface */
60     private Navigation navigation;
61     /** browser history interface */
62     private History browserHistory;
63     
64     /** browser event interface */
65     private EventRegistration eventRegistration;
66     
67     /** current URL */
68     private URL url;
69     /** standart helper variable */
70     private PropertyChangeSupport pcs;
71     
72     /** Current status message. */
73     private String JavaDoc statusMessage = ""; // NOI18N
74

75     /** Current value of title property. */
76     private String JavaDoc title = ""; // NOI18N
77

78
79     // init ......................................................................
80

81     /**
82     * Creates instance of browser.
83     */

84     public WebclientBrowserImpl (WebclientBrowser fact) {
85         pcs = new PropertyChangeSupport (this);
86         this.factory = fact;
87         
88         // Create the browser
89
try {
90             // This is a workaround - webclient requires jawt but it is not loaded yet (IDE is Swing app)
91
System.loadLibrary("jawt"); // NOI18N
92

93             if (factory.getAppData () != null)
94                 BrowserControlFactory.setAppData (factory.getAppData ().getAbsolutePath ());
95             else
96                 throw new IllegalStateException JavaDoc (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()); // NOI18N
103

104             browser = new WebclientBrowserComponent (this);
105             
106         }
107         catch(Exception JavaDoc 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                 // PENDING: Need to wait until it gets inited
118
for (int i=0; i<20; i++) {
119                     if (debug) System.out.println("Checking ("+i+") NativeWebShell="+winCtrl.getNativeWebShell()); // NOI18N
120
if (winCtrl.getNativeWebShell() != -1)
121                         break;
122                     try {
123                         Thread.currentThread().sleep(1000);
124                     }
125                     catch (InterruptedException JavaDoc e) { // do nothing
126
}
127                 }
128                 if (winCtrl.getNativeWebShell() == -1)
129                     throw new IllegalStateException JavaDoc ("Cannot get native web shell"); // NOI18N
130

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 JavaDoc 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 JavaDoc 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 JavaDoc) {
152                                     try {
153                                         url = new URL ((String JavaDoc)o);
154                                         pcs.firePropertyChange (PROP_URL, null, null);
155                                     }
156                                     catch (MalformedURLException ex) {
157                                         // do nothing - URL won't be updated
158
}
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 JavaDoc)
178                                 setStatusMessage ((String JavaDoc)event.getEventData ());
179                         }
180                     }
181                 });
182                 
183             }
184             catch(Exception JavaDoc 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     /**
193      * Closes BrowserControl resources
194      */

195     public void destroy () {
196         /*
197         System.out.println("destroy");
198         BrowserControlFactory.deleteBrowserControl(browserControl);
199         browserControl = null;
200         // if it is last
201         try {
202             BrowserControlFactory.appTerminate();
203         }
204         catch (Exception ex) {
205             ex.printStackTrace();
206         }
207          */

208     }
209
210
211     // HtmpBrowser.Impl implementation ...........................................
212

213     /**
214     * Returns visual component of html browser.
215      * Actually returns null to allow custom window handling.
216     *
217     * @return <CODE>null</CODE>.
218     */

219     public java.awt.Component JavaDoc getComponent () {
220         return null;
221     }
222
223     /**
224     * Reloads current html page.
225     */

226     public void reloadDocument () {
227         initialize ();
228         navigation.refresh (Navigation.LOAD_FORCE_RELOAD);
229     }
230
231     /**
232     * Stops loading of current html page.
233     */

234     public void stopLoading () {
235         initialize ();
236         navigation.stop();
237     }
238
239     /**
240     * Sets current URL.
241     *
242     * @param url URL to show in the browser.
243     */

244     public void setURL (URL url) {
245         if (SwingUtilities.isEventDispatchThread ()) {
246             final URL newUrl = url;
247             RequestProcessor.getDefault(). post (
248                 new Runnable JavaDoc () {
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             // internal protocols cannot be displayed in external viewer
261
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 JavaDoc e) {
276             e.printStackTrace();
277         }
278     }
279
280     /**
281     * Returns current URL.
282     *
283     * @return current URL.
284     */

285     public URL getURL () {
286         return url;
287     }
288
289     /**
290     * Returns status message representing status of html browser.
291     *
292     * @return status message.
293     */

294     public String JavaDoc getStatusMessage () {
295         return statusMessage;
296     }
297
298
299     void setStatusMessage (String JavaDoc msg) {
300         // XXX might be better to use own status bar
301
StatusDisplayer.getDefault ().setStatusText (msg);
302         String JavaDoc old = statusMessage;
303         statusMessage = msg;
304         pcs.firePropertyChange (PROP_STATUS_MESSAGE, old, statusMessage);
305     }
306     
307     /** Returns title of the displayed page.
308     * @return title
309     */

310     public String JavaDoc getTitle () {
311         return title;
312     }
313
314     /** Is forward button enabled?
315     * @return true if it is
316     */

317     public boolean isForward () {
318         if (url == null) return false;
319         initialize ();
320         return browserHistory.canForward ();
321     }
322
323     /** Moves the browser forward. Failure is ignored.
324     */

325     public void forward () {
326         initialize ();
327         browserHistory.forward();
328     }
329
330     /** Is backward button enabled?
331     * @return true if it is
332     */

333     public boolean isBackward () {
334         if (url == null) return false;
335         initialize ();
336         return browserHistory.canBack ();
337     }
338
339     /** Moves the browser forward. Failure is ignored.
340     */

341     public void backward () {
342         initialize ();
343         browserHistory.back();
344     }
345
346     /** Is history button enabled?
347     * @return true if it is
348     */

349     public boolean isHistory () {
350         if (url == null) return false;
351         initialize ();
352         return (browserHistory.getHistoryLength() > 0);
353     }
354
355     /** Invoked when the history button is pressed.
356     */

357     public void showHistory () {
358         // PENDING: use history manager
359
}
360
361     /**
362     * Adds PropertyChangeListener to this browser.
363     *
364     * @param l Listener to add.
365     */

366     public void addPropertyChangeListener (PropertyChangeListener l) {
367         pcs.addPropertyChangeListener (l);
368     }
369
370     /**
371     * Removes PropertyChangeListener from this browser.
372     *
373     * @param l Listener to remove.
374     */

375     public void removePropertyChangeListener (PropertyChangeListener l) {
376         pcs.removePropertyChangeListener (l);
377     }
378
379
380     // other methods ..............................................................
381

382     
383     /**
384      * Returns BrowserControl interface
385      */

386     public BrowserControl getBrowserControl () {
387         return browserControl;
388     }
389     
390     /**
391      * Returns whether given protocol is internal or not.
392      * (Internal protocols cannot be displayed by external viewers.
393      * They must be wrapped somehow.)
394      *
395      * @return true if protocol is internal, false otherwise
396      */

397     protected final static boolean isInternalProtocol (String JavaDoc protocol) {
398         if (protocol.startsWith ("nb")) // NOI18N
399
return true;
400         
401         return false;
402     }
403 }
404
Popular Tags