1 /*
   2  * Copyright (c) 2014, 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 test.sandbox.app;
  27 
  28 import java.io.InputStream;
  29 import java.net.URL;
  30 import java.net.URLConnection;
  31 import javafx.fxml.FXMLLoader;
  32 import javafx.scene.Parent;
  33 import javafx.scene.Scene;
  34 import javafx.scene.control.Label;
  35 import javafx.scene.layout.VBox;
  36 import javafx.scene.paint.Color;
  37 import javafx.scene.web.WebEngine;
  38 import javafx.scene.web.WebView;
  39 
  40 import static test.sandbox.Constants.*;
  41 
  42 /**
  43  *
  44  * @author kcr
  45  */
  46 public class Util {
  47     private static final int WIDTH = 400;
  48     private static final int HEIGHT = 300;
  49 
  50     private static final String CSS_FILE_NAME = "test.css";
  51     private static final String FXML_FILE_NAME = "test.fxml";
  52     private static final String HTML_FILE_NAME = "test.html";
  53 
  54     // Convert the requested resource name into a URL string, and verify
  55     // that the resource can be accessed.
  56     private static URL toURL(String resourceName) throws Exception {
  57         URL url = FXApp.class.getResource(resourceName);
  58         URLConnection conn = url.openConnection();
  59         InputStream stream = conn.getInputStream();
  60         stream.close();
  61         return url;
  62     }
  63 
  64     // Create a JavaFX scene and populate it with content
  65     public static Scene createScene() throws Exception {
  66         VBox root = new VBox(10);
  67         Scene scene = new Scene(root, WIDTH, HEIGHT);
  68         scene.setFill(Color.WHITE);
  69 
  70         final String styleSheet = toURL(CSS_FILE_NAME).toExternalForm();
  71         scene.getStylesheets().clear();
  72         scene.getStylesheets().add(styleSheet);
  73 
  74         Label label = new Label();
  75         label.setText("Label");
  76 
  77         final URL fxmlFile = toURL(FXML_FILE_NAME);
  78         Parent fxmlRoot = (Parent)FXMLLoader.load(fxmlFile);
  79 
  80         final String webURLString = toURL(HTML_FILE_NAME).toExternalForm();
  81         WebView webView = new WebView();
  82         WebEngine webEngine = webView.getEngine();
  83         webEngine.load(webURLString);
  84 
  85         root.getChildren().addAll(label, fxmlRoot, webView);
  86         return scene;
  87     }
  88 
  89     public static void setupTimeoutThread() {
  90         // Timeout thread
  91         Thread th = new Thread(() -> {
  92             try {
  93                 Thread.sleep(TIMEOUT);
  94             } catch (InterruptedException ex) {
  95             }
  96             System.exit(ERROR_TIMEOUT);
  97         });
  98         th.setDaemon(true);
  99         th.start();
 100     }
 101 
 102     // No need to ever create an instance of this class
 103     private Util() {}
 104 
 105 }