2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA - Bruno Jofret
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 package org.scilab.modules.gui.events;
15 import java.awt.AWTEvent;
16 import java.awt.event.KeyEvent;
17 import java.awt.event.MouseEvent;
19 import org.scilab.modules.gui.bridge.canvas.SwingScilabCanvas;
20 import org.scilab.modules.gui.utils.SciTranslator;
24 * This class implements the xgetmouse Scilab method
25 * by updating the ClickInfos attributes
26 * @author Bruno Jofret
28 public final class Jxgetmouse {
30 private static int keyChar;
31 private static boolean isControlDown;
32 private static boolean watchMotion;
33 private static boolean watchRelease;
34 private static final int MOVED = -1;
37 * Tool Class, constructor is private.
39 private Jxgetmouse() { }
42 * Default call for xgetmouse.
44 public static void xgetmouse() {
45 xgetmouse(true, false);
49 * Scilab call point for xgetmouse.
51 * @param withMotion : Means xgetmouse is mouse motion sensitive.
52 * @param withRelease : Means xgetmouse is release sensitive.
54 public static void xgetmouse(boolean withMotion, boolean withRelease) {
55 watchMotion = withMotion;
56 watchRelease = withRelease;
58 * Enable the local keyActionFilter function
59 * to be called when a keyEvent is caught.
61 GlobalEventWatcher.enable(new GlobalKeyEventWatcher() {
62 public void keyEventFilter(KeyEvent keyEvent) {
63 keyActionFilter(keyEvent);
66 long eventMask = AWTEvent.MOUSE_EVENT_MASK;
68 eventMask += AWTEvent.MOUSE_MOTION_EVENT_MASK;
71 * Enable the local mouseActionFilter function
72 * to be called when a mouseEvent is caught.
74 GlobalEventWatcher.enable(new GlobalMouseEventWatcher(eventMask) {
75 public void mouseEventFilter(MouseEvent mouseEvent,
76 Integer axesUID, int scilabMouseAction, boolean isControlDown) {
77 mouseActionFilter(mouseEvent, axesUID, scilabMouseAction, isControlDown);
82 * Force xgetmouse not to catch/disable callback execution.
84 GlobalEventWatcher.disableCatchingCallback();
86 synchronized (ClickInfos.getInstance()) {
88 ClickInfos.getInstance().init();
89 ClickInfos.getInstance().wait();
90 } catch (InterruptedException e) {
94 GlobalEventWatcher.disable();
98 * @return the mouseButtonNumber
100 public static int getMouseButtonNumber() {
101 return ClickInfos.getInstance().getMouseButtonNumber();
104 * @return the xCoordinate
106 public static double getXCoordinate() {
107 return ClickInfos.getInstance().getXCoordinate();
110 * @return the yCoordinate
112 public static double getYCoordinate() {
113 return ClickInfos.getInstance().getYCoordinate();
116 * @return the WindowsID
118 public static Integer getWindowsID() {
119 return ClickInfos.getInstance().getWindowID();
125 * Manage xgetmouse behaviour for keyboard entry.
126 * 1 - Key has been pressed.
127 * 2 - CTRL + Key has been pressed.
128 * 3 - Key has been released
130 * @param keyEvent : the Key Event caught
132 private static void keyActionFilter(KeyEvent keyEvent) {
134 * If a key is pressed, only remember the key and if CTRL is pressed either.
136 if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
137 if (!Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
138 keyChar = keyEvent.getKeyChar();
140 if (keyEvent.isShiftDown()) {
141 keyChar = keyEvent.getKeyCode();
143 keyChar = Character.toLowerCase(keyEvent.getKeyCode());
146 isControlDown = keyEvent.isControlDown();
147 } else if (keyEvent.getSource() != null
148 && keyEvent.getSource() instanceof SwingScilabCanvas) {
149 /* Now we have have to be sure we are in a Canvas. */
151 * If a RELEASED is seen use -keyChar
153 if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
154 GlobalEventFilter.filterKey(-keyChar, GlobalEventWatcher.getAxesUID(), isControlDown);
155 } else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
157 * Or If a TYPED is seen use keyChar
159 GlobalEventFilter.filterKey(keyChar, GlobalEventWatcher.getAxesUID(), isControlDown);
164 * Manage xgetmouse behaviour for mouse entry.
166 * @param mouseEvent : the Mouse Event caught
167 * @param scilabMouseAction : the integer scilab code for mouse action.
168 * @param axes : the axes where action occurs.
169 * @param isControlDown true if the CTRL key has been pressed
171 private static void mouseActionFilter(MouseEvent mouseEvent, Integer axesUID, int scilabMouseAction, boolean isControlDown) {
172 if (scilabMouseAction != SciTranslator.MOVED
173 && scilabMouseAction != SciTranslator.RELEASED) {
174 GlobalEventFilter.filterMouse(mouseEvent, axesUID, scilabMouseAction, isControlDown);
175 } else if (watchMotion && scilabMouseAction == SciTranslator.MOVED) {
176 // Force false value to isControlDown
177 // MOVED do not care about CTRL Key...
178 GlobalEventFilter.filterMouse(mouseEvent, axesUID, MOVED, false);
179 } else if (watchRelease && scilabMouseAction == SciTranslator.RELEASED) {
180 // Force false value to isControlDown
181 // RELEASED do not care about CTRL Key...
182 GlobalEventFilter.filterMouse(mouseEvent, axesUID, scilabMouseAction, false);