Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.input;
027
028import javafx.event.EventTarget;
029import javafx.event.EventType;
030
031/**
032 * Swipe event indicates that user performed a swipe gesture such as
033 * dragging a finger in one direction on touch screen.
034 * <p>
035 * Unlike some other gestures, the swipe gesture is not continual - the whole
036 * gesture produces only one event. The event is delivered to the top-most
037 * node picked on the gesture coordinates.
038 * <p>
039 * The swipe gesture has four types according to the movement direction.
040 * The gesture can be performed by any number of touch points, the number
041 * is provided by {@code getTouchCount()} method.
042 * <p>
043 * Note that swipe and scroll gestures are not exclusive. A single touch screen
044 * action can result in both gestures being delivered.
045 * <p>
046 * Note that the capability to produce swipes is dependent on the used input
047 * devices and underlying platform's capabilities and settings (especially
048 * without touch-screen user's possibilities of producing swipes are
049 * significantly reduced).
050 * <p>
051 * As all gestures, swipe can be direct (performed directly at
052 * the concrete coordinates as on touch screen - the center of the gesture
053 * is used as gesture coordinates) or indirect (performed
054 * indirectly as on track pad - the mouse cursor location is usually used
055 * as the gesture coordinates in this case).
056 *
057 * @since 2.2
058 */
059public final class SwipeEvent extends GestureEvent {
060
061    private static final long serialVersionUID = 20121107L;
062
063    /**
064     * Common supertype for all swipe event types.
065     */
066    public static final EventType<SwipeEvent> ANY =
067            new EventType<SwipeEvent>(GestureEvent.ANY, "ANY_SWIPE");
068
069    /**
070     * This event occurs when user performs leftward swipe gesture.
071     */
072    public static final EventType<SwipeEvent> SWIPE_LEFT =
073            new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_LEFT");
074
075    /**
076     * This event occurs when user performs rightward swipe gesture.
077     */
078    public static final EventType<SwipeEvent> SWIPE_RIGHT =
079            new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_RIGHT");
080
081    /**
082     * This event occurs when user performs upward swipe gesture.
083     */
084    public static final EventType<SwipeEvent> SWIPE_UP =
085            new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_UP");
086
087    /**
088     * This event occurs when user performs downward swipe gesture.
089     */
090    public static final EventType<SwipeEvent> SWIPE_DOWN =
091            new EventType<SwipeEvent>(SwipeEvent.ANY, "SWIPE_DOWN");
092
093    /**
094     * Constructs new SwipeEvent event.
095     * @param source the source of the event. Can be null.
096     * @param target the target of the event. Can be null.
097     * @param eventType The type of the event.
098     * @param x The x with respect to the scene.
099     * @param y The y with respect to the scene.
100     * @param screenX The x coordinate relative to screen.
101     * @param screenY The y coordinate relative to screen.
102     * @param shiftDown true if shift modifier was pressed.
103     * @param controlDown true if control modifier was pressed.
104     * @param altDown true if alt modifier was pressed.
105     * @param metaDown true if meta modifier was pressed.
106     * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
107     * @param touchCount number of touch points
108     * @param pickResult pick result. Can be null, in this case a 2D pick result
109     *                   without any further values is constructed
110     *                   based on the scene coordinates and the target
111     */
112    public SwipeEvent(Object source, EventTarget target,
113            final EventType<SwipeEvent> eventType,
114            double x, double y,
115            double screenX, double screenY,
116            boolean shiftDown,
117            boolean controlDown,
118            boolean altDown,
119            boolean metaDown,
120            boolean direct,
121            int touchCount,
122            PickResult pickResult) {
123
124        super(source, target, eventType, x, y, screenX, screenY,
125                shiftDown, controlDown, altDown, metaDown, direct, false,
126                pickResult);
127        this.touchCount = touchCount;
128    }
129
130    /**
131     * Constructs new SwipeEvent event with null source and target.
132     * @param eventType The type of the event.
133     * @param x The x with respect to the scene.
134     * @param y The y with respect to the scene.
135     * @param screenX The x coordinate relative to screen.
136     * @param screenY The y coordinate relative to screen.
137     * @param shiftDown true if shift modifier was pressed.
138     * @param controlDown true if control modifier was pressed.
139     * @param altDown true if alt modifier was pressed.
140     * @param metaDown true if meta modifier was pressed.
141     * @param direct true if the event was caused by direct input device. See {@link #isDirect() }
142     * @param touchCount number of touch points
143     * @param pickResult pick result. Can be null, in this case a 2D pick result
144     *                   without any further values is constructed
145     *                   based on the scene coordinates
146     */
147    public SwipeEvent(final EventType<SwipeEvent> eventType,
148            double x, double y,
149            double screenX, double screenY,
150            boolean shiftDown,
151            boolean controlDown,
152            boolean altDown,
153            boolean metaDown,
154            boolean direct,
155            int touchCount,
156            PickResult pickResult) {
157        this(null, null, eventType, x, y, screenX, screenY, shiftDown, controlDown,
158                altDown, metaDown, direct, touchCount, pickResult);
159    }
160
161    private final int touchCount;
162
163    /**
164     * Gets number of touch points that caused this event.
165     * @return Number of touch points that caused this event
166     */
167    public int getTouchCount() {
168        return touchCount;
169    }
170
171    /**
172     * Returns a string representation of this {@code SwipeEvent} object.
173     * @return a string representation of this {@code SwipeEvent} object.
174     */
175    @Override public String toString() {
176        final StringBuilder sb = new StringBuilder("SwipeEvent [");
177
178        sb.append("source = ").append(getSource());
179        sb.append(", target = ").append(getTarget());
180        sb.append(", eventType = ").append(getEventType());
181        sb.append(", consumed = ").append(isConsumed());
182        sb.append(", touchCount = ").append(getTouchCount());
183
184        sb.append(", x = ").append(getX()).append(", y = ").append(getY())
185                .append(", z = ").append(getZ());
186        sb.append(isDirect() ? ", direct" : ", indirect");
187
188        if (isShiftDown()) {
189            sb.append(", shiftDown");
190        }
191        if (isControlDown()) {
192            sb.append(", controlDown");
193        }
194        if (isAltDown()) {
195            sb.append(", altDown");
196        }
197        if (isMetaDown()) {
198            sb.append(", metaDown");
199        }
200        if (isShortcutDown()) {
201            sb.append(", shortcutDown");
202        }
203        sb.append(", pickResult = ").append(getPickResult());
204
205        return sb.append("]").toString();
206    }
207
208    @Override
209    public SwipeEvent copyFor(Object newSource, EventTarget newTarget) {
210        return (SwipeEvent) super.copyFor(newSource, newTarget);
211    }
212    
213    /**
214     * Creates a copy of the given event with the given fields substituted.
215     * @param source the new source of the copied event
216     * @param target the new target of the copied event
217     * @param eventType the new eventType
218     * @return the event copy with the fields substituted
219     */
220    public SwipeEvent copyFor(Object newSource, EventTarget newTarget, EventType<SwipeEvent> type) {
221        SwipeEvent e = copyFor(newSource, newTarget);
222        e.eventType = type;
223        return e;
224    }
225
226    @Override
227    public EventType<SwipeEvent> getEventType() {
228        return (EventType<SwipeEvent>) super.getEventType();
229    }
230    
231    
232}