Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.application;
027
028import java.net.URI;
029
030import netscape.javascript.JSObject;
031
032import com.sun.javafx.application.HostServicesDelegate;
033
034/**
035 * This class provides HostServices for an Application. This includes
036 * methods to get the code base and document base for an Application,
037 * show a web page in a browser, and communicate with the enclosing web page
038 * using JavaScript if the Application is running in
039 * a browser.
040 */
041public final class HostServices {
042
043    private final HostServicesDelegate delegate;
044
045    /**
046     * Package scope constructor to create the HostServices object.
047     *
048     * @param app the application class
049     */
050    HostServices(Application app) {
051        delegate = HostServicesDelegate.getInstance(app);
052    }
053
054    /**
055     * Gets the code base URI for this application.
056     * If the application was launched via a JNLP file, this method returns
057     * the codebase parameter specified in the JNLP file.
058     * If the application was launched in standalone mode, this method returns
059     * the directory containing the application jar file. If the
060     * application is not packaged in a jar file, this method
061     * returns the empty string.
062     *
063     * @return the code base URI for this application.
064     */
065    public final String getCodeBase() {
066        return delegate.getCodeBase();
067    }
068
069    /**
070     * Gets the document base URI for this application.
071     * If the application is embedded in a browser, this method returns the
072     * URI of the web page containing the application.
073     * If the application was launched in webstart mode, this method returns
074     * the the codebase parameter specified in the JNLP file (the document
075     * base and the code base are the same in this mode).
076     * If the application was launched in standalone mode, this method returns
077     * the URI of the current directory.
078     *
079     * @return the document base URI for this application.
080     */
081    public final String getDocumentBase() {
082        return delegate.getDocumentBase();
083    }
084
085    /**
086     * Resolves the specified relative URI against the base URI and returns
087     * the resolved URI.
088     *
089     * <p>Example:</p>
090     * <ul>
091     * <pre>
092     * HostServices services = getHostServices();
093     * String myImage = services.resolveURI(services.getDocumentBase(),
094     *                                      "image.jpg");
095     * Image image = new Image(myImage);
096     * </pre>
097     * </ul>
098     *
099     * @param base the base URI against which to resolve the relative URI
100     *
101     * @param rel the relative URI to be resolved
102     *
103     * @throws NullPointerException if either the <code>base</code> or the
104     * <code>rel</code> strings are null.
105     * @throws IllegalArgumentException if there is an error parsing either
106     * the <code>base</code> or <code>rel</code> URI strings, or if there is
107     * any other error in resolving the URI.
108     *
109     * @return the fully resolved URI.
110     */
111    public final String resolveURI(String base, String rel) {
112        URI uri = URI.create(base).resolve(rel);
113        return uri.toString();
114    }
115
116    /**
117     * Opens the specified URI in a new browser window or tab.
118     * The determination of whether it is a new browser window or a tab in
119     * an existing browser window will be made by the browser preferences.
120     * Note that this will respect the pop-up blocker settings of the default
121     * browser; it will not try to circumvent them.
122     *
123     * @param uri the URI of the web page that will be opened in a browser.
124     */
125    public final void showDocument(String uri) {
126        delegate.showDocument(uri);
127    }
128
129    /**
130     * Returns the JavaScript handle of the enclosing DOM window of the web
131     * page containing this application.
132     * This handle is used to access the web page by calling from Java into
133     * JavaScript.
134     * If the application is not embedded into a web page, this method
135     * return null.
136     *
137     * <p>Example:</p>
138     * <ul>
139     * <pre>
140     * JSObject jsWin = getHostServices().getWebContext();
141     * if (jsWin != null) {
142     *     jsWin.eval("var b = document.body;" +
143     *                "var newdiv = document.createElement('div');" +
144     *                "newdiv.innerHTML = '&lt;br&gt;Hello from JavaScript!';" +
145     *                "b.appendChild(newdiv);");
146     * }
147     * </pre>
148     * </ul>
149     *
150     * @return handle of the enclosing DOM window of the web page containing
151     * this application
152     */
153    public final JSObject getWebContext() {
154        return delegate.getWebContext();
155    }
156
157}