e3370ef75cbf77436c92c54d8024e8a77a225880
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / Jxgetmouse.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
13 package org.scilab.modules.gui.events;
14
15 import java.awt.AWTEvent;
16 import java.awt.event.KeyEvent;
17 import java.awt.event.MouseEvent;
18
19 import org.scilab.modules.gui.bridge.tab.SwingScilabAxes;
20 import org.scilab.modules.gui.utils.SciTranslator;
21
22
23 /**
24  * This class implements the xgetmouse Scilab method
25  * by updating the ClickInfos attributes
26  * @author Bruno Jofret
27  */
28 public final class Jxgetmouse {
29
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;
35
36         /**
37          * Tool Class, constructor is private.
38          */
39         private Jxgetmouse() { }
40
41         /**
42          * Default call for xgetmouse.
43          */
44         public static void xgetmouse() {
45                 xgetmouse(true, false);
46         }
47
48         /**
49          * Scilab call point for xgetmouse.
50          *
51          * @param withMotion : Means xgetmouse is mouse motion sensitive.
52          * @param withRelease : Means xgetmouse is release sensitive.
53          */
54         public static void xgetmouse(boolean withMotion, boolean withRelease) {
55                 watchMotion = withMotion;
56                 watchRelease = withRelease;
57                 /*
58                  *  Enable the local keyActionFilter function
59                  *  to be called when a keyEvent is caught.
60                  */             
61                 GlobalEventWatcher.enable(new GlobalKeyEventWatcher() {
62                         public void keyEventFilter(KeyEvent keyEvent) {
63                                 keyActionFilter(keyEvent);
64                         }
65                 });
66                 long eventMask = AWTEvent.MOUSE_EVENT_MASK;
67                 if (watchMotion) {
68                         eventMask += AWTEvent.MOUSE_MOTION_EVENT_MASK;
69                 }
70                 /*
71                  *  Enable the local mouseActionFilter function
72                  *  to be called when a mouseEvent is caught.
73                  */             
74                 GlobalEventWatcher.enable(new GlobalMouseEventWatcher(eventMask)
75                 {
76                         public void mouseEventFilter(MouseEvent mouseEvent,
77                                         SwingScilabAxes axes, int scilabMouseAction, boolean isControlDown) {
78                                 mouseActionFilter(mouseEvent, axes, scilabMouseAction, isControlDown);  
79                         }
80                 });
81                 synchronized (ClickInfos.getInstance()) {
82                         try {
83                                 ClickInfos.getInstance().init();
84                                 ClickInfos.getInstance().wait();
85                         } catch (InterruptedException e) {
86                                 // TODO Auto-generated catch block
87                                 e.printStackTrace();
88                         }
89                 }
90                 GlobalEventWatcher.disable();
91         }
92
93         /**
94          * @return the mouseButtonNumber
95          */
96         public static int getMouseButtonNumber() {
97                 return ClickInfos.getInstance().getMouseButtonNumber();
98         }
99         /**
100          * @return the xCoordinate
101          */
102         public static double getXCoordinate() {
103                 return ClickInfos.getInstance().getXCoordinate();
104         }
105         /**
106          * @return the yCoordinate
107          */
108         public static double getYCoordinate() {
109                 return ClickInfos.getInstance().getYCoordinate();
110         }
111         /**
112          * @return the WindowsID
113          */
114         public static int getWindowsID() {
115                 return ClickInfos.getInstance().getWindowID();
116         }
117         
118         
119         
120         /**
121          * Manage xgetmouse behaviour for keyboard entry.
122          * 1 - Key has been pressed.
123          * 2 - CTRL + Key has been pressed.
124          * 3 - Key has been released
125          * 
126          * @param keyEvent : the Key Event caught
127          */
128         private static void keyActionFilter(KeyEvent keyEvent) {        
129                 /*
130                  * If a key is pressed, only remember the key and if CTRL is pressed either.
131                  */
132                 if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
133                         if (!Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
134                                 keyChar = keyEvent.getKeyChar(); 
135                         } else {
136                                 if (keyEvent.isShiftDown()) {
137                                         keyChar = keyEvent.getKeyCode();
138                                 } else {
139                                         keyChar = Character.toLowerCase(keyEvent.getKeyCode());
140                                 }
141                         }
142                         isControlDown = ((KeyEvent) keyEvent).isControlDown();
143                 } else if (keyEvent.getSource() instanceof SwingScilabAxes) {
144                         /* Now we have have to be sure we are in a Canvas. */
145                         /*
146                          * If a RELEASED is seen use -keyChar
147                          */
148                         if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
149                                 GlobalEventFilter.filterKey(-keyChar, ((SwingScilabAxes) keyEvent.getSource()).getFigureId(), isControlDown);
150                         } else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
151                                 /*
152                                  * Or If a TYPED is seen use keyChar
153                                  */
154                                 GlobalEventFilter.filterKey(keyChar, ((SwingScilabAxes) keyEvent.getSource()).getFigureId(), isControlDown);
155                         }       
156                 }
157         }
158         /**
159          * Manage xgetmouse behaviour for mouse entry.
160          * 
161          * @param mouseEvent : the Mouse Event caught
162          * @param scilabMouseAction : the integer scilab code for mouse action.
163          * @param axes : the axes where action occurs.
164          * @param isControlDown true if the CTRL key has been pressed
165          */
166         private static void mouseActionFilter(MouseEvent mouseEvent, SwingScilabAxes axes, int scilabMouseAction, boolean isControlDown) {      
167                 if (scilabMouseAction != SciTranslator.MOVED
168                                 && scilabMouseAction != SciTranslator.RELEASED) {
169                         GlobalEventFilter.filterMouse(mouseEvent, axes, scilabMouseAction, isControlDown);
170                 } else if (watchMotion && scilabMouseAction == SciTranslator.MOVED) {
171                         // Force false value to isControlDown
172                         // MOVED do not care about CTRL Key...
173                         GlobalEventFilter.filterMouse(mouseEvent, axes, MOVED, false);
174                 } else if (watchRelease && scilabMouseAction == SciTranslator.RELEASED) {
175                         // Force false value to isControlDown
176                         // RELEASED do not care about CTRL Key...
177                         GlobalEventFilter.filterMouse(mouseEvent, axes, scilabMouseAction, false);
178                 }
179         }
180
181
182 }
183