|
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.Rectangle; |
|
22 |
| import java.util.ArrayList; |
|
23 |
| import java.util.HashMap; |
|
24 |
| import java.util.List; |
|
25 |
| import java.util.Map; |
|
26 |
| |
|
27 |
| import javax.swing.JPanel; |
|
28 |
| |
|
29 |
| import org.ericmignot.modeler.data.BusinessObject; |
|
30 |
| import org.ericmignot.modeler.gui.StaticValues; |
|
31 |
| import org.ericmignot.modeler.gui.layout.ZoomLayoutManager; |
|
32 |
| import org.ericmignot.modeler.gui.layout.Zoomable; |
|
33 |
| import org.ericmignot.modeler.util.graphic.GraphicHelper; |
|
34 |
| |
|
35 |
| public class WorkArea extends JPanel |
|
36 |
| { |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| private Map categories; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| private ZoomLayoutManager zoomLayoutManager; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
160
| public WorkArea()
|
|
56 |
| { |
|
57 |
160
| this.clearModeler();
|
|
58 |
160
| this.zoomLayoutManager = new ZoomLayoutManager();
|
|
59 |
160
| this.setLayout(this.zoomLayoutManager);
|
|
60 |
160
| this.setBackground(GraphicHelper.buildColor(StaticValues.getInstance().get("Design.WorkArea.BackgroundColor", "236, 233, 216")));
|
|
61 |
| } |
|
62 |
25
| private void fireZoomChanged()
|
|
63 |
| { |
|
64 |
25
| List items = this.getItems();
|
|
65 |
25
| for (int i = 0; i < items.size(); i++)
|
|
66 |
| { |
|
67 |
2
| Item item = (Item) items.get(i);
|
|
68 |
2
| item.zoomChanged();
|
|
69 |
| } |
|
70 |
| } |
|
71 |
23
| public void setZoomFactor(double zoom)
|
|
72 |
| { |
|
73 |
23
| this.zoomLayoutManager.setZoomRatio(zoom);
|
|
74 |
23
| this.invalidate();
|
|
75 |
23
| this.fireZoomChanged();
|
|
76 |
23
| this.doLayout();
|
|
77 |
| } |
|
78 |
1
| public void zoomIn()
|
|
79 |
| { |
|
80 |
1
| this.zoomLayoutManager.zoomIn();
|
|
81 |
1
| this.invalidate();
|
|
82 |
1
| this.fireZoomChanged();
|
|
83 |
1
| this.doLayout();
|
|
84 |
| } |
|
85 |
| |
|
86 |
1
| public void zoomOut()
|
|
87 |
| { |
|
88 |
1
| this.zoomLayoutManager.zoomOut();
|
|
89 |
1
| this.invalidate();
|
|
90 |
1
| this.fireZoomChanged();
|
|
91 |
1
| this.doLayout();
|
|
92 |
| } |
|
93 |
| |
|
94 |
163
| public void clearModeler()
|
|
95 |
| { |
|
96 |
163
| this.categories = new HashMap();
|
|
97 |
163
| this.removeAll();
|
|
98 |
163
| this.invalidate();
|
|
99 |
163
| this.doLayout();
|
|
100 |
163
| this.repaint();
|
|
101 |
| } |
|
102 |
| |
|
103 |
31
| public List getItems()
|
|
104 |
| { |
|
105 |
31
| List toReturn = new ArrayList();
|
|
106 |
31
| for (int i = 0; i < this.getComponentCount(); i++)
|
|
107 |
| { |
|
108 |
14
| if (this.getComponent(i) instanceof Item)
|
|
109 |
| { |
|
110 |
14
| toReturn.add((Item) this.getComponent(i));
|
|
111 |
| } |
|
112 |
| } |
|
113 |
31
| return toReturn;
|
|
114 |
| } |
|
115 |
19
| public Item getItemAt(int x, int y)
|
|
116 |
| { |
|
117 |
19
| Item toReturn = null;
|
|
118 |
30
| for (int i = 0; i < this.getComponentCount(); i++)
|
|
119 |
| { |
|
120 |
30
| Rectangle bounds = this.getComponent(i).getBounds();
|
|
121 |
30
| if ((this.getComponent(i) instanceof Item)
|
|
122 |
| && bounds.contains(x, y)) |
|
123 |
| { |
|
124 |
19
| toReturn = (Item) this.getComponent(i);
|
|
125 |
19
| break;
|
|
126 |
| } |
|
127 |
| } |
|
128 |
19
| return toReturn;
|
|
129 |
| } |
|
130 |
15
| public List getItemsAt(int x, int y)
|
|
131 |
| { |
|
132 |
15
| List toReturn = new ArrayList();
|
|
133 |
15
| for (int i = 0; i < this.getComponentCount(); i++)
|
|
134 |
| { |
|
135 |
46
| Rectangle bounds = this.getComponent(i).getBounds();
|
|
136 |
46
| if ((this.getComponent(i) instanceof Item)
|
|
137 |
| && bounds.contains(x, y)) |
|
138 |
| { |
|
139 |
17
| toReturn.add((Item) this.getComponent(i));
|
|
140 |
| } |
|
141 |
| } |
|
142 |
15
| return toReturn;
|
|
143 |
| } |
|
144 |
| |
|
145 |
334
| public void addItem(Item item)
|
|
146 |
| { |
|
147 |
334
| String category = item.getBusinessObject().getClass().getName();
|
|
148 |
334
| if (this.categories.containsKey(category))
|
|
149 |
| { |
|
150 |
124
| List items = (List) this.categories.get(category);
|
|
151 |
124
| items.add(item);
|
|
152 |
| } |
|
153 |
| else |
|
154 |
| { |
|
155 |
210
| List items = new ArrayList();
|
|
156 |
210
| items.add(item);
|
|
157 |
210
| this.categories.put(category, items);
|
|
158 |
| } |
|
159 |
334
| item.setWorkArea(this);
|
|
160 |
334
| if (item instanceof Zoomable)
|
|
161 |
| { |
|
162 |
334
| ((Zoomable) item).zoomChanged();
|
|
163 |
| } |
|
164 |
334
| this.add(item);
|
|
165 |
334
| this.doLayout();
|
|
166 |
334
| item.doLayout();
|
|
167 |
334
| this.validate();
|
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
15
| public void removeItem(Item item)
|
|
172 |
| { |
|
173 |
15
| String category = item.getBusinessObject().getClass().getName();
|
|
174 |
15
| if (this.categories.containsKey(category))
|
|
175 |
| { |
|
176 |
15
| List items = (List) this.categories.get(category);
|
|
177 |
15
| items.remove(item);
|
|
178 |
| } |
|
179 |
15
| item.setWorkArea(null);
|
|
180 |
15
| this.remove(item);
|
|
181 |
15
| this.repaint();
|
|
182 |
| } |
|
183 |
| |
|
184 |
0
| public int getItemCount(String category)
|
|
185 |
| { |
|
186 |
0
| if (this.categories.containsKey(category))
|
|
187 |
| { |
|
188 |
0
| List items = (List) this.categories.get(category);
|
|
189 |
0
| return items.size();
|
|
190 |
| } |
|
191 |
| else |
|
192 |
| { |
|
193 |
0
| return 0;
|
|
194 |
| } |
|
195 |
| } |
|
196 |
| |
|
197 |
0
| public Item getItemByIndex(String category, int index)
|
|
198 |
| { |
|
199 |
0
| if (this.categories.containsKey(category))
|
|
200 |
| { |
|
201 |
0
| List items = (List) this.categories.get(category);
|
|
202 |
0
| return (Item) items.get(index);
|
|
203 |
| } |
|
204 |
| else |
|
205 |
| { |
|
206 |
0
| return null;
|
|
207 |
| } |
|
208 |
| } |
|
209 |
| |
|
210 |
266
| public Item getItemByBusinessObject(BusinessObject bo)
|
|
211 |
| { |
|
212 |
266
| String category = bo.getClass().getName();
|
|
213 |
266
| if (this.categories.containsKey(category))
|
|
214 |
| { |
|
215 |
258
| List items = (List) this.categories.get(category);
|
|
216 |
258
| Item toReturn = null;
|
|
217 |
371
| for (int i = 0; i < items.size(); i ++)
|
|
218 |
| { |
|
219 |
371
| Item current = (Item) items.get(i);
|
|
220 |
371
| if (current.getBusinessObject() == bo)
|
|
221 |
| { |
|
222 |
258
| toReturn = current;
|
|
223 |
258
| break;
|
|
224 |
| } |
|
225 |
| } |
|
226 |
258
| return toReturn;
|
|
227 |
| } |
|
228 |
| else |
|
229 |
| { |
|
230 |
8
| return null;
|
|
231 |
| } |
|
232 |
| } |
|
233 |
| |
|
234 |
0
| public Dimension getPreferredSize()
|
|
235 |
| { |
|
236 |
0
| int width = 0;
|
|
237 |
0
| int height = 0;
|
|
238 |
0
| for (int i = 0; i < this.getComponentCount(); i++)
|
|
239 |
| { |
|
240 |
0
| width = Math.max(width, this.getComponent(i).getX() + this.getComponent(i).getWidth());
|
|
241 |
0
| height = Math.max(height, this.getComponent(i).getY() + this.getComponent(i).getHeight());
|
|
242 |
| } |
|
243 |
0
| return new Dimension(width, height);
|
|
244 |
| } |
|
245 |
| |
|
246 |
193
| public double getZoomFactor()
|
|
247 |
| { |
|
248 |
193
| return this.zoomLayoutManager.getZoomRatio();
|
|
249 |
| } |
|
250 |
| } |