Remove autogenerated comments
[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                                 e.printStackTrace();
87                         }
88                 }
89                 GlobalEventWatcher.disable();
90         }
91
92         /**
93          * @return the mouseButtonNumber
94          */
95         public static int getMouseButtonNumber() {
96                 return ClickInfos.getInstance().getMouseButtonNumber();
97         }
98         /**
99          * @return the xCoordinate
100          */
101         public static double getXCoordinate() {
102                 return ClickInfos.getInstance().getXCoordinate();
103         }
104         /**
105          * @return the yCoordinate
106          */
107         public static double getYCoordinate() {
108                 return ClickInfos.getInstance().getYCoordinate();
109         }
110         /**
111          * @return the WindowsID
112          */
113         public static int getWindowsID() {
114                 return ClickInfos.getInstance().getWindowID();
115         }
116         
117         
118         
119         /**
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
124          * 
125          * @param keyEvent : the Key Event caught
126          */
127         private static void keyActionFilter(KeyEvent keyEvent) {        
128                 /*
129                  * If a key is pressed, only remember the key and if CTRL is pressed either.
130                  */
131                 if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
132                         if (!Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
133                                 keyChar = keyEvent.getKeyChar(); 
134                         } else {
135                                 if (keyEvent.isShiftDown()) {
136                                         keyChar = keyEvent.getKeyCode();
137                                 } else {
138                                         keyChar = Character.toLowerCase(keyEvent.getKeyCode());
139                                 }
140                         }
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. */
144                         /*
145                          * If a RELEASED is seen use -keyChar
146                          */
147                         if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
148                                 GlobalEventFilter.filterKey(-keyChar, ((SwingScilabAxes) keyEvent.getSource()).getFigureId(), isControlDown);
149                         } else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
150                                 /*
151                                  * Or If a TYPED is seen use keyChar
152                                  */
153                                 GlobalEventFilter.filterKey(keyChar, ((SwingScilabAxes) keyEvent.getSource()).getFigureId(), isControlDown);
154                         }       
155                 }
156         }
157         /**
158          * Manage xgetmouse behaviour for mouse entry.
159          * 
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
164          */
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);
177                 }
178         }
179
180
181 }
182