1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.ericmignot.modeler.gui; |
19 |
| |
20 |
| import java.awt.BorderLayout; |
21 |
| import java.awt.Dimension; |
22 |
| import java.awt.Point; |
23 |
| import java.awt.Rectangle; |
24 |
| import java.util.HashMap; |
25 |
| import java.util.List; |
26 |
| import java.util.Map; |
27 |
| |
28 |
| import javax.swing.JOptionPane; |
29 |
| import javax.swing.JPanel; |
30 |
| import javax.swing.JScrollPane; |
31 |
| |
32 |
| import org.ericmignot.modeler.Interoperable; |
33 |
| import org.ericmignot.modeler.data.BusinessObject; |
34 |
| import org.ericmignot.modeler.data.BusinessObjectListener; |
35 |
| import org.ericmignot.modeler.data.Link; |
36 |
| import org.ericmignot.modeler.data.Model; |
37 |
| import org.ericmignot.modeler.data.ModelListener; |
38 |
| import org.ericmignot.modeler.data.SimplePoint; |
39 |
| import org.ericmignot.modeler.gui.historic.HistoricPanel; |
40 |
| import org.ericmignot.modeler.gui.palette.Palette; |
41 |
| import org.ericmignot.modeler.gui.palette.PaletteTool; |
42 |
| import org.ericmignot.modeler.gui.palette.PaletteToolProvider; |
43 |
| import org.ericmignot.modeler.gui.workarea.Item; |
44 |
| import org.ericmignot.modeler.gui.workarea.ItemFactory; |
45 |
| import org.ericmignot.modeler.gui.workarea.LinkItemFactory; |
46 |
| import org.ericmignot.modeler.gui.workarea.PointItemFactory; |
47 |
| import org.ericmignot.modeler.gui.workarea.WorkArea; |
48 |
| import org.ericmignot.modeler.helpers.data.BusinessObjectFactory; |
49 |
| import org.ericmignot.modeler.util.action.ActionException; |
50 |
| import org.ericmignot.modeler.util.action.ActionHistoric; |
51 |
| import org.ericmignot.modeler.util.action.NotifyableAction; |
52 |
| import org.ericmignot.modeler.util.misc.CommandListener; |
53 |
| import org.ericmignot.modeler.util.misc.KeyboardAnalyzer; |
54 |
| import org.ericmignot.modeler.util.xml.XMLHelper; |
55 |
| import org.w3c.dom.Element; |
56 |
| import org.w3c.dom.NodeList; |
57 |
| |
58 |
| public class Controller implements ModelListener, BusinessObjectListener, CommandListener |
59 |
| { |
60 |
| private static final String COMMAND_DEBUG_MODEL = "dm"; |
61 |
| |
62 |
| private Model model; |
63 |
| private Map itemFactories; |
64 |
| |
65 |
| private JScrollPane scrollPanel; |
66 |
| private WorkArea workArea; |
67 |
| private int magneticGridStep; |
68 |
| |
69 |
| private ActionHistoric actionHistoric; |
70 |
| private HistoricPanel historicPanel; |
71 |
| |
72 |
| private PaletteToolProvider paletteToolProvider; |
73 |
| private Palette palette; |
74 |
| |
75 |
| private Interoperable interoperable; |
76 |
| |
77 |
111
| public Controller()
|
78 |
| { |
79 |
111
| this.magneticGridStep = -1;
|
80 |
111
| this.model = this.buildModel();
|
81 |
111
| this.model.register(this);
|
82 |
| |
83 |
111
| this.itemFactories = new HashMap();
|
84 |
111
| this.setItemFactory(SimplePoint.class, new PointItemFactory());
|
85 |
111
| LinkItemFactory linkItemFactory = new LinkItemFactory();
|
86 |
111
| linkItemFactory.setModel(this.model);
|
87 |
111
| this.setItemFactory(Link.class, linkItemFactory);
|
88 |
| |
89 |
111
| this.paletteToolProvider = null;
|
90 |
111
| this.initVisualComponents();
|
91 |
| } |
92 |
| |
93 |
111
| protected Model buildModel()
|
94 |
| { |
95 |
111
| return new Model();
|
96 |
| } |
97 |
| |
98 |
2
| public void setPaletteToolProvider(PaletteToolProvider toolProvider)
|
99 |
| { |
100 |
2
| this.paletteToolProvider = toolProvider;
|
101 |
2
| this.initPalette();
|
102 |
| } |
103 |
118
| protected void initPalette()
|
104 |
| { |
105 |
118
| if (this.paletteToolProvider !=null)
|
106 |
| { |
107 |
2
| PaletteTool[] tools = this.paletteToolProvider.getPaletteTools();
|
108 |
2
| for (int i = 0; i < tools.length; i++)
|
109 |
| { |
110 |
4
| tools[i].setController(this);
|
111 |
4
| this.palette.addPaletteTool(tools[i]);
|
112 |
| } |
113 |
2
| this.palette.selectPaletteTool(tools[0]);
|
114 |
| } |
115 |
| |
116 |
118
| KeyboardAnalyzer keyboardAnalyzer = new KeyboardAnalyzer();
|
117 |
118
| keyboardAnalyzer.setDelay(1000);
|
118 |
118
| keyboardAnalyzer.addCommand(COMMAND_DEBUG_MODEL);
|
119 |
118
| keyboardAnalyzer.addCommandListener(this);
|
120 |
118
| if (this.palette.getSelectedPaletteTool() != null)
|
121 |
| { |
122 |
2
| this.palette.getSelectedPaletteTool().getButton().addKeyListener(keyboardAnalyzer);
|
123 |
| } |
124 |
| } |
125 |
1
| public PaletteToolProvider getPaletteToolProvider()
|
126 |
| { |
127 |
1
| return paletteToolProvider;
|
128 |
| } |
129 |
46
| public Interoperable getInteroperable()
|
130 |
| { |
131 |
46
| return interoperable;
|
132 |
| } |
133 |
| |
134 |
12
| public void setInteroperable(Interoperable interoperable)
|
135 |
| { |
136 |
12
| this.interoperable = interoperable;
|
137 |
| } |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
5
| public void setStaticValues(String xml)
|
146 |
| { |
147 |
5
| Element allStaticValues = XMLHelper.getFirstElementByName(xml, "StaticValues");
|
148 |
5
| if (allStaticValues == null)
|
149 |
| { |
150 |
0
| throw new IllegalArgumentException("StaticValues tag expected");
|
151 |
| } |
152 |
5
| StaticValues.getInstance().init();
|
153 |
5
| NodeList staticValuesList = allStaticValues.getElementsByTagName("StaticValue");
|
154 |
5
| for (int i = 0; i < staticValuesList.getLength(); i ++)
|
155 |
| { |
156 |
14
| Element staticValueElement = (Element) staticValuesList.item(i);
|
157 |
14
| String id = staticValueElement.getAttribute("Id");
|
158 |
14
| String value = staticValueElement.getAttribute("Value");
|
159 |
14
| StaticValues.getInstance().put(id, value);
|
160 |
| } |
161 |
5
| this.initVisualComponents();
|
162 |
| } |
163 |
| |
164 |
116
| public void initVisualComponents()
|
165 |
| { |
166 |
116
| this.historicPanel = new HistoricPanel(this);
|
167 |
116
| this.actionHistoric = new ActionHistoric();
|
168 |
116
| this.workArea = new WorkArea();
|
169 |
116
| this.scrollPanel = new JScrollPane(this.workArea);
|
170 |
116
| this.palette = new Palette(this);
|
171 |
116
| this.initPalette();
|
172 |
| } |
173 |
1
| public ActionHistoric getActionHistoric()
|
174 |
| { |
175 |
1
| return this.actionHistoric;
|
176 |
| } |
177 |
1
| public int getVerticalScrollBarValue()
|
178 |
| { |
179 |
1
| return this.scrollPanel.getVerticalScrollBar().getValue();
|
180 |
| } |
181 |
1
| public int getHorizontalScrollBarValue()
|
182 |
| { |
183 |
1
| return this.scrollPanel.getHorizontalScrollBar().getValue();
|
184 |
| } |
185 |
140
| public Model getModel()
|
186 |
| { |
187 |
140
| return this.model;
|
188 |
| } |
189 |
1
| public JScrollPane getScrollPane()
|
190 |
| { |
191 |
1
| return this.scrollPanel;
|
192 |
| } |
193 |
170
| public WorkArea getWorkArea()
|
194 |
| { |
195 |
170
| return this.workArea;
|
196 |
| } |
197 |
5
| public Palette getPalette()
|
198 |
| { |
199 |
5
| return this.palette;
|
200 |
| } |
201 |
| |
202 |
3
| public void setData(String xml) throws IllegalArgumentException
|
203 |
| { |
204 |
3
| this.model.clearModel();
|
205 |
3
| this.workArea.clearModeler();
|
206 |
3
| this.actionHistoric = new ActionHistoric();
|
207 |
3
| this.addData(xml);
|
208 |
| } |
209 |
| |
210 |
4
| public void addData(String xml) throws IllegalArgumentException
|
211 |
| { |
212 |
4
| Element allBos = XMLHelper.getFirstElementByName(xml, "BusinessObjects");
|
213 |
4
| if (allBos == null)
|
214 |
| { |
215 |
1
| throw new IllegalArgumentException("BusinessObjects tag expected");
|
216 |
| } |
217 |
3
| NodeList boList = allBos.getElementsByTagName("BusinessObject");
|
218 |
3
| for (int i = 0; i < boList.getLength(); i ++)
|
219 |
| { |
220 |
9
| Element boElement = (Element) boList.item(i);
|
221 |
9
| BusinessObject bo = BusinessObjectFactory.getInstance().createBusinessObject(boElement, this.model);
|
222 |
9
| this.model.addBusinessObject(bo);
|
223 |
| } |
224 |
3
| this.updateScrollBarsFromStaticValues();
|
225 |
| } |
226 |
| |
227 |
3
| public void updateScrollBarsFromStaticValues()
|
228 |
| { |
229 |
3
| this.scrollPanel.getVerticalScrollBar().setValue(Integer.parseInt(
|
230 |
| StaticValues.getInstance().get("Design.WorkArea.VerticalScrollBarValue", "0"))); |
231 |
3
| this.scrollPanel.getHorizontalScrollBar().setValue(Integer.parseInt(
|
232 |
| StaticValues.getInstance().get("Design.WorkArea.HorizontalScrollBarValue", "0"))); |
233 |
| } |
234 |
| |
235 |
4
| public JPanel createMainPanel()
|
236 |
| { |
237 |
4
| this.scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
|
238 |
4
| this.scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
239 |
4
| this.scrollPanel.getVerticalScrollBar().setUnitIncrement(20);
|
240 |
4
| this.scrollPanel.getHorizontalScrollBar().setUnitIncrement(20);
|
241 |
4
| JPanel mainPanel = new JPanel(new BorderLayout());
|
242 |
4
| if ("On".equalsIgnoreCase(StaticValues.getInstance().get("Design.Historic.ShowOnOff", "On")))
|
243 |
| { |
244 |
2
| mainPanel.add(this.historicPanel, BorderLayout.WEST);
|
245 |
| } |
246 |
4
| mainPanel.add(this.scrollPanel, BorderLayout.CENTER);
|
247 |
4
| if ("On".equalsIgnoreCase(StaticValues.getInstance().get("Design.Palette.ShowOnOff", "On")))
|
248 |
| { |
249 |
1
| mainPanel.add(this.palette, BorderLayout.NORTH);
|
250 |
| } |
251 |
4
| mainPanel.invalidate();
|
252 |
4
| return mainPanel;
|
253 |
| } |
254 |
| |
255 |
| |
256 |
| |
257 |
1
| public int getItemFactoryCount()
|
258 |
| { |
259 |
1
| return this.itemFactories.size();
|
260 |
| } |
261 |
| |
262 |
3
| public ItemFactory getItemFactory(Class aClass)
|
263 |
| { |
264 |
3
| return (ItemFactory) this.itemFactories.get(aClass);
|
265 |
| } |
266 |
317
| public void setItemFactory(Class aClass, ItemFactory itemFactory)
|
267 |
| { |
268 |
317
| this.itemFactories.put(aClass, itemFactory);
|
269 |
| } |
270 |
| |
271 |
22
| public void treatExecute(NotifyableAction action)
|
272 |
| { |
273 |
22
| try
|
274 |
| { |
275 |
22
| BusinessObject target = (BusinessObject) this.actionHistoric.execute(action, null);
|
276 |
22
| if (this.getInteroperable() != null)
|
277 |
| { |
278 |
10
| this.getInteroperable().fireDoEvent(action, target);
|
279 |
| } |
280 |
| } |
281 |
| catch (ActionException ex) |
282 |
| { |
283 |
0
| ex.printStackTrace();
|
284 |
0
| JOptionPane.showMessageDialog(this.workArea, StaticValues.getInstance().get("Error.ActionFailed", "Action failed"));
|
285 |
| } |
286 |
| } |
287 |
| |
288 |
6
| public void treatUndo()
|
289 |
| { |
290 |
6
| if (this.actionHistoric.canUndo())
|
291 |
| { |
292 |
6
| try
|
293 |
| { |
294 |
6
| NotifyableAction action = (NotifyableAction) this.actionHistoric.getNextUndoAction();
|
295 |
6
| BusinessObject target = (BusinessObject) this.actionHistoric.undo(null);
|
296 |
6
| if (this.getInteroperable() != null)
|
297 |
| { |
298 |
6
| this.getInteroperable().fireUndoEvent(action, target);
|
299 |
| } |
300 |
| } |
301 |
| catch (ActionException e) |
302 |
| { |
303 |
0
| e.printStackTrace();
|
304 |
0
| JOptionPane.showMessageDialog(this.historicPanel, StaticValues.getInstance().get("Error.UndoFailed", "Undo Failed"));
|
305 |
| } |
306 |
| } |
307 |
| } |
308 |
1
| public void treatRedo()
|
309 |
| { |
310 |
1
| if (this.actionHistoric.canRedo())
|
311 |
| { |
312 |
1
| try
|
313 |
| { |
314 |
1
| NotifyableAction action = (NotifyableAction) this.actionHistoric.getNextRedoAction();
|
315 |
1
| BusinessObject target = (BusinessObject) this.actionHistoric.redo(null);
|
316 |
1
| if (this.getInteroperable() != null)
|
317 |
| { |
318 |
1
| this.getInteroperable().fireDoEvent(action, target);
|
319 |
| } |
320 |
| } |
321 |
| catch (ActionException e) |
322 |
| { |
323 |
0
| e.printStackTrace();
|
324 |
0
| JOptionPane.showMessageDialog(this.historicPanel, StaticValues.getInstance().get("Error.RedoFailed", "Redo Failed"));
|
325 |
| } |
326 |
| } |
327 |
| } |
328 |
23
| public void setZoomFactor(double zoom)
|
329 |
| { |
330 |
23
| this.workArea.setZoomFactor(zoom);
|
331 |
| } |
332 |
69
| public double getZoomFactor()
|
333 |
| { |
334 |
69
| return this.workArea.getZoomFactor();
|
335 |
| } |
336 |
1
| public void treatZoomIn()
|
337 |
| { |
338 |
1
| Point center = this.getVisibleCenter();
|
339 |
1
| this.workArea.zoomIn();
|
340 |
1
| this.setVisibleCenter(center);
|
341 |
| } |
342 |
| |
343 |
1
| public void treatZoomOut()
|
344 |
| { |
345 |
1
| Point center = this.getVisibleCenter();
|
346 |
1
| this.workArea.zoomOut();
|
347 |
1
| this.setVisibleCenter(center);
|
348 |
| } |
349 |
2
| public Point getVisibleCenter()
|
350 |
| { |
351 |
2
| Rectangle view = this.scrollPanel.getViewport().getViewRect();
|
352 |
2
| Point origin = view.getLocation();
|
353 |
2
| Dimension size = view.getSize();
|
354 |
2
| double zoomFactor = this.getZoomFactor();
|
355 |
2
| Point center = new Point(
|
356 |
| (int) ((origin.x + size.width/2) / zoomFactor), |
357 |
| (int) ((origin.y + size.height/2) / zoomFactor) |
358 |
| ); |
359 |
2
| return center;
|
360 |
| } |
361 |
| |
362 |
2
| public void setVisibleCenter(Point p)
|
363 |
| { |
364 |
| |
365 |
2
| Dimension size = this.scrollPanel.getViewport().getViewRect().getSize();
|
366 |
2
| double zoomFactor = this.getZoomFactor();
|
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
2
| Point position = new Point(
|
374 |
| (int) (p.x * zoomFactor - size.width/2), |
375 |
| (int) (p.y * zoomFactor - size.height/2) |
376 |
| ); |
377 |
| |
378 |
2
| this.scrollPanel.getVerticalScrollBar().setValue(position.y);
|
379 |
2
| this.scrollPanel.getHorizontalScrollBar().setValue(position.x);
|
380 |
| |
381 |
| |
382 |
| } |
383 |
| |
384 |
0
| public void treatSave()
|
385 |
| { |
386 |
0
| String saveFunction = StaticValues.getInstance().get("Interface.Save", null);
|
387 |
0
| if (saveFunction != null)
|
388 |
| { |
389 |
0
| if (this.getInteroperable() != null)
|
390 |
| { |
391 |
0
| this.getInteroperable().fireEvent(saveFunction);
|
392 |
| } |
393 |
| } |
394 |
| else |
395 |
| { |
396 |
0
| JOptionPane.showMessageDialog(this.historicPanel, StaticValues.getInstance().get("Error.SaveWorkflowInterfaceNotDefined", "The saveWorflowfunction interface is not defined"));
|
397 |
| } |
398 |
| } |
399 |
| |
400 |
30
| public int getMagneticGridStep()
|
401 |
| { |
402 |
30
| if (this.magneticGridStep == -1)
|
403 |
| { |
404 |
27
| String magneticGridStepAsString = StaticValues.getInstance().get("Design.WorkArea.MagneticGridStep", "1");
|
405 |
27
| try
|
406 |
| { |
407 |
27
| this.magneticGridStep = Integer.parseInt(magneticGridStepAsString);
|
408 |
| } |
409 |
| catch (Exception ex) |
410 |
| { |
411 |
0
| this.magneticGridStep = 1;
|
412 |
| } |
413 |
27
| if (this.magneticGridStep < 1)
|
414 |
| { |
415 |
0
| this.magneticGridStep = 1;
|
416 |
| } |
417 |
| } |
418 |
30
| int toReturn = (int) (this.magneticGridStep*this.workArea.getZoomFactor());
|
419 |
30
| return toReturn;
|
420 |
| } |
421 |
| |
422 |
1
| public void treatPaletteToolDeselected(PaletteTool paletteTool)
|
423 |
| { |
424 |
1
| List items = this.workArea.getItems();
|
425 |
1
| for (int i = 0; i < items.size(); i++)
|
426 |
| { |
427 |
3
| Item item = (Item) items.get(i);
|
428 |
3
| if (paletteTool.shouldItemRegister(item))
|
429 |
| { |
430 |
2
| paletteTool.unregisterItem(item);
|
431 |
| } |
432 |
| } |
433 |
1
| if (paletteTool.shouldWorkAreaRegister())
|
434 |
| { |
435 |
0
| this.getWorkArea().removeMouseListener(paletteTool);
|
436 |
0
| this.getWorkArea().removeMouseMotionListener(paletteTool);
|
437 |
| } |
438 |
| } |
439 |
5
| public void treatPaletteToolSelected(PaletteTool paletteTool)
|
440 |
| { |
441 |
5
| List items = this.workArea.getItems();
|
442 |
5
| for (int i = 0; i < items.size(); i++)
|
443 |
| { |
444 |
9
| Item item = (Item) items.get(i);
|
445 |
9
| if (paletteTool.shouldItemRegister(item))
|
446 |
| { |
447 |
4
| paletteTool.unregisterItem(item);
|
448 |
4
| paletteTool.registerItem(item);
|
449 |
| } |
450 |
| } |
451 |
5
| if (paletteTool.shouldWorkAreaRegister())
|
452 |
| { |
453 |
1
| this.getWorkArea().removeMouseListener(paletteTool);
|
454 |
1
| this.getWorkArea().addMouseListener(paletteTool);
|
455 |
1
| this.getWorkArea().removeMouseMotionListener(paletteTool);
|
456 |
1
| this.getWorkArea().addMouseMotionListener(paletteTool);
|
457 |
| } |
458 |
| } |
459 |
| |
460 |
| |
461 |
234
| public void businessObjectAdded(BusinessObject businessObject)
|
462 |
| { |
463 |
234
| ItemFactory itemFactory = (ItemFactory) this.itemFactories.get(businessObject.getClass());
|
464 |
1
| if (itemFactory == null) throw new RuntimeException("ItemFactory not found for " + businessObject.getClass());
|
465 |
233
| Item item = itemFactory.createModelerItems(businessObject, this.workArea);
|
466 |
233
| if (item != null)
|
467 |
| { |
468 |
232
| this.workArea.addItem(item);
|
469 |
232
| businessObject.addListener(this);
|
470 |
| } |
471 |
| |
472 |
233
| if (this.palette.getSelectedPaletteTool() != null)
|
473 |
| { |
474 |
0
| this.treatPaletteToolSelected(this.palette.getSelectedPaletteTool());
|
475 |
| } |
476 |
| } |
477 |
| |
478 |
15
| public void businessObjectRemoved(BusinessObject businessObject)
|
479 |
| { |
480 |
15
| Item item = this.workArea.getItemByBusinessObject(businessObject);
|
481 |
15
| this.workArea.removeItem(item);
|
482 |
15
| businessObject.removeListener(this);
|
483 |
| } |
484 |
| |
485 |
37
| public void businessObjectUpdated(BusinessObject businessObject)
|
486 |
| { |
487 |
37
| this.scrollPanel.revalidate();
|
488 |
| } |
489 |
| |
490 |
0
| public void commandFound(String command)
|
491 |
| { |
492 |
0
| if (COMMAND_DEBUG_MODEL.equalsIgnoreCase(command))
|
493 |
| { |
494 |
0
| if (this.model != null)
|
495 |
| { |
496 |
0
| System.out.println(XMLHelper.formatXML(this.model.getXML()));
|
497 |
| } |
498 |
| } |
499 |
| } |
500 |
| |
501 |
| |
502 |
| |
503 |
| |
504 |
| |
505 |
| } |