1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.ericmignot.modeler.gui.palette; |
19 |
| |
20 |
| import java.awt.Point; |
21 |
| |
22 |
| import javax.swing.ImageIcon; |
23 |
| import javax.swing.JToggleButton; |
24 |
| |
25 |
| import org.ericmignot.modeler.action.MoveAction; |
26 |
| import org.ericmignot.modeler.gui.StaticValues; |
27 |
| import org.ericmignot.modeler.gui.workarea.Item; |
28 |
| import org.ericmignot.modeler.gui.workarea.Movable; |
29 |
| import org.ericmignot.modeler.util.action.ActionException; |
30 |
| import org.ericmignot.modeler.util.action.NotifyableAction; |
31 |
| import org.ericmignot.modeler.util.graphic.GraphicHelper; |
32 |
| import org.ericmignot.modeler.util.graphic.MagneticGridMover; |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class MoveTool extends PaletteTool |
37 |
| { |
38 |
| private JToggleButton button; |
39 |
| |
40 |
| private MagneticGridMover magneticGridMover; |
41 |
| |
42 |
| private Point initialReferencePoint; |
43 |
| private Point newReferencePoint; |
44 |
| |
45 |
32
| public MoveTool()
|
46 |
| { |
47 |
32
| this.reset();
|
48 |
| } |
49 |
| |
50 |
16
| public JToggleButton getButton()
|
51 |
| { |
52 |
16
| if (this.button == null)
|
53 |
| { |
54 |
6
| this.button = new JToggleButton(
|
55 |
| StaticValues.getInstance().get("Design.Palette.Move", "Move"), |
56 |
| new ImageIcon(this.getClass().getClassLoader().getResource( |
57 |
| "org/ericmignot/modeler/gui/palette/pointer.gif"))); |
58 |
6
| this.button.setToolTipText(StaticValues.getInstance().get("Design.Palette.MoveTooltip", "Move tool"));
|
59 |
| } |
60 |
16
| return this.button;
|
61 |
| } |
62 |
| |
63 |
6
| public boolean shouldWorkAreaRegister()
|
64 |
| { |
65 |
6
| return false;
|
66 |
| } |
67 |
| |
68 |
18
| public boolean shouldItemRegister(Item item)
|
69 |
| { |
70 |
18
| if (item instanceof Movable)
|
71 |
| { |
72 |
12
| return true;
|
73 |
| } |
74 |
| else |
75 |
| { |
76 |
6
| return false;
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
4
| public void registerItem(Item item)
|
81 |
| { |
82 |
4
| item.addMouseListener(this);
|
83 |
4
| item.addMouseMotionListener(this);
|
84 |
| } |
85 |
6
| public void unregisterItem(Item item)
|
86 |
| { |
87 |
6
| item.removeMouseListener(this);
|
88 |
6
| item.removeMouseMotionListener(this);
|
89 |
| } |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
0
| public Item getTargetedItem()
|
95 |
| { |
96 |
0
| return this.targetedItem;
|
97 |
| } |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
32
| private void reset()
|
104 |
| { |
105 |
32
| this.targetedItem = null;
|
106 |
32
| this.initialReferencePoint = null;
|
107 |
32
| this.newReferencePoint = null;
|
108 |
| } |
109 |
| |
110 |
24
| public void itemPressed(Item item, int x, int y)
|
111 |
| { |
112 |
24
| this.targetedItem = item;
|
113 |
24
| this.initialReferencePoint = GraphicHelper.getPanelCenter(item);
|
114 |
24
| this.newReferencePoint = null;
|
115 |
| |
116 |
24
| this.magneticGridMover = new MagneticGridMover(this.getController().getMagneticGridStep());
|
117 |
24
| this.magneticGridMover.setCandidatPointToMove(this.initialReferencePoint);
|
118 |
24
| this.magneticGridMover.setInitialEvent(x, y);
|
119 |
| } |
120 |
| |
121 |
25
| public void itemDragged(int x, int y)
|
122 |
| { |
123 |
25
| Point p = this.magneticGridMover.getNewPositionFromMouseDraggedEvent(x, y);
|
124 |
25
| if (p != null)
|
125 |
| { |
126 |
0
| if (!isDragAuthorized(p)) return;
|
127 |
19
| this.newReferencePoint = p;
|
128 |
19
| Point currentPosition = this.magneticGridMover.getCandidatPointToMove();
|
129 |
19
| if (this.newReferencePoint.distance(currentPosition) > 0)
|
130 |
| { |
131 |
19
| Point delta = computeDelta(currentPosition, this.newReferencePoint);
|
132 |
19
| MoveAction action = new MoveAction(this.targetedItem.getBusinessObject(), delta);
|
133 |
19
| try {
|
134 |
19
| action.execute(null);
|
135 |
| } catch (ActionException e) { |
136 |
0
| e.printStackTrace();
|
137 |
| } |
138 |
| } |
139 |
19
| this.magneticGridMover.setCandidatPointToMove(this.newReferencePoint);
|
140 |
| } |
141 |
| } |
142 |
| |
143 |
24
| public NotifyableAction manageReleased()
|
144 |
| { |
145 |
24
| if (this.newReferencePoint != null)
|
146 |
| { |
147 |
18
| Point deltaToInitial = computeDelta(this.newReferencePoint, this.initialReferencePoint);
|
148 |
18
| MoveAction actionToInitial = new MoveAction(this.targetedItem.getBusinessObject(), deltaToInitial);
|
149 |
18
| try
|
150 |
| { |
151 |
18
| actionToInitial.execute(null);
|
152 |
18
| return this.getAction();
|
153 |
| } |
154 |
| catch (ActionException e) |
155 |
| { |
156 |
0
| e.printStackTrace();
|
157 |
| } |
158 |
| } |
159 |
6
| return null;
|
160 |
| } |
161 |
| |
162 |
19
| protected boolean isDragAuthorized(Point newPoint)
|
163 |
| { |
164 |
19
| return true;
|
165 |
| } |
166 |
| |
167 |
55
| private Point computeDelta(Point start, Point end)
|
168 |
| { |
169 |
55
| Point delta = new Point(end.x-start.x, end.y-start.y);
|
170 |
55
| double zoom = this.getController().getZoomFactor();
|
171 |
55
| delta.x = (int)(delta.x / zoom);
|
172 |
55
| delta.y = (int)(delta.y / zoom);
|
173 |
55
| return delta;
|
174 |
| } |
175 |
| |
176 |
18
| private NotifyableAction getAction()
|
177 |
| { |
178 |
18
| if (this.newReferencePoint != null
|
179 |
| && this.newReferencePoint.distance(this.initialReferencePoint) > 0) |
180 |
| { |
181 |
18
| Point delta = computeDelta(this.initialReferencePoint, this.newReferencePoint);
|
182 |
18
| MoveAction action = new MoveAction(this.targetedItem.getBusinessObject(), delta);
|
183 |
18
| return action;
|
184 |
| } |
185 |
| else |
186 |
| { |
187 |
0
| return null;
|
188 |
| } |
189 |
| } |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| } |