71b128999088b964143a5e3356ad950fda64394d
[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.tab.SwingScilabAxes;
19 import org.scilab.modules.gui.utils.SciTranslator;
20
21 /**
22  * This class implements the xclick Scilab method
23  * by updating the ClickInfos attributes.
24  * @author Bruno Jofret
25  */
26 public final class Jxclick {
27
28         private static int keyChar;
29
30         /**
31          * PRIVATE constructor
32          * We do not want a Jxclick object.
33          */
34         private Jxclick() { }
35
36         /**
37          * Scilab call point for xclick.
38          */
39         public static void xclick() {
40                 /*
41                  *  Enable the local keyActionFilter function
42                  *  to be called when a keyEvent is caught.
43                  */     
44                 GlobalEventWatcher.enable(new GlobalKeyEventWatcher() {
45                         public void keyEventFilter(KeyEvent keyEvent) {
46                                 keyActionFilter(keyEvent);
47                         }
48                 });
49                 /*
50                  *  Enable the local mouseActionFilter function
51                  *  to be called when a mouseEvent is caught.
52                  */             
53                 GlobalEventWatcher.enable(new GlobalMouseEventWatcher(AWTEvent.MOUSE_EVENT_MASK) {
54                         public void mouseEventFilter(MouseEvent mouseEvent,
55                                         SwingScilabAxes axes, int scilabMouseAction, boolean isControlDown) {
56                                 mouseActionFilter(mouseEvent, axes, scilabMouseAction, isControlDown);  
57                         }
58                 });
59                 synchronized (ClickInfos.getInstance()) {
60                         try {
61                                 ClickInfos.getInstance().init();
62                                 ClickInfos.getInstance().wait();
63                         } catch (InterruptedException e) {
64                                 // TODO Auto-generated catch block
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 int 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                         } 
114                         else {
115                                 if (keyEvent.isShiftDown()) {
116                                         keyChar = keyEvent.getKeyCode();
117                                 } 
118                                 else {
119                                         keyChar = Character.toLowerCase(keyEvent.getKeyCode());
120                                 }
121                         }
122                 } 
123                 else if (keyEvent.getID() == KeyEvent.KEY_TYPED) {      
124                         if (keyEvent.getSource() instanceof SwingScilabAxes) {
125                                 if (GlobalEventWatcher.isActivated()) {
126                                         GlobalEventFilter.filterKey(keyChar, ((SwingScilabAxes)keyEvent.getSource()).getFigureId(),((KeyEvent) keyEvent).isControlDown());
127                                 }
128                         }
129                 } 
130         }
131         /**
132          * Manage xclick behaviour for mouse entry.
133          * 
134          * @param mouseEvent : the Mouse Event caught
135          * @param scilabMouseAction : the integer scilab code for mouse action.
136          * @param axes : the axes where action occurs.
137          * @param isControlDown true if the CTRL key has been pressed
138          */
139         private static void mouseActionFilter(MouseEvent mouseEvent, SwingScilabAxes axes, int scilabMouseAction, boolean isControlDown) {      
140                 if (scilabMouseAction == SciTranslator.PRESSED
141                                 || scilabMouseAction == SciTranslator.CLICKED
142                                 || scilabMouseAction == SciTranslator.DCLICKED) {
143                         GlobalEventFilter.filterMouse(mouseEvent, axes, scilabMouseAction, isControlDown);
144                 }
145         }       
146 }