1 /*
   2  * Copyright (c) 2016, 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 hello;
  27 
  28 import javafx.application.Application;
  29 import javafx.application.HostServices;
  30 import javafx.geometry.Insets;
  31 import javafx.scene.Scene;
  32 import javafx.scene.control.Button;
  33 import javafx.scene.control.Label;
  34 import javafx.scene.layout.HBox;
  35 import javafx.scene.layout.VBox;
  36 import javafx.stage.Stage;
  37 
  38 public class HelloHostServices extends Application {
  39 
  40     @Override
  41     public void start(Stage stage) {
  42         final HostServices hs = getHostServices();
  43         final Label codeBaseText = new Label();
  44         final Label documentBaseText = new Label();
  45         final Label showDocumentText = new Label();
  46 
  47         stage.setTitle("Hello HostServices");
  48 
  49         String userDir = System.getProperty("user.dir");
  50         userDir = userDir.replace("\\", "/");
  51         System.err.println("userDir = " + userDir);
  52         if (!userDir.startsWith("/")) {
  53             userDir = "/" + userDir;
  54         }
  55         String testDocBase = "file:" + userDir + "/";
  56         System.err.println("testDocBase = " + testDocBase);
  57         System.err.println("DocumentBase = " + hs.getDocumentBase());
  58         System.err.println("testDocBase.equals(hs.getDocumentBase()) = "
  59                 + testDocBase.equals(hs.getDocumentBase()));
  60 
  61         Button getCodeBaseBtn = new Button("Get CodeBase");
  62         getCodeBaseBtn.setOnAction(event -> codeBaseText.setText(
  63                 hs.getCodeBase().isEmpty() ? "EMPTY" : hs.getCodeBase()));
  64 
  65         documentBaseText.setPrefWidth(400);
  66         Button getDocumentBaseBtn = new Button("Get DocumentBase");
  67         getDocumentBaseBtn.setOnAction(event -> documentBaseText.setText(hs.getDocumentBase()));
  68 
  69         Button showDocmentBtn = new Button("Show Document");
  70         showDocmentBtn.setOnAction(event
  71                 -> showDocument(hs, showDocumentText, "http://www.oracle.com/java/"));
  72 
  73         VBox textBox = new VBox(15);
  74         textBox.setFillWidth(false);
  75         textBox.getChildren().addAll(codeBaseText, documentBaseText, showDocumentText);
  76 
  77         VBox buttonBox = new VBox(5);
  78         buttonBox.getChildren().addAll(
  79                 getCodeBaseBtn,
  80                 getDocumentBaseBtn,
  81                 showDocmentBtn);
  82 
  83         HBox root = new HBox(7);
  84         root.setPadding(new Insets(11, 12, 12, 11));
  85         root.getChildren().addAll(buttonBox, textBox);
  86         stage.setScene(new Scene(root, 600, 150));
  87         stage.show();
  88     }
  89 
  90     private void showDocument(HostServices hs, Label showDocumentText, String urlStr) {
  91         showDocumentText.setText(urlStr + " will be shown in the browser.");
  92         hs.showDocument(urlStr);
  93     }
  94 
  95     /**
  96      * @param args the command line arguments
  97      */
  98     public static void main(String[] args) {
  99         Application.launch(args);
 100     }
 101 }