KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > jdic > JdicBrowserImpl


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.jdic;
21
22 import java.beans.*;
23 import java.net.*;
24
25 import org.jdesktop.jdic.browser.*;
26 import org.openide.awt.HtmlBrowser.Impl;
27 import org.openide.util.NbBundle;
28
29 /**
30  * JDIC browser support
31  *
32  * @author Martin Grebac
33  */

34 public class JdicBrowserImpl extends org.openide.awt.HtmlBrowser.Impl {
35
36     private WebBrowser webBrowser = null;
37
38     /** standard helper variable */
39     private PropertyChangeSupport pcs;
40
41     /** requested URL */
42     private URL url;
43     private String JavaDoc title = ""; // NOI18N
44
private String JavaDoc statusMsg = ""; // NOI18N
45
private boolean isForward = false;
46     private boolean isBackward = false;
47     
48     /** Default constructor.
49       * <p>Builds PropertyChangeSupport.
50       */

51     public JdicBrowserImpl() {
52         pcs = new PropertyChangeSupport (this);
53     }
54     
55     public boolean isHistory() { return false; }
56     public void showHistory() {}
57
58     public boolean isBackward() {
59         return isBackward;
60     }
61     
62     public boolean isForward() {
63         return isForward;
64     }
65
66     /** Returns status message representing status of html browser.
67      *
68      * @return status message.
69      */

70     public String JavaDoc getStatusMessage() {
71         return statusMsg;
72     }
73         
74     public String JavaDoc getTitle() {
75         return title;
76     }
77
78     protected void setTitle (String JavaDoc title) {
79         this.title = title;
80     }
81
82     /** Returns current URL.
83      *
84      * @return current URL.
85      */

86     public URL getURL() {
87         return url;
88     }
89
90     public void stopLoading() {
91         webBrowser.stop();
92     }
93
94     public void backward() {
95         if (webBrowser == null) {
96             init();
97         }
98         webBrowser.back();
99     }
100     
101     public void forward() {
102         if (webBrowser == null) {
103             init();
104         }
105         webBrowser.forward();
106     }
107
108     /** Call setURL again to force reloading.
109      * Browser must be set to reload document and do not cache them.
110      */

111     public void reloadDocument() {
112         if (webBrowser == null) {
113             init();
114         }
115         webBrowser.refresh();
116     }
117             
118     /** Sets current URL.
119      *
120      * @param url URL to show in the browser.
121      */

122     public synchronized void setURL(final URL url) {
123         this.url = url;
124         if (webBrowser == null) {
125             init();
126         }
127         webBrowser.setURL(url);
128     }
129     
130     /** Returns visual component of html browser.
131      *
132      * @return visual component of html browser.
133      */

134     public final java.awt.Component JavaDoc getComponent() {
135         if (webBrowser == null) {
136             init();
137         }
138         return webBrowser;
139     }
140     
141     private synchronized void init() {
142         if (webBrowser == null) {
143             try {
144                 webBrowser = new WebBrowser();
145                 //webBrowser.setDebug(true); // debugging info to console
146
} catch (Exception JavaDoc e) {
147                 // TODO
148
return;
149             }
150
151             webBrowser.addWebBrowserListener(new WebBrowserListener() {
152                 public void downloadStarted(WebBrowserEvent event) {
153                     String JavaDoc oldMsg = statusMsg;
154                     statusMsg = NbBundle.getMessage(JdicBrowserImpl.class, "MSG_Download_started"); //NOI18N
155
pcs.firePropertyChange(PROP_STATUS_MESSAGE, oldMsg, statusMsg);
156                 }
157
158                 public void downloadCompleted(WebBrowserEvent event) {
159                     //update status message
160
String JavaDoc oldMsg = statusMsg;
161                     statusMsg = NbBundle.getMessage(JdicBrowserImpl.class, "MSG_Download_completed"); //NOI18N
162
pcs.firePropertyChange(PROP_STATUS_MESSAGE, oldMsg, statusMsg);
163
164                     //update fwd and back buttons' state
165
boolean oldFwd = isForward;
166                     boolean oldBack = isBackward;
167                     isForward = webBrowser.getStatus().isForwardEnabled();
168                     isBackward = webBrowser.getStatus().isBackEnabled();
169
170                     System.err.println("back: " + isBackward);
171                     if (isForward != oldFwd) {
172                         pcs.firePropertyChange(PROP_FORWARD, oldFwd, isForward);
173                     }
174                     if (isBackward != oldBack) {
175                         pcs.firePropertyChange(PROP_BACKWARD, oldBack, isBackward);
176                     }
177
178                     //update url
179
URL old = url;
180                     URL url = webBrowser.getURL();
181                     if (old != url) {
182                         pcs.firePropertyChange(PROP_URL, old, url);
183                     }
184                 }
185
186                 public void downloadProgress(WebBrowserEvent event) {
187                 }
188
189                 public void downloadError(WebBrowserEvent event) {
190                     String JavaDoc oldMsg = statusMsg;
191                     statusMsg = event.getData();
192                     pcs.firePropertyChange(PROP_STATUS_MESSAGE, oldMsg, statusMsg);
193                 }
194
195                 public void titleChange(WebBrowserEvent event) {
196                     String JavaDoc old = title;
197                     title = event.getData();
198                     pcs.firePropertyChange(Impl.PROP_TITLE, old, title);
199                 }
200
201                 public void statusTextChange(WebBrowserEvent event) {
202                     String JavaDoc oldMsg = statusMsg;
203                     statusMsg = event.getData();
204                     pcs.firePropertyChange(PROP_STATUS_MESSAGE, oldMsg, statusMsg);
205                 }
206             });
207         }
208     }
209
210     /** Adds PropertyChangeListener to this browser.
211      *
212      * @param l Listener to add.
213      */

214     public void addPropertyChangeListener(PropertyChangeListener l) {
215         pcs.addPropertyChangeListener (l);
216     }
217     
218     /** Removes PropertyChangeListener from this browser.
219      *
220      * @param l Listener to remove.
221      */

222     public void removePropertyChangeListener(PropertyChangeListener l) {
223         pcs.removePropertyChangeListener (l);
224     }
225     
226 }
227
Popular Tags