1 /*
   2  * Copyright (c) 2011, 2017, 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 com.sun.javafx.application.HostServicesDelegate;
  31 
  32 /**
  33  * This class provides HostServices for an Application. This includes
  34  * methods to get the code base and document base for an Application,
  35  * and to show a web page in a browser.
  36  *
  37  * @since JavaFX 2.0
  38  */
  39 public final class HostServices {
  40 
  41     private final HostServicesDelegate delegate;
  42 
  43     /**
  44      * Package scope constructor to create the HostServices object.
  45      *
  46      * @param app the application class
  47      */
  48     HostServices(Application app) {
  49         delegate = HostServicesDelegate.getInstance(app);
  50     }
  51 
  52     /**
  53      * Gets the code base URI for this application.
  54      * If the application was launched via a JNLP file, this method returns
  55      * the codebase parameter specified in the JNLP file.
  56      * If the application was launched in standalone mode, this method returns
  57      * the directory containing the application jar file. If the
  58      * application is not packaged in a jar file, this method
  59      * returns the empty string.
  60      *
  61      * @return the code base URI for this application.
  62      */
  63     public final String getCodeBase() {
  64         return delegate.getCodeBase();
  65     }
  66 
  67     /**
  68      * Gets the document base URI for this application.
  69      * If the application is embedded in a browser, this method returns the
  70      * URI of the web page containing the application.
  71      * If the application was launched in webstart mode, this method returns
  72      * the the codebase parameter specified in the JNLP file (the document
  73      * base and the code base are the same in this mode).
  74      * If the application was launched in standalone mode, this method returns
  75      * the URI of the current directory.
  76      *
  77      * @return the document base URI for this application.
  78      */
  79     public final String getDocumentBase() {
  80         return delegate.getDocumentBase();
  81     }
  82 
  83     /**
  84      * Resolves the specified relative URI against the base URI and returns
  85      * the resolved URI.
  86      *
  87      * <p>Example:</p>
  88      * <pre>
  89      *     HostServices services = getHostServices();
  90      *     String myImage = services.resolveURI(services.getDocumentBase(),
  91      *                                          "image.jpg");
  92      *     Image image = new Image(myImage);
  93      * </pre>
  94      *
  95      * @param base the base URI against which to resolve the relative URI
  96      *
  97      * @param rel the relative URI to be resolved
  98      *
  99      * @throws NullPointerException if either the <code>base</code> or the
 100      * <code>rel</code> strings are null.
 101      * @throws IllegalArgumentException if there is an error parsing either
 102      * the <code>base</code> or <code>rel</code> URI strings, or if there is
 103      * any other error in resolving the URI.
 104      *
 105      * @return the fully resolved URI.
 106      */
 107     public final String resolveURI(String base, String rel) {
 108         URI uri = URI.create(base).resolve(rel);
 109         return uri.toString();
 110     }
 111 
 112     /**
 113      * Opens the specified URI in a new browser window or tab.
 114      * The determination of whether it is a new browser window or a tab in
 115      * an existing browser window will be made by the browser preferences.
 116      * Note that this will respect the pop-up blocker settings of the default
 117      * browser; it will not try to circumvent them.
 118      *
 119      * @param uri the URI of the web page that will be opened in a browser.
 120      */
 121     public final void showDocument(String uri) {
 122         delegate.showDocument(uri);
 123     }
 124 
 125 }