1 /*
   2  * Copyright (c) 2014, 2016, 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 package javafx.scene.control.test.fxcanvas;
  26 
  27 import java.util.concurrent.Semaphore;
  28 import javafx.beans.value.ChangeListener;
  29 import javafx.beans.value.ObservableValue;
  30 import javafx.concurrent.Worker.State;
  31 import javafx.scene.control.Button;
  32 import org.eclipse.swt.SWT;
  33 import org.eclipse.swt.graphics.Rectangle;
  34 import org.eclipse.swt.layout.FillLayout;
  35 import org.eclipse.swt.widgets.Display;
  36 import org.eclipse.swt.widgets.Monitor;
  37 import org.eclipse.swt.widgets.Shell;
  38 import javafx.scene.Scene;
  39 import javafx.scene.layout.VBox;
  40 import javafx.embed.swt.FXCanvas;
  41 import javafx.event.ActionEvent;
  42 import javafx.event.EventHandler;
  43 import javafx.scene.control.Label;
  44 import javafx.scene.layout.HBox;
  45 import javafx.scene.layout.Priority;
  46 import javafx.scene.web.WebView;
  47 import test.javaclient.shared.OtherThreadRunner;
  48 import test.javaclient.shared.Utils;
  49 
  50 public class FXCanvasBrowserApp {
  51 
  52     public static final String CONTENT_ID = "content.id";
  53     public static final String BUTTON_ID = "button.id";
  54     public static final String SUCCESS_LABEL_ID = "SUCCESS_LABEL_ID";
  55     public static final String SUCCESS_MESSAGE = "SUCCESS";
  56     public static int SCENE_WIDTH = 200;
  57     public static int SCENE_HEIGHT = 200;
  58     protected Scene scene;
  59     protected Label successLabel;
  60     WebView browser = null;
  61     Shell shell;
  62 
  63     protected FXCanvasBrowserApp() {
  64         shell = new Shell();
  65         shell.setText(this.getClass().getSimpleName());
  66         shell.setLayout(new FillLayout());
  67         FXCanvas fxCanvas = new FXCanvas(shell, SWT.BORDER);
  68 
  69         browser = new WebView();
  70         browser.getEngine().getLoadWorker().stateProperty().addListener(
  71                 new ChangeListener<State>() {
  72             public void changed(ObservableValue ov, State oldState, State newState) {
  73                 if (newState == State.SUCCEEDED) {
  74                     successLabel.setText(SUCCESS_MESSAGE);
  75                 }
  76             }
  77         });
  78         fxCanvas.setScene(createScene());
  79 
  80         shell.pack();
  81         Monitor monitor = shell.getMonitor();
  82         Rectangle monitorRect = monitor.getClientArea();
  83         Rectangle shellRect = shell.getBounds();
  84         shellRect.x = Math.max(0, (monitorRect.width - shellRect.width) / 2);
  85         shellRect.y = Math.max(0, (monitorRect.height - shellRect.height) / 2);
  86         shell.setBounds(shellRect);
  87         shell.open();
  88     }
  89 
  90     protected Scene createScene() {
  91         final VBox pane = new VBox();
  92         pane.setMinSize(SCENE_WIDTH, SCENE_HEIGHT);
  93         pane.setMaxSize(SCENE_WIDTH, SCENE_HEIGHT);
  94         pane.setPrefSize(SCENE_WIDTH, SCENE_HEIGHT);
  95         Scene scene = new Scene(pane, SCENE_WIDTH, SCENE_HEIGHT);
  96 
  97         Button button = new Button("Refresh");
  98         button.setId(BUTTON_ID);
  99         button.setOnAction(new EventHandler<ActionEvent>() {
 100             public void handle(ActionEvent t) {
 101                 browser.getEngine().load("http://education.oracle.com/");
 102             }
 103         });
 104         browser.setId(CONTENT_ID);
 105 
 106         successLabel = new Label();
 107         successLabel.setId(SUCCESS_LABEL_ID);
 108 
 109         pane.setVgrow(browser, Priority.ALWAYS);
 110         pane.getChildren().add(new HBox(button, successLabel));
 111         pane.getChildren().add(browser);
 112 
 113         Utils.setCustomFont(scene);
 114 
 115         return scene;
 116     }
 117 
 118     public static void startAndWaitShell() throws InterruptedException {
 119         Semaphore shellWaiter = new Semaphore(0);
 120         OtherThreadRunner.invokeOnMainThread(() -> {
 121             Display display = new Display();
 122             Shell shell = new FXCanvasBrowserApp().shell;
 123             shellWaiter.release();
 124             while (!shell.isDisposed() && OtherThreadRunner.isRunning()) {
 125                 if (!display.readAndDispatch()) {
 126                     display.sleep();
 127                 }
 128             }
 129             display.dispose();
 130         });
 131         shellWaiter.acquire();
 132     }
 133 
 134     public static void main(String[] args) {
 135         try {
 136             startAndWaitShell();
 137         } catch (InterruptedException ex) {
 138             System.err.printf("Failed to start SWT application: %s.\n", ex);
 139         }
 140     }
 141 }