1 | /* |
2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.resource.edit.parts; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.List; |
8 | |
9 | import org.eclipse.draw2d.FlowLayout; |
10 | import org.eclipse.draw2d.Graphics; |
11 | import org.eclipse.draw2d.GridData; |
12 | import org.eclipse.draw2d.GridLayout; |
13 | import org.eclipse.draw2d.IFigure; |
14 | import org.eclipse.draw2d.MarginBorder; |
15 | import org.eclipse.draw2d.RectangleFigure; |
16 | import org.eclipse.draw2d.Shape; |
17 | import org.eclipse.draw2d.StackLayout; |
18 | import org.eclipse.draw2d.geometry.Dimension; |
19 | import org.eclipse.draw2d.geometry.Point; |
20 | import org.eclipse.draw2d.geometry.PointList; |
21 | import org.eclipse.draw2d.geometry.Rectangle; |
22 | import org.eclipse.gef.EditPart; |
23 | import org.eclipse.gef.EditPolicy; |
24 | import org.eclipse.gef.Request; |
25 | import org.eclipse.gef.commands.Command; |
26 | import org.eclipse.gef.editpolicies.LayoutEditPolicy; |
27 | import org.eclipse.gef.editpolicies.NonResizableEditPolicy; |
28 | import org.eclipse.gef.requests.CreateRequest; |
29 | import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter; |
30 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
31 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart; |
32 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy; |
33 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
34 | import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest; |
35 | import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout; |
36 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; |
37 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
38 | import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure; |
39 | import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure; |
40 | import org.eclipse.gmf.runtime.notation.View; |
41 | import org.eclipse.swt.graphics.Color; |
42 | |
43 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.policies.ResourceContainerItemSemanticEditPolicy; |
44 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelVisualIDRegistry; |
45 | import de.uka.ipd.sdq.pcm.gmf.resource.providers.PalladioComponentModelElementTypes; |
46 | |
47 | /** |
48 | * @generated |
49 | */ |
50 | public class ResourceContainerEditPart extends ShapeNodeEditPart { |
51 | |
52 | /** |
53 | * @generated |
54 | */ |
55 | public static final int VISUAL_ID = 2001; |
56 | |
57 | /** |
58 | * @generated |
59 | */ |
60 | protected IFigure contentPane; |
61 | |
62 | /** |
63 | * @generated |
64 | */ |
65 | protected IFigure primaryShape; |
66 | |
67 | /** |
68 | * @generated |
69 | */ |
70 | public ResourceContainerEditPart(View view) { |
71 | super(view); |
72 | } |
73 | |
74 | /** |
75 | * @generated |
76 | */ |
77 | protected void createDefaultEditPolicies() { |
78 | installEditPolicy(EditPolicyRoles.CREATION_ROLE, |
79 | new CreationEditPolicy()); |
80 | super.createDefaultEditPolicies(); |
81 | installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, |
82 | new ResourceContainerItemSemanticEditPolicy()); |
83 | installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy()); |
84 | // XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies |
85 | // removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE); |
86 | } |
87 | |
88 | /** |
89 | * @generated |
90 | */ |
91 | protected LayoutEditPolicy createLayoutEditPolicy() { |
92 | LayoutEditPolicy lep = new LayoutEditPolicy() { |
93 | |
94 | protected EditPolicy createChildEditPolicy(EditPart child) { |
95 | EditPolicy result = child |
96 | .getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
97 | if (result == null) { |
98 | result = new NonResizableEditPolicy(); |
99 | } |
100 | return result; |
101 | } |
102 | |
103 | protected Command getMoveChildrenCommand(Request request) { |
104 | return null; |
105 | } |
106 | |
107 | protected Command getCreateCommand(CreateRequest request) { |
108 | return null; |
109 | } |
110 | }; |
111 | return lep; |
112 | } |
113 | |
114 | /** |
115 | * @generated |
116 | */ |
117 | protected IFigure createNodeShape() { |
118 | ResourceContainerFigure figure = new ResourceContainerFigure(); |
119 | return primaryShape = figure; |
120 | } |
121 | |
122 | /** |
123 | * @generated |
124 | */ |
125 | public ResourceContainerFigure getPrimaryShape() { |
126 | return (ResourceContainerFigure) primaryShape; |
127 | } |
128 | |
129 | /** |
130 | * @generated |
131 | */ |
132 | protected boolean addFixedChild(EditPart childEditPart) { |
133 | if (childEditPart instanceof ResourceContainerEntityNameEditPart) { |
134 | ((ResourceContainerEntityNameEditPart) childEditPart) |
135 | .setLabel(getPrimaryShape() |
136 | .getFigureResourceContainerNameFigure()); |
137 | return true; |
138 | } |
139 | if (childEditPart instanceof ResourceContainerResourceContainerCompartmentEditPart) { |
140 | IFigure pane = getPrimaryShape() |
141 | .getFigureResourceContainerCompartmentFigure(); |
142 | setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way |
143 | pane |
144 | .add(((ResourceContainerResourceContainerCompartmentEditPart) childEditPart) |
145 | .getFigure()); |
146 | return true; |
147 | } |
148 | return false; |
149 | } |
150 | |
151 | /** |
152 | * @generated |
153 | */ |
154 | protected boolean removeFixedChild(EditPart childEditPart) { |
155 | if (childEditPart instanceof ResourceContainerEntityNameEditPart) { |
156 | return true; |
157 | } |
158 | if (childEditPart instanceof ResourceContainerResourceContainerCompartmentEditPart) { |
159 | IFigure pane = getPrimaryShape() |
160 | .getFigureResourceContainerCompartmentFigure(); |
161 | setupContentPane(pane); // FIXME each comparment should handle his content pane in his own way |
162 | pane |
163 | .remove(((ResourceContainerResourceContainerCompartmentEditPart) childEditPart) |
164 | .getFigure()); |
165 | return true; |
166 | } |
167 | return false; |
168 | } |
169 | |
170 | /** |
171 | * @generated |
172 | */ |
173 | protected void addChildVisual(EditPart childEditPart, int index) { |
174 | if (addFixedChild(childEditPart)) { |
175 | return; |
176 | } |
177 | super.addChildVisual(childEditPart, -1); |
178 | } |
179 | |
180 | /** |
181 | * @generated |
182 | */ |
183 | protected void removeChildVisual(EditPart childEditPart) { |
184 | if (removeFixedChild(childEditPart)) { |
185 | return; |
186 | } |
187 | super.removeChildVisual(childEditPart); |
188 | } |
189 | |
190 | /** |
191 | * @generated |
192 | */ |
193 | protected IFigure getContentPaneFor(IGraphicalEditPart editPart) { |
194 | if (editPart instanceof ResourceContainerResourceContainerCompartmentEditPart) { |
195 | return getPrimaryShape() |
196 | .getFigureResourceContainerCompartmentFigure(); |
197 | } |
198 | return getContentPane(); |
199 | } |
200 | |
201 | /** |
202 | * @generated |
203 | */ |
204 | protected NodeFigure createNodePlate() { |
205 | DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(40, 40); |
206 | return result; |
207 | } |
208 | |
209 | /** |
210 | * Creates figure for this edit part. |
211 | * |
212 | * Body of this method does not depend on settings in generation model |
213 | * so you may safely remove <i>generated</i> tag and modify it. |
214 | * |
215 | * @generated |
216 | */ |
217 | protected NodeFigure createNodeFigure() { |
218 | NodeFigure figure = createNodePlate(); |
219 | figure.setLayoutManager(new StackLayout()); |
220 | IFigure shape = createNodeShape(); |
221 | figure.add(shape); |
222 | contentPane = setupContentPane(shape); |
223 | return figure; |
224 | } |
225 | |
226 | /** |
227 | * Default implementation treats passed figure as content pane. |
228 | * Respects layout one may have set for generated figure. |
229 | * @param nodeShape instance of generated figure class |
230 | * @generated |
231 | */ |
232 | protected IFigure setupContentPane(IFigure nodeShape) { |
233 | if (nodeShape.getLayoutManager() == null) { |
234 | ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout(); |
235 | layout.setSpacing(5); |
236 | nodeShape.setLayoutManager(layout); |
237 | } |
238 | return nodeShape; // use nodeShape itself as contentPane |
239 | } |
240 | |
241 | /** |
242 | * @generated |
243 | */ |
244 | public IFigure getContentPane() { |
245 | if (contentPane != null) { |
246 | return contentPane; |
247 | } |
248 | return super.getContentPane(); |
249 | } |
250 | |
251 | /** |
252 | * @generated |
253 | */ |
254 | protected void setForegroundColor(Color color) { |
255 | if (primaryShape != null) { |
256 | primaryShape.setForegroundColor(color); |
257 | } |
258 | } |
259 | |
260 | /** |
261 | * @generated |
262 | */ |
263 | protected void setBackgroundColor(Color color) { |
264 | if (primaryShape != null) { |
265 | primaryShape.setBackgroundColor(color); |
266 | } |
267 | } |
268 | |
269 | /** |
270 | * @generated |
271 | */ |
272 | protected void setLineWidth(int width) { |
273 | if (primaryShape instanceof Shape) { |
274 | ((Shape) primaryShape).setLineWidth(width); |
275 | } |
276 | } |
277 | |
278 | /** |
279 | * @generated |
280 | */ |
281 | protected void setLineType(int style) { |
282 | if (primaryShape instanceof Shape) { |
283 | ((Shape) primaryShape).setLineStyle(style); |
284 | } |
285 | } |
286 | |
287 | /** |
288 | * @generated |
289 | */ |
290 | public EditPart getPrimaryChildEditPart() { |
291 | return getChildBySemanticHint(PalladioComponentModelVisualIDRegistry |
292 | .getType(ResourceContainerEntityNameEditPart.VISUAL_ID)); |
293 | } |
294 | |
295 | /** |
296 | * @generated |
297 | */ |
298 | public List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/getMARelTypesOnTarget() { |
299 | List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/types = new ArrayList/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/(); |
300 | types |
301 | .add(PalladioComponentModelElementTypes.LinkingResourceConnectedResourceContainers_LinkingResource_4002); |
302 | return types; |
303 | } |
304 | |
305 | /** |
306 | * @generated |
307 | */ |
308 | public List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/getMATypesForSource( |
309 | IElementType relationshipType) { |
310 | List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/types = new ArrayList/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/(); |
311 | if (relationshipType == PalladioComponentModelElementTypes.LinkingResourceConnectedResourceContainers_LinkingResource_4002) { |
312 | types.add(PalladioComponentModelElementTypes.LinkingResource_2003); |
313 | } |
314 | return types; |
315 | } |
316 | |
317 | /** |
318 | * @generated |
319 | */ |
320 | public EditPart getTargetEditPart(Request request) { |
321 | if (request instanceof CreateViewAndElementRequest) { |
322 | CreateElementRequestAdapter adapter = ((CreateViewAndElementRequest) request) |
323 | .getViewAndElementDescriptor() |
324 | .getCreateElementRequestAdapter(); |
325 | IElementType type = (IElementType) adapter |
326 | .getAdapter(IElementType.class); |
327 | if (type == PalladioComponentModelElementTypes.ProcessingResourceSpecification_3001) { |
328 | return getChildBySemanticHint(PalladioComponentModelVisualIDRegistry |
329 | .getType(ResourceContainerResourceContainerCompartmentEditPart.VISUAL_ID)); |
330 | } |
331 | } |
332 | return super.getTargetEditPart(request); |
333 | } |
334 | |
335 | /** |
336 | * @generated |
337 | */ |
338 | public class ResourceContainerFigure extends RectangleFigure { |
339 | |
340 | /** |
341 | * @generated |
342 | */ |
343 | private WrappingLabel fFigureResourceContainerNameFigure; |
344 | |
345 | /** |
346 | * @generated |
347 | */ |
348 | private RectangleFigure fFigureResourceContainerCompartmentFigure; |
349 | |
350 | /** |
351 | * @generated |
352 | */ |
353 | public ResourceContainerFigure() { |
354 | this.setLayoutManager(new StackLayout()); |
355 | this.setFill(false); |
356 | this.setOutline(false); |
357 | this.setLineWidth(0); |
358 | this.setMinimumSize(new Dimension(getMapMode().DPtoLP(250), |
359 | getMapMode().DPtoLP(350))); |
360 | |
361 | this.setBorder(new MarginBorder(getMapMode().DPtoLP(0), |
362 | getMapMode().DPtoLP(0), getMapMode().DPtoLP(0), |
363 | getMapMode().DPtoLP(0))); |
364 | createContents(); |
365 | } |
366 | |
367 | /** |
368 | * @generated |
369 | */ |
370 | private void createContents() { |
371 | |
372 | class Polyline0Class extends Shape { |
373 | /** |
374 | * @generated |
375 | */ |
376 | private final PointList myTemplate = new PointList(); |
377 | /** |
378 | * @generated |
379 | */ |
380 | private Rectangle myTemplateBounds; |
381 | |
382 | /** |
383 | * @generated |
384 | */ |
385 | public void addPoint(Point point) { |
386 | myTemplate.addPoint(point); |
387 | myTemplateBounds = null; |
388 | } |
389 | |
390 | /** |
391 | * @generated |
392 | */ |
393 | protected void fillShape(Graphics graphics) { |
394 | Rectangle bounds = getBounds(); |
395 | graphics.pushState(); |
396 | graphics.translate(bounds.x, bounds.y); |
397 | graphics.fillPolygon(scalePointList()); |
398 | graphics.popState(); |
399 | } |
400 | |
401 | /** |
402 | * @generated |
403 | */ |
404 | protected void outlineShape(Graphics graphics) { |
405 | Rectangle bounds = getBounds(); |
406 | graphics.pushState(); |
407 | graphics.translate(bounds.x, bounds.y); |
408 | graphics.drawPolygon(scalePointList()); |
409 | graphics.popState(); |
410 | } |
411 | |
412 | /** |
413 | * @generated |
414 | */ |
415 | private Rectangle getTemplateBounds() { |
416 | if (myTemplateBounds == null) { |
417 | myTemplateBounds = myTemplate.getBounds().getCopy() |
418 | .union(0, 0); |
419 | //just safety -- we are going to use this as divider |
420 | if (myTemplateBounds.width < 1) { |
421 | myTemplateBounds.width = 1; |
422 | } |
423 | if (myTemplateBounds.height < 1) { |
424 | myTemplateBounds.height = 1; |
425 | } |
426 | } |
427 | return myTemplateBounds; |
428 | } |
429 | |
430 | /** |
431 | * @generated |
432 | */ |
433 | private int[] scalePointList() { |
434 | Rectangle pointsBounds = getTemplateBounds(); |
435 | Rectangle actualBounds = getBounds(); |
436 | |
437 | float xScale = ((float) actualBounds.width) |
438 | / pointsBounds.width; |
439 | float yScale = ((float) actualBounds.height) |
440 | / pointsBounds.height; |
441 | |
442 | if (xScale == 1 && yScale == 1) { |
443 | return myTemplate.toIntArray(); |
444 | } |
445 | int[] scaled = (int[]) myTemplate.toIntArray().clone(); |
446 | for (int i = 0; i < scaled.length; i += 2) { |
447 | scaled[i] = (int) Math.floor(scaled[i] * xScale); |
448 | scaled[i + 1] = (int) Math |
449 | .floor(scaled[i + 1] * yScale); |
450 | } |
451 | return scaled; |
452 | } |
453 | } |
454 | ; |
455 | Polyline0Class polyline0 = new Polyline0Class(); |
456 | |
457 | polyline0.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode() |
458 | .DPtoLP(1))); |
459 | polyline0.addPoint(new Point(getMapMode().DPtoLP(1), getMapMode() |
460 | .DPtoLP(0))); |
461 | polyline0.addPoint(new Point(getMapMode().DPtoLP(30), getMapMode() |
462 | .DPtoLP(0))); |
463 | polyline0.addPoint(new Point(getMapMode().DPtoLP(30), getMapMode() |
464 | .DPtoLP(40))); |
465 | polyline0.addPoint(new Point(getMapMode().DPtoLP(29), getMapMode() |
466 | .DPtoLP(41))); |
467 | polyline0.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode() |
468 | .DPtoLP(41))); |
469 | polyline0.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode() |
470 | .DPtoLP(1))); |
471 | polyline0.addPoint(new Point(getMapMode().DPtoLP(29), getMapMode() |
472 | .DPtoLP(1))); |
473 | polyline0.addPoint(new Point(getMapMode().DPtoLP(30), getMapMode() |
474 | .DPtoLP(0))); |
475 | polyline0.addPoint(new Point(getMapMode().DPtoLP(29), getMapMode() |
476 | .DPtoLP(1))); |
477 | polyline0.addPoint(new Point(getMapMode().DPtoLP(29), getMapMode() |
478 | .DPtoLP(41))); |
479 | polyline0.addPoint(new Point(getMapMode().DPtoLP(30), getMapMode() |
480 | .DPtoLP(40))); |
481 | polyline0.addPoint(new Point(getMapMode().DPtoLP(29), getMapMode() |
482 | .DPtoLP(41))); |
483 | polyline0.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode() |
484 | .DPtoLP(41))); |
485 | polyline0.setFill(true); |
486 | polyline0.setLineWidth(1); |
487 | |
488 | this.add(polyline0); |
489 | polyline0.setLayoutManager(new StackLayout()); |
490 | |
491 | RectangleFigure rect0 = new RectangleFigure(); |
492 | rect0.setFill(false); |
493 | rect0.setOutline(false); |
494 | rect0.setLineWidth(1); |
495 | |
496 | rect0.setBorder(new MarginBorder(getMapMode().DPtoLP(10), |
497 | getMapMode().DPtoLP(3), getMapMode().DPtoLP(0), |
498 | getMapMode().DPtoLP(0))); |
499 | |
500 | this.add(rect0); |
501 | |
502 | GridLayout layoutRect0 = new GridLayout(); |
503 | layoutRect0.numColumns = 1; |
504 | layoutRect0.makeColumnsEqualWidth = true; |
505 | layoutRect0.horizontalSpacing = 0; |
506 | layoutRect0.verticalSpacing = 0; |
507 | layoutRect0.marginWidth = 0; |
508 | layoutRect0.marginHeight = 0; |
509 | rect0.setLayoutManager(layoutRect0); |
510 | |
511 | fFigureResourceContainerNameFigure = new WrappingLabel(); |
512 | fFigureResourceContainerNameFigure.setText("ResourceContainer"); |
513 | |
514 | rect0.add(fFigureResourceContainerNameFigure); |
515 | |
516 | fFigureResourceContainerCompartmentFigure = new RectangleFigure(); |
517 | fFigureResourceContainerCompartmentFigure.setFill(false); |
518 | fFigureResourceContainerCompartmentFigure.setOutline(false); |
519 | fFigureResourceContainerCompartmentFigure.setLineWidth(1); |
520 | fFigureResourceContainerCompartmentFigure |
521 | .setMinimumSize(new Dimension(getMapMode().DPtoLP(0), |
522 | getMapMode().DPtoLP(0))); |
523 | |
524 | fFigureResourceContainerCompartmentFigure |
525 | .setBorder(new MarginBorder(getMapMode().DPtoLP(10), |
526 | getMapMode().DPtoLP(0), getMapMode().DPtoLP(0), |
527 | getMapMode().DPtoLP(26))); |
528 | |
529 | GridData constraintFFigureResourceContainerCompartmentFigure = new GridData(); |
530 | constraintFFigureResourceContainerCompartmentFigure.verticalAlignment = GridData.FILL; |
531 | constraintFFigureResourceContainerCompartmentFigure.horizontalAlignment = GridData.FILL; |
532 | constraintFFigureResourceContainerCompartmentFigure.horizontalIndent = 0; |
533 | constraintFFigureResourceContainerCompartmentFigure.horizontalSpan = 1; |
534 | constraintFFigureResourceContainerCompartmentFigure.verticalSpan = 1; |
535 | constraintFFigureResourceContainerCompartmentFigure.grabExcessHorizontalSpace = true; |
536 | constraintFFigureResourceContainerCompartmentFigure.grabExcessVerticalSpace = false; |
537 | rect0.add(fFigureResourceContainerCompartmentFigure, |
538 | constraintFFigureResourceContainerCompartmentFigure); |
539 | |
540 | } |
541 | |
542 | /** |
543 | * @generated |
544 | */ |
545 | private boolean myUseLocalCoordinates = true; |
546 | |
547 | /** |
548 | * @generated |
549 | */ |
550 | protected boolean useLocalCoordinates() { |
551 | return myUseLocalCoordinates; |
552 | } |
553 | |
554 | /** |
555 | * @generated |
556 | */ |
557 | protected void setUseLocalCoordinates(boolean useLocalCoordinates) { |
558 | myUseLocalCoordinates = useLocalCoordinates; |
559 | } |
560 | |
561 | /** |
562 | * @generated |
563 | */ |
564 | public WrappingLabel getFigureResourceContainerNameFigure() { |
565 | return fFigureResourceContainerNameFigure; |
566 | } |
567 | |
568 | /** |
569 | * @generated |
570 | */ |
571 | public RectangleFigure getFigureResourceContainerCompartmentFigure() { |
572 | return fFigureResourceContainerCompartmentFigure; |
573 | } |
574 | |
575 | } |
576 | |
577 | } |