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 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 * <pre> 92 * HostServices services = getHostServices(); 93 * String myImage = services.resolveURI(services.getDocumentBase(), 94 * "image.jpg"); 95 * Image image = new Image(myImage); 96 * </pre> 97 * 98 * @param base the base URI against which to resolve the relative URI 99 * 100 * @param rel the relative URI to be resolved 101 * 102 * @throws NullPointerException if either the <code>base</code> or the 103 * <code>rel</code> strings are null. 104 * @throws IllegalArgumentException if there is an error parsing either 105 * the <code>base</code> or <code>rel</code> URI strings, or if there is 106 * any other error in resolving the URI. 107 * 108 * @return the fully resolved URI. 109 */ 110 public final String resolveURI(String base, String rel) { 111 URI uri = URI.create(base).resolve(rel); 112 return uri.toString(); 113 } 114 115 /** 116 * Opens the specified URI in a new browser window or tab. 117 * The determination of whether it is a new browser window or a tab in 118 * an existing browser window will be made by the browser preferences. 119 * Note that this will respect the pop-up blocker settings of the default 120 * browser; it will not try to circumvent them. 121 * 122 * @param uri the URI of the web page that will be opened in a browser. 123 */ 124 public final void showDocument(String uri) { 125 delegate.showDocument(uri); 126 } 127 128 /** 129 * Returns the JavaScript handle of the enclosing DOM window of the web 130 * page containing this application. 131 * This handle is used to access the web page by calling from Java into 132 * JavaScript. 133 * If the application is not embedded into a web page, this method 134 * return null. 135 * 136 * <p>Example:</p> 137 * <pre>{@code 138 * JSObject jsWin = getHostServices().getWebContext(); 139 * if (jsWin != null) { 140 * jsWin.eval("var b = document.body;" + 141 * "var newdiv = document.createElement('div');" + 142 * "newdiv.innerHTML = '<br>Hello from JavaScript!';" + 143 * "b.appendChild(newdiv);"); 144 * } 145 * }</pre> 146 * 147 * @return handle of the enclosing DOM window of the web page containing 148 * this application 149 * 150 * @deprecated This method is deprecated as of JDK 9 because the 151 * {@link java.applet.Applet Applet} API is deprecated. 152 */ 153 @Deprecated(since="9", forRemoval=true) 154 public final JSObject getWebContext() { 155 return delegate.getWebContext(); 156 } 157 158 }