Remove ToolBar/MenuBar from UIElementMapper
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / editor / EditorEventListener.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Pedro Arthur dos S. Souza
4  * Copyright (C) 2012 - Caio Lucas dos S. Souza
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11  *
12  */
13
14 package org.scilab.modules.gui.editor;
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.gui.datatip.DatatipCreate;
23 import org.scilab.modules.gui.datatip.DatatipDelete;
24 import org.scilab.modules.gui.datatip.DatatipMove;
25 import org.scilab.modules.gui.datatip.DatatipDrag;
26 import org.scilab.modules.gui.datatip.DatatipOrientation;
27 import org.scilab.modules.gui.datatip.DatatipManagerMode;
28 import org.scilab.modules.gui.datatip.DatatipManager;
29
30 import org.scilab.modules.gui.editor.Editor;
31 import org.scilab.modules.gui.editor.EditorManager;
32 import org.scilab.modules.gui.editor.EntityPicker;
33 import org.scilab.modules.gui.events.GlobalEventWatcher;
34
35 import org.scilab.modules.gui.ged.Inspector;
36
37 import org.scilab.modules.gui.plotbrowser.PlotBrowser;
38
39
40 /**
41  * Event listener for the figure editor.
42  *
43  *
44  * @author Caio Souza <caioc2bolado@gmail.com>
45  * @author Pedro Souza <bygrandao@gmail.com>
46  *
47  * @since 2012-06-01
48  */
49
50 public class EditorEventListener implements KeyListener, MouseListener, MouseMotionListener {
51
52     Integer windowUid;
53
54     Integer picked;
55     EntityPicker ep;
56
57     DatatipManagerMode datatipManagerMode;
58     Integer lastDatatip = null;
59     Integer[] lastClick = new Integer[2];
60     boolean changeOrientation = false;
61
62     Editor editor;
63     boolean isInRotation = false;
64     boolean isLeftButtonPressed = false;
65     boolean enabled = false;
66
67     public EditorEventListener(Integer uid) {
68         windowUid = uid;
69         editor = EditorManager.newEditor(uid);
70         ep = new EntityPicker();
71         datatipManagerMode = DatatipManager.newDatatipManagerMode(uid);
72     }
73
74     public void setEnable(boolean enabled) {
75         this.enabled = enabled;
76     }
77
78     public boolean isEnable() {
79         return enabled;
80     }
81
82     public boolean isDatatipEnable() {
83         return datatipManagerMode.isEnabled();
84     }
85
86     public boolean canProcess() {
87         return enabled && !GlobalEventWatcher.isActivated() && !datatipManagerMode.isInteractiveZoom();
88     }
89
90     public void onExit() {
91         EditorManager.deleteEditor(editor.getFigureUid());
92         DatatipManager.deleteDatatipManager(windowUid);
93     }
94
95     public void keyPressed(KeyEvent arg0) {
96         if (enabled && !GlobalEventWatcher.isActivated()) {
97             editor.onKeyPressed(arg0);
98             if (datatipManagerMode.isEnabled()) {
99                 if (datatipManagerMode.getSelectedTip() != null) {
100                     switch (arg0.getKeyCode()) {
101                         case KeyEvent.VK_RIGHT:
102                             DatatipMove.moveRight(datatipManagerMode.getSelectedTip());
103                             break;
104                         case KeyEvent.VK_LEFT:
105                             DatatipMove.moveLeft(datatipManagerMode.getSelectedTip());
106                             break;
107                         case KeyEvent.VK_DELETE:
108                         case KeyEvent.VK_BACK_SPACE:
109                             DatatipDelete.deleteDatatip(datatipManagerMode.getSelectedTip());
110                             break;
111                     }
112                 }
113             }
114         }
115     }
116
117     public void keyReleased(KeyEvent arg0) {
118     }
119
120     public void keyTyped(KeyEvent arg0) {
121     }
122
123     /**
124      * @param arg0 MouseEvent
125      */
126     public void mouseClicked(MouseEvent arg0) {
127     }
128
129     public void mouseEntered(MouseEvent arg0) {
130     }
131
132     public void mouseExited(MouseEvent arg0) {
133     }
134
135     /**
136      * On left mouse press: pass event to editor,
137      * if datatip manager mode is enabled try select/ create a datatip
138      * @param arg0 MouseEvent
139      */
140     public void mousePressed(MouseEvent arg0) {
141         if (canProcess() && arg0.getButton() == 1) {
142             isLeftButtonPressed = true;
143             if (!datatipManagerMode.isEnabled()) {
144                 editor.onLeftMouseDown(arg0);
145
146                 // Part responsible for the exchange of properties of the GED.
147                 //If the GED is open, so the code is executed.
148                 if (Inspector.isInspectorOpened()) {
149                     editor.onClickGED();
150                 }
151                 if (PlotBrowser.isPlotBrowserOpened()) {
152                     PlotBrowser.getPlotBrowser(windowUid);
153                 }
154             } else {
155                 if (!datatipManagerMode.pickAndHighlight(arg0.getX(), arg0.getY())) {
156                     picked = ep.pick(windowUid, arg0.getX(), arg0.getY());
157                     if (picked != null) {
158                         lastClick[0] = arg0.getX();
159                         lastClick[1] = arg0.getY();
160                         lastDatatip = DatatipCreate.createDatatip(windowUid, arg0.getX(), arg0.getY());
161                         changeOrientation = true;
162                     }
163                 } else {
164                     // we are in datatip mode and we clicked on a datatip
165                     datatipManagerMode.enableTranslation(false);
166                 }
167             }
168         }
169     }
170
171     /**
172      * On right mouse release: pass event to editor to make
173      * the popup menu visible if datatip's manager mode is off,
174      * else try delete a datatip.
175      * @param arg0 MouseEvent
176      */
177     public void mouseReleased(MouseEvent arg0) {
178         if (canProcess()) {
179             if (!datatipManagerMode.isEnabled()) {
180                 if (arg0.getButton() == 3) {
181                     if (!isInRotation) {
182                         editor.onRightMouseClick(arg0);
183                     }
184                 } else if (arg0.getButton() == 1) {
185                     editor.onLeftMouseRelease(arg0);
186                 }
187             } else {
188                 if (arg0.getButton() == 3 && !isInRotation) {
189                     if (!datatipManagerMode.pickAndDelete(arg0.getX(), arg0.getY())) {
190                         datatipManagerMode.onRightMouseClick(arg0);
191                     }
192                 }
193                 datatipManagerMode.enableTranslation(true);
194             }
195             isInRotation = false;
196             isLeftButtonPressed = false;
197             changeOrientation = false;
198             editor.setUpdateDrag(true);
199         }
200     }
201
202     /**
203      * On left mouse dragged: pass event to editor if
204      * datatip maneger mode is disabled else
205      * drag/ change datatip orientation
206      */
207     public void mouseDragged(MouseEvent arg0) {
208         if (canProcess()) {
209
210             if (!datatipManagerMode.isEnabled()) {
211
212                 if (isLeftButtonPressed) {
213                     editor.onMouseDragged(arg0);
214
215                 } else {
216                     isInRotation = true;
217                 }
218             } else {
219                 if (changeOrientation) {
220                     Integer[] curClick = {arg0.getX(), arg0.getY()};
221                     DatatipOrientation.setOrientation(lastDatatip, lastClick, curClick);
222                 } else if (datatipManagerMode.getSelectedTip() != null && isLeftButtonPressed) {
223                     DatatipDrag.dragDatatip(datatipManagerMode.getSelectedTip(), arg0.getX(), arg0.getY());
224                 }
225             }
226             editor.setUpdateDrag(false);
227         }
228     }
229
230     public void mouseMoved(MouseEvent arg0) {
231     }
232 }
233