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 |
| import java.awt.Rectangle; |
22 |
| import java.awt.event.MouseEvent; |
23 |
| |
24 |
| import javax.swing.ImageIcon; |
25 |
| import javax.swing.JToggleButton; |
26 |
| |
27 |
| import org.ericmignot.modeler.action.CreationAction; |
28 |
| import org.ericmignot.modeler.data.BusinessObject; |
29 |
| import org.ericmignot.modeler.data.Link; |
30 |
| import org.ericmignot.modeler.gui.StaticValues; |
31 |
| import org.ericmignot.modeler.gui.workarea.Item; |
32 |
| import org.ericmignot.modeler.gui.workarea.LinkItem; |
33 |
| import org.ericmignot.modeler.gui.workarea.Linkable; |
34 |
| import org.ericmignot.modeler.helpers.data.DefaultBusinessObject; |
35 |
| import org.ericmignot.modeler.helpers.gui.workarea.DefaultItem; |
36 |
| import org.ericmignot.modeler.util.action.NotifyableAction; |
37 |
| |
38 |
| public class LinkCreationTool extends PaletteTool |
39 |
| { |
40 |
| protected JToggleButton button; |
41 |
| |
42 |
| private Item startItem; |
43 |
| private Item endItem; |
44 |
| |
45 |
| private boolean ghostVisible; |
46 |
| private DefaultBusinessObject ghostBo; |
47 |
| private Link ghostLink; |
48 |
| private DefaultItem ghostItem; |
49 |
| private LinkItem ghostLinkItem; |
50 |
| |
51 |
14
| public LinkCreationTool()
|
52 |
| { |
53 |
14
| this.ghostVisible = false;
|
54 |
14
| this.reset();
|
55 |
14
| this.button = new JToggleButton(
|
56 |
| StaticValues.getInstance().get("Design.Palette.Link", "Link"), |
57 |
| new ImageIcon(this.getClass().getClassLoader().getResource( |
58 |
| "org/ericmignot/modeler/gui/palette/link.gif"))); |
59 |
14
| this.button.setToolTipText(StaticValues.getInstance().get("Design.Palette.LinkTooltip", "Link creation tool"));
|
60 |
| |
61 |
| } |
62 |
| |
63 |
17
| private void reset()
|
64 |
| { |
65 |
17
| this.startItem = null;
|
66 |
17
| this.endItem = null;
|
67 |
17
| if (this.ghostVisible
|
68 |
| && this.getController()!=null |
69 |
| && this.getController().getWorkArea() != null) |
70 |
| { |
71 |
0
| this.getController().getWorkArea().remove(this.ghostItem);
|
72 |
0
| this.getController().getWorkArea().remove(this.ghostLinkItem);
|
73 |
0
| this.getController().getWorkArea().repaint();
|
74 |
0
| this.ghostVisible = false;
|
75 |
| } |
76 |
17
| this.ghostBo = null;
|
77 |
17
| this.ghostLink = null;
|
78 |
17
| this.ghostItem = null;
|
79 |
17
| this.ghostLinkItem = null;
|
80 |
| } |
81 |
8
| public void setStartItem(Item item)
|
82 |
| { |
83 |
8
| this.startItem = item;
|
84 |
| } |
85 |
46
| public Item getStartItem()
|
86 |
| { |
87 |
46
| return this.startItem;
|
88 |
| } |
89 |
| |
90 |
3
| public void setEndItem(Item item)
|
91 |
| { |
92 |
3
| this.endItem = item;
|
93 |
| } |
94 |
19
| public Item getEndItem()
|
95 |
| { |
96 |
19
| return this.endItem;
|
97 |
| } |
98 |
| |
99 |
2
| public JToggleButton getButton()
|
100 |
| { |
101 |
2
| return this.button;
|
102 |
| } |
103 |
| |
104 |
2
| public boolean shouldItemRegister(Item item)
|
105 |
| { |
106 |
2
| return false;
|
107 |
| } |
108 |
| |
109 |
1
| public boolean shouldWorkAreaRegister()
|
110 |
| { |
111 |
1
| return true;
|
112 |
| } |
113 |
| |
114 |
0
| public void registerItem(Item item)
|
115 |
| { |
116 |
| } |
117 |
| |
118 |
0
| public void unregisterItem(Item item)
|
119 |
| { |
120 |
| } |
121 |
| |
122 |
0
| public void mousePressed(MouseEvent e){}
|
123 |
0
| public void itemPressed(Item item, int x, int y) {}
|
124 |
| |
125 |
0
| public void itemDragged(int x, int y){}
|
126 |
| |
127 |
0
| public NotifyableAction manageReleased() {return null;}
|
128 |
0
| public void mouseReleased(MouseEvent e)
|
129 |
| { |
130 |
0
| if (e.isConsumed()) return;
|
131 |
0
| NotifyableAction action = this.workAreaReleased(e.getX(), e.getY());
|
132 |
0
| if (action != null)
|
133 |
| { |
134 |
0
| action.setDoEvent(this.getDoEvent());
|
135 |
0
| action.setUndoEvent(this.getUndoEvent());
|
136 |
0
| if (this.getController() != null)
|
137 |
| { |
138 |
0
| this.getController().treatExecute(action);
|
139 |
| } |
140 |
| } |
141 |
0
| e.consume();
|
142 |
| } |
143 |
10
| public NotifyableAction workAreaReleased(int x, int y)
|
144 |
| { |
145 |
10
| Item item = this.getItemUnder(x, y);
|
146 |
10
| if (item != null && item instanceof Linkable && item!=this.ghostItem)
|
147 |
| { |
148 |
10
| if (this.getStartItem() == null)
|
149 |
| { |
150 |
7
| this.setStartItem(item);
|
151 |
7
| return null;
|
152 |
| } |
153 |
| else |
154 |
| { |
155 |
3
| if (item == this.getStartItem())
|
156 |
| { |
157 |
1
| this.reset();
|
158 |
1
| return null;
|
159 |
| } |
160 |
| else |
161 |
| { |
162 |
2
| this.setEndItem(item);
|
163 |
2
| CreationAction action = new CreationAction(
|
164 |
| this.getController().getModel(), this.buildBusinessObject()); |
165 |
2
| this.reset();
|
166 |
2
| return action;
|
167 |
| } |
168 |
| } |
169 |
| } |
170 |
0
| return null;
|
171 |
| } |
172 |
| |
173 |
| |
174 |
0
| protected void buildGhostsIfNeeded()
|
175 |
| { |
176 |
0
| if (this.ghostBo == null)
|
177 |
| { |
178 |
0
| this.ghostBo = new DefaultBusinessObject();
|
179 |
| } |
180 |
0
| if (this.ghostItem == null)
|
181 |
| { |
182 |
0
| this.ghostItem = new DefaultItem();
|
183 |
0
| this.ghostItem.setBusinessObject(this.ghostBo);
|
184 |
0
| this.ghostItem.setSize(1, 1);
|
185 |
0
| this.getController().getWorkArea().addItem(this.ghostItem);
|
186 |
| } |
187 |
0
| if (this.ghostLink == null)
|
188 |
| { |
189 |
0
| this.ghostLink = new Link();
|
190 |
0
| this.ghostLink.setLinkStart(this.getStartItem().getBusinessObject());
|
191 |
0
| this.ghostLink.setLinkEnd(this.ghostBo);
|
192 |
| } |
193 |
0
| if (this.ghostLinkItem == null)
|
194 |
| { |
195 |
0
| this.ghostLinkItem = new LinkItem();
|
196 |
0
| this.ghostLinkItem.setLinkStart(this.getStartItem());
|
197 |
0
| this.ghostLinkItem.setLinkEnd(this.ghostItem);
|
198 |
0
| this.ghostLinkItem.setBusinessObject(this.ghostLink);
|
199 |
0
| this.getController().getWorkArea().addItem(this.ghostLinkItem);
|
200 |
| } |
201 |
0
| this.ghostVisible = true;
|
202 |
| } |
203 |
0
| public void mouseMoved(MouseEvent e)
|
204 |
| { |
205 |
0
| if (e.isConsumed()) return;
|
206 |
0
| Point p = this.workAreaMoved(e.getX(), e.getY());
|
207 |
0
| if (p != null)
|
208 |
| { |
209 |
0
| this.buildGhostsIfNeeded();
|
210 |
0
| this.ghostBo.setCenter(p);
|
211 |
| } |
212 |
0
| e.consume();
|
213 |
| } |
214 |
13
| public Point workAreaMoved(int x, int y)
|
215 |
| { |
216 |
13
| if (this.getStartItem() != null && this.getEndItem()==null)
|
217 |
| { |
218 |
8
| Rectangle bounds = this.getStartItem().getBounds();
|
219 |
8
| if (bounds.contains(x, y))
|
220 |
| { |
221 |
5
| return null;
|
222 |
| } |
223 |
| else |
224 |
| { |
225 |
3
| double zoomFactor = this.getController().getZoomFactor();
|
226 |
3
| return new Point((int) (x/zoomFactor), (int) (y/zoomFactor));
|
227 |
| } |
228 |
| } |
229 |
5
| return null;
|
230 |
| } |
231 |
| |
232 |
4
| public BusinessObject buildBusinessObject()
|
233 |
| { |
234 |
4
| if (this.getStartItem()!= null && this.getEndItem()!=null)
|
235 |
| { |
236 |
3
| Link link = new Link();
|
237 |
3
| link.setLinkStart(this.getStartItem().getBusinessObject());
|
238 |
3
| link.setLinkEnd(this.getEndItem().getBusinessObject());
|
239 |
3
| return link;
|
240 |
| } |
241 |
| else |
242 |
| { |
243 |
1
| return null;
|
244 |
| } |
245 |
| } |
246 |
| } |