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 * This method returns 55 * the directory containing the application jar file. If the 56 * application is not packaged in a jar file, this method 57 * returns the empty string. 58 * 59 * @return the code base URI for this application. 60 */ 61 public final String getCodeBase() { 62 return delegate.getCodeBase(); 63 } 64 65 /** 66 * Gets the document base URI for this application. 67 * This method returns 68 * the URI of the current directory. 69 * 70 * @return the document base URI for this application. 71 */ 72 public final String getDocumentBase() { 73 return delegate.getDocumentBase(); 74 } 75 76 /** 77 * Resolves the specified relative URI against the base URI and returns 78 * the resolved URI. 79 * 80 * <p>Example:</p> 81 * <pre> 82 * HostServices services = getHostServices(); 83 * String myImage = services.resolveURI(services.getDocumentBase(), 84 * "image.jpg"); 85 * Image image = new Image(myImage); 86 * </pre> 87 * 88 * @param base the base URI against which to resolve the relative URI 89 * 90 * @param rel the relative URI to be resolved 91 * 92 * @throws NullPointerException if either the <code>base</code> or the 93 * <code>rel</code> strings are null. 94 * @throws IllegalArgumentException if there is an error parsing either 95 * the <code>base</code> or <code>rel</code> URI strings, or if there is 96 * any other error in resolving the URI. 97 * 98 * @return the fully resolved URI. 99 */ 100 public final String resolveURI(String base, String rel) { 101 URI uri = URI.create(base).resolve(rel); 102 return uri.toString(); 103 } 104 105 /** 106 * Opens the specified URI in a new browser window or tab. 107 * The determination of whether it is a new browser window or a tab in 108 * an existing browser window will be made by the browser preferences. 109 * Note that this will respect the pop-up blocker settings of the default 110 * browser; it will not try to circumvent them. 111 * 112 * @param uri the URI of the web page that will be opened in a browser. 113 */ 114 public final void showDocument(String uri) { 115 delegate.showDocument(uri); 116 } 117 118 }