diff options
Diffstat (limited to 'uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram')
9 files changed, 2154 insertions, 0 deletions
diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/action/handlers/ActivityClipboardActionHandler.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/action/handlers/ActivityClipboardActionHandler.java new file mode 100644 index 00000000000..edb25a1a242 --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/action/handlers/ActivityClipboardActionHandler.java @@ -0,0 +1,185 @@ +/******************************************************************************* + * Copyright (c) 2008 + * Conselleria de Infraestructuras y Transporte, Generalitat de la Comunitat Valenciana . + * All rights reserved. This program + * and the accompanying materials are made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Francisco Javier Cano Muñoz (Prodevelop) - initial API implementation + ******************************************************************************/ +package org.eclipse.papyrus.diagram.activity.action.handlers; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.edit.command.AddCommand; +import org.eclipse.emf.edit.command.PasteFromClipboardCommand; +import org.eclipse.emf.edit.command.RemoveCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.emf.transaction.util.TransactionUtil; +import org.eclipse.gef.EditPart; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionContext; +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.papyrus.diagram.activity.edit.commands.helpers.ActivityPartitionActivity; +import org.eclipse.papyrus.diagram.common.actions.handlers.ClipboardActionHandler; +import org.eclipse.papyrus.diagram.common.command.wrappers.GMFtoEMFCommandWrapper; +import org.eclipse.papyrus.diagram.common.commands.AddEObjectReferencesToDiagram; +import org.eclipse.papyrus.diagram.common.util.DiagramEditPartsUtil; +import org.eclipse.papyrus.diagram.common.util.MDTUtil; +import org.eclipse.uml2.uml.Activity; +import org.eclipse.uml2.uml.ActivityNode; +import org.eclipse.uml2.uml.ActivityPartition; +import org.eclipse.uml2.uml.UMLPackage; + +public class ActivityClipboardActionHandler extends ClipboardActionHandler { + + @Override + protected void executePasteAfterCopy(IGlobalActionContext cntxt) { + + ISelection selection = cntxt.getSelection(); + List<EditPart> editParts = MDTUtil.getEditPartsFromSelection(selection); + + if (editParts.size() == 1) { + EditPart editPart = editParts.get(0); + EObject element = MDTUtil.resolveSemantic(editPart); + if (element instanceof ActivityPartition) { + ActivityPartition activityPartition = (ActivityPartition) element; + // we want to paste in an Activity Partition + // special actions required + TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(element); + if (domain != null && domain.getClipboard() != null && domain.getClipboard().size() > 0) { + + // All the objects in the clipboard are supposed to be of + // the + // same type + + Object[] objects = domain.getClipboard().toArray(); + EObject firstObjectToBePasted = (EObject) objects[0]; + EStructuralFeature feature = getFeature(firstObjectToBePasted, editPart); + + // elements pasted on an ActivityPartition ares stored in an + // Activity + Activity activity = ActivityPartitionActivity.getActivityPartitionActivity(activityPartition); + // Execute paste + Command pasteCommand = PasteFromClipboardCommand.create(domain, activity, feature); + domain.getCommandStack().execute(pasteCommand); + + // The new elements must be shown on the diagram + List<EObject> eobjects = new ArrayList<EObject>(); + for (Object o : pasteCommand.getAffectedObjects()) { + if (o instanceof EObject) { + eobjects.add((EObject) o); + } + } + + // The affected objects must be set inside the + // ActivityPartition + for (EObject eObject : eobjects) { + if (eObject instanceof ActivityNode) { + ActivityNode activityNode = (ActivityNode) eObject; + Command addCommand = AddCommand.create(domain, activityPartition, UMLPackage.eINSTANCE.getActivityPartition_Node(), activityNode); + if (addCommand != null) { + domain.getCommandStack().execute(addCommand); + } + } + } + // ad these objects' references to the Diagram + ICommand command = new AddEObjectReferencesToDiagram(domain, DiagramEditPartsUtil.findDiagramFromEditPart(editPart), eobjects); + domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(command)); + + // refresh the affected EditPart + if (editPart instanceof IGraphicalEditPart) { + DiagramEditPartsUtil.updateEditPart((IGraphicalEditPart) editPart); + } + } + } else { + // if not an ActivityPartition, default behavior + super.executePasteAfterCopy(cntxt); + } + } + } + + @Override + protected void executePasteAfterCut(IGlobalActionContext cntxt) { + + ISelection selection = cntxt.getSelection(); + List<EditPart> editParts = MDTUtil.getEditPartsFromSelection(selection); + + if (editParts.size() == 1) { + EditPart editPart = editParts.get(0); + EObject element = MDTUtil.resolveSemantic(editPart); + if (element instanceof ActivityPartition) { + ActivityPartition activityPartition = (ActivityPartition) element; + // elements pasted on an ActivityPartition ares stored in an + // Activity + Activity activity = ActivityPartitionActivity.getActivityPartitionActivity(activityPartition); + // we want to paste in an Activity Partition + // special actions required + TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(element); + if (domain != null && domain.getClipboard() != null && domain.getClipboard().size() > 0) { + // command + CompoundCommand cc = new CompoundCommand("Paste"); + + // get list of ActivityNodes to move + List<ActivityNode> activityNodes = new ArrayList<ActivityNode>(); + for (Object object : domain.getClipboard()) { + EObject eObject = MDTUtil.resolveSemantic(object); + if (eObject instanceof ActivityNode) { + activityNodes.add((ActivityNode) eObject); + } + } + + // add all ActivityNodes to the selected ActivityPartition's + // node feature + for (ActivityNode activityNode : activityNodes) { + Command setCommand = AddCommand.create(domain, activity, UMLPackage.eINSTANCE.getActivity_Node(), activityNode); + if (setCommand != null && setCommand.canExecute()) { + cc.append(setCommand); + } + setCommand = AddCommand.create(domain, activityNode, UMLPackage.eINSTANCE.getActivityNode_InPartition(), activityPartition); + if (setCommand != null && setCommand.canExecute()) { + cc.append(setCommand); + } + } + + // delete copied object + Command removeCommand = RemoveCommand.create(domain, activity, UMLPackage.eINSTANCE.getActivity_Node(), clipboard); + cc.append(removeCommand); + // delete copied object's views + for (EditPart ep : editPartsInClipboard) { + Command c = getDeleteViewCommand(ep); + if (c != null) { + cc.append(c); + } + } + + if (cc.canExecute()) { + domain.getCommandStack().execute(cc); + } + + // ad these objects' references to the Diagram + ICommand command = new AddEObjectReferencesToDiagram(domain, DiagramEditPartsUtil.findDiagramFromEditPart(editPart), Arrays.asList(activityNodes.toArray(new EObject[activityNodes + .size()]))); + domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(command)); + + // refresh the affected EditPart + if (editPart instanceof IGraphicalEditPart) { + DiagramEditPartsUtil.updateEditPart((IGraphicalEditPart) editPart); + } + } + } else { + // if not an ActivityPartition, default behavior + super.executePasteAfterCut(cntxt); + } + } + } +} diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/action/handlers/ClipboardActionHandlerProvider.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/action/handlers/ClipboardActionHandlerProvider.java new file mode 100644 index 00000000000..1a65c2a38ae --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/action/handlers/ClipboardActionHandlerProvider.java @@ -0,0 +1,101 @@ +/***************************************************************************
+ * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte,
+ * Generalitat de la Comunitat Valenciana . All rights reserved. This program
+ * and the accompanying materials are made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution, and is
+ * available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors: Mario Cervera Ubeda (Prodevelop)
+ *
+ ******************************************************************************/
+package org.eclipse.papyrus.diagram.activity.action.handlers;
+
+import java.util.Hashtable;
+
+import org.eclipse.gmf.runtime.common.ui.services.action.global.AbstractGlobalActionHandlerProvider;
+import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandler;
+import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandlerContext;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.IWorkbenchPart;
+
+public class ClipboardActionHandlerProvider extends AbstractGlobalActionHandlerProvider {
+
+ /**
+ * List for handlers.
+ */
+ private Hashtable handlerList = new Hashtable();
+
+ /**
+ * Returns a global action handler that supports operations (cut, copy, and paste).
+ */
+ @Override
+ public IGlobalActionHandler getGlobalActionHandler(final IGlobalActionHandlerContext context) {
+
+ /* Create the handler */
+
+ if (!getHandlerList().containsKey(context.getActivePart())) {
+
+ getHandlerList().put(context.getActivePart(), new ActivityClipboardActionHandler());
+
+ /*
+ * Register as a part listener so that the cache can be cleared when the part is disposed
+ */
+ context.getActivePart().getSite().getPage().addPartListener(new IPartListener() {
+
+ private IWorkbenchPart localPart = context.getActivePart();
+
+ /**
+ * @see org.eclipse.ui.IPartListener#partActivated(IWorkbenchPart)
+ */
+ public void partActivated(IWorkbenchPart part) {
+ // Do nothing
+ }
+
+ /**
+ * @see org.eclipse.ui.IPartListener#partBroughtToTop(IWorkbenchPart)
+ */
+ public void partBroughtToTop(IWorkbenchPart part) {
+ // Do nothing
+ }
+
+ /**
+ * @see org.eclipse.ui.IPartListener#partClosed(IWorkbenchPart)
+ */
+ public void partClosed(IWorkbenchPart part) {
+ /* Remove the cache associated with the part */
+ if (part != null && part == localPart && getHandlerList().containsKey(part)) {
+ getHandlerList().remove(part);
+ localPart.getSite().getPage().removePartListener(this);
+ localPart = null;
+ }
+ }
+
+ /**
+ * @see org.eclipse.ui.IPartListener#partDeactivated(IWorkbenchPart)
+ */
+ public void partDeactivated(IWorkbenchPart part) {
+ // Do nothing
+ }
+
+ /**
+ * @see org.eclipse.ui.IPartListener#partOpened(IWorkbenchPart)
+ */
+ public void partOpened(IWorkbenchPart part) {
+ // Do nothing
+ }
+ });
+ }
+
+ return (IGlobalActionHandler) getHandlerList().get(context.getActivePart());
+ }
+
+ /**
+ * Returns the handlerList.
+ *
+ * @return Hashtable
+ */
+ private Hashtable getHandlerList() {
+ return handlerList;
+ }
+
+}
diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/draw2d/PinArrowFigure.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/draw2d/PinArrowFigure.java new file mode 100644 index 00000000000..3f1acb40b0a --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/draw2d/PinArrowFigure.java @@ -0,0 +1,126 @@ +/*******************************************************************************
+ * Copyright (c) 2008
+ * Conselleria de Infraestructuras y Transporte, Generalitat de la Comunitat Valenciana .
+ * All rights reserved. This program
+ * and the accompanying materials are made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution, and is
+ * available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Francisco Javier Cano Muñoz (Prodevelop) - initial API implementation
+ ******************************************************************************/
+package org.eclipse.papyrus.diagram.activity.draw2d;
+
+import org.eclipse.draw2d.Graphics;
+import org.eclipse.draw2d.PositionConstants;
+import org.eclipse.draw2d.Shape;
+import org.eclipse.draw2d.geometry.Rectangle;
+import org.eclipse.gef.EditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.BorderedBorderItemEditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.InputPin2EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.InputPin3EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.InputPin4EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.InputPin5EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.InputPin6EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.InputPinEditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.OutputPin2EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.OutputPin3EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.OutputPin4EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.OutputPinEditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.ValuePin2EditPart;
+import org.eclipse.papyrus.diagram.activity.edit.parts.ValuePinEditPart;
+
+public class PinArrowFigure extends Shape {
+
+ protected EditPart editPart;
+
+ public PinArrowFigure(EditPart EP) {
+ super();
+ editPart = EP;
+ }
+
+ @Override
+ protected void outlineShape(Graphics graphics) {
+ Rectangle r = getBounds();
+
+ if (editPart instanceof ValuePinEditPart || editPart instanceof ValuePin2EditPart) {
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.WEST)
+ drawWEST(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.EAST)
+ drawEAST(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.NORTH)
+ drawNORTH(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.SOUTH)
+ drawSOUTH(graphics, r);
+
+ drawV(graphics, r);
+ } else if (editPart instanceof InputPinEditPart || editPart instanceof InputPin2EditPart || editPart instanceof InputPin3EditPart || editPart instanceof InputPin4EditPart
+ || editPart instanceof InputPin5EditPart || editPart instanceof InputPin6EditPart) {
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.WEST)
+ drawWEST(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.EAST)
+ drawEAST(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.NORTH)
+ drawNORTH(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.SOUTH)
+ drawSOUTH(graphics, r);
+ } else if (editPart instanceof OutputPinEditPart || editPart instanceof OutputPin2EditPart || editPart instanceof OutputPin3EditPart || editPart instanceof OutputPin4EditPart) {
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.WEST)
+ drawEAST(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.EAST)
+ drawWEST(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.NORTH)
+ drawSOUTH(graphics, r);
+ if (((BorderedBorderItemEditPart) editPart).getBorderItemLocator().getCurrentSideOfParent() == PositionConstants.SOUTH)
+ drawNORTH(graphics, r);
+ }
+ }
+
+ private void drawWEST(Graphics graphics, Rectangle r) {
+ // center line
+ graphics.drawLine(r.getTopLeft().x + 1, r.getTopLeft().y + 7, r.getBottomRight().x - 2, r.getBottomRight().y - 8);
+ // top line
+ graphics.drawLine(r.getBottomRight().x - 2, r.getBottomRight().y - 8, r.getTopRight().x - 6, r.getTopRight().y + 3);
+ // bottom line
+ graphics.drawLine(r.getBottomRight().x - 2, r.getBottomRight().y - 8, r.getBottomRight().x - 6, r.getBottomRight().y - 4);
+ }
+
+ private void drawEAST(Graphics graphics, Rectangle r) {
+ // center line
+ graphics.drawLine(r.getBottomRight().x - 2, r.getBottomRight().y - 8, r.getTopLeft().x + 1, r.getTopLeft().y + 7);
+ // top line
+ graphics.drawLine(r.getTopLeft().x + 1, r.getTopLeft().y + 7, r.getTopLeft().x + 5, r.getTopLeft().y + 3);
+ // bottom line
+ graphics.drawLine(r.getTopLeft().x + 1, r.getTopLeft().y + 7, r.getBottomLeft().x + 5, r.getBottomLeft().y - 4);
+ }
+
+ private void drawNORTH(Graphics graphics, Rectangle r) {
+ // center line
+ graphics.drawLine(r.getTopLeft().x + 7, r.getTopLeft().y + 1, r.getBottomRight().x - 8, r.getBottomRight().y - 2);
+ // left line
+ graphics.drawLine(r.getBottomRight().x - 8, r.getBottomRight().y - 2, r.getBottomLeft().x + 3, r.getBottomLeft().y - 6);
+ // right line
+ graphics.drawLine(r.getBottomRight().x - 8, r.getBottomRight().y - 2, r.getBottomRight().x - 4, r.getBottomRight().y - 6);
+ }
+
+ private void drawSOUTH(Graphics graphics, Rectangle r) {
+ // center line
+ graphics.drawLine(r.getTopLeft().x + 7, r.getTopLeft().y + 1, r.getBottomRight().x - 8, r.getBottomRight().y - 2);
+ // left line
+ graphics.drawLine(r.getTopLeft().x + 7, r.getTopLeft().y + 1, r.getTopLeft().x + 3, r.getTopLeft().y + 6);
+ // right line
+ graphics.drawLine(r.getTopLeft().x + 7, r.getTopLeft().y + 1, r.getTopRight().x - 4, r.getTopRight().y + 6);
+ }
+
+ private void drawV(Graphics graphics, Rectangle r) {
+ // Left V line
+ graphics.drawLine(r.getBottomRight().x - 3, r.getBottomRight().y - 2, r.getBottomRight().x - 4, r.getBottomRight().y - 4);
+
+ // Right V lint
+ graphics.drawLine(r.getBottomRight().x - 3, r.getBottomRight().y - 2, r.getBottomRight().x - 2, r.getBottomRight().y - 4);
+ }
+
+ @Override
+ protected void fillShape(Graphics graphics) {
+ }
+}
diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/ActivityPartitionViewAlignerCommand.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/ActivityPartitionViewAlignerCommand.java new file mode 100644 index 00000000000..5dc6c4ed79b --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/ActivityPartitionViewAlignerCommand.java @@ -0,0 +1,426 @@ +/******************************************************************************* + * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte, Generalitat + * de la Comunitat Valenciana . All rights reserved. This program + * and the accompanying materials are made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Francisco Javier Cano Mu�oz (Prodevelop) - Initial API implementation. + * Marc Gil Sendra (Prodevelop) - Lets to reorder the activity partitions. + * + ******************************************************************************/ +package org.eclipse.papyrus.diagram.activity.edit.commands; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.draw2d.geometry.Dimension; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.EditPart; +import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; +import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; +import org.eclipse.gmf.runtime.notation.NotationPackage; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.uml2.uml.ActivityPartition; + +// TODO: Auto-generated Javadoc +/** + * A command to set the bounds (location/size) of a <code>View</code>. + * + * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a> + * @author <a href="mailto:mgil@prodevelop.es">Marc Gil Sendra</a> + */ +public class ActivityPartitionViewAlignerCommand extends AbstractTransactionalCommand { + + /** The adapter. */ + private IAdaptable adapter; + + /** The location. */ + private Point locationDelta; + + /** The size. */ + private Dimension sizeDelta; + + /** Reorder ActivityPartitions */ + private boolean reorder; + + /** + * Creates a <code>SetBoundsCommand</code> for the given view adapter with a given bounds. + * + * @param editingDomain + * the editing domain through which model changes are made + * @param label + * The command label + * @param adapter + * An adapter to the <code>View</code> + * @param bounds + * The new bounds + */ + public ActivityPartitionViewAlignerCommand(TransactionalEditingDomain editingDomain, String label, IAdaptable adapter, Rectangle boundsDelta) { + super(editingDomain, label, null); + this.adapter = adapter; + this.locationDelta = boundsDelta.getLocation(); + this.sizeDelta = boundsDelta.getSize(); + this.reorder = false; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable) + */ + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + + if (adapter == null) + return CommandResult.newErrorCommandResult("SetBoundsCommand: viewAdapter does not adapt to IView.class"); //$NON-NLS-1$ + + List<GraphicalEditPart> activityPartitions = getAllActivityPartitions(); + activityPartitions = SortActivitiesPartitionsByX(activityPartitions); + + GraphicalEditPart apep = (GraphicalEditPart) adapter.getAdapter(GraphicalEditPart.class); + + if (activityPartitions.size() == 0) + return CommandResult.newCancelledCommandResult(); + + // if the ActivityPartition is the first and we move left or + // if the ActivityPartition is the last and we move right, only move + if ((apep == activityPartitions.get(0) && locationDelta.x < 0) || (apep == activityPartitions.get(activityPartitions.size() - 1) && locationDelta.x > 0)) { + RelocateActivitiesPartitions(activityPartitions); + return CommandResult.newOKCommandResult(); + } + + // otherwise, try to move o reorder ActivityPartitions + activityPartitions = SortActivitiesPartitionsReorderedByX(activityPartitions); + RelocateActivitiesPartitions(activityPartitions); + + return CommandResult.newOKCommandResult(); + } + + /** + * Sets the view location. + * + * @param view + * the view + * @param location + * the location + */ + private void SetViewLocation(View view, Point location) { + ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X(), new Integer(location.x)); + ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y(), new Integer(location.y)); + } + + /** + * Sets the view size. + * + * @param view + * the view + * @param size + * the size + */ + private void SetViewSize(View view, Dimension size) { + ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Width(), new Integer(size.width)); + ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Height(), new Integer(size.height)); + } + + /** + * Sets the edit part location and size. + * + * @param apep + * the apep + * @param location + * the location + * @param size + * the size + */ + private void SetEditPartLocationAndSize(GraphicalEditPart apep, Point location, Dimension size) { + IAdaptable adapter = apep; + + if (apep == null) + return; + + View view = (View) apep.getAdapter(View.class); + + if (view == null) + return; + + SetViewLocation(view, location); + + SetViewSize(view, size); + } + + /** + * Gets the all activity partitions. + * + * @return the all activity partitions + */ + private List<GraphicalEditPart> getAllActivityPartitions() { + List<GraphicalEditPart> activities = new LinkedList<GraphicalEditPart>(); + + GraphicalEditPart apep = (GraphicalEditPart) adapter.getAdapter(GraphicalEditPart.class); + + if (apep != null) { + EditPart container = apep.getParent(); + + if (container == null) + return activities; + + for (Iterator<EditPart> it = container.getChildren().iterator(); it.hasNext();) { + EditPart ep = it.next(); + if (((View) ep.getModel()).getElement() instanceof ActivityPartition) { + activities.add((GraphicalEditPart) ep); + } + } + } + + return activities; + } + + /** + * Sort activities partitions by x. + * + * @param activities + * the activities + * + * @return the list< graphical edit part> + */ + private List<GraphicalEditPart> SortActivitiesPartitionsByX(List<GraphicalEditPart> activities) { + // Compare two ActivityPartitionEditParts by their x coordinate + Comparator<GraphicalEditPart> comp = new Comparator<GraphicalEditPart>() { + + public int compare(GraphicalEditPart o1, GraphicalEditPart o2) { + + int o1x, o2x; + o1x = o1.getFigure().getBounds().x; + o2x = o2.getFigure().getBounds().x; + + if (o1x > o2x) { + return 1; + } else if (o1x < o2x) { + return -1; + } else + return 0; + } + + }; + + GraphicalEditPart[] activitiesArray = new GraphicalEditPart[activities.size()]; + activities.toArray(activitiesArray); + + Arrays.sort(activitiesArray, comp); + + List<GraphicalEditPart> sortedActivities = new LinkedList<GraphicalEditPart>(); + + for (GraphicalEditPart ap : activitiesArray) { + sortedActivities.add(ap); + } + + return sortedActivities; + } + + /** + * Sort activities partitions by x. + * + * @param activities + * the activities + * + * @return the list< graphical edit part> + */ + private List<GraphicalEditPart> SortActivitiesPartitionsReorderedByX(List<GraphicalEditPart> activities) { + // Compare two ActivityPartitionEditParts by their x coordinate + Comparator<GraphicalEditPart> comp = new Comparator<GraphicalEditPart>() { + + GraphicalEditPart apep = (GraphicalEditPart) adapter.getAdapter(GraphicalEditPart.class); + + public int compare(GraphicalEditPart o1, GraphicalEditPart o2) { + + int o1x, o2x; + o1x = o1.getFigure().getBounds().x; + o2x = o2.getFigure().getBounds().x; + + // o1 is the Activity Partition we want to move + if (o1 == apep) { + // move to right + if (locationDelta.x > 0 && (o1.getFigure().getBounds().x < o2.getFigure().getBounds().x)) { + if ((o1.getFigure().getBounds().getTopLeft().x + locationDelta.x) > (o2.getFigure().getBounds().x + o2.getFigure().getBounds().width)) { + reorder = true; + return 1; + } else if ((o1.getFigure().getBounds().getTopLeft().x + locationDelta.x) == (o2.getFigure().getBounds().x + o2.getFigure().getBounds().width)) + return 0; + else + return -1; + } + // move to left + else if (locationDelta.x < 0 && (o1.getFigure().getBounds().x > o2.getFigure().getBounds().x)) { + if ((o1.getFigure().getBounds().getTopLeft().x + locationDelta.x) < o2.getFigure().getBounds().x) { + reorder = true; + return -1; + } else if ((o1.getFigure().getBounds().getTopLeft().x + locationDelta.x) == o2.getFigure().getBounds().x) + return 0; + else + return 1; + } else { + if (o1x > o2x) { + return 1; + } else if (o1x < o2x) { + return -1; + } else + return 0; + } + } + // o2 is the Activity Partition we want to move + else if (o2 == apep) { + // move to right + if (locationDelta.x > 0 && (o2.getFigure().getBounds().x < o1.getFigure().getBounds().x)) { + if ((o2.getFigure().getBounds().x + locationDelta.x) > (o1.getFigure().getBounds().x + o1.getFigure().getBounds().width)) { + reorder = true; + return -1; + } else if ((o2.getFigure().getBounds().x + locationDelta.x) == (o1.getFigure().getBounds().x + o1.getFigure().getBounds().width)) + return 0; + else + return 1; + } + // move to left + else if (locationDelta.x < 0 && (o2.getFigure().getBounds().x > o1.getFigure().getBounds().x)) { + if ((o2.getFigure().getBounds().x + locationDelta.x) < o1.getFigure().getBounds().x) { + reorder = true; + return 1; + } else if ((o2.getFigure().getBounds().x + locationDelta.x) == o1.getFigure().getBounds().x) + return 0; + else + return -1; + } else { + if (o1x > o2x) { + return 1; + } else if (o1x < o2x) { + return -1; + } else + return 0; + } + } else { + if (o1x > o2x) { + return 1; + } else if (o1x < o2x) { + return -1; + } else + return 0; + } + } + + }; + + GraphicalEditPart[] activitiesArray = new GraphicalEditPart[activities.size()]; + activities.toArray(activitiesArray); + + Arrays.sort(activitiesArray, comp); + + List<GraphicalEditPart> sortedActivities = new LinkedList<GraphicalEditPart>(); + + for (GraphicalEditPart ap : activitiesArray) { + sortedActivities.add(ap); + } + + return sortedActivities; + } + + /** + * Relocate activities partitions. + * + * @param activities + * the activities + */ + private void RelocateActivitiesPartitions(List<GraphicalEditPart> activities) { + + GraphicalEditPart apep = (GraphicalEditPart) adapter.getAdapter(GraphicalEditPart.class); + + if (reorder) { + int minX = activities.get(0).getFigure().getBounds().x; + for (int i = 1; i < activities.size(); i++) { + if (activities.get(i).getFigure().getBounds().x < minX) { + minX = activities.get(i).getFigure().getBounds().x; + } + } + + GraphicalEditPart gep = activities.get(0); + Rectangle previousBounds = gep.getFigure().getBounds().getCopy(); + previousBounds.x = minX; + SetEditPartLocationAndSize(activities.get(0), new Point(previousBounds.x, previousBounds.y), new Dimension(previousBounds.width, previousBounds.height)); + + Rectangle thisBounds; + for (int i = 1; i < activities.size(); i++, previousBounds = thisBounds.getCopy()) { + gep = activities.get(i); + thisBounds = gep.getFigure().getBounds().getCopy(); + thisBounds.x = previousBounds.x + previousBounds.width; + SetEditPartLocationAndSize(activities.get(i), new Point(thisBounds.x, thisBounds.y), new Dimension(thisBounds.width, thisBounds.height)); + } + + reorder = false; + } else { + int actual = -1; + + for (int i = 0; i < activities.size(); i++) { + if (activities.get(i) == apep) { + actual = i; + break; + } + } + + if (actual <= -1) { + return; + } + + GraphicalEditPart gep = activities.get(actual); + Rectangle newBounds = gep.getFigure().getBounds().getCopy(); + newBounds.x += locationDelta.x; + newBounds.y += locationDelta.y; + newBounds.height += sizeDelta.height; + newBounds.width += sizeDelta.width; + SetEditPartLocationAndSize(gep, new Point(newBounds.x, newBounds.y), new Dimension(newBounds.width, newBounds.height)); + + Rectangle previousBounds = newBounds.getCopy(), thisBounds = null; + for (int i = actual - 1; i >= 0; i--, previousBounds = thisBounds.getCopy()) { + gep = activities.get(i); + thisBounds = gep.getFigure().getBounds().getCopy(); + thisBounds.x = previousBounds.x - thisBounds.width; + thisBounds.y = previousBounds.y; + thisBounds.height = previousBounds.height; + SetEditPartLocationAndSize(gep, new Point(thisBounds.x, thisBounds.y), new Dimension(thisBounds.width, thisBounds.height)); + } + previousBounds = newBounds.getCopy(); + for (int i = actual + 1; i < activities.size(); i++, previousBounds = thisBounds.getCopy()) { + gep = activities.get(i); + thisBounds = gep.getFigure().getBounds().getCopy(); + thisBounds.x = previousBounds.x + previousBounds.width; + thisBounds.y = previousBounds.y; + thisBounds.height = previousBounds.height; + SetEditPartLocationAndSize(gep, new Point(thisBounds.x, thisBounds.y), new Dimension(thisBounds.width, thisBounds.height)); + } + } + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#getAffectedFiles() + */ + @Override + public List getAffectedFiles() { + if (adapter != null) { + View view = (View) adapter.getAdapter(View.class); + if (view != null) { + return getWorkspaceFiles(view); + } + } + return super.getAffectedFiles(); + } +} diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/AddChildToActivityPartitionCommand.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/AddChildToActivityPartitionCommand.java new file mode 100644 index 00000000000..f8be4b68b6b --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/AddChildToActivityPartitionCommand.java @@ -0,0 +1,623 @@ +/******************************************************************************* + * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte, Generalitat + * de la Comunitat Valenciana . All rights reserved. This program + * and the accompanying materials are made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Francisco Javier Cano Muñoz (Prodevelop) – Initial API + * implementation. + * + ******************************************************************************/ +package org.eclipse.papyrus.diagram.activity.edit.commands; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.emf.common.util.BasicEList; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.commands.CompoundCommand; +import org.eclipse.gef.requests.ChangeBoundsRequest; +import org.eclipse.gmf.runtime.diagram.core.commands.AddCommand; +import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand; +import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; +import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand; +import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart; +import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; +import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.diagram.common.commands.RemoveValueCommand; +import org.eclipse.papyrus.diagram.common.commands.RemoveValueRequest; +import org.eclipse.uml2.uml.Activity; +import org.eclipse.uml2.uml.ActivityEdge; +import org.eclipse.uml2.uml.ActivityNode; +import org.eclipse.uml2.uml.ActivityPartition; +import org.eclipse.uml2.uml.OpaqueBehavior; +import org.eclipse.uml2.uml.UMLPackage; + +// TODO: Auto-generated Javadoc +/** + * The Class AddChildToActivityPartitionCommand. + * + * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a> + */ +public class AddChildToActivityPartitionCommand extends Command { + + /** The cc. */ + CompoundCommand cc = new CompoundCommand(); + + /** + * Instantiates a new adds the child to activity partition command. + * + * @param request + * the request + * @param parent + * the parent + */ + public AddChildToActivityPartitionCommand(ChangeBoundsRequest request, IAdaptable parent) { + setLabel("Move child"); + List<EditPart> editParts = request.getEditParts(); + GraphicalEditPart parentEditPart = (GraphicalEditPart) parent.getAdapter(GraphicalEditPart.class); + + Command command = null; + for (EditPart ep : editParts) { + + if (!(ep instanceof GraphicalEditPart)) { + continue; + } + + if (((GraphicalEditPart) ep).resolveSemanticElement() instanceof ActivityPartition) { + command = getDeleteViewCommand((GraphicalEditPart) ep); + if (command != null) { + cc.add(command); + command = getSetValueCommand(((GraphicalEditPart) ep).resolveSemanticElement(), parentEditPart.resolveSemanticElement()); + if (command != null) { + cc.add(command); + } + } + continue; + } + + command = getAddCommand(ep, parentEditPart); + if (command == null) + continue; + cc.add(command); + + command = getSetValueCommand(((View) ep.getModel()).getElement(), ((View) parentEditPart.getModel()).getElement()); + if (command != null) { + cc.add(command); + } + + command = getEdgesCommand(ep, parentEditPart); + if (command != null) { + cc.add(command); + } + + command = getSetBoundsCommand(request, (GraphicalEditPart) ep, parentEditPart); + if (command != null) { + cc.add(command); + } + } + } + + /** + * Gets the adds the command. + * + * @param editPart + * the edit part + * @param parent + * the parent + * + * @return the adds the command + */ + private Command getAddCommand(EditPart editPart, GraphicalEditPart parent) { + EObject element = ((View) editPart.getModel()).getElement(); + + if (element instanceof ActivityPartition || element instanceof ActivityNode || element instanceof OpaqueBehavior) { + TransactionalEditingDomain domain = parent.getEditingDomain(); + IAdaptable parentAdaptable = (IAdaptable) parent.getAdapter(IAdaptable.class); + IAdaptable childAdaptable = (IAdaptable) editPart.getAdapter(IAdaptable.class); + + if (parentAdaptable != null && childAdaptable != null && domain != null) { + return new ICommandProxy(new AddCommand(domain, parentAdaptable, childAdaptable)); + } + } + + return null; + } + + private Command getDeleteViewCommand(GraphicalEditPart editPart) { + if (editPart == null) { + return null; + } + return new ICommandProxy(new DeleteCommand(editPart.getNotationView())); + } + + /** + * Gets the sets the value command. + * + * @param element + * the element + * @param parent + * the parent + * + * @return the sets the value command + */ + private Command getSetValueCommand(EObject element, EObject parent) { + EReference partitionReference = null, activityReference = null; + Object partitionValue = null, activityValue = null; + SetRequest partitionRequest = null, activityRequest = null; + + EList parentList; + Command activityPartitionChildrenCommand = null; + if (element instanceof ActivityNode) { + activityReference = UMLPackage.eINSTANCE.getActivityNode_Activity(); + partitionReference = UMLPackage.eINSTANCE.getActivityNode_InPartition(); + + parentList = new BasicEList(); + if (parent instanceof Activity) { + partitionValue = parentList; + activityValue = parent; + } else if (parent instanceof ActivityPartition) { + parentList.add(parent); + partitionValue = parentList; + activityValue = getActivityPartitionActivity(((ActivityPartition) parent)); + } else { + return null; + } + partitionRequest = new SetRequest(element, partitionReference, parentList); + } else if (element instanceof OpaqueBehavior) { + return null; + } else if (element instanceof ActivityPartition) { + SetRequest setRequest1 = null; + // SetRequest setRequest2 = null; + activityReference = UMLPackage.eINSTANCE.getActivityGroup_InActivity(); + partitionReference = UMLPackage.eINSTANCE.getActivityPartition_SuperPartition(); + if (parent instanceof Activity) { + setRequest1 = new SetRequest(element, activityReference, parent); + // setRequest2 = new SetRequest(element, partitionReference, SetCommand.UNSET_VALUE); + } else if (parent instanceof ActivityPartition) { + // setRequest1 = new SetRequest(element, activityReference, SetCommand.UNSET_VALUE); + setRequest1 = new SetRequest(element, partitionReference, parent); + } else { + return null; + } + Command setCommand = new ICommandProxy(new SetValueCommand(setRequest1)); + // if (setCommand != null) { + // setCommand = setCommand.chain(new ICommandProxy(new SetValueCommand(setRequest2))); + // } + return setCommand; + } + + activityRequest = new SetRequest(element, activityReference, activityValue); + + if (partitionRequest != null && activityRequest != null) { + CompoundCommand cc = new CompoundCommand(); + cc.setLabel("Activity Hierarchy"); + + cc.add(new ICommandProxy(new SetValueCommand(partitionRequest))); + cc.add(new ICommandProxy(new SetValueCommand(activityRequest))); + + if (activityPartitionChildrenCommand != null) { + cc.add(activityPartitionChildrenCommand); + } + + return cc; + } + + return null; + } + + /** + * Gets the activity partition activity. + * + * @param activityPartition + * the activity partition + * + * @return the activity partition activity + */ + private Activity getActivityPartitionActivity(ActivityPartition activityPartition) { + if (activityPartition.getInActivity() != null) { + return activityPartition.getInActivity(); + } + + ActivityPartition superPartition, actualPartition; + + superPartition = null; + actualPartition = activityPartition; + + while ((superPartition = actualPartition.getSuperPartition()) != null) { + actualPartition = superPartition; + } + + return actualPartition.getInActivity(); + } + + /** + * Gets the sets the bounds command. + * + * @param request + * the request + * @param child + * the child + * @param parent + * the parent + * + * @return the sets the bounds command + */ + private Command getSetBoundsCommand(ChangeBoundsRequest request, GraphicalEditPart child, GraphicalEditPart parent) { + + if (((View) child.getModel()).getElement() instanceof ActivityPartition) { + Rectangle bounds = getActivityPartitionBounds(parent); + if (bounds != null) { + IAdaptable viewAdaptable = (IAdaptable) child.getAdapter(IAdaptable.class); + return new ICommandProxy(new SetBoundsCommand(parent.getEditingDomain(), "Relocate ActivityPartition", viewAdaptable, bounds)); + } + } + + Point point = request.getLocation().getCopy(); + Point localPoint = translateToLocal(parent, point); + + TransactionalEditingDomain domain = parent.getEditingDomain(); + IAdaptable childAdaptable = (IAdaptable) child.getAdapter(IAdaptable.class); + + SetBoundsCommand command = new SetBoundsCommand(domain, "Relocate childEditPart", childAdaptable, localPoint); + return new ICommandProxy(command); + } + + /** + * Gets the activity partition bounds. + * + * @param parent + * the parent + * + * @return the activity partition bounds + */ + private Rectangle getActivityPartitionBounds(GraphicalEditPart parent) { + List<GraphicalEditPart> sortedPartitions = SortActivitiesPartitionsByX(getAllActivityPartitions(parent)); + + if (sortedPartitions.size() <= 0) + return null; + + GraphicalEditPart last = sortedPartitions.get(sortedPartitions.size() - 1); + Rectangle lastBounds = last.getFigure().getBounds(); + Rectangle bounds = new Rectangle(); + bounds.x = lastBounds.x + lastBounds.width; + bounds.y = lastBounds.y; + bounds.height = lastBounds.height; + bounds.width = lastBounds.width <= 200 ? lastBounds.width : 200; + + return bounds; + } + + /** + * Gets the all activity partitions. + * + * @param apep + * the apep + * + * @return the all activity partitions + */ + private List<GraphicalEditPart> getAllActivityPartitions(GraphicalEditPart apep) { + + List<GraphicalEditPart> activities = new LinkedList<GraphicalEditPart>(); + for (Iterator<EditPart> it = apep.getChildren().iterator(); it.hasNext();) { + EditPart ep = it.next(); + if (((View) ep.getModel()).getElement() instanceof ActivityPartition) { + activities.add((GraphicalEditPart) ep); + } + } + + return activities; + } + + /** + * Sort activities partitions by x. + * + * @param activities + * the activities + * + * @return the list< graphical edit part> + */ + private List<GraphicalEditPart> SortActivitiesPartitionsByX(List<GraphicalEditPart> activities) { + // Compare two ActivityPartitionEditParts by their x coordinate + Comparator<GraphicalEditPart> comp = new Comparator<GraphicalEditPart>() { + + public int compare(GraphicalEditPart o1, GraphicalEditPart o2) { + + int o1x, o2x; + o1x = o1.getFigure().getBounds().x; + o2x = o2.getFigure().getBounds().y; + + if (o1x > o2x) { + return 1; + } else if (o1x < o2x) { + return -1; + } + + return 0; + } + + }; + + GraphicalEditPart[] activitiesArray = new GraphicalEditPart[activities.size()]; + activities.toArray(activitiesArray); + + Arrays.sort(activitiesArray, comp); + + List<GraphicalEditPart> sortedActivities = new LinkedList<GraphicalEditPart>(); + + for (GraphicalEditPart ap : activitiesArray) { + sortedActivities.add(ap); + } + + return sortedActivities; + } + + /** + * Translate to local. + * + * @param part + * the part + * @param location + * the location + * + * @return the point + */ + private Point translateToLocal(GraphicalEditPart part, Point location) { + Point point = new Point(); + Point partPoint = part.getFigure().getBounds().getTopLeft().getCopy(); + part.getFigure().translateToAbsolute(partPoint); + Rectangle partBounds = part.getFigure().getBounds(); + + point.x = location.x - partPoint.x; + point.y = location.y - partPoint.y; + + return point; + } + + /** + * Gets the absolute location. + * + * @param part + * the part + * + * @return the absolute location + */ + private Point getAbsoluteLocation(GraphicalEditPart part) { + Point point = part.getFigure().getBounds().getTopLeft(); + + GraphicalEditPart parent = null; + if (part.getParent() instanceof GraphicalEditPart) { + parent = (GraphicalEditPart) part.getParent(); + } + + if (parent != null) { + Point parentPoint = getAbsoluteLocation(parent); + Point myPoint = new Point(point.x + parentPoint.x, point.y + parentPoint.y); + return myPoint; + } + + return point; + } + + /** + * Gets the edges command. + * + * @param editPart + * the edit part + * @param targetEP + * the target ep + * + * @return the edges command + */ + private Command getEdgesCommand(EditPart editPart, EditPart targetEP) { + // source and target editParts -> model elements + EObject element = ((View) editPart.getModel()).getElement(); + EObject target = ((View) targetEP.getModel()).getElement(); + EObject source = null; + + CompoundCommand ccommand = new CompoundCommand(); + + // get source container + if (element instanceof ActivityNode) { + if (((ActivityNode) element).getInPartitions().size() > 0) { + source = ((ActivityNode) element).getInPartitions().get(0); + } else { + source = ((ActivityNode) element).getActivity(); + } + } + + ActivityNode anElement = (ActivityNode) element; + + // get all edges connected to this ActivityNode + EList<ActivityEdge> edges = new BasicEList<ActivityEdge>(); + edges.addAll(anElement.getIncomings()); + edges.addAll(anElement.getOutgoings()); + + // no edges means no command + if (edges.size() <= 0) { + return null; + } + + // remove al edges related to this node from source container + if (source instanceof ActivityPartition) { + Command command = removeEdgesFromSource(edges, (ActivityPartition) source); + if (command != null) { + ccommand.add(command); + } + } + + // remove all edges related to this node from target container + if (target instanceof ActivityPartition) { + Command command = removeEdgesFromSource(edges, (ActivityPartition) target); + if (command != null) { + ccommand.add(command); + } + } + + // add appliable edges to target container + EList<ActivityEdge> edgesToAddToTarget = new BasicEList<ActivityEdge>(); + + // incoming edges + EList<ActivityEdge> edgesIncoming = anElement.getIncomings(); + for (ActivityEdge e : edgesIncoming) { + ActivityNode sourceObject = e.getSource(); + EObject parent = null; + if (sourceObject.getInPartitions().size() > 0) { + parent = sourceObject.getInPartitions().get(0); + } else { + parent = sourceObject.getActivity(); + } + + if (parent != target) { + if (parent != null && parent instanceof ActivityPartition) { + SetRequest request = new SetRequest(parent, UMLPackage.eINSTANCE.getActivityPartition_Edge(), e); + SetValueCommand command = new SetValueCommand(request); + ccommand.add(new ICommandProxy(command)); + } + if (target != null && target instanceof ActivityPartition) { + SetRequest request = new SetRequest(target, UMLPackage.eINSTANCE.getActivityPartition_Edge(), e); + SetValueCommand command = new SetValueCommand(request); + ccommand.add(new ICommandProxy(command)); + } + } + } + + // outgoing edges + EList<ActivityEdge> edgesOutgoing = anElement.getOutgoings(); + for (ActivityEdge e : edgesOutgoing) { + ActivityNode targetObject = e.getTarget(); + EObject parent = null; + if (targetObject.getInPartitions().size() > 0) { + parent = targetObject.getInPartitions().get(0); + } else { + parent = targetObject.getActivity(); + } + + if (parent != target) { + if (parent != null && parent instanceof ActivityPartition) { + SetRequest request = new SetRequest(parent, UMLPackage.eINSTANCE.getActivityPartition_Edge(), e); + SetValueCommand command = new SetValueCommand(request); + ccommand.add(new ICommandProxy(command)); + } + if (target != null && target instanceof ActivityPartition) { + SetRequest request = new SetRequest(target, UMLPackage.eINSTANCE.getActivityPartition_Edge(), e); + SetValueCommand command = new SetValueCommand(request); + ccommand.add(new ICommandProxy(command)); + } + } + } + + return ccommand; + } + + /** + * Removes the edges from source. + * + * @param edges + * the edges + * @param source + * the source + * + * @return the command + */ + private Command removeEdgesFromSource(EList<ActivityEdge> edges, ActivityPartition source) { + + if (edges.size() <= 0) { + return null; + } + + EList<ActivityEdge> sourceEdges = source.getEdges(); + EReference reference = UMLPackage.eINSTANCE.getActivityPartition_Edge(); + + RemoveValueRequest request = new RemoveValueRequest(source, reference, edges); + RemoveValueCommand command = new RemoveValueCommand(request); + + return new ICommandProxy(command); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#canExecute() + */ + @Override + public boolean canExecute() { + return cc.canExecute(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#canUndo() + */ + @Override + public boolean canUndo() { + return cc.canUndo(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#chain(org.eclipse.gef.commands.Command) + */ + @Override + public Command chain(Command command) { + cc.chain(command); + return this; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#dispose() + */ + @Override + public void dispose() { + cc.dispose(); + super.dispose(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#execute() + */ + @Override + public void execute() { + cc.execute(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#redo() + */ + @Override + public void redo() { + cc.redo(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#undo() + */ + @Override + public void undo() { + cc.undo(); + } + +} diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/RefreshEditPartCommand.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/RefreshEditPartCommand.java new file mode 100644 index 00000000000..cb85db81a38 --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/RefreshEditPartCommand.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte, Generalitat + * de la Comunitat Valenciana . All rights reserved. This program + * and the accompanying materials are made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Francisco Javier Cano Muñoz (Prodevelop) – Initial API + * implementation. + * + ******************************************************************************/ +package org.eclipse.papyrus.diagram.activity.edit.commands; + +import org.eclipse.gef.commands.Command; +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; + +// TODO: Auto-generated Javadoc +/** + * The Class RefreshEditPartCommand. + * + * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a> + */ +public class RefreshEditPartCommand extends Command { + + /** The edit part. */ + IGraphicalEditPart editPart = null; + + /** + * Instantiates a new refresh edit part command. + * + * @param editPart + * the edit part + */ + public RefreshEditPartCommand(IGraphicalEditPart editPart) { + this.editPart = editPart; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#canExecute() + */ + @Override + public boolean canExecute() { + return editPart != null; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#dispose() + */ + @Override + public void dispose() { + editPart = null; + super.dispose(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#execute() + */ + @Override + public void execute() { + editPart.getFigure().invalidate(); + editPart.refresh(); + } + +} diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/SetActivityPartitionChildrenInActivity.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/SetActivityPartitionChildrenInActivity.java new file mode 100644 index 00000000000..12541be0048 --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/SetActivityPartitionChildrenInActivity.java @@ -0,0 +1,231 @@ +/******************************************************************************* + * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte, Generalitat + * de la Comunitat Valenciana . All rights reserved. This program + * and the accompanying materials are made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Francisco Javier Cano Muñoz (Prodevelop) – Initial API + * implementation. + * + ******************************************************************************/ +package org.eclipse.papyrus.diagram.activity.edit.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.commands.CompoundCommand; +import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; +import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand; +import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; +import org.eclipse.uml2.uml.Activity; +import org.eclipse.uml2.uml.ActivityNode; +import org.eclipse.uml2.uml.ActivityPartition; +import org.eclipse.uml2.uml.UMLPackage; + +// TODO: Auto-generated Javadoc +/** + * The Class SetActivityPartitionChildrenInActivity. + * + * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a> + */ +public class SetActivityPartitionChildrenInActivity extends Command { + + /** The parent activity. */ + Activity parentActivity = null; + + /** The actual command. */ + CompoundCommand actualCommand = new CompoundCommand(); + + /** + * Instantiates a new sets the activity partition children in activity. + * + * @param activityPartition + * the activity partition + * @param activity + * the activity + */ + public SetActivityPartitionChildrenInActivity(ActivityPartition activityPartition, Activity activity) { + parentActivity = activity; + + addChildCommands(activityPartition, actualCommand); + } + + /** + * Adds the child commands. + * + * @param activityPartition + * the activity partition + * @param cc + * the cc + */ + private void addChildCommands(ActivityPartition activityPartition, CompoundCommand cc) { + List<EObject> children = new ArrayList(); + children.addAll(activityPartition.getNodes()); + children.addAll(activityPartition.getSubpartitions()); + + for (EObject eo : children) { + addEObjectCommand(eo, cc); + } + } + + /** + * Adds the e object command. + * + * @param object + * the object + * @param cc + * the cc + */ + private void addEObjectCommand(EObject object, CompoundCommand cc) { + EReference reference = null; + + if (object instanceof ActivityNode) { + reference = UMLPackage.eINSTANCE.getActivityNode_Activity(); + SetRequest request = new SetRequest(object, reference, parentActivity); + cc.add(new ICommandProxy(new SetValueCommand(request))); + return; + } + if (object instanceof ActivityPartition) { + addChildCommands((ActivityPartition) object, cc); + reference = UMLPackage.eINSTANCE.getActivityGroup_InActivity(); + SetRequest request = new SetRequest(object, reference, parentActivity); + cc.add(new ICommandProxy(new SetValueCommand(request))); + return; + } + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#canExecute() + */ + @Override + public boolean canExecute() { + + if (actualCommand.size() <= 0) + return true; + + return actualCommand.canExecute(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#canUndo() + */ + @Override + public boolean canUndo() { + if (actualCommand.size() <= 0) + return true; + + return actualCommand.canUndo(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#chain(org.eclipse.gef.commands.Command) + */ + @Override + public Command chain(Command command) { + return actualCommand.chain(command); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#dispose() + */ + @Override + public void dispose() { + actualCommand.dispose(); + super.dispose(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#execute() + */ + @Override + public void execute() { + + if (actualCommand.size() <= 0) + return; + + actualCommand.execute(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#getDebugLabel() + */ + @Override + public String getDebugLabel() { + return actualCommand.getDebugLabel(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#getLabel() + */ + @Override + public String getLabel() { + return actualCommand.getLabel(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#redo() + */ + @Override + public void redo() { + + if (actualCommand.size() <= 0) + return; + + actualCommand.redo(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#setDebugLabel(java.lang.String) + */ + @Override + public void setDebugLabel(String label) { + actualCommand.setDebugLabel(label); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#setLabel(java.lang.String) + */ + @Override + public void setLabel(String label) { + actualCommand.setLabel(label); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.gef.commands.Command#undo() + */ + @Override + public void undo() { + + if (actualCommand.size() <= 0) + return; + + actualCommand.undo(); + } + +} diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/helpers/ActivityPartitionActivity.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/helpers/ActivityPartitionActivity.java new file mode 100644 index 00000000000..c6106166d71 --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/commands/helpers/ActivityPartitionActivity.java @@ -0,0 +1,41 @@ +/***************************************************************************
+ * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte,
+ * Generalitat de la Comunitat Valenciana . All rights reserved. This program
+ * and the accompanying materials are made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution, and is
+ * available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Francisco Javier Cano Mu�oz (Prodevelop) - initial API and implementation
+ * Marc Gil Sendra (Prodevelop)
+ *
+ *
+ ******************************************************************************/
+package org.eclipse.papyrus.diagram.activity.edit.commands.helpers;
+
+import org.eclipse.uml2.uml.Activity;
+import org.eclipse.uml2.uml.ActivityPartition;
+
+/**
+ *
+ * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a>
+ */
+public class ActivityPartitionActivity {
+
+ public static Activity getActivityPartitionActivity(ActivityPartition activityPartition) {
+ if (activityPartition.getInActivity() != null) {
+ return activityPartition.getInActivity();
+ }
+
+ ActivityPartition superPartition, actualPartition;
+
+ superPartition = null;
+ actualPartition = activityPartition;
+
+ while ((superPartition = actualPartition.getSuperPartition()) != null) {
+ actualPartition = superPartition;
+ }
+
+ return actualPartition.getInActivity();
+ }
+}
diff --git a/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/policies/ActivityXYLayoutEditPolicy.java b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/policies/ActivityXYLayoutEditPolicy.java new file mode 100644 index 00000000000..e88a4b1dc28 --- /dev/null +++ b/uml/org.eclipse.papyrus.diagram.activity/custom-src/org/eclipse/papyrus/diagram/activity/edit/policies/ActivityXYLayoutEditPolicy.java @@ -0,0 +1,351 @@ +/*************************************************************************** + * Copyright (c) 2007 Conselleria de Infraestructuras y Transporte, + * Generalitat de la Comunitat Valenciana . All rights reserved. This program + * and the accompanying materials are made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Francisco Javier Cano Muñoz (Prodevelop) - initial API and implementation + * Marc Gil Sendra (Prodevelop) + * + * + ******************************************************************************/ + +package org.eclipse.papyrus.diagram.activity.edit.policies; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.Request; +import org.eclipse.gef.RequestConstants; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.commands.UnexecutableCommand; +import org.eclipse.gef.requests.ChangeBoundsRequest; +import org.eclipse.gef.requests.GroupRequest; +import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; +import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand; +import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editpolicies.XYLayoutEditPolicy; +import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest; +import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest; +import org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandRequestWrapper; +import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest.ViewDescriptor; +import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; +import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.diagram.activity.edit.commands.ActivityPartitionViewAlignerCommand; +import org.eclipse.papyrus.diagram.activity.edit.parts.ActivityPartition2EditPart; +import org.eclipse.papyrus.diagram.activity.edit.parts.ActivityPartitionEditPart; +import org.eclipse.papyrus.diagram.activity.part.UMLVisualIDRegistry; +import org.eclipse.papyrus.diagram.activity.providers.UMLElementTypes; +import org.eclipse.uml2.uml.ActivityPartition; + +// TODO: Auto-generated Javadoc +/** + * The Class ActivityXYLayoutEditPolicy. + */ +public class ActivityXYLayoutEditPolicy extends XYLayoutEditPolicy { + + /* + * (non-Javadoc) + * + * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.XYLayoutEditPolicy#getCommand(org.eclipse.gef.Request) + */ + @Override + public Command getCommand(Request request) { + + // element moved + if (request instanceof ChangeBoundsRequest) { + ChangeBoundsRequest chr = (ChangeBoundsRequest) request; + + // if the element to move is a Label, deny it + for (Object o : chr.getEditParts()) { + if (o instanceof LabelEditPart) + return UnexecutableCommand.INSTANCE; + } + + for (Iterator<EditPart> it = chr.getEditParts().iterator(); it.hasNext();) { + EditPart ep = it.next(); + + if (((View) ep.getModel()).getElement() instanceof ActivityPartition) { + GraphicalEditPart apep = (GraphicalEditPart) ep; + + TransactionalEditingDomain editingDomain; + String label = "Move or resize ActivityPartition"; + Rectangle boundsDelta; + + editingDomain = apep.getEditingDomain(); + boundsDelta = new Rectangle(chr.getMoveDelta().x, chr.getMoveDelta().y, chr.getSizeDelta().width, chr.getSizeDelta().height); + Command superCommand = super.getCommand(request); + if (superCommand != null) { + superCommand = superCommand.chain(new ICommandProxy(new ActivityPartitionViewAlignerCommand(editingDomain, label, ep, boundsDelta))); + } + + return superCommand; + } + } + } + + // element and view creation + if (request instanceof CreateViewAndElementRequest) { + GraphicalEditPart host = (GraphicalEditPart) getHost(); + + CreateElementRequest createElementRequest = (CreateElementRequest) ((CreateViewAndElementRequest) request).getViewAndElementDescriptor().getCreateElementRequestAdapter().getAdapter( + CreateElementRequest.class); + + IAdaptable adaptableToView = ((CreateViewAndElementRequest) request).getViewAndElementDescriptor(); + Point location = ((CreateViewAndElementRequest) request).getLocation(); + + if (!createElementRequest.getElementType().equals(UMLElementTypes.ActivityPartition_2031) && !createElementRequest.getElementType().equals(UMLElementTypes.ActivityPartition_2030)) { + ((CreateViewAndElementRequest) request).setLocation(adaptLocationToVisibleRegion(host, ((CreateViewAndElementRequest) request).getLocation())); + return new ICommandProxy(new SetBoundsCommand(host.getEditingDomain(), "Set Element initial location", adaptableToView, adaptLocationToVisibleRegion(host, location))); + } + + Rectangle bounds = getActivityPartitionBounds(host, ((CreateViewAndElementRequest) request).getLocation()); + if (bounds != null) { + SetBoundsCommand setBoundsCommand = new SetBoundsCommand(host.getEditingDomain(), "Set ActivityPartition initial Location", adaptableToView, bounds); + return new ICommandProxy(setBoundsCommand); + } + } + + // view creation + if (request instanceof CreateViewRequest) { + GraphicalEditPart host = (GraphicalEditPart) getHost(); + + CreateViewRequest createViewRequest = (CreateViewRequest) request; + ViewDescriptor viewDescriptor = createViewRequest.getViewDescriptors().size() > 0 ? ((ViewDescriptor) createViewRequest.getViewDescriptors().get(0)) : null; + if (viewDescriptor == null) { + return super.getCommand(request); + } + Object adapted = viewDescriptor.getElementAdapter().getAdapter(EObject.class); + if (adapted instanceof EObject == false) { + return super.getCommand(request); + } + int elementType = UMLVisualIDRegistry.getNodeVisualID(host.getNotationView(), (EObject) adapted); + if (elementType != ActivityPartitionEditPart.VISUAL_ID && elementType != ActivityPartition2EditPart.VISUAL_ID) { + createViewRequest.setLocation(adaptLocationToVisibleRegion(host, createViewRequest.getLocation())); + return new ICommandProxy(new SetBoundsCommand(host.getEditingDomain(), "Set Element initial location", viewDescriptor, adaptLocationToVisibleRegion(host, createViewRequest + .getLocation()))); + } + + Rectangle bounds = getActivityPartitionBounds(host, createViewRequest.getLocation()); + if (bounds != null) { + SetBoundsCommand setBoundsCommand = new SetBoundsCommand(host.getEditingDomain(), "Set ActivityPartition initial Location", viewDescriptor, bounds); + return new ICommandProxy(setBoundsCommand); + } + } + + // delete partition by keyboard + if (RequestConstants.REQ_DELETE.equals(request.getType())) { + GroupRequest groupRequest = (GroupRequest) request; + // check that there are any EditParts selected. + if (groupRequest.getEditParts() != null && groupRequest.getEditParts().size() > 0) { + + for (Iterator<EditPart> it = this.getHost().getChildren().iterator(); it.hasNext();) { + EditPart ep = it.next(); + + // if the ActivityPartition selected is the same of the + // recent + // element, continue with the next + if (ep.equals(groupRequest.getEditParts().get(0))) + continue; + + if (ep instanceof ActivityPartitionEditPart || ep instanceof ActivityPartition2EditPart) { + GraphicalEditPart apep = (GraphicalEditPart) ep; + + TransactionalEditingDomain editingDomain; + String label = "Move or resize ActivityPartition"; + Rectangle boundsDelta; + + editingDomain = apep.getEditingDomain(); + boundsDelta = new Rectangle(0, 0, 0, 0); + EditPart epart = (EditPart) groupRequest.getEditParts().get(0); + View v = (View) epart.getModel(); + EObject e = v.getElement(); + + return new ICommandProxy(new ActivityPartitionViewAlignerCommand(editingDomain, label, ep, boundsDelta)); + } + } + } + } + + // delete partition by context menu + if (request instanceof EditCommandRequestWrapper && ((EditCommandRequestWrapper) request).getEditCommandRequest() instanceof DestroyElementRequest) { + EditCommandRequestWrapper ecrw = (EditCommandRequestWrapper) request; + DestroyElementRequest der = (DestroyElementRequest) ecrw.getEditCommandRequest(); + + for (Iterator<EditPart> it = this.getHost().getChildren().iterator(); it.hasNext();) { + EditPart ep = it.next(); + + if (der.getElementToDestroy() != null && ((ep instanceof ActivityPartitionEditPart && !((ActivityPartitionEditPart) ep).resolveSemanticElement().equals(der.getElementToDestroy()))) + || (ep instanceof ActivityPartition2EditPart && !((ActivityPartition2EditPart) ep).resolveSemanticElement().equals(der.getElementToDestroy()))) { + GraphicalEditPart apep = (GraphicalEditPart) ep; + + TransactionalEditingDomain editingDomain; + String label = "Move or resize ActivityPartition"; + Rectangle boundsDelta; + + editingDomain = apep.getEditingDomain(); + boundsDelta = new Rectangle(0, 0, 0, 0); + return new ICommandProxy(new ActivityPartitionViewAlignerCommand(editingDomain, label, ep, boundsDelta)); + } + if (der.getElementToDestroy() == null && ep instanceof ActivityPartitionEditPart) { + GraphicalEditPart apep = (GraphicalEditPart) ep; + + TransactionalEditingDomain editingDomain; + String label = "Move or resize ActivityPartition"; + Rectangle boundsDelta; + + editingDomain = apep.getEditingDomain(); + boundsDelta = new Rectangle(0, 0, 0, 0); + return new ICommandProxy(new ActivityPartitionViewAlignerCommand(editingDomain, label, ep, boundsDelta)); + } + } + } + + Command command = super.getCommand(request); + return command; + } + + /** + * Adapt location to visible region. + * + * @param host + * the host + * @param bounds + * the bounds + * + * @return the point + */ + + private Point adaptLocationToVisibleRegion(GraphicalEditPart host, Point bounds) { + Point newBounds = new Point(); + + Point hostPoint = host.getFigure().getBounds().getTopLeft().getCopy(); + host.getFigure().translateToAbsolute(hostPoint); + Rectangle hostBounds = host.getFigure().getBounds(); + + newBounds.x = bounds.x - hostPoint.x; + newBounds.y = bounds.y - hostPoint.y; + + if (newBounds.x < 0) + newBounds.x = 0; + if (newBounds.y < 0) + newBounds.y = 0; + if (newBounds.x >= hostBounds.width) + newBounds.x = hostBounds.width - (int) (hostBounds.width * 0.2); + if (newBounds.y >= hostBounds.height) + newBounds.y = hostBounds.height - (int) (hostBounds.height * 0.2); + + return newBounds; + } + + /** + * Gets the activity partition bounds. + * + * @param host + * the host + * @param location + * the location + * + * @return the activity partition bounds + */ + private Rectangle getActivityPartitionBounds(GraphicalEditPart host, Point location) { + List<GraphicalEditPart> sortedPartitions = SortActivitiesPartitionsByX(getAllActivityPartitions(host)); + + Rectangle bounds = new Rectangle(); + + if (sortedPartitions.size() <= 0) { + Point adaptedBounds = adaptLocationToVisibleRegion(host, location); + bounds.x = adaptedBounds.x; + bounds.y = adaptedBounds.y; + bounds.width = -1; + bounds.height = -1; + } else { + GraphicalEditPart last = sortedPartitions.get(sortedPartitions.size() - 1); + Rectangle lastBounds = last.getFigure().getBounds(); + + bounds.x = lastBounds.x + lastBounds.width; + bounds.y = lastBounds.y; + bounds.height = lastBounds.height; + bounds.width = lastBounds.width <= 200 ? lastBounds.width : 200; + } + + return bounds; + } + + /** + * Gets the all activity partitions. + * + * @param apep + * the apep + * + * @return the all activity partitions + */ + private List<GraphicalEditPart> getAllActivityPartitions(GraphicalEditPart apep) { + + List<GraphicalEditPart> activities = new LinkedList<GraphicalEditPart>(); + for (Iterator<EditPart> it = apep.getChildren().iterator(); it.hasNext();) { + EditPart ep = it.next(); + if (((View) ep.getModel()).getElement() instanceof ActivityPartition) { + activities.add((GraphicalEditPart) ep); + } + } + + return activities; + } + + /** + * Sort activities partitions by x. + * + * @param activities + * the activities + * + * @return the list< graphical edit part> + */ + private List<GraphicalEditPart> SortActivitiesPartitionsByX(List<GraphicalEditPart> activities) { + // Compare two ActivityPartitionEditParts by their x coordinate + Comparator<GraphicalEditPart> comp = new Comparator<GraphicalEditPart>() { + + public int compare(GraphicalEditPart o1, GraphicalEditPart o2) { + + int o1x, o2x; + o1x = o1.getFigure().getBounds().x; + o2x = o2.getFigure().getBounds().x; + + if (o1x > o2x) { + return 1; + } else if (o1x < o2x) { + return -1; + } + + return 0; + } + + }; + + GraphicalEditPart[] activitiesArray = new GraphicalEditPart[activities.size()]; + activities.toArray(activitiesArray); + + Arrays.sort(activitiesArray, comp); + + List<GraphicalEditPart> sortedActivities = new LinkedList<GraphicalEditPart>(); + + for (GraphicalEditPart ap : activitiesArray) { + sortedActivities.add(ap); + } + + return sortedActivities; + } +} |