Merge lp:~tapaal-contributor/tapaal/cancel-arc-draw-fix-1718892 into lp:tapaal

Proposed by Mads Johannsen
Status: Merged
Approved by: Jiri Srba
Approved revision: 946
Merged at revision: 946
Proposed branch: lp:~tapaal-contributor/tapaal/cancel-arc-draw-fix-1718892
Merge into: lp:tapaal
Diff against target: 250 lines (+51/-98)
4 files modified
src/pipe/gui/DrawingSurfaceImpl.java (+0/-10)
src/pipe/gui/graphicElements/Arc.java (+46/-1)
src/pipe/gui/handler/ArcKeyboardEventHandler.java (+0/-71)
src/pipe/gui/handler/PlaceTransitionObjectHandler.java (+5/-16)
To merge this branch: bzr merge lp:~tapaal-contributor/tapaal/cancel-arc-draw-fix-1718892
Reviewer Review Type Date Requested Status
Jiri Srba Approve
Review via email: mp+331615@code.launchpad.net

Description of the change

The ArcKeyboardEventHandler did not handle the cancellation of drawn arcs consistently, probably due to the loss of focus in the GUI.
This proposal implements the same functionality by using the JComponent inputmap and actionmap.

The unused variable "metaDown" from DrawingSurfaceImpl.java has been removed.

To post a comment you must log in.
Revision history for this message
Jiri Srba (srba) wrote :

Tested and works as expected. Great work!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/pipe/gui/DrawingSurfaceImpl.java'
2--- src/pipe/gui/DrawingSurfaceImpl.java 2017-07-02 18:03:21 +0000
3+++ src/pipe/gui/DrawingSurfaceImpl.java 2017-09-30 14:13:41 +0000
4@@ -53,11 +53,6 @@
5
6 private AnimationHandler animationHandler = new AnimationHandler();
7
8- // When i'm using GNU/Linux, isMetaDown() doesn't return true when I press
9- // "Windows key". I don't know if a problem of my configuration or what.
10- // metaDown is used in this case
11- boolean metaDown = false;
12-
13 private SelectionManager selection;
14 private UndoManager undoManager;
15 private ArrayList<PetriNetObject> petriNetObjects = new ArrayList<PetriNetObject>();
16@@ -384,11 +379,6 @@
17 super.removeAll();
18 }
19
20- //
21- public void setMetaDown(boolean down) {
22- metaDown = down;
23- }
24-
25 public Point getPointer() {
26 return getMousePosition();
27 }
28
29=== modified file 'src/pipe/gui/graphicElements/Arc.java'
30--- src/pipe/gui/graphicElements/Arc.java 2015-11-23 17:12:41 +0000
31+++ src/pipe/gui/graphicElements/Arc.java 2017-09-30 14:13:41 +0000
32@@ -1,9 +1,10 @@
33 package pipe.gui.graphicElements;
34
35 import java.awt.Rectangle;
36+import java.awt.event.ActionEvent;
37 import java.awt.geom.Point2D;
38
39-import javax.swing.JLayeredPane;
40+import javax.swing.*;
41
42 import pipe.gui.CreateGui;
43 import pipe.gui.DrawingSurfaceImpl;
44@@ -339,4 +340,48 @@
45 return super.clone();
46 }
47
48+ /**
49+ * Handles keyboard input when drawing arcs in the GUI. Keys are bound to action names,
50+ * and action names are mapped to action objects. The key bindings are disabled when the
51+ * arc object is deleted, or the arc is connected to a place/transition.
52+ */
53+ public void enableDrawingKeyBindings() {
54+ InputMap iMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
55+ ActionMap aMap = this.getActionMap();
56+
57+ // Bind keyboard keys to action names.
58+ iMap.put(KeyStroke.getKeyStroke("ESCAPE"), "deleteArc");
59+ iMap.put(KeyStroke.getKeyStroke("DELETE"), "deleteArc");
60+
61+ // Associate action names with actions.
62+ aMap.put("deleteArc", new DeleteAction(this));
63+ }
64+
65+ public void disableDrawingKeyBindings() {
66+ this.getInputMap().clear();
67+ this.getActionMap().clear();
68+ }
69+
70+ private class DeleteAction extends AbstractAction {
71+ Arc arcBeingDraw;
72+
73+ DeleteAction(Arc arc) {
74+ arcBeingDraw = arc;
75+ }
76+
77+ @Override
78+ public void actionPerformed(ActionEvent e) {
79+ DrawingSurfaceImpl aView = CreateGui.getView();
80+ if (aView.createArc == arcBeingDraw) {
81+ aView.createArc = null;
82+ delete();
83+
84+ if ((CreateGui.getApp().getMode() == Pipe.ElementType.FAST_PLACE)
85+ || (CreateGui.getApp().getMode() == Pipe.ElementType.FAST_TRANSITION)) {
86+ CreateGui.getApp().endFastMode();
87+ }
88+ aView.repaint();
89+ }
90+ }
91+ }
92 }
93
94=== removed file 'src/pipe/gui/handler/ArcKeyboardEventHandler.java'
95--- src/pipe/gui/handler/ArcKeyboardEventHandler.java 2017-05-20 20:13:25 +0000
96+++ src/pipe/gui/handler/ArcKeyboardEventHandler.java 1970-01-01 00:00:00 +0000
97@@ -1,71 +0,0 @@
98-package pipe.gui.handler;
99-
100-import java.awt.event.KeyAdapter;
101-import java.awt.event.KeyEvent;
102-
103-import pipe.gui.CreateGui;
104-import pipe.gui.DrawingSurfaceImpl;
105-import pipe.gui.Pipe.ElementType;
106-import pipe.gui.graphicElements.Arc;
107-
108-/**
109- * @authors Michael Camacho and Tom Barnwell
110- *
111- */
112-public class ArcKeyboardEventHandler extends KeyAdapter {
113-
114- private Arc arcBeingDrawn;
115-
116- public ArcKeyboardEventHandler(Arc anArc) {
117- arcBeingDrawn = anArc;
118- }
119-
120- @Override
121- public void keyPressed(KeyEvent e) {
122- switch (e.getKeyCode()) {
123- case KeyEvent.VK_META:
124- case KeyEvent.VK_WINDOWS:
125- // I don't know if it's a java's bug or if I have a configuration
126- // problem with my linux box, but there is an issue with the
127- // Windows key under linux, so the space key is used as a
128- // provisional
129- // solution
130- case KeyEvent.VK_SPACE: // provisional
131- ((DrawingSurfaceImpl) arcBeingDrawn.getParent()).setMetaDown(true);
132- break;
133-
134- case KeyEvent.VK_ESCAPE:
135- case KeyEvent.VK_DELETE:
136- DrawingSurfaceImpl aView = ((DrawingSurfaceImpl) arcBeingDrawn
137- .getParent());
138- aView.createArc = null;
139- arcBeingDrawn.delete();
140-
141- if ((CreateGui.getApp().getMode() == ElementType.FAST_PLACE)
142- || (CreateGui.getApp().getMode() == ElementType.FAST_TRANSITION)) {
143- CreateGui.getApp().endFastMode();
144- }
145- aView.repaint();
146- break;
147-
148- default:
149- break;
150- }
151- }
152-
153- @Override
154- public void keyReleased(KeyEvent e) {
155- switch (e.getKeyCode()) {
156- case KeyEvent.VK_META:
157- case KeyEvent.VK_WINDOWS:
158- case KeyEvent.VK_SPACE: // provisional
159- ((DrawingSurfaceImpl) arcBeingDrawn.getParent()).setMetaDown(false);
160- break;
161-
162- default:
163- break;
164- }
165- e.consume();
166- }
167-
168-}
169
170=== modified file 'src/pipe/gui/handler/PlaceTransitionObjectHandler.java'
171--- src/pipe/gui/handler/PlaceTransitionObjectHandler.java 2011-11-28 15:14:40 +0000
172+++ src/pipe/gui/handler/PlaceTransitionObjectHandler.java 2017-09-30 14:13:41 +0000
173@@ -46,7 +46,6 @@
174 private static final String ERROR_MSG_TWO_ARCS = "We do not allow two arcs from a place to a transition or a transition to a place.";
175 private DataLayer guiModel;
176 private TimedArcPetriNet model;
177- ArcKeyboardEventHandler keyHandler = null;
178
179 public PlaceTransitionObjectHandler(Container contentpane,
180 PlaceTransitionObject obj, DataLayer guiModel,
181@@ -70,15 +69,14 @@
182 CreateGui.getView().createArc = newArc;
183 // addPetriNetObject a handler for shift & esc actions drawing arc
184 // this is removed when the arc is finished drawing:
185- keyHandler = new ArcKeyboardEventHandler(newArc);
186- newArc.addKeyListener(keyHandler);
187 newArc.requestFocusInWindow();
188 newArc.setSelectable(false);
189+ newArc.enableDrawingKeyBindings();
190 }
191- // Call this function to cleanup temporary key handlers for an new arc object
192+
193+ // Disable key bindings that are only available when drawing arcs.
194 private void freeArc(Arc newArc){
195- newArc.removeKeyListener(keyHandler);
196- keyHandler = null;
197+ newArc.disableDrawingKeyBindings();
198 CreateGui.getView().createArc = null;
199 }
200
201@@ -194,10 +192,7 @@
202 undoManager.addNewEdit(new AddTimedInhibitorArcCommand(
203 createTAPNInhibitorArc, model, guiModel, view));
204
205- // arc is drawn, remove handler:
206 freeArc(createTAPNInhibitorArc);
207-
208-
209 }
210
211 break;
212@@ -296,7 +291,6 @@
213 guiModel.addArc((TimedOutputArcComponent) transportArcToCreate);
214 view.addNewPetriNetObject(transportArcToCreate);
215
216- // arc is drawn, remove handlers:
217 freeArc(transportArcToCreate);
218
219 // Create the next arc
220@@ -361,12 +355,9 @@
221 guiModel,
222 view));
223
224- // arc is drawn, remove handlers:
225 freeArc(transportArcToCreate);
226
227 arc2.setGroupNr(arc1.getGroupNr());
228-
229-
230 }
231
232 }
233@@ -470,8 +461,7 @@
234 model, guiModel, view));
235
236 }
237- // arc is drawn, remove handler:
238- // arc is drawn, remove handlers:
239+
240 freeArc(timedArcToCreate);
241 }
242 break;
243@@ -488,7 +478,6 @@
244
245 private void cleanupArc(Arc arc, DrawingSurfaceImpl view) {
246 arc.delete();
247- // arc is drawn, remove handlers:
248 freeArc(arc);
249
250 view.remove(arc);

Subscribers

People subscribed via source and target branches