Enable event_handler on Frame component
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / ScilabEventListener.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008-2008 - 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.1-en.txt
10  *
11  */
12 package org.scilab.modules.gui.events;
13
14 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_ID__;
15
16 import java.awt.event.KeyEvent;
17 import java.awt.event.KeyListener;
18 import java.awt.event.MouseEvent;
19 import java.awt.event.MouseListener;
20 import java.awt.event.MouseMotionListener;
21
22 import org.scilab.modules.action_binding.InterpreterManagement;
23 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
24 import org.scilab.modules.gui.utils.SciTranslator;
25 /*
26  * This class is to manage scilab callback through seteventhandler
27  * it means call a dedicated scilab function like this :
28  * function my_eventhandler(windowsId, mouse X, mouse Y, mouse Button)
29  */
30 public class ScilabEventListener implements KeyListener, MouseListener, MouseMotionListener {
31
32     private String callback;
33     private Integer windowsUID;
34     private int mouseX = 0;
35     private int mouseY = 0;
36     private SciTranslator eventTranslator = new SciTranslator();
37     private boolean freedom = true;
38     private boolean inCanvas = false;
39     private boolean useHandle = true;
40
41     public ScilabEventListener(String callback, Integer windowsUID) {
42         eventTranslator.setClickAction(SciTranslator.UNMANAGED);
43         this.callback = callback;
44         this.windowsUID = windowsUID;
45     }
46
47     // Remove this constructor
48     // once event_handler call are unified using handle 
49     public ScilabEventListener(String callback, Integer windowsUID, boolean useHandle) {
50         eventTranslator.setClickAction(SciTranslator.UNMANAGED);
51         this.callback = callback;
52         this.windowsUID = windowsUID;
53         this.useHandle = useHandle;
54     }
55
56     private void callScilab() {
57         // @FIXME : choose to send it to scilab or to display it
58         //
59         if (useHandle) { 
60             InterpreterManagement.requestScilabExec(callback + "(getcallbackobject(" + windowsUID + ")," + mouseX + ',' + mouseY + ',' + eventTranslator.getClickAction() + ')');
61         } else {
62             int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
63             InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.getClickAction() + ')');
64         }
65         //
66         //System.out.println("call " + callback+'('+windowsId+','+mouseX+','+mouseY+','+eventTranslator.getClickAction()+')');
67     }
68
69     private void invokeScilab() {
70         // @FIXME : choose to send it to scilab or to display it
71         //
72         if (useHandle) { 
73             InterpreterManagement.requestScilabExec(callback + "(getcallbackobject(" + windowsUID + ")," + mouseX + ',' + mouseY + ',' + eventTranslator.javaClick2Scilab() + ')');
74         } else {
75             int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
76             InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.javaClick2Scilab() + ')');
77         }
78         //
79         //System.out.println("invoke " + callback+'('+windowsId+','+mouseX+','+mouseY+','+eventTranslator.javaClick2Scilab()+')');
80     }
81
82     public void keyPressed(KeyEvent keyEvent) {
83         if (inCanvas) {
84             if (Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
85                 eventTranslator.setClickAction(SciTranslator.javaKey2Scilab(keyEvent.getKeyChar(), keyEvent.isControlDown()));
86                 callScilab();
87             } else {
88                 int keyChar;
89                 if (keyEvent.isShiftDown()) {
90                     keyChar = keyEvent.getKeyCode();
91                 } else {
92                     keyChar = Character.toLowerCase(keyEvent.getKeyCode());
93                     callScilab();
94                 }
95                 eventTranslator.setClickAction(SciTranslator.javaKey2Scilab(keyChar,
96                                                keyEvent.isControlDown()));
97                 callScilab();
98             }
99         }
100     }
101
102     public void keyReleased(KeyEvent arg0) {
103         if (inCanvas && eventTranslator.getClickAction() != SciTranslator.UNMANAGED) {
104             eventTranslator.setClickAction(-eventTranslator.getClickAction());
105             callScilab();
106             eventTranslator.setClickAction(SciTranslator.UNMANAGED);
107         }
108     }
109
110     public void keyTyped(KeyEvent arg0) {
111         // Do nothing !!!
112     }
113
114     public void mouseClicked(MouseEvent arg0) {
115         mouseX = arg0.getX();
116         mouseY = arg0.getY();
117         if (arg0.getClickCount() == 1) {
118             eventTranslator.setClickAction(
119                 SciTranslator.javaButton2Scilab(
120                     arg0.getButton(),
121                     SciTranslator.CLICKED,
122                     arg0.isControlDown()));
123         } else {
124             /* Means mouseEvent.getClickCount() >= 2 */
125             eventTranslator.setClickAction(
126                 SciTranslator.javaButton2Scilab(
127                     arg0.getButton(),
128                     SciTranslator.DCLICKED,
129                     arg0.isControlDown()));
130             // To unlock javaClick2Scilab done in launchfilter
131             synchronized (eventTranslator) {
132                 eventTranslator.notify();
133             }
134         }
135     }
136
137     public void mouseEntered(MouseEvent arg0) {
138         inCanvas = true;
139     }
140
141     public void mouseExited(MouseEvent arg0) {
142         inCanvas = false;
143     }
144
145     public void mousePressed(MouseEvent arg0) {
146         if (this.freedom) {
147             this.freedom = false;
148             mouseX = arg0.getX();
149             mouseY = arg0.getY();
150             eventTranslator.setClickAction(
151                 SciTranslator.javaButton2Scilab(
152                     arg0.getButton(),
153                     SciTranslator.PRESSED,
154                     arg0.isControlDown()));
155             Thread launchMe = new Thread() {
156                 public void run() {
157                     invokeScilab();
158                     freedom = true;
159                     eventTranslator.setClickAction(SciTranslator.UNMANAGED);
160                 }
161             };
162             launchMe.start();
163         }
164     }
165
166     public void mouseReleased(MouseEvent arg0) {
167         if (eventTranslator.getClickAction() == SciTranslator.UNMANAGED || 
168                 eventTranslator.getClickAction() == SciTranslator.SCIMOVED) { 
169             eventTranslator.setClickAction(
170                 SciTranslator.javaButton2Scilab(arg0.getButton(),
171                                                 SciTranslator.RELEASED,
172                                                 arg0.isControlDown()));
173             mouseX = arg0.getX();
174             mouseY = arg0.getY();
175             callScilab();
176         }
177     }
178
179     public void mouseDragged(MouseEvent arg0) {
180         if (eventTranslator.getClickAction() == SciTranslator.javaButton2Scilab(MouseEvent.BUTTON1, SciTranslator.PRESSED, false)) { /* If LEFT BUTTON PRESSED */
181             this.freedom = false;
182             mouseX = arg0.getX();
183             mouseY = arg0.getY();
184             callScilab();
185             freedom = true;
186             eventTranslator.setClickAction(SciTranslator.SCIMOVED);
187         } else {
188             eventTranslator.setClickAction(SciTranslator.SCIMOVED);
189             mouseX = arg0.getX();
190             mouseY = arg0.getY();
191             callScilab();
192         }
193         eventTranslator.setClickAction(SciTranslator.UNMANAGED);
194     }
195
196     public void mouseMoved(MouseEvent arg0) {
197         eventTranslator.setClickAction(SciTranslator.SCIMOVED);
198         mouseX = arg0.getX();
199         mouseY = arg0.getY();
200         callScilab();
201         eventTranslator.setClickAction(SciTranslator.UNMANAGED);
202     }
203
204 }