|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.ericmignot.modeler.gui.workarea; |
|
19 |
| |
|
20 |
| import java.awt.Dimension; |
|
21 |
| import java.awt.Point; |
|
22 |
| |
|
23 |
| import javax.swing.JPanel; |
|
24 |
| |
|
25 |
| import org.ericmignot.modeler.data.BusinessObject; |
|
26 |
| import org.ericmignot.modeler.data.BusinessObjectListener; |
|
27 |
| import org.ericmignot.modeler.gui.layout.Zoomable; |
|
28 |
| |
|
29 |
| |
|
30 |
| public abstract class Item extends JPanel implements Zoomable, BusinessObjectListener |
|
31 |
| { |
|
32 |
| private Class expectedBusinessObjectClass; |
|
33 |
| private BusinessObject businessObject; |
|
34 |
| private WorkArea workArea; |
|
35 |
| |
|
36 |
362
| protected Item()
|
|
37 |
| { |
|
38 |
362
| this.setExpectedBusinessObjectClass (BusinessObject.class);
|
|
39 |
362
| this.businessObject = null;
|
|
40 |
| } |
|
41 |
| |
|
42 |
5852
| public BusinessObject getBusinessObject()
|
|
43 |
| { |
|
44 |
5852
| return businessObject;
|
|
45 |
| } |
|
46 |
| |
|
47 |
725
| public Class getExpectedBusinessObjectClass()
|
|
48 |
| { |
|
49 |
725
| return this.expectedBusinessObjectClass;
|
|
50 |
| } |
|
51 |
366
| public void setExpectedBusinessObjectClass(Class aClass)
|
|
52 |
| { |
|
53 |
366
| if (this.businessObject != null && aClass!=null)
|
|
54 |
| { |
|
55 |
3
| if (! (aClass.isAssignableFrom(this.businessObject.getClass())))
|
|
56 |
| { |
|
57 |
1
| throw new IllegalArgumentException();
|
|
58 |
| } |
|
59 |
| } |
|
60 |
365
| this.expectedBusinessObjectClass = aClass;
|
|
61 |
| } |
|
62 |
| |
|
63 |
361
| protected void checkSetBusinessObjectAllowed(BusinessObject businessObject)
|
|
64 |
| { |
|
65 |
361
| if (businessObject != null && this.getExpectedBusinessObjectClass()!=null)
|
|
66 |
| { |
|
67 |
361
| if (! (this.getExpectedBusinessObjectClass().isAssignableFrom(businessObject.getClass())))
|
|
68 |
| { |
|
69 |
1
| throw new IllegalArgumentException();
|
|
70 |
| } |
|
71 |
| } |
|
72 |
| } |
|
73 |
| |
|
74 |
361
| public void setBusinessObject(BusinessObject businessObject)
|
|
75 |
| { |
|
76 |
361
| this.checkSetBusinessObjectAllowed(businessObject);
|
|
77 |
360
| if (this.businessObject != null)
|
|
78 |
| { |
|
79 |
2
| this.businessObject.removeListener(this);
|
|
80 |
| } |
|
81 |
360
| this.businessObject = businessObject;
|
|
82 |
360
| if (this.businessObject != null)
|
|
83 |
| { |
|
84 |
360
| this.businessObject.addListener(this);
|
|
85 |
| } |
|
86 |
360
| this.businessObjectUpdated(businessObject);
|
|
87 |
| } |
|
88 |
| |
|
89 |
936
| public Point getZoomedLocation(double zoomFactor)
|
|
90 |
| { |
|
91 |
936
| Dimension halfSize = this.businessObject.getHalfSize();
|
|
92 |
936
| Point center = this.businessObject.getCenter();
|
|
93 |
936
| return new Point (
|
|
94 |
| (int) (zoomFactor*(center.x-halfSize.width)), |
|
95 |
| (int) (zoomFactor*(center.y-halfSize.height))); |
|
96 |
| } |
|
97 |
| |
|
98 |
936
| public Dimension getZoomedSize(double zoomFactor)
|
|
99 |
| { |
|
100 |
936
| Dimension halfSize = this.businessObject.getHalfSize();
|
|
101 |
936
| int width = (int) (1+2*halfSize.width*zoomFactor);
|
|
102 |
936
| int height = (int) (1+2*halfSize.height*zoomFactor);
|
|
103 |
936
| return new Dimension (width, height);
|
|
104 |
| } |
|
105 |
| |
|
106 |
754
| public WorkArea getWorkArea()
|
|
107 |
| { |
|
108 |
754
| return workArea;
|
|
109 |
| } |
|
110 |
| |
|
111 |
349
| public void setWorkArea(WorkArea workArea)
|
|
112 |
| { |
|
113 |
349
| this.workArea = workArea;
|
|
114 |
| } |
|
115 |
| |
|
116 |
521
| final public void businessObjectUpdated(BusinessObject businessObject)
|
|
117 |
| { |
|
118 |
521
| this.businessObjectUpdatedImpl(businessObject);
|
|
119 |
521
| if (this.getWorkArea()!=null)
|
|
120 |
| { |
|
121 |
129
| this.getWorkArea().doLayout();
|
|
122 |
| } |
|
123 |
| } |
|
124 |
| protected abstract void businessObjectUpdatedImpl(BusinessObject businessObject); |
|
125 |
| } |