Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 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 com.sun.javafx.scene.input.InputEventUtils;
029import java.io.IOException;
030import javafx.event.EventTarget;
031import javafx.event.EventType;
032import javafx.geometry.Point3D;
033import javafx.scene.Node;
034
035// PENDING_DOC_REVIEW
036/**
037 * When the user requests a context menu, this event occurs.  Context
038 * menus can be triggered by the mouse or the keyboard.  The exact
039 * sequence of mouse or keyboard events that is used to request a
040 * menu is platform specific.  For example, on Windows, Shift+F10
041 * requests a context menu.
042 * <p>
043 * The event coordinates contain default position for the context menu.
044 * For mouse-triggered events it is the position of the
045 * mouse cursor, for keyboard-triggered events it is a point
046 * inside of bounds of current focus owner (which is the event's target).
047 */
048public class ContextMenuEvent extends InputEvent {
049
050    private static final long serialVersionUID = 20121107L;
051
052    /**
053     * This event occurs when a context menu is requested.
054     */
055    public static final EventType<ContextMenuEvent> CONTEXT_MENU_REQUESTED =
056            new EventType<ContextMenuEvent>(InputEvent.ANY, "CONTEXTMENUREQUESTED");
057
058    /**
059     * Common supertype for all context menu event types.
060     */
061    public static final EventType<ContextMenuEvent> ANY = CONTEXT_MENU_REQUESTED;
062
063    /**
064     * Constructs new ContextMenu event.
065     * @param source the source of the event. Can be null.
066     * @param target the target of the event. Can be null.
067     * @param eventType The type of the event.
068     * @param x The x with respect to the scene
069     * @param y The y with respect to the scene
070     * @param screenX The x coordinate relative to screen.
071     * @param screenY The y coordinate relative to screen.
072     * @param keyboardTrigger true if this event was triggered by keyboard.
073     * @param pickResult pick result. Can be null, in this case a 2D pick result
074     *                   without any further values is constructed
075     *                   based on the scene coordinates and the target
076     */
077    public ContextMenuEvent(Object source, EventTarget target, EventType<ContextMenuEvent> eventType, double x, double y,
078            double screenX, double screenY, boolean keyboardTrigger,
079            PickResult pickResult) {
080        super(source, target, eventType);
081        this.screenX = screenX;
082        this.screenY = screenY;
083        this.sceneX = x;
084        this.sceneY = y;
085        this.x = x;
086        this.y = y;
087        this.pickResult = pickResult != null ? pickResult : new PickResult(target, x, y);
088        final Point3D p = InputEventUtils.recomputeCoordinates(this.pickResult, null);
089        this.x = p.getX();
090        this.y = p.getY();
091        this.z = p.getZ();
092        this.keyboardTrigger = keyboardTrigger;
093     }
094
095    /**
096     * Constructs new ContextMenu event with empty source and target.
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 keyboardTrigger true if this event was triggered by keyboard.
103     * @param pickResult pick result. Can be null, in this case a 2D pick result
104     *                   without any further values is constructed
105     *                   based on the scene coordinates
106     */
107    public ContextMenuEvent(EventType<ContextMenuEvent> eventType, double x, double y,
108            double screenX, double screenY, boolean keyboardTrigger,
109            PickResult pickResult) {
110        this(null, null, eventType, x, y, screenX, screenY, keyboardTrigger,
111                pickResult);
112    }
113
114    /**
115     * Fills the given event by this event's coordinates recomputed to the given
116     * source object
117     * @param newEvent Event whose coordinates are to be filled
118     * @param newSource Source object to compute coordinates for
119     */
120    private void recomputeCoordinatesToSource(ContextMenuEvent newEvent, Object newSource) {
121
122        final Point3D newCoordinates = InputEventUtils.recomputeCoordinates(
123                pickResult, newSource);
124
125        newEvent.x = newCoordinates.getX();
126        newEvent.y = newCoordinates.getY();
127        newEvent.z = newCoordinates.getZ();
128    }
129
130    @Override
131    public ContextMenuEvent copyFor(Object newSource, EventTarget newTarget) {
132        ContextMenuEvent e = (ContextMenuEvent) super.copyFor(newSource, newTarget);
133        recomputeCoordinatesToSource(e, newSource);
134        return e;
135    }
136
137    @Override
138    public EventType<ContextMenuEvent> getEventType() {
139        return (EventType<ContextMenuEvent>) super.getEventType();
140    }
141    
142    /**
143     * The boolean that indicates the event was triggered by a keyboard gesture.
144     */
145    private final boolean keyboardTrigger;
146    
147    /**
148     * Determines whether this event originated from the keyboard.
149     *
150     * @return true if the event was caused by the keyboard
151     */
152    public boolean isKeyboardTrigger() {
153        return keyboardTrigger;
154    }
155
156    /**
157     * Horizontal x position of the event relative to the
158     * origin of the ContextMenuEvent's node.
159     */
160    private transient double x;
161
162    /**
163     * Horizontal position of the event relative to the
164     * origin of the ContextMenuEvent's source.
165     * For more information about this event's coordinate semantics please see
166     * the general description of {@link ContextMenuEvent}.
167     *
168     * @return horizontal position of the event relative to the
169     * origin of the ContextMenuEvent's source.
170     */
171    public final double getX() {
172        return x;
173    }
174
175    /**
176     * Vertical y position of the event relative to the
177     * origin of the ContextMenuEvent's node.
178     */
179    private transient double y;
180
181    /**
182     * Vertical position of the event relative to the
183     * origin of the ContextMenuEvent's source.
184     * For more information about this event's coordinate semantics please see
185     * the general description of {@link ContextMenuEvent}.
186     *
187     * @return vertical position of the event relative to the
188     * origin of the ContextMenuEvent's source.
189     */
190    public final double getY() {
191        return y;
192    }
193
194    /**
195     * Depth z position of the event relative to the
196     * origin of the MouseEvent's node.
197     */
198    private transient double z;
199
200    /**
201     * Depth position of the event relative to the
202     * origin of the MouseEvent's source.
203     *
204     * @return depth position of the event relative to the
205     * origin of the MouseEvent's source.
206     */
207    public final double getZ() {
208        return z;
209    }
210
211    /**
212     * Absolute horizontal x position of the event.
213     */
214    private final double screenX;
215
216    /**
217     * Returns absolute horizontal position of the event.
218     * For more information about this event's coordinate semantics please see
219     * the general description of {@link ContextMenuEvent}.
220     * @return absolute horizontal position of the event
221     */
222    public final double getScreenX() {
223        return screenX;
224    }
225
226    /**
227     * Absolute vertical y position of the event.
228     */
229    private final double screenY;
230
231    /**
232     * Returns absolute vertical position of the event.
233     * For more information about this event's coordinate semantics please see
234     * the general description of {@link ContextMenuEvent}.
235     * @return absolute vertical position of the event
236     */
237    public final double getScreenY() {
238        return screenY;
239    }
240
241    /**
242     * Horizontal x position of the event relative to the
243     * origin of the {@code Scene} that contains the ContextMenuEvent's node.
244     * If the node is not in a {@code Scene}, then the value is relative to
245     * the boundsInParent of the root-most parent of the ContextMenuEvent's node.
246     */
247    private final double sceneX;
248
249    /**
250     * Returns horizontal position of the event relative to the
251     * origin of the {@code Scene} that contains the ContextMenuEvent's source.
252     * If the node is not in a {@code Scene}, then the value is relative to
253     * the boundsInParent of the root-most parent of the ContextMenuEvent's node.
254     * For more information about this event's coordinate semantics please see
255     * the general description of {@link ContextMenuEvent}.
256     * Note that in 3D scene, this represents the flat coordinates after
257     * applying the projection transformations.
258     *
259     * @return horizontal position of the event relative to the
260     * origin of the {@code Scene} that contains the ContextMenuEvent's source
261     */
262    public final double getSceneX() {
263        return sceneX;
264    }
265
266    /**
267     * Vertical y position of the event relative to the
268     * origin of the {@code Scene} that contains the ContextMenuEvent's node.
269     * If the node is not in a {@code Scene}, then the value is relative to
270     * the boundsInParent of the root-most parent of the ContextMenuEvent's node.
271     */
272    private final double sceneY;
273
274    /**
275     * Returns vertical position of the event relative to the
276     * origin of the {@code Scene} that contains the ContextMenuEvent's source.
277     * If the node is not in a {@code Scene}, then the value is relative to
278     * the boundsInParent of the root-most parent of the ContextMenuEvent's node.
279     * For more information about this event's coordinate semantics please see
280     * the general description of {@link ContextMenuEvent}.
281     * Note that in 3D scene, this represents the flat coordinates after
282     * applying the projection transformations.
283     *
284     * @return vertical position of the event relative to the
285     * origin of the {@code Scene} that contains the ContextMenuEvent's source
286     */
287    public final double getSceneY() {
288        return sceneY;
289    }
290
291    /**
292     * Information about the pick if the picked {@code Node} is a
293     * {@code Shape3D} node and its pickOnBounds is false.
294     */
295    private PickResult pickResult;
296
297    /**
298     * Returns information about the pick.
299     *
300     * @return new PickResult object that contains information about the pick
301     */
302    public final PickResult getPickResult() {
303        return pickResult;
304    }
305
306    /**
307     * Returns a string representation of this {@code ContextMenuEvent} object.
308     * @return a string representation of this {@code ContextMenuEvent} object.
309     */
310    @Override public String toString() {
311        final StringBuilder sb = new StringBuilder("ContextMenuEvent [");
312
313        sb.append("source = ").append(getSource());
314        sb.append(", target = ").append(getTarget());
315        sb.append(", eventType = ").append(getEventType());
316        sb.append(", consumed = ").append(isConsumed());
317
318        sb.append(", x = ").append(getX()).append(", y = ").append(getY())
319                .append(", z = ").append(getZ());
320        sb.append(", pickResult = ").append(getPickResult());
321
322        return sb.append("]").toString();
323    }
324
325    private void readObject(java.io.ObjectInputStream in)
326            throws IOException, ClassNotFoundException {
327        in.defaultReadObject();
328        x = sceneX;
329        y = sceneY;
330    }
331}