1 /*
   2  * Copyright (c) 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 
  26 import javafx.embed.swt.FXCanvas;
  27 import javafx.scene.Scene;
  28 import javafx.scene.control.TextArea;
  29 import javafx.scene.layout.AnchorPane;
  30 import org.eclipse.swt.SWT;
  31 import org.eclipse.swt.layout.FillLayout;
  32 import org.eclipse.swt.widgets.Display;
  33 import org.eclipse.swt.widgets.Shell;
  34 
  35 public class FXCanvasGestureEventsTest {
  36 
  37     static final String instructions =
  38             "This tests that SWT gesture events are properly transferred to JavaFX. " +
  39                     "It passes if proper event sequences for ZOOM (Mac, Windows), ROTATE (Mac, Windows), SCROLL (Mac, Windows)" +
  40                     "are printed to the console (SWT does not support any gestures on Linux yet, and SWIPE does not seem to be supported any more):\n\n " +
  41                     " 1) Perform a simple ZOOM gesture and observe that a 'ZoomStarted (Zoom)+ ZoomFinished' event sequence is printed out. The finish event should provide a zoom of '1.0' and a totalZoom that corresponds to the product of the zoom values of all preceding events.\n\n" +
  42                     " 2) Perform a simple ROTATE gesture and observe that a 'RotationStarted (Rotate)+ RotationFinished' event sequence is printed out. The finish event should provide an angle of '0.0' and a totalAngle that corresponds to the sum of the angle values of all preceding events. Note that with SWT < 3.8, rotate values will all be zero on MacOS 64-bit due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=349812.\n\n" +
  43                     " 3) Perform a complex ROTATE-ZOOM gesture (start with ROTATE then continue with ZOOM) and observe that a 'RotationStarted (Rotate)+ ZoomStarted (Zoom | Rotate)+ ZoomFinished (Rotate)* RotationFinished' event sequence is printed out.\n\n" +
  44                     " 4) Perform a complex ZOOM-ROTATE gesture (start with ZOOM then continue with ROTATE) and observe that a 'ZoomStarted (Zoom)+ RotationStarted (Zoom | Rotate)+ RotationFinished (Zoom)* ZoomFinished' event sequence is printed out.\n\n" +
  45                     " 5) Perform a simple vertical SCROLL gesture and observe that a 'ScrollStarted (Scroll)+ ScrollFinished (Scroll)*' event sequence is printed out. The finish event should provide a scrollY value of 0 and a totalScrollY value that corresponds to the sum of the scrollY events of all preceding events. The scroll events that occur after the finish event should have inertia set to true, while all others should have set inertia to false.\n\n" +
  46                     " 6) Perform a simple horizontal SCROLL gesture and observe that a 'ScrollStarted (Scroll)+ ScrollFinished (Scroll)*' event sequence is printed out. The finish event should provide a scrollX value of 0 and a totalScrollX value that corresponds to the sum of the scrollX events of all preceding events. The scroll events that occur after the finish event should have inertia set to true, while all others should have set inertia to false.\n\n";
  47 
  48     private static TextArea createInfo(String msg) {
  49         TextArea t = new TextArea(msg);
  50         t.setWrapText(true);
  51         t.setEditable(false);
  52         return t;
  53     }
  54 
  55     public static void main(String[] args) {
  56         final Display display = new Display();
  57         final Shell shell = new Shell(display);
  58         shell.setText("FXCanvasGestureEventsTest");
  59         shell.setSize(1000, 500);
  60         shell.setLayout(new FillLayout());
  61         final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
  62         shell.open();
  63 
  64         // create and hook scene
  65         TextArea info = createInfo(instructions);
  66         AnchorPane root = new AnchorPane();
  67         root.getChildren().add(info);
  68         AnchorPane.setBottomAnchor(info, 0d);
  69         AnchorPane.setTopAnchor(info, 0d);
  70         AnchorPane.setLeftAnchor(info, 0d);
  71         AnchorPane.setRightAnchor(info, 0d);
  72         final Scene scene = new Scene(root, 600, 400);
  73         canvas.setScene(scene);
  74 
  75         final int[] zoomEventCount = {0, 0, 0};
  76         root.setOnZoomStarted(zoomEvent -> {
  77             System.out.println("ZoomStarted event #" + zoomEventCount[0]++ + ": zoom: " + zoomEvent.getZoomFactor() + ", totalZoom: " + zoomEvent.getTotalZoomFactor());
  78         });
  79         root.setOnZoom(zoomEvent -> {
  80             System.out.println("Zoom event #" + zoomEventCount[1]++ + ": zoom: " + zoomEvent.getZoomFactor() + ", totalZoom: " + zoomEvent.getTotalZoomFactor());
  81         });
  82         root.setOnZoomFinished(zoomEvent -> {
  83             System.out.println("ZoomFinished event #" + zoomEventCount[2]++ + ": zoom: " + zoomEvent.getZoomFactor() + ", totalZoom: " + zoomEvent.getTotalZoomFactor());
  84         });
  85 
  86         final int[] scrollEventCount = {0, 0, 0};
  87         root.setOnScrollStarted(scrollEvent -> {
  88             System.out.println("ScrollStarted event #" + scrollEventCount[0]++ + ": scrollX: " + scrollEvent.getDeltaX() + ", scrollY: " + scrollEvent.getDeltaY() + ", totalScrollX: " + scrollEvent.getTotalDeltaX() + ", totalScrollY: " + scrollEvent.getTotalDeltaY());
  89         });
  90         root.setOnScroll(scrollEvent -> {
  91             System.out.println("Scroll event #" + scrollEventCount[1]++ + ": scrollX: " + scrollEvent.getDeltaX() + ", scrollY: " + scrollEvent.getDeltaY() + ", totalScrollX: " + scrollEvent.getTotalDeltaX() + ", totalScrollY: " + scrollEvent.getTotalDeltaY() + ", inertia: " + scrollEvent.isInertia());
  92         });
  93         root.setOnScrollFinished(scrollEvent -> {
  94             System.out.println("ScrollFinished event #" + scrollEventCount[2]++ + ": scrollX: " + scrollEvent.getDeltaX() + ", scrollY: " + scrollEvent.getDeltaY() + ", totalScrollX: " + scrollEvent.getTotalDeltaX() + ", totalScrollY: " + scrollEvent.getTotalDeltaY());
  95         });
  96 
  97         final int[] rotateEventCount = {0, 0, 0};
  98         root.setOnRotationStarted(rotateEvent -> {
  99             System.out.println("RotationStarted event #" + rotateEventCount[0]++ + ": angle: " + rotateEvent.getAngle() + ", totalAngle: " + rotateEvent.getTotalAngle());
 100         });
 101         root.setOnRotate(rotateEvent -> {
 102             System.out.println("Rotate event #" + rotateEventCount[0]++ + ": angle: " + rotateEvent.getAngle() + ", totalAngle: " + rotateEvent.getTotalAngle());
 103         });
 104         root.setOnRotationFinished(rotateEvent -> {
 105             System.out.println("RotationFinished event #" + rotateEventCount[0]++ + ": angle: " + rotateEvent.getAngle() + ", totalAngle: " + rotateEvent.getTotalAngle());
 106         });
 107 
 108         final int[] swipeEventCount = {0};
 109         root.setOnSwipeDown(swipeEvent -> {
 110             System.out.println("Swipe DOWN event #" + swipeEventCount[0]++);
 111         });
 112         root.setOnSwipeUp(swipeEvent -> {
 113             System.out.println("Swipe UP event #" + swipeEventCount[0]++);
 114         });
 115         root.setOnSwipeLeft(swipeEvent -> {
 116             System.out.println("Swipe LEFT event #" + swipeEventCount[0]++);
 117         });
 118         root.setOnSwipeRight(swipeEvent -> {
 119             System.out.println("Swipe RIGHT event #" + swipeEventCount[0]++);
 120         });
 121 
 122         while (!shell.isDisposed()) {
 123             // run SWT event loop
 124             if (!display.readAndDispatch()) {
 125                 display.sleep();
 126             }
 127         }
 128         display.dispose();
 129     }
 130 }