| 1 | /* |
| 2 | * Copyright 2007, SDQ, IPD, U KA |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts; |
| 5 | |
| 6 | import java.util.Iterator; |
| 7 | |
| 8 | import org.eclipse.draw2d.IFigure; |
| 9 | import org.eclipse.draw2d.geometry.Point; |
| 10 | import org.eclipse.draw2d.geometry.Rectangle; |
| 11 | import org.eclipse.emf.ecore.EObject; |
| 12 | import org.eclipse.gef.RootEditPart; |
| 13 | import org.eclipse.gef.commands.Command; |
| 14 | import org.eclipse.gef.requests.ChangeBoundsRequest; |
| 15 | import org.eclipse.gmf.runtime.common.core.command.CompositeCommand; |
| 16 | import org.eclipse.gmf.runtime.diagram.core.commands.AddCommand; |
| 17 | import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; |
| 18 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
| 19 | import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand; |
| 20 | import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramRootEditPart; |
| 21 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 22 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeCompartmentEditPart; |
| 23 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy; |
| 24 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DragDropEditPolicy; |
| 25 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
| 26 | import org.eclipse.gmf.runtime.diagram.ui.figures.ResizableCompartmentFigure; |
| 27 | import org.eclipse.gmf.runtime.diagram.ui.requests.DropObjectsRequest; |
| 28 | import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout; |
| 29 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
| 30 | import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; |
| 31 | import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
| 32 | import org.eclipse.gmf.runtime.notation.Bounds; |
| 33 | import org.eclipse.gmf.runtime.notation.Diagram; |
| 34 | import org.eclipse.gmf.runtime.notation.Node; |
| 35 | import org.eclipse.gmf.runtime.notation.View; |
| 36 | |
| 37 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
| 38 | import de.uka.ipd.sdq.pcm.allocation.AllocationPackage; |
| 39 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.policies.ResourceContainerAllocationCompartmentCanonicalEditPolicy; |
| 40 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.policies.ResourceContainerAllocationCompartmentItemSemanticEditPolicy; |
| 41 | import de.uka.ipd.sdq.pcm.gmf.allocation.part.Messages; |
| 42 | |
| 43 | /** |
| 44 | * @generated |
| 45 | */ |
| 46 | public class ResourceContainerAllocationCompartmentEditPart extends |
| 47 | ShapeCompartmentEditPart { |
| 48 | |
| 49 | /** |
| 50 | * @generated |
| 51 | */ |
| 52 | public static final int VISUAL_ID = 7002; |
| 53 | |
| 54 | /** |
| 55 | * @generated |
| 56 | */ |
| 57 | public ResourceContainerAllocationCompartmentEditPart(View view) { |
| 58 | super(view); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @generated |
| 63 | */ |
| 64 | public String getCompartmentName() { |
| 65 | return Messages.ResourceContainerAllocationCompartmentEditPart_title; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @generated |
| 70 | */ |
| 71 | public IFigure createFigure() { |
| 72 | ResizableCompartmentFigure result = (ResizableCompartmentFigure) super |
| 73 | .createFigure(); |
| 74 | result.setTitleVisibility(false); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @generated not |
| 80 | */ |
| 81 | protected void createDefaultEditPolicies() { |
| 82 | super.createDefaultEditPolicies(); |
| 83 | installEditPolicy( |
| 84 | EditPolicyRoles.SEMANTIC_ROLE, |
| 85 | new ResourceContainerAllocationCompartmentItemSemanticEditPolicy()); |
| 86 | installEditPolicy(EditPolicyRoles.CREATION_ROLE, |
| 87 | new CreationEditPolicy()); |
| 88 | installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE, |
| 89 | new DragDropEditPolicy() { |
| 90 | |
| 91 | /* (non-Javadoc) |
| 92 | * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.DragDropEditPolicy#getDropCommand(org.eclipse.gef.requests.ChangeBoundsRequest) |
| 93 | * Override this to implement Drag Drop of Allocation Contexts |
| 94 | */ |
| 95 | @Override |
| 96 | protected Command getDropCommand(ChangeBoundsRequest request) { |
| 97 | CompositeCommand cc = new CompositeCommand("Drag Element"); |
| 98 | for (Object editPart : request.getEditParts()) { |
| 99 | if (editPart instanceof IGraphicalEditPart) { |
| 100 | EObject element = resolveElement((IGraphicalEditPart) editPart); |
| 101 | Node allocationContextView = (Node) ((IGraphicalEditPart)editPart).getModel(); |
| 102 | if (element != null && element instanceof AllocationContext) { |
| 103 | Rectangle newBounds = getNewBounds(allocationContextView,element,(IGraphicalEditPart) editPart,request); |
| 104 | addAddViewCommand(cc,allocationContextView,element); |
| 105 | addChangeSemanticModelCommand(cc,element); |
| 106 | addChangeBoundsCommand(cc,allocationContextView,element,newBounds); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return new ICommandProxy(cc); |
| 111 | } |
| 112 | |
| 113 | private void addChangeBoundsCommand(CompositeCommand cc, |
| 114 | Node source, |
| 115 | EObject element, Rectangle newBounds) { |
| 116 | SetBoundsCommand setBounds = new SetBoundsCommand(ResourceContainerAllocationCompartmentEditPart.this.getEditingDomain(), |
| 117 | "Set Position", |
| 118 | new EObjectAdapter(source), |
| 119 | newBounds); |
| 120 | cc.add(setBounds); |
| 121 | } |
| 122 | |
| 123 | private Rectangle getNewBounds(Node source, EObject element, IGraphicalEditPart editPart, ChangeBoundsRequest request) { |
| 124 | Bounds b = (Bounds) source.getLayoutConstraint(); |
| 125 | Rectangle newBounds = new Rectangle(b.getX(),b.getY(),b.getWidth(),b.getHeight()); |
| 126 | editPart.getFigure().translateToAbsolute(newBounds); |
| 127 | newBounds = request.getTransformedRectangle(newBounds); |
| 128 | ResourceContainerAllocationCompartmentEditPart.this.getContentPane().translateToRelative(newBounds); |
| 129 | Point targetPoint = new Point(request.getLocation()); |
| 130 | getFigure().translateToRelative(targetPoint); |
| 131 | |
| 132 | return newBounds; |
| 133 | } |
| 134 | |
| 135 | private void addChangeSemanticModelCommand( |
| 136 | CompositeCommand cc, EObject element) { |
| 137 | SetRequest setRequest = new SetRequest(element, |
| 138 | AllocationPackage.eINSTANCE.getAllocationContext_ResourceContainer_AllocationContext(), |
| 139 | ResourceContainerAllocationCompartmentEditPart.this.resolveSemanticElement()); |
| 140 | SetValueCommand cmd = new SetValueCommand(setRequest); |
| 141 | cc.add(cmd); |
| 142 | } |
| 143 | |
| 144 | private void addAddViewCommand(CompositeCommand cc, |
| 145 | Node source, |
| 146 | EObject element) { |
| 147 | View targetView = (View) ResourceContainerAllocationCompartmentEditPart.this.getModel(); |
| 148 | AddCommand addCommand = new AddCommand(ResourceContainerAllocationCompartmentEditPart.this.getEditingDomain(),new EObjectAdapter(targetView),new EObjectAdapter(source)); |
| 149 | cc.add(addCommand); |
| 150 | } |
| 151 | |
| 152 | private EObject resolveElement(IGraphicalEditPart editPart) { |
| 153 | EObject element = ViewUtil |
| 154 | .resolveSemanticElement((View) ((IGraphicalEditPart) editPart) |
| 155 | .getModel()); |
| 156 | return element; |
| 157 | } |
| 158 | |
| 159 | }); |
| 160 | installEditPolicy(EditPolicyRoles.CANONICAL_ROLE, |
| 161 | new ResourceContainerAllocationCompartmentCanonicalEditPolicy()); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @generated |
| 166 | */ |
| 167 | protected void setRatio(Double ratio) { |
| 168 | if (getFigure().getParent().getLayoutManager() instanceof ConstrainedToolbarLayout) { |
| 169 | super.setRatio(ratio); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | } |