1 /*
   2  * Copyright (c) 2010, 2013, 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 javafx.scene.input;
  27 
  28 import javafx.beans.NamedArg;
  29 import javafx.event.EventTarget;
  30 import javafx.event.EventType;
  31 
  32 /**
  33  * Swipe event indicates that user performed a swipe gesture such as
  34  * dragging a finger in one direction on touch screen.
  35  * <p>
  36  * Unlike some other gestures, the swipe gesture is not continual - the whole
  37  * gesture produces only one event. The event is delivered to the top-most
  38  * node picked on the gesture coordinates.
  39  * <p>
  40  * The swipe gesture has four types according to the movement direction.
  41  * The gesture can be performed by any number of touch points, the number
  42  * is provided by {@code getTouchCount()} method.
  43  * <p>
  44  * Note that swipe and scroll gestures are not exclusive. A single touch screen
  45  * action can result in both gestures being delivered.
  46  * <p>
  47  * Note that the capability to produce swipes is dependent on the used input
  48  * devices and underlying platform's capabilities and settings (especially
  49  * without touch-screen user's possibilities of producing swipes are
  50  * significantly reduced).
  51  * <p>
  52  * As all gestures, swipe can be direct (performed directly at
  53  * the concrete coordinates as on touch screen - the center of the gesture
  54  * is used as gesture coordinates) or indirect (performed
  55  * indirectly as on track pad - the mouse cursor location is usually used
  56  * as the gesture coordinates in this case).
  57  *
  58  * @since JavaFX 2.2
  59  */
  60 public final class SwipeEvent extends GestureEvent {
  61 
  62     private static final long serialVersionUID = 20121107L;
  63 
  64     /**
  65      * Common supertype for all swipe event types.
  66      */
  67     public static final EventType<SwipeEvent> ANY =
  68             new EventType<SwipeEvent>(GestureEvent.ANY, "ANY_SWIPE");
  69 
  70     /**
  71      * This event occurs when user performs leftward swipe gesture.
  72      */
  73     public static final EventType<SwipeEvent> SWIPE_LEFT =
  74             new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_LEFT");
  75 
  76     /**
  77      * This event occurs when user performs rightward swipe gesture.
  78      */
  79     public static final EventType<SwipeEvent> SWIPE_RIGHT =
  80             new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_RIGHT");
  81 
  82     /**
  83      * This event occurs when user performs upward swipe gesture.
  84      */
  85     public static final EventType<SwipeEvent> SWIPE_UP =
  86             new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_UP");
  87 
  88     /**
  89      * This event occurs when user performs downward swipe gesture.
  90      */
  91     public static final EventType<SwipeEvent> SWIPE_DOWN =
  92             new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_DOWN");
  93 
  94     /**
  95      * Constructs new SwipeEvent event.
  96      * @param source the source of the event. Can be null.
  97      * @param target the target of the event. Can be null.
  98      * @param eventType The type of the event.
  99      * @param x The x with respect to the scene.
 100      * @param y The y with respect to the scene.
 101      * @param screenX The x coordinate relative to screen.
 102      * @param screenY The y coordinate relative to screen.
 103      * @param shiftDown true if shift modifier was pressed.
 104      * @param controlDown true if control modifier was pressed.
 105      * @param altDown true if alt modifier was pressed.
 106      * @param metaDown true if meta modifier was pressed.
 107      * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
 108      * @param touchCount number of touch points
 109      * @param pickResult pick result. Can be null, in this case a 2D pick result
 110      *                   without any further values is constructed
 111      *                   based on the scene coordinates and the target
 112      * @since JavaFX 8.0
 113      */
 114     public SwipeEvent(@NamedArg("source") Object source, @NamedArg("target") EventTarget target,
 115             final @NamedArg("eventType") EventType<SwipeEvent> eventType,
 116             @NamedArg("x") double x, @NamedArg("y") double y,
 117             @NamedArg("screenX") double screenX, @NamedArg("screenY") double screenY,
 118             @NamedArg("shiftDown") boolean shiftDown,
 119             @NamedArg("controlDown") boolean controlDown,
 120             @NamedArg("altDown") boolean altDown,
 121             @NamedArg("metaDown") boolean metaDown,
 122             @NamedArg("direct") boolean direct,
 123             @NamedArg("touchCount") int touchCount,
 124             @NamedArg("pickResult") PickResult pickResult) {
 125 
 126         super(source, target, eventType, x, y, screenX, screenY,
 127                 shiftDown, controlDown, altDown, metaDown, direct, false,
 128                 pickResult);
 129         this.touchCount = touchCount;
 130     }
 131 
 132     /**
 133      * Constructs new SwipeEvent event with null source and target.
 134      * @param eventType The type of the event.
 135      * @param x The x with respect to the scene.
 136      * @param y The y with respect to the scene.
 137      * @param screenX The x coordinate relative to screen.
 138      * @param screenY The y coordinate relative to screen.
 139      * @param shiftDown true if shift modifier was pressed.
 140      * @param controlDown true if control modifier was pressed.
 141      * @param altDown true if alt modifier was pressed.
 142      * @param metaDown true if meta modifier was pressed.
 143      * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
 144      * @param touchCount number of touch points
 145      * @param pickResult pick result. Can be null, in this case a 2D pick result
 146      *                   without any further values is constructed
 147      *                   based on the scene coordinates
 148      * @since JavaFX 8.0
 149      */
 150     public SwipeEvent(final @NamedArg("eventType") EventType<SwipeEvent> eventType,
 151             @NamedArg("x") double x, @NamedArg("y") double y,
 152             @NamedArg("screenX") double screenX, @NamedArg("screenY") double screenY,
 153             @NamedArg("shiftDown") boolean shiftDown,
 154             @NamedArg("controlDown") boolean controlDown,
 155             @NamedArg("altDown") boolean altDown,
 156             @NamedArg("metaDown") boolean metaDown,
 157             @NamedArg("direct") boolean direct,
 158             @NamedArg("touchCount") int touchCount,
 159             @NamedArg("pickResult") PickResult pickResult) {
 160         this(null, null, eventType, x, y, screenX, screenY, shiftDown, controlDown,
 161                 altDown, metaDown, direct, touchCount, pickResult);
 162     }
 163 
 164     private final int touchCount;
 165 
 166     /**
 167      * Gets number of touch points that caused this event.
 168      * @return Number of touch points that caused this event
 169      */
 170     public int getTouchCount() {
 171         return touchCount;
 172     }
 173 
 174     /**
 175      * Returns a string representation of this {@code SwipeEvent} object.
 176      * @return a string representation of this {@code SwipeEvent} object.
 177      */
 178     @Override public String toString() {
 179         final StringBuilder sb = new StringBuilder("SwipeEvent [");
 180 
 181         sb.append("source = ").append(getSource());
 182         sb.append(", target = ").append(getTarget());
 183         sb.append(", eventType = ").append(getEventType());
 184         sb.append(", consumed = ").append(isConsumed());
 185         sb.append(", touchCount = ").append(getTouchCount());
 186 
 187         sb.append(", x = ").append(getX()).append(", y = ").append(getY())
 188                 .append(", z = ").append(getZ());
 189         sb.append(isDirect() ? ", direct" : ", indirect");
 190 
 191         if (isShiftDown()) {
 192             sb.append(", shiftDown");
 193         }
 194         if (isControlDown()) {
 195             sb.append(", controlDown");
 196         }
 197         if (isAltDown()) {
 198             sb.append(", altDown");
 199         }
 200         if (isMetaDown()) {
 201             sb.append(", metaDown");
 202         }
 203         if (isShortcutDown()) {
 204             sb.append(", shortcutDown");
 205         }
 206         sb.append(", pickResult = ").append(getPickResult());
 207 
 208         return sb.append("]").toString();
 209     }
 210 
 211     @Override
 212     public SwipeEvent copyFor(Object newSource, EventTarget newTarget) {
 213         return (SwipeEvent) super.copyFor(newSource, newTarget);
 214     }
 215 
 216     /**
 217      * Creates a copy of the given event with the given fields substituted.
 218      * @param source the new source of the copied event
 219      * @param target the new target of the copied event
 220      * @param eventType the new eventType
 221      * @return the event copy with the fields substituted
 222      * @since JavaFX 8.0
 223      */
 224     public SwipeEvent copyFor(Object newSource, EventTarget newTarget, EventType<SwipeEvent> type) {
 225         SwipeEvent e = copyFor(newSource, newTarget);
 226         e.eventType = type;
 227         return e;
 228     }
 229 
 230     @Override
 231     public EventType<SwipeEvent> getEventType() {
 232         return (EventType<SwipeEvent>) super.getEventType();
 233     }
 234 
 235 
 236 }