KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > HtmlBrowserComponent


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.core;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import org.openide.awt.HtmlBrowser;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.NbBundle;
36 import org.openide.windows.CloneableTopComponent;
37
38 /**
39  * Formerly HtmlBrowser.BrowserComponent.
40  */

41 class HtmlBrowserComponent extends CloneableTopComponent implements PropertyChangeListener JavaDoc {
42     /** generated Serialized Version UID */
43     static final long serialVersionUID = 2912844785502987960L;
44
45     // variables .........................................................................................
46

47     /** Delegating component */
48     private HtmlBrowser browserComponent;
49     
50
51     // initialization ....................................................................................
52

53     /**
54     * Creates new html browser with toolbar and status line.
55     */

56     public HtmlBrowserComponent() {
57         this (true, true);
58     }
59
60     /**
61     * Creates new html browser with toolbar and status line.
62     */

63     public HtmlBrowserComponent(boolean toolbar, boolean statusLine) {
64         this (null, toolbar, statusLine);
65     }
66
67     /**
68     * Creates new html browser.
69     */

70     public HtmlBrowserComponent(HtmlBrowser.Factory fact, boolean toolbar, boolean statusLine) {
71         setName (""); // NOI18N
72
setLayout (new BorderLayout JavaDoc ());
73         add (browserComponent = new HtmlBrowser (fact, toolbar, statusLine), "Center"); // NOI18N
74

75         browserComponent.getBrowserImpl().addPropertyChangeListener (this);
76
77         // Ensure closed browsers are not stored:
78
if (browserComponent.getBrowserComponent() != null) {
79             putClientProperty("InternalBrowser", Boolean.TRUE); // NOI18N
80
}
81         setToolTipText(NbBundle.getBundle(HtmlBrowser.class).getString("HINT_WebBrowser"));
82     }
83     
84     public int getPersistenceType() {
85         return PERSISTENCE_ONLY_OPENED;
86     }
87     
88     public void propertyChange (PropertyChangeEvent JavaDoc e) {
89         if (!HtmlBrowser.Impl.PROP_TITLE.equals (e.getPropertyName ())) return;
90         String JavaDoc title = browserComponent.getBrowserImpl().getTitle ();
91         if ((title == null) || (title.length () < 1)) return;
92         HtmlBrowserComponent.this.setName (title);
93         HtmlBrowserComponent.this.setDisplayName(title);
94     }
95     
96     /** always open this top component in our special mode, if
97     * no mode for this component is specified yet */

98     public void open() {
99         // do not open this component if this is dummy browser
100
if (browserComponent.getBrowserComponent() == null)
101             return;
102         
103         // behave like superclass
104
super.open();
105     }
106     
107     /** Serializes browser component -> writes Replacer object which
108     * holds browser content and look. */

109     protected Object JavaDoc writeReplace ()
110     throws java.io.ObjectStreamException JavaDoc {
111         return new BrowserReplacer (this);
112     }
113      
114     /* Deserialize this top component. Now it is here for backward compatibility
115     * @param in the stream to deserialize from
116     */

117     public void readExternal (ObjectInput JavaDoc in)
118     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
119         super.readExternal (in);
120         setStatusLineVisible (in.readBoolean ());
121         setToolbarVisible (in.readBoolean ());
122         browserComponent.setURL ((URL JavaDoc) in.readObject ());
123     }
124
125     // TopComponent support ...................................................................
126

127     protected CloneableTopComponent createClonedObject () {
128         HtmlBrowserComponent bc = new HtmlBrowserComponent(); // PENDING: this should pass all three params to create the same browser
129
bc.setURL (getDocumentURL ());
130         return bc;
131     }
132
133     public HelpCtx getHelpCtx () {
134         return new HelpCtx(HtmlBrowserComponent.class);
135     }
136
137     protected void componentActivated () {
138         browserComponent.getBrowserImpl().getComponent ().requestFocusInWindow ();
139         super.componentActivated ();
140     }
141
142     public java.awt.Image JavaDoc getIcon () {
143         return new ImageIcon JavaDoc (HtmlBrowser.class.getResource ("/org/openide/resources/html/htmlView.gif")).getImage (); // NOI18N
144
}
145     
146
147     // public methods ....................................................................................
148

149     /**
150     * Sets new URL.
151     *
152     * @param str URL to show in this browser.
153     */

154     public void setURL (String JavaDoc str) {
155         browserComponent.setURL (str);
156     }
157
158     /**
159     * Sets new URL.
160     *
161     * @param url URL to show in this browser.
162     */

163     public void setURL (final URL JavaDoc url) {
164         browserComponent.setURL (url);
165     }
166
167     /**
168     * Gets current document url.
169     */

170     public final URL JavaDoc getDocumentURL () {
171         return browserComponent.getDocumentURL ();
172     }
173
174     /**
175     * Enables/disables Home button.
176     */

177     public final void setEnableHome (boolean b) {
178         browserComponent.setEnableHome (b);
179     }
180
181     /**
182     * Enables/disables location.
183     */

184     public final void setEnableLocation (boolean b) {
185         browserComponent.setEnableLocation (b);
186     }
187
188     /**
189     * Gets status line state.
190     */

191     public boolean isStatusLineVisible () {
192         return browserComponent.isStatusLineVisible ();
193     }
194
195     /**
196     * Shows/hides status line.
197     */

198     public void setStatusLineVisible (boolean v) {
199         browserComponent.setStatusLineVisible (v);
200     }
201
202     /**
203     * Gets status toolbar.
204     */

205     public boolean isToolbarVisible () {
206         return browserComponent.isToolbarVisible ();
207     }
208
209     /**
210     * Shows/hides toolbar.
211     */

212     public void setToolbarVisible (boolean v) {
213         browserComponent.setToolbarVisible (v);
214     }
215
216     protected java.lang.String JavaDoc preferredID() {
217         return "HtmlBrowserComponent"; //NOI18N
218
}
219
220 public static final class BrowserReplacer implements java.io.Externalizable JavaDoc {
221     
222     /** serial version UID */
223     static final long serialVersionUID = 5915713034827048413L;
224
225     
226     /** browser window to be serialized */
227     private transient HtmlBrowserComponent bComp = null;
228     transient boolean statLine;
229     transient boolean toolbar;
230     transient URL JavaDoc url;
231     
232     public BrowserReplacer () {
233     }
234     
235     public BrowserReplacer (HtmlBrowserComponent comp) {
236         bComp = comp;
237     }
238     
239
240     /* Serialize this top component.
241     * @param out the stream to serialize to
242     */

243     public void writeExternal (ObjectOutput JavaDoc out)
244     throws IOException JavaDoc {
245         out.writeBoolean (bComp.isStatusLineVisible ());
246         out.writeBoolean (bComp.isToolbarVisible ());
247         out.writeObject (bComp.getDocumentURL ());
248     }
249      
250     /* Deserialize this top component.
251       * @param in the stream to deserialize from
252       */

253     public void readExternal (ObjectInput JavaDoc in)
254     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
255         statLine = in.readBoolean ();
256         toolbar = in.readBoolean ();
257         url = (URL JavaDoc) in.readObject ();
258         
259     }
260
261
262     private Object JavaDoc readResolve ()
263     throws java.io.ObjectStreamException JavaDoc {
264         // return singleton instance
265
try {
266             if ("http".equals(url.getProtocol()) // NOI18N
267
&& InetAddress.getByName (url.getHost ()).equals (InetAddress.getLocalHost ())) {
268                 url.openStream ();
269             }
270         }
271         // ignore exceptions thrown during our test of accessibility and restore browser
272
catch (java.net.UnknownHostException JavaDoc exc) {}
273         catch (java.lang.SecurityException JavaDoc exc) {}
274         catch (java.lang.NullPointerException JavaDoc exc) {}
275         
276         catch (java.io.IOException JavaDoc exc) {
277             // do not restore JSP/servlet pages - covers FileNotFoundException, ConnectException
278
return null;
279         }
280         catch (java.lang.Exception JavaDoc exc) {
281             // unknown exception - write log message & restore browser
282
Logger.getLogger(HtmlBrowserComponent.class.getName()).log(Level.WARNING, null, exc);
283         }
284         
285         bComp = new HtmlBrowserComponent(statLine, toolbar);
286         bComp.setURL (url);
287         return bComp;
288     }
289
290 } // end of BrowserReplacer inner class
291

292 }
293
Popular Tags