1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.application;
  27 
  28 import java.net.URI;
  29 
  30 import netscape.javascript.JSObject;
  31 
  32 import com.sun.javafx.application.HostServicesDelegate;
  33 
  34 /**
  35  * This class provides HostServices for an Application. This includes
  36  * methods to get the code base and document base for an Application,
  37  * show a web page in a browser, and communicate with the enclosing web page
  38  * using JavaScript if the Application is running in
  39  * a browser.
  40  * @since JavaFX 2.0
  41  */
  42 public final class HostServices {
  43 
  44     private final HostServicesDelegate delegate;
  45 
  46     /**
  47      * Package scope constructor to create the HostServices object.
  48      *
  49      * @param app the application class
  50      */
  51     HostServices(Application app) {
  52         delegate = HostServicesDelegate.getInstance(app);
  53     }
  54 
  55     /**
  56      * Gets the code base URI for this application.
  57      * If the application was launched via a JNLP file, this method returns
  58      * the codebase parameter specified in the JNLP file.
  59      * If the application was launched in standalone mode, this method returns
  60      * the directory containing the application jar file. If the
  61      * application is not packaged in a jar file, this method
  62      * returns the empty string.
  63      *
  64      * @return the code base URI for this application.
  65      */
  66     public final String getCodeBase() {
  67         return delegate.getCodeBase();
  68     }
  69 
  70     /**
  71      * Gets the document base URI for this application.
  72      * If the application is embedded in a browser, this method returns the
  73      * URI of the web page containing the application.
  74      * If the application was launched in webstart mode, this method returns
  75      * the the codebase parameter specified in the JNLP file (the document
  76      * base and the code base are the same in this mode).
  77      * If the application was launched in standalone mode, this method returns
  78      * the URI of the current directory.
  79      *
  80      * @return the document base URI for this application.
  81      */
  82     public final String getDocumentBase() {
  83         return delegate.getDocumentBase();
  84     }
  85 
  86     /**
  87      * Resolves the specified relative URI against the base URI and returns
  88      * the resolved URI.
  89      *
  90      * <p>Example:</p>
  91      * <ul>
  92      * <pre>
  93      * HostServices services = getHostServices();
  94      * String myImage = services.resolveURI(services.getDocumentBase(),
  95      *                                      "image.jpg");
  96      * Image image = new Image(myImage);
  97      * </pre>
  98      * </ul>
  99      *
 100      * @param base the base URI against which to resolve the relative URI
 101      *
 102      * @param rel the relative URI to be resolved
 103      *
 104      * @throws NullPointerException if either the <code>base</code> or the
 105      * <code>rel</code> strings are null.
 106      * @throws IllegalArgumentException if there is an error parsing either
 107      * the <code>base</code> or <code>rel</code> URI strings, or if there is
 108      * any other error in resolving the URI.
 109      *
 110      * @return the fully resolved URI.
 111      */
 112     public final String resolveURI(String base, String rel) {
 113         URI uri = URI.create(base).resolve(rel);
 114         return uri.toString();
 115     }
 116 
 117     /**
 118      * Opens the specified URI in a new browser window or tab.
 119      * The determination of whether it is a new browser window or a tab in
 120      * an existing browser window will be made by the browser preferences.
 121      * Note that this will respect the pop-up blocker settings of the default
 122      * browser; it will not try to circumvent them.
 123      *
 124      * @param uri the URI of the web page that will be opened in a browser.
 125      */
 126     public final void showDocument(String uri) {
 127         delegate.showDocument(uri);
 128     }
 129 
 130     /**
 131      * Returns the JavaScript handle of the enclosing DOM window of the web
 132      * page containing this application.
 133      * This handle is used to access the web page by calling from Java into
 134      * JavaScript.
 135      * If the application is not embedded into a web page, this method
 136      * return null.
 137      *
 138      * <p>Example:</p>
 139      * <ul>
 140      * <pre>
 141      * JSObject jsWin = getHostServices().getWebContext();
 142      * if (jsWin != null) {
 143      *     jsWin.eval("var b = document.body;" +
 144      *                "var newdiv = document.createElement('div');" +
 145      *                "newdiv.innerHTML = '&lt;br&gt;Hello from JavaScript!';" +
 146      *                "b.appendChild(newdiv);");
 147      * }
 148      * </pre>
 149      * </ul>
 150      *
 151      * @return handle of the enclosing DOM window of the web page containing
 152      * this application
 153      *
 154      * @deprecated This method is deprecated as of JDK 9 because the
 155      * {@link java.applet.Applet Applet} API is deprecated.
 156      */
 157     @Deprecated // FIXME: (since = "9")
 158     public final JSObject getWebContext() {
 159         return delegate.getWebContext();
 160     }
 161 
 162 }