1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| package org.ericmignot.modeler.gui.palette; |
20 |
| |
21 |
| import java.awt.Point; |
22 |
| import java.awt.event.MouseEvent; |
23 |
| |
24 |
| import org.ericmignot.modeler.util.action.ActionException; |
25 |
| import org.ericmignot.modeler.util.action.NotifyableAction; |
26 |
| import org.ericmignot.modeler.util.graphic.MagneticGridMover; |
27 |
| |
28 |
| public abstract class DragAndDropPaletteTool extends ItemPaletteTool |
29 |
| { |
30 |
| |
31 |
| protected MagneticGridMover magneticGridMover; |
32 |
| protected int xPressed; |
33 |
| protected int yPressed; |
34 |
| protected NotifyableAction mousePressedAction; |
35 |
| |
36 |
0
| public void mousePressed(MouseEvent e)
|
37 |
| { |
38 |
0
| if (e.isConsumed()) return;
|
39 |
0
| this.mousePressed(e.getX(), e.getY());
|
40 |
0
| e.consume();
|
41 |
| } |
42 |
7
| protected void mousePressed(int x, int y)
|
43 |
| { |
44 |
7
| this.xPressed = x;
|
45 |
7
| this.yPressed = y;
|
46 |
7
| this.targetedItem = null;
|
47 |
7
| this.mousePressedAction = this.workAreaReleased(this.xPressed, this.yPressed);
|
48 |
7
| if (this.mousePressedAction != null)
|
49 |
| { |
50 |
6
| this.magneticGridMover = new MagneticGridMover(this.getController().getMagneticGridStep());
|
51 |
6
| this.magneticGridMover.setCandidatPointToMove(new Point(this.xPressed, this.yPressed));
|
52 |
6
| this.magneticGridMover.setInitialEvent(this.xPressed, this.yPressed);
|
53 |
6
| this.mousePressedImpl();
|
54 |
| } |
55 |
| } |
56 |
| protected abstract void mousePressedImpl(); |
57 |
| |
58 |
| |
59 |
0
| public void mouseDragged(MouseEvent e)
|
60 |
| { |
61 |
0
| if (e.isConsumed()) return;
|
62 |
0
| this.mouseDragged(e.getX(), e.getY());
|
63 |
0
| e.consume();
|
64 |
| } |
65 |
6
| protected void mouseDragged(int x, int y)
|
66 |
| { |
67 |
6
| if (this.targetedItem != null)
|
68 |
| { |
69 |
6
| Point p = this.magneticGridMover.getNewPositionFromMouseDraggedEvent(x, y);
|
70 |
6
| if (p != null)
|
71 |
| { |
72 |
4
| Point delta = computeDelta(new Point(this.xPressed, this.yPressed), new Point(x, y));
|
73 |
4
| this.mouseDraggedImpl(delta);
|
74 |
4
| this.xPressed = x;
|
75 |
4
| this.yPressed = y;
|
76 |
4
| this.magneticGridMover.setCandidatPointToMove(p);
|
77 |
4
| this.magneticGridMover.setInitialEvent(p);
|
78 |
| } |
79 |
| } |
80 |
| } |
81 |
4
| protected Point computeDelta(Point start, Point end)
|
82 |
| { |
83 |
4
| Point delta = new Point(end.x-start.x, end.y-start.y);
|
84 |
4
| double zoom = this.getController().getZoomFactor();
|
85 |
4
| delta.x = (int)(delta.x / zoom);
|
86 |
4
| delta.y = (int)(delta.y / zoom);
|
87 |
4
| return delta;
|
88 |
| } |
89 |
| protected abstract void mouseDraggedImpl(Point delta); |
90 |
| |
91 |
| |
92 |
| |
93 |
0
| public void mouseReleased(MouseEvent e)
|
94 |
| { |
95 |
0
| if (e.isConsumed()) return;
|
96 |
0
| this.mouseReleased();
|
97 |
0
| e.consume();
|
98 |
| } |
99 |
1
| public void mouseReleased()
|
100 |
| { |
101 |
1
| if (this.mousePressedAction != null)
|
102 |
| { |
103 |
1
| try {
|
104 |
1
| this.mousePressedAction.cancel(null);
|
105 |
| } catch (ActionException e) { |
106 |
0
| e.printStackTrace();
|
107 |
| } |
108 |
1
| if (this.getController() != null)
|
109 |
| { |
110 |
1
| this.getController().treatExecute(mousePressedAction);
|
111 |
| } |
112 |
1
| this.fireItemClicked();
|
113 |
| } |
114 |
| } |
115 |
| |
116 |
| |
117 |
| } |