Merge lp:~yrke/tapaal/cleanup-drawingsurface-mouseEvents into lp:tapaal/dev

Proposed by Kenneth Yrke Jørgensen on 2019-06-15
Status: Merged
Merged at revision: 1023
Proposed branch: lp:~yrke/tapaal/cleanup-drawingsurface-mouseEvents
Merge into: lp:tapaal/dev
Diff against target: 382 lines (+138/-151)
1 file modified
src/pipe/gui/DrawingSurfaceImpl.java (+138/-151)
To merge this branch: bzr merge lp:~yrke/tapaal/cleanup-drawingsurface-mouseEvents
Reviewer Review Type Date Requested Status
Kenneth Yrke Jørgensen Approve on 2019-06-15
Review via email: mp+368857@code.launchpad.net
To post a comment you must log in.
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 2019-04-24 07:11:25 +0000
3+++ src/pipe/gui/DrawingSurfaceImpl.java 2019-06-15 08:31:30 +0000
4@@ -37,7 +37,7 @@
5 private boolean animationmode = false;
6
7 public Arc createArc; // no longer static
8-
9+
10 private static final int DRAWING_SURFACE_GROW = 100;
11
12 private SelectionManager selection;
13@@ -225,10 +225,10 @@
14 private void calculateNewBoundsForScrollPane(Rectangle rect) {
15 boolean changed = false;
16 Dimension current = getPreferredSize();
17-
18+
19 int this_width = (rect.x + rect.width + 2);
20 int this_height = (rect.y + rect.height+ 2);
21-
22+
23 if (this_width > current.width) {
24 current.width = this_width; changed=true;
25 }
26@@ -270,7 +270,7 @@
27 public ArrayList<PetriNetObject> getPNObjects() {
28 return petriNetObjects;
29 }
30-
31+
32 public ArrayList<PetriNetObject> getPlaceTransitionObjects(){
33 ArrayList<PetriNetObject> result = new ArrayList<PetriNetObject>();
34 for (PetriNetObject pnObject : petriNetObjects) {
35@@ -340,7 +340,7 @@
36
37 int zoomPercent = getZoom();
38
39- JViewport viewport = (JViewport) getParent();
40+ JViewport viewport = (JViewport) getParent();
41
42 Component[] children = getComponents();
43
44@@ -350,7 +350,7 @@
45 ((Zoomable) child).zoomUpdate(zoomPercent);
46 }
47 }
48-
49+
50 // Calculate new position of the Drawing Surface.
51 double newZoomedX = Zoomer.getZoomedValue(point.x, zoomPercent);
52 double newZoomedY = Zoomer.getZoomedValue(point.y, zoomPercent);
53@@ -377,8 +377,6 @@
54
55 class MouseHandler extends MouseInputAdapter {
56
57- private PetriNetObject pnObject;
58-
59 private DrawingSurfaceImpl view;
60
61 private DataLayer guiModel;
62@@ -398,7 +396,7 @@
63 guiModel = _model;
64 }
65
66- private Point adjustPoint(Point p, int zoom) {
67+ private Point adjustPointToZoom(Point p, int zoom) {
68 int offset = (int) (Zoomer.getScaleFactor(zoom)
69 * Pipe.PLACE_TRANSITION_HEIGHT / 2);
70
71@@ -408,34 +406,71 @@
72 p.setLocation(x, y);
73 return p;
74 }
75-
76- private PlaceTransitionObject newTimedPlace(Point p) {
77- p = adjustPoint(p, view.getZoom());
78+ private Point adjustPointToGrid(Point p) {
79+ int x = Grid.getModifiedX(p.x);
80+ int y = Grid.getModifiedY(p.y);
81+
82+ return new Point(x, y);
83+ }
84+
85+ private Point adjustPointToGridAndZoom(Point p, int zoom) {
86+ Point newP = adjustPointToZoom(p, zoom);
87+ newP = adjustPointToGrid(newP);
88+
89+ return newP;
90+ }
91+
92+ private void addPetriNetObjectToModelandView(PetriNetObject pnObject) {
93+ guiModel.addPetriNetObject(pnObject);
94+ view.addNewPetriNetObject(pnObject);
95+ }
96+
97+ private PlaceTransitionObject newTimedPlaceAddToModelView(Point p) {
98+ p = adjustPointToGridAndZoom(p, view.getZoom());
99+
100 dk.aau.cs.model.tapn.LocalTimedPlace tp = new dk.aau.cs.model.tapn.LocalTimedPlace(nameGenerator.getNewPlaceName(model));
101- pnObject = new TimedPlaceComponent(Grid.getModifiedX(p.x), Grid
102- .getModifiedY(p.y), tp);
103+ TimedPlaceComponent pnObject = new TimedPlaceComponent(p.x, p.y, tp);
104 model.add(tp);
105- guiModel.addPetriNetObject(pnObject);
106- view.addNewPetriNetObject(pnObject);
107- return (PlaceTransitionObject) pnObject;
108+ addPetriNetObjectToModelandView(pnObject);
109+
110+ getUndoManager().addNewEdit(new AddTimedPlaceCommand(pnObject, model, guiModel, view));
111+
112+ return pnObject;
113 }
114
115- private PlaceTransitionObject newTAPNTransition(Point p, boolean timed) {
116- p = adjustPoint(p, view.getZoom());
117- dk.aau.cs.model.tapn.TimedTransition transition = new dk.aau.cs.model.tapn.TimedTransition(
118- nameGenerator.getNewTransitionName(model));
119-
120- pnObject = new TimedTransitionComponent(Grid.getModifiedX(p.x),
121- Grid.getModifiedY(p.y), transition);
122-
123+ private PlaceTransitionObject newTAPNTransitionAddToModelView(Point p) {
124+ p = adjustPointToGridAndZoom(p, view.getZoom());
125+
126+ dk.aau.cs.model.tapn.TimedTransition transition = new dk.aau.cs.model.tapn.TimedTransition(nameGenerator.getNewTransitionName(model));
127+
128+ TimedTransitionComponent pnObject = new TimedTransitionComponent(p.x, p.y, transition);
129+
130 model.add(transition);
131- guiModel.addPetriNetObject(pnObject);
132- view.addNewPetriNetObject(pnObject);
133- return (PlaceTransitionObject) pnObject;
134+ addPetriNetObjectToModelandView(pnObject);
135+
136+ getUndoManager().addNewEdit(new AddTimedTransitionCommand(pnObject, model, guiModel, view));
137+ return pnObject;
138 }
139
140- private PlaceTransitionObject newTAPNTransition(Point p) {
141- return newTAPNTransition(p, false);
142+ private AnnotationNote newAnnotationNoteAddToModelView(Point clickPoint) {
143+ Point p = adjustPointToGridAndZoom(clickPoint, view.getZoom());
144+
145+ AnnotationNote pnObject = new AnnotationNote(p.x, p.y, true);
146+ addPetriNetObjectToModelandView(pnObject);
147+
148+ getUndoManager().addNewEdit(new AddPetriNetObjectEdit(pnObject, view, guiModel));
149+ pnObject.enableEditMode();
150+ return pnObject;
151+ }
152+
153+ private void continueFastMode(MouseEvent e, PlaceTransitionObject pto, ElementType nextMode) {
154+ // connect arc
155+ app.setMode(ElementType.TAPNARC);
156+ pto.getMouseHandler().mousePressed(e);
157+ pto.getMouseHandler().mouseReleased(e);
158+ app.setMode(nextMode);
159+ // enter fast mode
160+ pto.dispatchEvent(e);
161 }
162
163 @Override
164@@ -444,135 +479,89 @@
165
166 // check for control down here enables it to attach the arc being drawn to an existing place/transition
167
168- Point start = e.getPoint();
169- Point p;
170+
171+ Point clickPoint = e.getPoint();
172+
173 if (SwingUtilities.isLeftMouseButton(e)) {
174
175 Pipe.ElementType mode = app.getMode();
176- PlaceTransitionObject pto;
177+ PlaceTransitionObject newpto; //declared here as switch is one big scope
178+
179 switch (mode) {
180-
181- case TAPNPLACE:
182- // create place
183- PlaceTransitionObject pto2 = newTimedPlace(e.getPoint());
184- getUndoManager().addNewEdit(
185- new AddTimedPlaceCommand(
186- (TimedPlaceComponent) pto2, model,
187- guiModel, view));
188- if (e.isControlDown()) {
189- // connect arc
190- app.setMode(ElementType.TAPNARC);
191- pto2.getMouseHandler().mousePressed(e);
192- pto2.getMouseHandler().mouseReleased(e);
193- app.setMode(ElementType.FAST_TRANSITION);
194- // enter fast mode
195- pnObject.dispatchEvent(e);
196- }
197- break;
198-
199- case TAPNTRANS:
200- // create transition
201- pto = newTAPNTransition(e.getPoint());
202- getUndoManager().addNewEdit(
203- new AddTimedTransitionCommand(
204- (TimedTransitionComponent) pto, model,
205- guiModel, view));
206- if (e.isControlDown()) {
207- // connect arc
208- app.setMode(ElementType.TAPNARC);
209- pto.getMouseHandler().mousePressed(e);
210- pto.getMouseHandler().mouseReleased(e);
211- // enter fast mode
212- app.setMode(ElementType.FAST_PLACE);
213- pnObject.dispatchEvent(e);
214- }
215- break;
216-
217- case ARC:
218- case TAPNARC:
219- case INHIBARC:
220- // Add point to arc in creation
221- if (createArc != null) {
222- addPoint(createArc, e);
223- }
224- break;
225-
226- case ANNOTATION:
227- p = adjustPoint(e.getPoint(), view.getZoom());
228-
229- pnObject = new AnnotationNote(p.x, p.y, true);
230- guiModel.addPetriNetObject(pnObject);
231- view.addNewPetriNetObject(pnObject);
232- getUndoManager()
233- .addNewEdit(
234- new AddPetriNetObjectEdit(pnObject, view,
235- guiModel));
236- ((AnnotationNote) pnObject).enableEditMode();
237- break;
238- case TRANSPORTARC:
239- if (createArc != null) {
240- addPoint(createArc, e);
241- }
242- break;
243- case TAPNINHIBITOR_ARC:
244- // Add point to arc in creation
245- if (createArc != null) {
246- addPoint(createArc, e);
247- }
248- break;
249- case DRAG:
250- dragStart = new Point(start);
251- break;
252-
253- case FAST_TRANSITION:
254- // create transition
255- pto = newTAPNTransition(e.getPoint());
256- getUndoManager().addNewEdit(new AddTimedTransitionCommand((TimedTransitionComponent) pto, model, guiModel, view));
257- app.setMode(ElementType.TAPNARC);
258- pto.getMouseHandler().mouseReleased(e);
259-
260- if (e.isControlDown()) {
261- // connect arc
262- pnObject.dispatchEvent(e);
263- app.setMode(ElementType.TAPNARC);
264- pto.getMouseHandler().mousePressed(e);
265- pto.getMouseHandler().mouseReleased(e);
266- // enter fast mode
267- app.setMode(ElementType.FAST_PLACE);
268- } else{
269- app.endFastMode();
270- }
271- break;
272- case FAST_PLACE:
273- // create place
274- PlaceTransitionObject pto3 = newTimedPlace(e.getPoint());
275- getUndoManager().addNewEdit(new AddTimedPlaceCommand((TimedPlaceComponent) pto3, model, guiModel, view));
276- app.setMode(ElementType.TAPNARC);
277- pto3.getMouseHandler().mouseReleased(e);
278-
279- if (e.isControlDown()) {
280- // connect arc
281- pnObject.dispatchEvent(e);
282- app.setMode(ElementType.TAPNARC);
283- pto3.getMouseHandler().mousePressed(e);
284- pto3.getMouseHandler().mouseReleased(e);
285- // enter fast mode
286- app.setMode(ElementType.FAST_TRANSITION);
287- } else{
288- app.endFastMode();
289- }
290- break;
291- default:
292- break;
293+ case TAPNPLACE:
294+ // create place
295+ newpto = newTimedPlaceAddToModelView(clickPoint);
296+
297+ if (e.isControlDown()) {
298+ continueFastMode(e, newpto, ElementType.FAST_TRANSITION);
299+ }
300+ break;
301+
302+ case TAPNTRANS:
303+ // create transition
304+ newpto = newTAPNTransitionAddToModelView(clickPoint);
305+
306+ if (e.isControlDown()) {
307+ continueFastMode(e, newpto, ElementType.FAST_PLACE);
308+ }
309+ break;
310+
311+ case ANNOTATION:
312+ newAnnotationNoteAddToModelView(clickPoint);
313+ break;
314+
315+ case ARC:
316+ case TAPNARC:
317+ case INHIBARC:
318+ case TRANSPORTARC:
319+ case TAPNINHIBITOR_ARC:
320+ // Add point to arc in creation
321+ if (createArc != null) {
322+ addArcPathPoint(createArc, e);
323+ }
324+ break;
325+
326+
327+ case DRAG:
328+ dragStart = new Point(clickPoint);
329+ break;
330+
331+ case FAST_TRANSITION:
332+ // create transition
333+ newpto = newTAPNTransitionAddToModelView(e.getPoint());
334+
335+ fastDrawAction(e, newpto, ElementType.FAST_PLACE);
336+ break;
337+
338+ case FAST_PLACE:
339+ // create place
340+ newpto = newTimedPlaceAddToModelView(e.getPoint());
341+
342+ fastDrawAction(e, newpto, ElementType.FAST_TRANSITION);
343+ break;
344+
345+ default:
346+ break;
347 }
348 } else {
349 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
350- dragStart = new Point(start);
351+ dragStart = new Point(clickPoint);
352 }
353 updatePreferredSize();
354 }
355
356- private void addPoint(final Arc createArc, final MouseEvent e) {
357+ private void fastDrawAction(MouseEvent e, PlaceTransitionObject newpto, ElementType fastTransition) {
358+ app.setMode(ElementType.TAPNARC);
359+ newpto.getMouseHandler().mouseReleased(e);
360+
361+ if (e.isControlDown()) {
362+ continueFastMode(e, newpto, fastTransition);
363+ } else {
364+ app.endFastMode();
365+ }
366+ }
367+
368+ private void addArcPathPoint(final Arc createArc, final MouseEvent e) {
369 int x = Grid.getModifiedX(e.getX());
370 int y = Grid.getModifiedY(e.getY());
371
372@@ -594,9 +583,7 @@
373 }
374 }
375
376- /**
377- * @see javax.swing.event.MouseInputAdapter#mouseDragged(java.awt.event.MouseEvent)
378- */
379+
380 @Override
381 public void mouseDragged(MouseEvent e) {
382 // if (CreateGui.getApp().getMode() == Pipe.DRAG){

Subscribers

People subscribed via source and target branches

to all changes: