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-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.tab.SwingScilabAxes;
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)
76 public void mouseEventFilter(MouseEvent mouseEvent,
77 SwingScilabAxes axes, int scilabMouseAction, boolean isControlDown) {
78 mouseActionFilter(mouseEvent, axes, scilabMouseAction, isControlDown);
81 synchronized (ClickInfos.getInstance()) {
83 ClickInfos.getInstance().init();
84 ClickInfos.getInstance().wait();
85 } catch (InterruptedException e) {
89 GlobalEventWatcher.disable();
93 * @return the mouseButtonNumber
95 public static int getMouseButtonNumber() {
96 return ClickInfos.getInstance().getMouseButtonNumber();
99 * @return the xCoordinate
101 public static double getXCoordinate() {
102 return ClickInfos.getInstance().getXCoordinate();
105 * @return the yCoordinate
107 public static double getYCoordinate() {
108 return ClickInfos.getInstance().getYCoordinate();
111 * @return the WindowsID
113 public static int getWindowsID() {
114 return ClickInfos.getInstance().getWindowID();
120 * Manage xgetmouse behaviour for keyboard entry.
121 * 1 - Key has been pressed.
122 * 2 - CTRL + Key has been pressed.
123 * 3 - Key has been released
125 * @param keyEvent : the Key Event caught
127 private static void keyActionFilter(KeyEvent keyEvent) {
129 * If a key is pressed, only remember the key and if CTRL is pressed either.
131 if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
132 if (!Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
133 keyChar = keyEvent.getKeyChar();
135 if (keyEvent.isShiftDown()) {
136 keyChar = keyEvent.getKeyCode();
138 keyChar = Character.toLowerCase(keyEvent.getKeyCode());
141 isControlDown = ((KeyEvent) keyEvent).isControlDown();
142 } else if (keyEvent.getSource() instanceof SwingScilabAxes) {
143 /* Now we have have to be sure we are in a Canvas. */
145 * If a RELEASED is seen use -keyChar
147 if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
148 GlobalEventFilter.filterKey(-keyChar, ((SwingScilabAxes) keyEvent.getSource()).getFigureId(), isControlDown);
149 } else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
151 * Or If a TYPED is seen use keyChar
153 GlobalEventFilter.filterKey(keyChar, ((SwingScilabAxes) keyEvent.getSource()).getFigureId(), isControlDown);
158 * Manage xgetmouse behaviour for mouse entry.
160 * @param mouseEvent : the Mouse Event caught
161 * @param scilabMouseAction : the integer scilab code for mouse action.
162 * @param axes : the axes where action occurs.
163 * @param isControlDown true if the CTRL key has been pressed
165 private static void mouseActionFilter(MouseEvent mouseEvent, SwingScilabAxes axes, int scilabMouseAction, boolean isControlDown) {
166 if (scilabMouseAction != SciTranslator.MOVED
167 && scilabMouseAction != SciTranslator.RELEASED) {
168 GlobalEventFilter.filterMouse(mouseEvent, axes, scilabMouseAction, isControlDown);
169 } else if (watchMotion && scilabMouseAction == SciTranslator.MOVED) {
170 // Force false value to isControlDown
171 // MOVED do not care about CTRL Key...
172 GlobalEventFilter.filterMouse(mouseEvent, axes, MOVED, false);
173 } else if (watchRelease && scilabMouseAction == SciTranslator.RELEASED) {
174 // Force false value to isControlDown
175 // RELEASED do not care about CTRL Key...
176 GlobalEventFilter.filterMouse(mouseEvent, axes, scilabMouseAction, false);