PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/o.n.core/src/org/netbeans/core/HtmlBrowserComponent.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 522 lines | 332 code | 71 blank | 119 comment | 57 complexity | b94d17fdd00b38200c3789e0599ad270 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.core;
  45. import java.awt.BorderLayout;
  46. import java.awt.Component;
  47. import java.beans.PropertyChangeEvent;
  48. import java.beans.PropertyChangeListener;
  49. import java.io.IOException;
  50. import java.io.ObjectInput;
  51. import java.io.ObjectOutput;
  52. import java.net.InetAddress;
  53. import java.net.MalformedURLException;
  54. import java.net.URL;
  55. import java.util.logging.Level;
  56. import java.util.logging.Logger;
  57. import javax.swing.ImageIcon;
  58. import javax.swing.SwingUtilities;
  59. import org.openide.awt.HtmlBrowser;
  60. import org.openide.awt.StatusDisplayer;
  61. import org.openide.nodes.Node;
  62. import org.openide.util.HelpCtx;
  63. import org.openide.util.Lookup;
  64. import org.openide.util.NbBundle;
  65. import org.openide.util.lookup.ProxyLookup;
  66. import org.openide.windows.CloneableTopComponent;
  67. /**
  68. * Formerly HtmlBrowser.BrowserComponent.
  69. */
  70. public class HtmlBrowserComponent extends CloneableTopComponent implements PropertyChangeListener {
  71. /** generated Serialized Version UID */
  72. static final long serialVersionUID = 2912844785502987960L;
  73. // variables .........................................................................................
  74. /** Delegating component */
  75. private HtmlBrowser browserComponent;
  76. private HtmlBrowser.Factory browserFactory;
  77. private final MyLookup proxyLookup = new MyLookup();
  78. // initialization ....................................................................................
  79. /**
  80. * Creates new html browser with toolbar and status line.
  81. */
  82. public HtmlBrowserComponent() {
  83. this (true, false);
  84. }
  85. /**
  86. * Creates new html browser with toolbar and status line.
  87. */
  88. public HtmlBrowserComponent(boolean toolbar, boolean statusLine) {
  89. this (IDESettings.getWWWBrowser(), toolbar, statusLine);
  90. }
  91. private HtmlBrowserComponent(boolean toolbar, boolean statusLine, URL url) {
  92. this (IDESettings.getWWWBrowser(), toolbar, statusLine);
  93. urlToLoad = null == url ? null : url.toExternalForm();
  94. }
  95. /**
  96. * Creates new html browser.
  97. */
  98. public HtmlBrowserComponent(HtmlBrowser.Factory fact, boolean toolbar, boolean statusLine) {
  99. setName (""); // NOI18N
  100. setLayout (new BorderLayout ());
  101. this.browserFactory = fact;
  102. setToolTipText(NbBundle.getBundle(HtmlBrowser.class).getString("HINT_WebBrowser")); //NOI18N
  103. //don't use page title for display name as it can be VERY long
  104. setName(NbBundle.getMessage(HtmlBrowserComponent.class, "Title_WebBrowser")); //NOI18N
  105. setDisplayName(getDefaultDisplayName());
  106. putClientProperty( "KeepNonPersistentTCInModelWhenClosed", Boolean.TRUE );
  107. setActivatedNodes( new Node[0] );
  108. }
  109. private String getDefaultDisplayName() {
  110. return NbBundle.getMessage(HtmlBrowserComponent.class, "Title_WebBrowser"); //NOI18N
  111. }
  112. @Override
  113. public Lookup getLookup() {
  114. return proxyLookup;
  115. }
  116. @Override
  117. public int getPersistenceType() {
  118. return PERSISTENCE_ONLY_OPENED;
  119. }
  120. @Override
  121. public void propertyChange (PropertyChangeEvent e) {
  122. if( HtmlBrowser.Impl.PROP_STATUS_MESSAGE.equals(e.getPropertyName()) ) {
  123. StatusDisplayer.getDefault().setStatusText(browserComponent.getBrowserImpl().getStatusMessage());
  124. return;
  125. } else if( HtmlBrowser.Impl.PROP_TITLE.equals (e.getPropertyName ()) ) {
  126. String title = browserComponent.getBrowserImpl().getTitle();
  127. if ((title == null) || (title.length () < 1))
  128. return;
  129. setToolTipText(title);
  130. setDisplayName( makeShort(title) );
  131. } else if( HtmlBrowser.Impl.PROP_LOADING.equals (e.getPropertyName ()) ) {
  132. boolean loading = ((Boolean)e.getNewValue()).booleanValue();
  133. makeBusy( loading );
  134. }
  135. }
  136. private static final int MAX_TITLE_LENGTH = 25;
  137. private static String makeShort( String title ) {
  138. if( title.length() > MAX_TITLE_LENGTH ) {
  139. title = title.substring( 0, MAX_TITLE_LENGTH);
  140. title += "…"; //NOI18N
  141. }
  142. return title;
  143. }
  144. /** always open this top component in our special mode, if
  145. * no mode for this component is specified yet */
  146. @Override
  147. public void open() {
  148. // do not open this component if this is dummy browser
  149. if (null != browserComponent && browserComponent.getBrowserComponent() == null) {
  150. return;
  151. }
  152. setDisplayName( getDefaultDisplayName() );
  153. // behave like superclass
  154. super.open();
  155. }
  156. /** Serializes browser component -> writes Replacer object which
  157. * holds browser content and look. */
  158. @Override
  159. protected Object writeReplace ()
  160. throws java.io.ObjectStreamException {
  161. return new BrowserReplacer (this);
  162. }
  163. /* Deserialize this top component. Now it is here for backward compatibility
  164. * @param in the stream to deserialize from
  165. */
  166. @Override
  167. public void readExternal (ObjectInput in)
  168. throws IOException, ClassNotFoundException {
  169. super.readExternal (in);
  170. setStatusLineVisible (in.readBoolean ());
  171. setToolbarVisible (in.readBoolean ());
  172. browserComponent.setURL ((URL) in.readObject ());
  173. }
  174. // TopComponent support ...................................................................
  175. @Override
  176. protected CloneableTopComponent createClonedObject () {
  177. HtmlBrowserComponent bc = new HtmlBrowserComponent(browserFactory, isToolbarVisible(), isStatusLineVisible());
  178. bc.setURL (getDocumentURL ());
  179. return bc;
  180. }
  181. @Override
  182. public HelpCtx getHelpCtx () {
  183. return new HelpCtx(HtmlBrowserComponent.class);
  184. }
  185. @Override
  186. protected void componentActivated () {
  187. if( null != browserComponent ) {
  188. HtmlBrowser.Impl impl = browserComponent.getBrowserImpl();
  189. if( null != impl ) {
  190. Component c = impl.getComponent();
  191. if( null != c )
  192. c.requestFocusInWindow();
  193. }
  194. }
  195. super.componentActivated ();
  196. SwingUtilities.invokeLater( new Runnable() {
  197. @Override
  198. public void run() {
  199. setEnableHome(enableHome);
  200. setEnableLocation(enableLocation);
  201. setToolbarVisible(toolbarVisible);
  202. setStatusLineVisible(statusVisible);
  203. if( null != urlToLoad ) {
  204. setURL(urlToLoad);
  205. }
  206. urlToLoad = null;
  207. }
  208. });
  209. }
  210. @Override
  211. protected void componentClosed() {
  212. releaseBrowser();
  213. }
  214. /**
  215. * Creates and initializes the internal browser component again.
  216. */
  217. public void recreateBrowser() {
  218. releaseBrowser();
  219. createBrowser();
  220. }
  221. private void releaseBrowser() {
  222. if( null != browserComponent ) {
  223. toolbarVisible = isToolbarVisible();
  224. statusVisible = isStatusLineVisible();
  225. URL url = browserComponent.getBrowserImpl().getURL();
  226. urlToLoad = null == url ? null : url.toExternalForm();
  227. browserComponent.getBrowserImpl().removePropertyChangeListener(this);
  228. browserComponent.getBrowserImpl().dispose();
  229. }
  230. removeAll();
  231. browserComponent = null;
  232. }
  233. private void createBrowser() {
  234. if( null == browserComponent ) {
  235. browserComponent = createBrowser( browserFactory, toolbarVisible, statusVisible );
  236. initBrowser();
  237. }
  238. }
  239. Lookup getBrowserLookup() {
  240. if (browserComponent != null) {
  241. return browserComponent.getBrowserImpl().getLookup();
  242. } else {
  243. return Lookup.EMPTY;
  244. }
  245. }
  246. @Override
  247. protected void componentOpened() {
  248. createBrowser();
  249. }
  250. @Override
  251. public java.awt.Image getIcon () {
  252. return new ImageIcon (HtmlBrowser.class.getResource ("/org/openide/resources/html/htmlView.gif")).getImage (); // NOI18N
  253. }
  254. protected HtmlBrowser createBrowser( HtmlBrowser.Factory factory, boolean showToolbar, boolean showStatus ) {
  255. return new HtmlBrowser( factory, showToolbar, showStatus );
  256. }
  257. private void initBrowser() {
  258. add( browserComponent, BorderLayout.CENTER );
  259. // associate with this TopComponent lookup provided by browser (HtmlBrowser.Impl.getLookup)
  260. proxyLookup.setLookup(getBrowserLookup());
  261. browserComponent.getBrowserImpl().addPropertyChangeListener (this);
  262. // Ensure closed browsers are not stored:
  263. if (browserComponent.getBrowserComponent() != null) {
  264. putClientProperty("InternalBrowser", Boolean.TRUE); // NOI18N
  265. }
  266. }
  267. // public methods ....................................................................................
  268. /**
  269. * Sets new URL.
  270. *
  271. * @param str URL to show in this browser.
  272. */
  273. public void setURL (String str) {
  274. if( null == browserComponent ) {
  275. urlToLoad = str;
  276. return;
  277. }
  278. urlToLoad = null;
  279. browserComponent.setURL (str);
  280. }
  281. private String urlToLoad;
  282. /**
  283. * Sets new URL.
  284. *
  285. * @param url URL to show in this browser.
  286. */
  287. public void setURL (final URL url) {
  288. if( null == browserComponent ) {
  289. urlToLoad = null == url ? null : url.toExternalForm();
  290. return;
  291. }
  292. urlToLoad = null;
  293. browserComponent.setURL (url);
  294. }
  295. /**
  296. * Gets current document url.
  297. */
  298. public final URL getDocumentURL () {
  299. if( null == browserComponent ) {
  300. URL url = null;
  301. if( null != urlToLoad ) {
  302. try {
  303. url = new URL(urlToLoad);
  304. } catch( MalformedURLException ex ) {
  305. }
  306. }
  307. return url;
  308. }
  309. return browserComponent.getDocumentURL ();
  310. }
  311. private boolean enableHome = true;
  312. /**
  313. * Enables/disables Home button.
  314. */
  315. public final void setEnableHome (boolean b) {
  316. if( null == browserComponent ) {
  317. enableHome = b;
  318. return;
  319. }
  320. browserComponent.setEnableHome (b);
  321. }
  322. private boolean enableLocation = true;
  323. /**
  324. * Enables/disables location.
  325. */
  326. public final void setEnableLocation (boolean b) {
  327. if( null == browserComponent ) {
  328. enableLocation = b;
  329. return;
  330. }
  331. browserComponent.setEnableLocation (b);
  332. }
  333. private boolean statusVisible = false;
  334. /**
  335. * Gets status line state.
  336. */
  337. public boolean isStatusLineVisible () {
  338. if( null == browserComponent )
  339. return statusVisible;
  340. return browserComponent.isStatusLineVisible ();
  341. }
  342. /**
  343. * Shows/hides status line.
  344. */
  345. public void setStatusLineVisible (boolean v) {
  346. if( null == browserComponent ) {
  347. statusVisible = v;
  348. return;
  349. }
  350. browserComponent.setStatusLineVisible (v);
  351. }
  352. private boolean toolbarVisible = true;
  353. /**
  354. * Gets status toolbar.
  355. */
  356. public boolean isToolbarVisible () {
  357. if( null == browserComponent )
  358. return toolbarVisible;
  359. return browserComponent.isToolbarVisible ();
  360. }
  361. /**
  362. * Shows/hides toolbar.
  363. */
  364. public void setToolbarVisible (boolean v) {
  365. if( null == browserComponent ) {
  366. toolbarVisible = v;
  367. return;
  368. }
  369. browserComponent.setToolbarVisible (v);
  370. }
  371. @Override
  372. protected java.lang.String preferredID() {
  373. return "HtmlBrowserComponent"; //NOI18N
  374. }
  375. public void setURLAndOpen( URL url ) {
  376. createBrowser();
  377. browserComponent.setURL(url);
  378. urlToLoad = null;
  379. if( null != browserComponent.getBrowserComponent() ) {
  380. open();
  381. requestActive();
  382. }
  383. }
  384. public HtmlBrowser.Impl getBrowserImpl(){
  385. return browserComponent.getBrowserImpl();
  386. }
  387. public static final class BrowserReplacer implements java.io.Externalizable {
  388. /** serial version UID */
  389. static final long serialVersionUID = 5915713034827048413L;
  390. /** browser window to be serialized */
  391. private transient HtmlBrowserComponent bComp = null;
  392. transient boolean statLine;
  393. transient boolean toolbar;
  394. transient URL url;
  395. public BrowserReplacer () {
  396. }
  397. public BrowserReplacer (HtmlBrowserComponent comp) {
  398. bComp = comp;
  399. }
  400. /* Serialize this top component.
  401. * @param out the stream to serialize to
  402. */
  403. public void writeExternal (ObjectOutput out)
  404. throws IOException {
  405. out.writeBoolean (bComp.isStatusLineVisible ());
  406. out.writeBoolean (bComp.isToolbarVisible ());
  407. out.writeObject (bComp.getDocumentURL ());
  408. }
  409. /* Deserialize this top component.
  410. * @param in the stream to deserialize from
  411. */
  412. public void readExternal (ObjectInput in)
  413. throws IOException, ClassNotFoundException {
  414. statLine = in.readBoolean ();
  415. toolbar = in.readBoolean ();
  416. url = (URL) in.readObject ();
  417. }
  418. private Object readResolve ()
  419. throws java.io.ObjectStreamException {
  420. // return singleton instance
  421. try {
  422. if ("http".equals(url.getProtocol()) // NOI18N
  423. && InetAddress.getByName (url.getHost ()).equals (InetAddress.getLocalHost ())) {
  424. url.openStream ();
  425. }
  426. }
  427. // ignore exceptions thrown during our test of accessibility and restore browser
  428. catch (java.net.UnknownHostException exc) {}
  429. catch (java.lang.SecurityException exc) {}
  430. catch (java.lang.NullPointerException exc) {}
  431. catch (java.io.IOException exc) {
  432. // do not restore JSP/servlet pages - covers FileNotFoundException, ConnectException
  433. return null;
  434. }
  435. catch (java.lang.Exception exc) {
  436. // unknown exception - write log message & restore browser
  437. Logger.getLogger(HtmlBrowserComponent.class.getName()).log(Level.WARNING, null, exc);
  438. }
  439. bComp = new HtmlBrowserComponent(statLine, toolbar, url);
  440. return bComp;
  441. }
  442. } // end of BrowserReplacer inner class
  443. private static class MyLookup extends ProxyLookup {
  444. public MyLookup() {
  445. super( Lookup.EMPTY );
  446. }
  447. public void setLookup( Lookup lkp ) {
  448. setLookups( new Lookup[] { lkp } );
  449. }
  450. }
  451. }