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
12 package org.scilab.modules.gui.events;
14 import java.awt.AWTEvent;
15 import java.awt.event.KeyEvent;
16 import java.awt.event.MouseEvent;
18 import org.scilab.modules.gui.bridge.canvas.SwingScilabCanvas;
19 import org.scilab.modules.gui.utils.SciTranslator;
23 * This class implements the xclick Scilab method
24 * by updating the ClickInfos attributes.
25 * @author Bruno Jofret
27 public final class Jxclick {
29 private static int keyChar;
33 * We do not want a Jxclick object.
38 * Scilab call point for xclick.
40 public static void xclick() {
42 * Enable the local keyActionFilter function
43 * to be called when a keyEvent is caught.
45 GlobalEventWatcher.enable(new GlobalKeyEventWatcher() {
46 public void keyEventFilter(KeyEvent keyEvent) {
47 keyActionFilter(keyEvent);
51 * Enable the local mouseActionFilter function
52 * to be called when a mouseEvent is caught.
54 GlobalEventWatcher.enable(new GlobalMouseEventWatcher(AWTEvent.MOUSE_EVENT_MASK) {
55 public void mouseEventFilter(MouseEvent mouseEvent,
56 Integer axesUID, int scilabMouseAction, boolean isControlDown) {
57 mouseActionFilter(mouseEvent, axesUID, scilabMouseAction, isControlDown);
62 * Force xclick not to catch/disable callback execution.
64 GlobalEventWatcher.enableCatchingCallback();
66 synchronized (ClickInfos.getInstance()) {
68 ClickInfos.getInstance().init();
69 ClickInfos.getInstance().wait();
70 } catch (InterruptedException e) {
74 GlobalEventWatcher.disable();
78 * @return the mouseButtonNumber
80 public static int getMouseButtonNumber() {
81 return ClickInfos.getInstance().getMouseButtonNumber();
84 * @return the xCoordinate
86 public static double getXCoordinate() {
87 return ClickInfos.getInstance().getXCoordinate();
90 * @return the yCoordinate
92 public static double getYCoordinate() {
93 return ClickInfos.getInstance().getYCoordinate();
96 * @return the windowID
98 public static int getWindowID() {
99 return ClickInfos.getInstance().getWindowID();
102 * @return the menuCallback
104 public static String getMenuCallback() {
105 return ClickInfos.getInstance().getMenuCallback();
109 * Manage xclick behaviour for keyboard entry.
110 * 1 - Key has been pressed.
111 * 2 - CTRL + Key has been pressed.
113 * @param keyEvent : the Key Event caught
115 private static void keyActionFilter(KeyEvent keyEvent) {
116 if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
117 if (!Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
118 keyChar = keyEvent.getKeyChar();
120 if (keyEvent.isShiftDown()) {
121 keyChar = keyEvent.getKeyCode();
123 keyChar = Character.toLowerCase(keyEvent.getKeyCode());
126 } else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
127 if (keyEvent.getSource() != null
128 && keyEvent.getSource() instanceof SwingScilabCanvas) {
129 if (GlobalEventWatcher.isActivated()) {
130 GlobalEventFilter.filterKey(keyChar, GlobalEventWatcher.getAxesUID(), keyEvent.isControlDown());
136 * Manage xclick behaviour for mouse entry.
138 * @param mouseEvent : the Mouse Event caught
139 * @param scilabMouseAction : the integer scilab code for mouse action.
140 * @param axes : the axes where action occurs.
141 * @param isControlDown true if the CTRL key has been pressed
143 private static void mouseActionFilter(MouseEvent mouseEvent, Integer axesUID, int scilabMouseAction, boolean isControlDown) {
144 if (scilabMouseAction == SciTranslator.PRESSED
145 || scilabMouseAction == SciTranslator.CLICKED
146 || scilabMouseAction == SciTranslator.DCLICKED) {
147 GlobalEventFilter.filterMouse(mouseEvent, axesUID, scilabMouseAction, isControlDown);