001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.awt.BorderLayout;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.IOException;
048: import java.io.ObjectInput;
049: import java.io.ObjectOutput;
050: import java.net.InetAddress;
051: import java.net.URL;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054: import javax.swing.ImageIcon;
055: import org.openide.awt.HtmlBrowser;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.NbBundle;
058: import org.openide.windows.CloneableTopComponent;
059:
060: /**
061: * Formerly HtmlBrowser.BrowserComponent.
062: */
063: class HtmlBrowserComponent extends CloneableTopComponent implements
064: PropertyChangeListener {
065: /** generated Serialized Version UID */
066: static final long serialVersionUID = 2912844785502987960L;
067:
068: // variables .........................................................................................
069:
070: /** Delegating component */
071: private HtmlBrowser browserComponent;
072:
073: // initialization ....................................................................................
074:
075: /**
076: * Creates new html browser with toolbar and status line.
077: */
078: public HtmlBrowserComponent() {
079: this (true, true);
080: }
081:
082: /**
083: * Creates new html browser with toolbar and status line.
084: */
085: public HtmlBrowserComponent(boolean toolbar, boolean statusLine) {
086: this (null, toolbar, statusLine);
087: }
088:
089: /**
090: * Creates new html browser.
091: */
092: public HtmlBrowserComponent(HtmlBrowser.Factory fact,
093: boolean toolbar, boolean statusLine) {
094: setName(""); // NOI18N
095: setLayout(new BorderLayout());
096: add(browserComponent = new HtmlBrowser(fact, toolbar,
097: statusLine), "Center"); // NOI18N
098:
099: browserComponent.getBrowserImpl().addPropertyChangeListener(
100: this );
101:
102: // Ensure closed browsers are not stored:
103: if (browserComponent.getBrowserComponent() != null) {
104: putClientProperty("InternalBrowser", Boolean.TRUE); // NOI18N
105: }
106: setToolTipText(NbBundle.getBundle(HtmlBrowser.class).getString(
107: "HINT_WebBrowser"));
108: }
109:
110: public int getPersistenceType() {
111: return PERSISTENCE_ONLY_OPENED;
112: }
113:
114: public void propertyChange(PropertyChangeEvent e) {
115: if (!HtmlBrowser.Impl.PROP_TITLE.equals(e.getPropertyName()))
116: return;
117: String title = browserComponent.getBrowserImpl().getTitle();
118: if ((title == null) || (title.length() < 1))
119: return;
120: HtmlBrowserComponent.this .setName(title);
121: HtmlBrowserComponent.this .setDisplayName(title);
122: }
123:
124: /** always open this top component in our special mode, if
125: * no mode for this component is specified yet */
126: public void open() {
127: // do not open this component if this is dummy browser
128: if (browserComponent.getBrowserComponent() == null)
129: return;
130:
131: // behave like superclass
132: super .open();
133: }
134:
135: /** Serializes browser component -> writes Replacer object which
136: * holds browser content and look. */
137: protected Object writeReplace()
138: throws java.io.ObjectStreamException {
139: return new BrowserReplacer(this );
140: }
141:
142: /* Deserialize this top component. Now it is here for backward compatibility
143: * @param in the stream to deserialize from
144: */
145: public void readExternal(ObjectInput in) throws IOException,
146: ClassNotFoundException {
147: super .readExternal(in);
148: setStatusLineVisible(in.readBoolean());
149: setToolbarVisible(in.readBoolean());
150: browserComponent.setURL((URL) in.readObject());
151: }
152:
153: // TopComponent support ...................................................................
154:
155: protected CloneableTopComponent createClonedObject() {
156: HtmlBrowserComponent bc = new HtmlBrowserComponent(); // PENDING: this should pass all three params to create the same browser
157: bc.setURL(getDocumentURL());
158: return bc;
159: }
160:
161: public HelpCtx getHelpCtx() {
162: return new HelpCtx(HtmlBrowserComponent.class);
163: }
164:
165: protected void componentActivated() {
166: browserComponent.getBrowserImpl().getComponent()
167: .requestFocusInWindow();
168: super .componentActivated();
169: }
170:
171: public java.awt.Image getIcon() {
172: return new ImageIcon(
173: HtmlBrowser.class
174: .getResource("/org/openide/resources/html/htmlView.gif"))
175: .getImage(); // NOI18N
176: }
177:
178: // public methods ....................................................................................
179:
180: /**
181: * Sets new URL.
182: *
183: * @param str URL to show in this browser.
184: */
185: public void setURL(String str) {
186: browserComponent.setURL(str);
187: }
188:
189: /**
190: * Sets new URL.
191: *
192: * @param url URL to show in this browser.
193: */
194: public void setURL(final URL url) {
195: browserComponent.setURL(url);
196: }
197:
198: /**
199: * Gets current document url.
200: */
201: public final URL getDocumentURL() {
202: return browserComponent.getDocumentURL();
203: }
204:
205: /**
206: * Enables/disables Home button.
207: */
208: public final void setEnableHome(boolean b) {
209: browserComponent.setEnableHome(b);
210: }
211:
212: /**
213: * Enables/disables location.
214: */
215: public final void setEnableLocation(boolean b) {
216: browserComponent.setEnableLocation(b);
217: }
218:
219: /**
220: * Gets status line state.
221: */
222: public boolean isStatusLineVisible() {
223: return browserComponent.isStatusLineVisible();
224: }
225:
226: /**
227: * Shows/hides status line.
228: */
229: public void setStatusLineVisible(boolean v) {
230: browserComponent.setStatusLineVisible(v);
231: }
232:
233: /**
234: * Gets status toolbar.
235: */
236: public boolean isToolbarVisible() {
237: return browserComponent.isToolbarVisible();
238: }
239:
240: /**
241: * Shows/hides toolbar.
242: */
243: public void setToolbarVisible(boolean v) {
244: browserComponent.setToolbarVisible(v);
245: }
246:
247: protected java.lang.String preferredID() {
248: return "HtmlBrowserComponent"; //NOI18N
249: }
250:
251: public static final class BrowserReplacer implements
252: java.io.Externalizable {
253:
254: /** serial version UID */
255: static final long serialVersionUID = 5915713034827048413L;
256:
257: /** browser window to be serialized */
258: private transient HtmlBrowserComponent bComp = null;
259: transient boolean statLine;
260: transient boolean toolbar;
261: transient URL url;
262:
263: public BrowserReplacer() {
264: }
265:
266: public BrowserReplacer(HtmlBrowserComponent comp) {
267: bComp = comp;
268: }
269:
270: /* Serialize this top component.
271: * @param out the stream to serialize to
272: */
273: public void writeExternal(ObjectOutput out) throws IOException {
274: out.writeBoolean(bComp.isStatusLineVisible());
275: out.writeBoolean(bComp.isToolbarVisible());
276: out.writeObject(bComp.getDocumentURL());
277: }
278:
279: /* Deserialize this top component.
280: * @param in the stream to deserialize from
281: */
282: public void readExternal(ObjectInput in) throws IOException,
283: ClassNotFoundException {
284: statLine = in.readBoolean();
285: toolbar = in.readBoolean();
286: url = (URL) in.readObject();
287:
288: }
289:
290: private Object readResolve()
291: throws java.io.ObjectStreamException {
292: // return singleton instance
293: try {
294: if ("http".equals(url.getProtocol()) // NOI18N
295: && InetAddress.getByName(url.getHost()).equals(
296: InetAddress.getLocalHost())) {
297: url.openStream();
298: }
299: }
300: // ignore exceptions thrown during our test of accessibility and restore browser
301: catch (java.net.UnknownHostException exc) {
302: } catch (java.lang.SecurityException exc) {
303: } catch (java.lang.NullPointerException exc) {
304: }
305:
306: catch (java.io.IOException exc) {
307: // do not restore JSP/servlet pages - covers FileNotFoundException, ConnectException
308: return null;
309: } catch (java.lang.Exception exc) {
310: // unknown exception - write log message & restore browser
311: Logger.getLogger(HtmlBrowserComponent.class.getName())
312: .log(Level.WARNING, null, exc);
313: }
314:
315: bComp = new HtmlBrowserComponent(statLine, toolbar);
316: bComp.setURL(url);
317: return bComp;
318: }
319:
320: } // end of BrowserReplacer inner class
321:
322: }
|