5c2ba4f01691851f5fa62a42abd8ec6cfe9097fa
[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
40     public ScilabEventListener(String callback, Integer windowsUID) {
41         eventTranslator.setClickAction(SciTranslator.UNMANAGED);
42         this.callback = callback;
43         this.windowsUID = windowsUID;
44     }
45
46     private void callScilab() {
47         // @FIXME : choose to send it to scilab or to display it
48         //
49         int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
50         InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.getClickAction() + ')');
51         //
52         //System.out.println("call " + callback+'('+windowsId+','+mouseX+','+mouseY+','+eventTranslator.getClickAction()+')');
53     }
54
55     private void invokeScilab() {
56         // @FIXME : choose to send it to scilab or to display it
57         //
58         int windowsId = (Integer) GraphicController.getController().getProperty(windowsUID, __GO_ID__);
59         InterpreterManagement.requestScilabExec(callback + '(' + windowsId + ',' + mouseX + ',' + mouseY + ',' + eventTranslator.javaClick2Scilab() + ')');
60         //
61         //System.out.println("invoke " + callback+'('+windowsId+','+mouseX+','+mouseY+','+eventTranslator.javaClick2Scilab()+')');
62     }
63
64     public void keyPressed(KeyEvent keyEvent) {
65         if (inCanvas) {
66             if (Character.isJavaIdentifierStart(keyEvent.getKeyChar())) {
67                 eventTranslator.setClickAction(SciTranslator.javaKey2Scilab(keyEvent.getKeyChar(), keyEvent.isControlDown()));
68                 callScilab();
69             } else {
70                 int keyChar;
71                 if (keyEvent.isShiftDown()) {
72                     keyChar = keyEvent.getKeyCode();
73                 } else {
74                     keyChar = Character.toLowerCase(keyEvent.getKeyCode());
75                     callScilab();
76                 }
77                 eventTranslator.setClickAction(SciTranslator.javaKey2Scilab(keyChar,
78                                                keyEvent.isControlDown()));
79                 callScilab();
80             }
81         }
82     }
83
84     public void keyReleased(KeyEvent arg0) {
85         if (inCanvas && eventTranslator.getClickAction() != SciTranslator.UNMANAGED) {
86             eventTranslator.setClickAction(-eventTranslator.getClickAction());
87             callScilab();
88             eventTranslator.setClickAction(SciTranslator.UNMANAGED);
89         }
90     }
91
92     public void keyTyped(KeyEvent arg0) {
93         // Do nothing !!!
94     }
95
96     public void mouseClicked(MouseEvent arg0) {
97         mouseX = arg0.getX();
98         mouseY = arg0.getY();
99         if (arg0.getClickCount() == 1) {
100             eventTranslator.setClickAction(
101                 SciTranslator.javaButton2Scilab(
102                     arg0.getButton(),
103                     SciTranslator.CLICKED,
104                     arg0.isControlDown()));
105         } else {
106             /* Means mouseEvent.getClickCount() >= 2 */
107             eventTranslator.setClickAction(
108                 SciTranslator.javaButton2Scilab(
109                     arg0.getButton(),
110                     SciTranslator.DCLICKED,
111                     arg0.isControlDown()));
112             // To unlock javaClick2Scilab done in launchfilter
113             synchronized (eventTranslator) {
114                 eventTranslator.notify();
115             }
116         }
117     }
118
119     public void mouseEntered(MouseEvent arg0) {
120         inCanvas = true;
121     }
122
123     public void mouseExited(MouseEvent arg0) {
124         inCanvas = false;
125     }
126
127     public void mousePressed(MouseEvent arg0) {
128         if (this.freedom) {
129             this.freedom = false;
130             mouseX = arg0.getX();
131             mouseY = arg0.getY();
132             eventTranslator.setClickAction(
133                 SciTranslator.javaButton2Scilab(
134                     arg0.getButton(),
135                     SciTranslator.PRESSED,
136                     arg0.isControlDown()));
137             Thread launchMe = new Thread() {
138                 public void run() {
139                     invokeScilab();
140                     freedom = true;
141                 }
142             };
143             launchMe.start();
144         }
145     }
146
147     public void mouseReleased(MouseEvent arg0) {
148         // TODO Auto-generated method stub
149         if (eventTranslator.getClickAction() == SciTranslator.UNMANAGED) {
150             eventTranslator.setClickAction(
151                 SciTranslator.javaButton2Scilab(arg0.getButton(),
152                                                 SciTranslator.RELEASED,
153                                                 arg0.isControlDown()));
154             mouseX = arg0.getX();
155             mouseY = arg0.getY();
156             callScilab();
157         }
158     }
159
160     public void mouseDragged(MouseEvent arg0) {
161         if (eventTranslator.getClickAction() == SciTranslator.javaButton2Scilab(MouseEvent.BUTTON1, SciTranslator.PRESSED, false)) { /* If LEFT BUTTON PRESSED */
162             this.freedom = false;
163             mouseX = arg0.getX();
164             mouseY = arg0.getY();
165             callScilab();
166             freedom = true;
167             eventTranslator.setClickAction(SciTranslator.SCIMOVED);
168         } else {
169             eventTranslator.setClickAction(SciTranslator.SCIMOVED);
170             mouseX = arg0.getX();
171             mouseY = arg0.getY();
172             callScilab();
173         }
174     }
175
176     public void mouseMoved(MouseEvent arg0) {
177         eventTranslator.setClickAction(SciTranslator.SCIMOVED);
178         mouseX = arg0.getX();
179         mouseY = arg0.getY();
180         callScilab();
181     }
182
183 }