4491af471339e3b1680dc70d568a89ab13ba216a
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / Jxclick.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Bruno Jofret
4  *
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
10  *
11  */
12 package org.scilab.modules.gui.events;
13
14 import java.awt.AWTEvent;
15 import java.awt.event.KeyEvent;
16 import java.awt.event.MouseEvent;
17
18 import org.scilab.modules.gui.bridge.canvas.SwingScilabCanvas;
19 import org.scilab.modules.gui.utils.SciTranslator;
20
21
22 /**
23  * This class implements the xclick Scilab method
24  * by updating the ClickInfos attributes.
25  * @author Bruno Jofret
26  */
27 public final class Jxclick {
28
29     private static int keyChar;
30
31     /**
32      * PRIVATE constructor
33      * We do not want a Jxclick object.
34      */
35     private Jxclick() { }
36
37     /**
38      * Scilab call point for xclick.
39      */
40     public static void xclick() {
41         /*
42          *  Enable the local keyActionFilter function
43          *  to be called when a keyEvent is caught.
44          */
45         GlobalEventWatcher.enable(new GlobalKeyEventWatcher() {
46             public void keyEventFilter(KeyEvent keyEvent) {
47                 keyActionFilter(keyEvent);
48             }
49         });
50         /*
51          *  Enable the local mouseActionFilter function
52          *  to be called when a mouseEvent is caught.
53          */
54         GlobalEventWatcher.enable(new GlobalMouseEventWatcher(AWTEvent.MOUSE_EVENT_MASK) {
55             public void mouseEventFilter(MouseEvent mouseEvent,
56             String axesUID, int scilabMouseAction, boolean isControlDown) {
57                 mouseActionFilter(mouseEvent, axesUID, scilabMouseAction, isControlDown);
58             }
59         });
60         synchronized (ClickInfos.getInstance()) {
61             try {
62                 ClickInfos.getInstance().init();
63                 ClickInfos.getInstance().wait();
64             } catch (InterruptedException e) {
65                 e.printStackTrace();
66             }
67         }
68         GlobalEventWatcher.disable();
69     }
70
71     /**
72      * @return the mouseButtonNumber
73      */
74     public static int getMouseButtonNumber() {
75         return ClickInfos.getInstance().getMouseButtonNumber();
76     }
77     /**
78      * @return the xCoordinate
79      */
80     public static double getXCoordinate() {
81         return ClickInfos.getInstance().getXCoordinate();
82     }
83     /**
84      * @return the yCoordinate
85      */
86     public static double getYCoordinate() {
87         return ClickInfos.getInstance().getYCoordinate();
88     }
89     /**
90      * @return the windowID
91      */
92     public static String getWindowID() {
93         return ClickInfos.getInstance().getWindowID();
94     }
95     /**
96      * @return the menuCallback
97      */
98     public static String getMenuCallback() {
99         return ClickInfos.getInstance().getMenuCallback();
100     }
101
102     /**
103      * Manage xclick behaviour for keyboard entry.
104      * 1 - Key has been pressed.
105      * 2 - CTRL + Key has been pressed.
106      *
107      * @param keyEvent : the Key Event caught
108      */
109     private static void keyActionFilter(KeyEvent keyEvent) {
110         if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
111             if (!Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
112                 keyChar = keyEvent.getKeyChar();
113             } else {
114                 if (keyEvent.isShiftDown()) {
115                     keyChar = keyEvent.getKeyCode();
116                 } else {
117                     keyChar = Character.toLowerCase(keyEvent.getKeyCode());
118                 }
119             }
120         } else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
121             if (keyEvent.getSource() != null
122                         && keyEvent.getSource() instanceof SwingScilabCanvas) {
123                 if (GlobalEventWatcher.isActivated()) {
124                     GlobalEventFilter.filterKey(keyChar, GlobalEventWatcher.getAxesUID(), keyEvent.isControlDown());
125                 }
126             }
127         }
128     }
129     /**
130      * Manage xclick behaviour for mouse entry.
131      *
132      * @param mouseEvent : the Mouse Event caught
133      * @param scilabMouseAction : the integer scilab code for mouse action.
134      * @param axes : the axes where action occurs.
135      * @param isControlDown true if the CTRL key has been pressed
136      */
137     private static void mouseActionFilter(MouseEvent mouseEvent, String axesUID, int scilabMouseAction, boolean isControlDown) {
138         if (scilabMouseAction == SciTranslator.PRESSED
139                 || scilabMouseAction == SciTranslator.CLICKED
140                 || scilabMouseAction == SciTranslator.DCLICKED) {
141             GlobalEventFilter.filterMouse(mouseEvent, axesUID, scilabMouseAction, isControlDown);
142         }
143     }
144 }