diff options
Diffstat (limited to 'plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util')
6 files changed, 1627 insertions, 1572 deletions
diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/GateHelper.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/GateHelper.java index 2c7a2534a6d..493c86316bf 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/GateHelper.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/GateHelper.java @@ -1,559 +1,561 @@ -/*****************************************************************************
- * Copyright (c) 2010 CEA
- *
- *
- * 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:
- * Soyatec - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.diagram.sequence.util;
-
-import java.util.ArrayList;
-import java.util.Collections;
-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.IFigure;
-import org.eclipse.draw2d.geometry.Point;
-import org.eclipse.draw2d.geometry.Rectangle;
-import org.eclipse.emf.common.command.CommandStack;
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.common.util.TreeIterator;
-import org.eclipse.emf.ecore.EAnnotation;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.transaction.RollbackException;
-import org.eclipse.emf.transaction.Transaction;
-import org.eclipse.emf.transaction.TransactionalCommandStack;
-import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.gmf.runtime.common.core.command.CommandResult;
-import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
-import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
-import org.eclipse.gmf.runtime.notation.DecorationNode;
-import org.eclipse.gmf.runtime.notation.NotationFactory;
-import org.eclipse.gmf.runtime.notation.Shape;
-import org.eclipse.gmf.runtime.notation.View;
-import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
-import org.eclipse.papyrus.uml.diagram.common.util.MessageDirection;
-import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.GateEditPart;
-import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.GateNameEditPart;
-import org.eclipse.papyrus.uml.diagram.sequence.locator.GateLocator;
-import org.eclipse.uml2.uml.CombinedFragment;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.Gate;
-import org.eclipse.uml2.uml.Interaction;
-import org.eclipse.uml2.uml.InteractionFragment;
-import org.eclipse.uml2.uml.InteractionUse;
-import org.eclipse.uml2.uml.Lifeline;
-import org.eclipse.uml2.uml.Message;
-import org.eclipse.uml2.uml.MessageEnd;
-import org.eclipse.uml2.uml.OccurrenceSpecification;
-
-/**
- * @author Jin Liu (jin.liu@soyatec.com)
- */
-public class GateHelper {
-
- private static final String CF_GATE_DATA = "CombinedFragment.InnerGate";
-
- private static final String CF_GATE_INSIDE = "insideCF";
-
- private static final String CF_GATE_OUTSIDE = "outsideCF";
-
- private static final String GATE_NAME_VOLATILE = "Gate.name.volatile";
-
- /**
- * Constructor.
- *
- */
- private GateHelper() {
- }
-
- /**
- * Create and add View for Gate.
- *
- * @param containerView
- * @param gate
- * @param innerGate
- * for CombinedFragment.
- * @return
- */
- public static View createView(View containerView, Gate gate) {
- if (containerView == null || gate == null) {
- return null;
- }
- Shape node = NotationFactory.eINSTANCE.createShape();
- node.setType(GateEditPart.GATE_TYPE);
- node.setElement(gate);
- node.setLayoutConstraint(NotationFactory.eINSTANCE.createLocation());
- ViewUtil.insertChildView(containerView, node, ViewUtil.APPEND, true);
- // label
- DecorationNode label = NotationFactory.eINSTANCE.createDecorationNode();
- label.setType(GateNameEditPart.GATE_NAME_TYPE);
- // Add possible to move the label.
- label.setLayoutConstraint(NotationFactory.eINSTANCE.createLocation());
- ViewUtil.insertChildView(node, label, ViewUtil.APPEND, true);
- return node;
- }
-
- /**
- * Constraints:
- * 1. The message leading to/from an actualGate of an InteractionUse must correspond to the message leading from/to the
- * formalGate with the same name of the Interaction referenced by the InteractionUse.
- * 2. The message leading to/from an (expression) Gate within a CombinedFragment must correspond to the message leading
- * from/to the CombinedFragment on its outside.
- */
- public static Gate createGate(Element parent, boolean createInnerCFGate) {
- if (parent == null) {
- return null;
- }
- String name = generateGateName(parent, "gate");
- Gate gate = null;
- if (parent instanceof Interaction) {
- gate = ((Interaction) parent).getFormalGate(name, false, true);
- } else if (parent instanceof CombinedFragment) {
- gate = ((CombinedFragment) parent).getCfragmentGate(name, false, true);
- if (createInnerCFGate) {
- Gate innerGate = ((CombinedFragment) parent).createCfragmentGate(generateGateName(parent, "gate"));
- EAnnotation ann = innerGate.createEAnnotation(CF_GATE_DATA);
- ann.getDetails().put(CF_GATE_OUTSIDE, getIdentification(gate));
- ann = gate.createEAnnotation(CF_GATE_DATA);
- ann.getDetails().put(CF_GATE_INSIDE, getIdentification(innerGate));
- // setVolatile(innerGate, true);
- }
- // setVolatile(gate, true);
- } else if (parent instanceof InteractionUse) {
- InteractionUse interactionUse = (InteractionUse) parent;
- Interaction refersTo = interactionUse.getRefersTo();
- if (refersTo != null) {
- refersTo.getFormalGate(name, false, true);
- }
- gate = interactionUse.getActualGate(name, false, true);
- }
- return gate;
- }
-
- protected static String getIdentification(Gate gate) {
- if (gate == null) {
- return null;
- }
- if (gate.eResource() != null) {
- return gate.eResource().getURIFragment(gate);
- } else {
- return gate.getName();
- }
- }
-
- protected static Gate findGate(CombinedFragment cf, String identification) {
- if (cf == null || identification == null) {
- return null;
- }
- if (cf.eResource() != null) {
- EObject eObject = cf.eResource().getEObject(identification);
- if (eObject instanceof Gate) {
- return (Gate) eObject;
- }
- }
- return cf.getCfragmentGate(identification);
- }
-
- public static Gate getInnerCFGate(Gate outerGate) {
- if (outerGate == null || !(outerGate.eContainer() instanceof CombinedFragment)) {
- return null;
- }
- CombinedFragment cf = (CombinedFragment) outerGate.eContainer();
- EAnnotation ann = outerGate.getEAnnotation(CF_GATE_DATA);
- if (ann != null) {
- String name = ann.getDetails().get(CF_GATE_INSIDE);
- return findGate(cf, name);
- }
- return null;
- }
-
- public static Gate getOuterCFGate(Gate innerGate) {
- if (innerGate == null || !(innerGate.eContainer() instanceof CombinedFragment)) {
- return null;
- }
- CombinedFragment cf = (CombinedFragment) innerGate.eContainer();
- EAnnotation ann = innerGate.getEAnnotation(CF_GATE_DATA);
- if (ann != null) {
- String name = ann.getDetails().get(CF_GATE_OUTSIDE);
- return findGate(cf, name);
- }
- return null;
- }
-
- public static boolean isInnerCFGate(Gate gate) {
- if (gate == null || !(gate.eContainer() instanceof CombinedFragment)) {
- return false;
- }
- CombinedFragment cf = (CombinedFragment) gate.eContainer();
- EAnnotation ann = gate.getEAnnotation(CF_GATE_DATA);
- if (ann != null) {
- String name = ann.getDetails().get(CF_GATE_OUTSIDE);
- return findGate(cf, name) != null;
- }
- return false;
- }
-
- public static Message getOuterCFMessage(Message message) {
- if (message == null) {
- return null;
- }
- MessageEnd sendEvent = message.getSendEvent();
- if (sendEvent instanceof Gate) {
- Gate gate = (Gate) sendEvent;
- if (!isInnerCFGate(gate)) {
- return null;
- }
- Gate outerGate = getOuterCFGate(gate);
- if (outerGate != null) {
- return outerGate.getMessage();
- }
- }
- MessageEnd receiveEvent = message.getReceiveEvent();
- if (receiveEvent instanceof Gate) {
- Gate gate = (Gate) receiveEvent;
- if (!isInnerCFGate(gate)) {
- return null;
- }
- Gate outerGate = getOuterCFGate(gate);
- if (outerGate != null) {
- return outerGate.getMessage();
- }
- }
- return null;
- }
-
- public static Gate getActualGate(Gate gate) {
- if (gate == null || !(gate.eContainer() instanceof Interaction)) {
- return null;
- }
- Interaction interaction = (Interaction) gate.eContainer();
- List<InteractionUse> refersToBy = findRefersToBy(interaction);
- for (InteractionUse interactionUse : refersToBy) {
- Gate actualGate = interactionUse.getActualGate(gate.getName());
- if (actualGate != null) {
- return actualGate;
- }
- }
- return null;
- }
-
- public static List<InteractionUse> findRefersToBy(Interaction interaction) {
- if (interaction == null) {
- return Collections.emptyList();
- }
- List<InteractionUse> refersToBy = new ArrayList<InteractionUse>();
- Resource eResource = interaction.eResource();
- if (eResource != null) {
- ResourceSet resourceSet = eResource.getResourceSet();
- if (resourceSet != null) {
- EList<Resource> resources = resourceSet.getResources();
- for (Resource resource : resources) {
- TreeIterator<EObject> contents = resource.getAllContents();
- while (contents.hasNext()) {
- EObject next = contents.next();
- if (next instanceof InteractionUse && interaction == ((InteractionUse) next).getRefersTo()) {
- refersToBy.add((InteractionUse) next);
- }
- }
- }
- } else {
- TreeIterator<EObject> contents = eResource.getAllContents();
- while (contents.hasNext()) {
- EObject next = contents.next();
- if (next instanceof InteractionUse && interaction == ((InteractionUse) next).getRefersTo()) {
- refersToBy.add((InteractionUse) next);
- }
- }
- }
- } else {
- EList<InteractionFragment> fragments = interaction.getFragments();
- for (InteractionFragment fragment : fragments) {
- if (!(fragment instanceof InteractionUse)) {
- continue;
- }
- if (interaction == ((InteractionUse) fragment).getRefersTo()) {
- refersToBy.add(((InteractionUse) fragment));
- }
- }
- }
- return refersToBy;
- }
-
- /**
- * Generate name for Gate.
- *
- * @param container
- * @param object
- * @param direction
- * @return
- */
- public static String generateGateName(EObject container, String suffix) {
- // Update gate name to hold a global sequence number.
- int index = getTotalGatesNumber(container);
- String name = suffix;
- if (container instanceof Interaction) {
- Interaction interaction = (Interaction) container;
- if (!"gate".equals(suffix) && interaction.getFormalGate(suffix) == null) {
- return suffix;
- }
- String gateName = name + index;
- while (interaction.getFormalGate(gateName = name + index) != null) {
- index++;
- }
- name = gateName;
- } else if (container instanceof InteractionUse) {
- InteractionUse iUse = (InteractionUse) container;
- if (!"gate".equals(suffix) && iUse.getActualGate(suffix) == null) {
- return suffix;
- }
- String gateName = name + index;
- while (iUse.getActualGate(gateName = name + index) != null) {
- index++;
- }
- name = gateName;
- } else if (container instanceof CombinedFragment) {
- CombinedFragment cf = (CombinedFragment) container;
- if (!"gate".equals(suffix) && cf.getCfragmentGate(suffix) == null) {
- return suffix;
- }
- String gateName = name + index;
- while (cf.getCfragmentGate(gateName = name + index) != null) {
- index++;
- }
- name = gateName;
- }
- return name;
- }
-
- private static int getTotalGatesNumber(EObject parent) {
- // fixed bug: start count for each Interaction.
- Interaction interaction = getRootInteraction(parent);
- if (interaction == null) {
- return 0;
- }
- int size = 0;
- TreeIterator<EObject> allContents = interaction.eAllContents();
- while (allContents.hasNext()) {
- if (allContents.next() instanceof Gate) {
- size++;
- }
- }
- return size;
- }
-
- private static Interaction getRootInteraction(EObject eObj) {
- if (eObj == null) {
- return null;
- } else if (eObj instanceof Interaction) {
- return (Interaction) eObj;
- }
- return getRootInteraction(eObj.eContainer());
- }
-
- public static Point computeGateLocation(Point pt, IFigure hostFigure, IFigure gateFigure) {
- if (pt == null || hostFigure == null) {
- return pt;
- }
- Point location = pt.getCopy();
- Rectangle bounds = new Rectangle(location, GateEditPart.DEFAULT_SIZE);
- hostFigure.translateToRelative(bounds);
- GateLocator locator = new GateLocator(hostFigure);
- Rectangle validLocation = locator.getValidLocation(bounds, gateFigure);
- return validLocation.getLocation().getTranslated(0, -GateEditPart.DEFAULT_SIZE.height / 2 - 1);
- }
-
- public static void updateGateName(TransactionalEditingDomain editingDomain, final Gate gate, final String newName) {
- if (gate == null) {
- return;
- }
- String name = gate.getName();
- if (name == null && newName == null) {
- return;
- } else if (name != null && name.equals(newName)) {
- return;
- } else {
- AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(editingDomain, "", null) {
-
- @Override
- protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
- gate.setName(newName);
- return CommandResult.newOKCommandResult();
- }
- };
- CommandStack commandStack = editingDomain.getCommandStack();
- GMFtoEMFCommandWrapper command = new GMFtoEMFCommandWrapper(cmd);
- if (commandStack instanceof TransactionalCommandStack) {
- try {
- ((TransactionalCommandStack) commandStack).execute(command, Collections.singletonMap(Transaction.OPTION_UNPROTECTED, Boolean.TRUE));
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (RollbackException e) {
- e.printStackTrace();
- }
- } else {
- commandStack.execute(command);
- }
- }
- }
-
- public static String getGateLabel(Gate gate) {
- Message message = gate.getMessage();
- if (message != null) {
- MessageDirection direction = null;
- EObject parent = gate.eContainer();
- if (parent instanceof CombinedFragment) {
- CombinedFragment cf = ((CombinedFragment) parent);
- if (gate == message.getSendEvent()) {
- MessageEnd receiveEvent = message.getReceiveEvent();
- Lifeline lifeline = getCoveredBy(receiveEvent);
- if (!cf.getCovereds().contains(lifeline)) {
- direction = MessageDirection.OUT;
- } else {
- direction = MessageDirection.IN;
- }
- } else {
- MessageEnd sendEvent = message.getSendEvent();
- Lifeline coveredBy = getCoveredBy(sendEvent);
- if (!cf.getCovereds().contains(coveredBy)) {
- direction = MessageDirection.IN;
- } else {
- direction = MessageDirection.OUT;
- }
- }
- } else if (parent instanceof Interaction) {
- if (gate == message.getSendEvent()) {
- direction = MessageDirection.IN;
- } else if (gate == message.getReceiveEvent()) {
- direction = MessageDirection.OUT;
- }
- }
- if (direction == null) {
- if (gate == message.getSendEvent()) {
- direction = MessageDirection.OUT;
- } else if (gate == message.getReceiveEvent()) {
- direction = MessageDirection.IN;
- }
- }
- if (direction != null) {
- StringBuffer buf = new StringBuffer();
- buf.append(direction.getName() + "_");
- buf.append(message.getName());
- return new String(buf);
- }
- }
- return gate.getLabel();
- }
-
- private static Lifeline getCoveredBy(MessageEnd messageEnd) {
- if (messageEnd == null) {
- return null;
- }
- if (messageEnd instanceof OccurrenceSpecification) {
- return ((OccurrenceSpecification) messageEnd).getCovered();
- }
- return null;
- }
-
- public static void setVolatile(Gate gate, boolean value) {
- if (gate == null) {
- return;
- }
- EAnnotation annotation = gate.getEAnnotation(GATE_NAME_VOLATILE);
- if (true == value) {
- if (annotation == null) {
- annotation = gate.createEAnnotation(GATE_NAME_VOLATILE);
- }
- annotation.getDetails().put(GATE_NAME_VOLATILE, Boolean.toString(value));
- } else if (annotation != null) {
- gate.getEAnnotations().remove(annotation);
- }
- if (gate.eContainer() instanceof InteractionUse) {
- Interaction refersTo = ((InteractionUse) gate.eContainer()).getRefersTo();
- if (refersTo != null) {
- Gate formalGate = refersTo.getFormalGate(gate.getName());
- if (formalGate != null) {
- setVolatile(formalGate, value);
- }
- }
- } else if (!GateHelper.isInnerCFGate(gate)) {
- Gate innerGate = GateHelper.getInnerCFGate(gate);
- if (innerGate != null) {
- setVolatile(innerGate, value);
- }
- }
- }
-
- public static boolean isVolatile(Gate gate) {
- if (gate == null) {
- return false;
- }
- EAnnotation ann = gate.getEAnnotation(GATE_NAME_VOLATILE);
- return ann != null;
- }
-
- public static void updateGateWithMessage(Message message, boolean force) {
- if (message == null) {
- return;
- }
- MessageEnd sendEvent = message.getSendEvent();
- MessageEnd receiveEvent = message.getReceiveEvent();
- // Suggest a name for gate with message.
- if (sendEvent instanceof Gate) {
- Gate gate = (Gate) sendEvent;
- updateGateName(gate, force);
- }
- if (receiveEvent instanceof Gate) {
- Gate gate = (Gate) receiveEvent;
- updateGateName(gate, force);
- }
- }
-
- protected static void updateGateName(Gate gate, boolean force) {
- if (gate == null) {
- return;
- }
- if (gate.eContainer() instanceof InteractionUse) {
- Interaction refersTo = ((InteractionUse) gate.eContainer()).getRefersTo();
- if (refersTo != null) {
- Gate formalGate = refersTo.getFormalGate(gate.getName());
- if (formalGate != null && isVolatile(formalGate)) {
- formalGate.setName(GateHelper.getGateLabel(gate));
- if (force) {
- setVolatile(formalGate, false);
- }
- }
- } else if (isVolatile(gate)) {
- gate.setName(GateHelper.getGateLabel(gate));
- if (force) {
- setVolatile(gate, false);
- }
- }
- } else if (!GateHelper.isInnerCFGate(gate)) {
- String newName = GateHelper.getGateLabel(gate);
- if (isVolatile(gate)) {
- gate.setName(newName);
- }
- Gate innerGate = GateHelper.getInnerCFGate(gate);
- if (innerGate != null && (isVolatile(innerGate))) {
- innerGate.setName(newName);
- if (force) {
- setVolatile(innerGate, false);
- }
- }
- if (force) {
- setVolatile(gate, false);
- }
- }
- }
-}
+/***************************************************************************** + * Copyright (c) 2010 CEA + * + * + * 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: + * Soyatec - Initial API and implementation + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.sequence.util; + +import java.util.ArrayList; +import java.util.Collections; +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.IFigure; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.emf.common.command.CommandStack; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.TreeIterator; +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.transaction.RollbackException; +import org.eclipse.emf.transaction.Transaction; +import org.eclipse.emf.transaction.TransactionalCommandStack; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gmf.runtime.common.core.command.CommandResult; +import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; +import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; +import org.eclipse.gmf.runtime.notation.DecorationNode; +import org.eclipse.gmf.runtime.notation.NotationFactory; +import org.eclipse.gmf.runtime.notation.Shape; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper; +import org.eclipse.papyrus.uml.diagram.common.util.MessageDirection; +import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.GateEditPart; +import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.GateNameEditPart; +import org.eclipse.papyrus.uml.diagram.sequence.locator.GateLocator; +import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization; +import org.eclipse.uml2.uml.CombinedFragment; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Gate; +import org.eclipse.uml2.uml.Interaction; +import org.eclipse.uml2.uml.InteractionFragment; +import org.eclipse.uml2.uml.InteractionUse; +import org.eclipse.uml2.uml.Lifeline; +import org.eclipse.uml2.uml.Message; +import org.eclipse.uml2.uml.MessageEnd; +import org.eclipse.uml2.uml.OccurrenceSpecification; + +/** + * @author Jin Liu (jin.liu@soyatec.com) + */ +public class GateHelper { + + private static final String CF_GATE_DATA = "CombinedFragment.InnerGate"; + + private static final String CF_GATE_INSIDE = "insideCF"; + + private static final String CF_GATE_OUTSIDE = "outsideCF"; + + private static final String GATE_NAME_VOLATILE = "Gate.name.volatile"; + + /** + * Constructor. + * + */ + private GateHelper() { + } + + /** + * Create and add View for Gate. + * + * @param containerView + * @param gate + * @param innerGate + * for CombinedFragment. + * @return + */ + public static View createView(View containerView, Gate gate) { + if (containerView == null || gate == null) { + return null; + } + Shape node = NotationFactory.eINSTANCE.createShape(); + node.setType(GateEditPart.GATE_TYPE); + node.setElement(gate); + node.setLayoutConstraint(NotationFactory.eINSTANCE.createLocation()); + ViewUtil.insertChildView(containerView, node, ViewUtil.APPEND, true); + // label + DecorationNode label = NotationFactory.eINSTANCE.createDecorationNode(); + label.setType(GateNameEditPart.GATE_NAME_TYPE); + // Add possible to move the label. + label.setLayoutConstraint(NotationFactory.eINSTANCE.createLocation()); + ViewUtil.insertChildView(node, label, ViewUtil.APPEND, true); + return node; + } + + /** + * Constraints: + * 1. The message leading to/from an actualGate of an InteractionUse must correspond to the message leading from/to the + * formalGate with the same name of the Interaction referenced by the InteractionUse. + * 2. The message leading to/from an (expression) Gate within a CombinedFragment must correspond to the message leading + * from/to the CombinedFragment on its outside. + */ + public static Gate createGate(Element parent, boolean createInnerCFGate) { + if (parent == null) { + return null; + } + String name = generateGateName(parent, "gate"); + Gate gate = null; + if (parent instanceof Interaction) { + gate = ((Interaction) parent).getFormalGate(name, false, true); + } else if (parent instanceof CombinedFragment) { + gate = ((CombinedFragment) parent).getCfragmentGate(name, false, true); + if (createInnerCFGate) { + Gate innerGate = ((CombinedFragment) parent).createCfragmentGate(generateGateName(parent, "gate")); + EAnnotation ann = innerGate.createEAnnotation(CF_GATE_DATA); + ann.getDetails().put(CF_GATE_OUTSIDE, getIdentification(gate)); + ann = gate.createEAnnotation(CF_GATE_DATA); + ann.getDetails().put(CF_GATE_INSIDE, getIdentification(innerGate)); + // setVolatile(innerGate, true); + } + // setVolatile(gate, true); + } else if (parent instanceof InteractionUse) { + InteractionUse interactionUse = (InteractionUse) parent; + Interaction refersTo = interactionUse.getRefersTo(); + if (refersTo != null) { + refersTo.getFormalGate(name, false, true); + } + gate = interactionUse.getActualGate(name, false, true); + } + return gate; + } + + protected static String getIdentification(Gate gate) { + if (gate == null) { + return null; + } + if (gate.eResource() != null) { + return gate.eResource().getURIFragment(gate); + } else { + return gate.getName(); + } + } + + protected static Gate findGate(CombinedFragment cf, String identification) { + if (cf == null || identification == null) { + return null; + } + if (cf.eResource() != null) { + EObject eObject = cf.eResource().getEObject(identification); + if (eObject instanceof Gate) { + return (Gate) eObject; + } + } + return cf.getCfragmentGate(identification); + } + + public static Gate getInnerCFGate(Gate outerGate) { + if (outerGate == null || !(outerGate.eContainer() instanceof CombinedFragment)) { + return null; + } + CombinedFragment cf = (CombinedFragment) outerGate.eContainer(); + EAnnotation ann = outerGate.getEAnnotation(CF_GATE_DATA); + if (ann != null) { + String name = ann.getDetails().get(CF_GATE_INSIDE); + return findGate(cf, name); + } + return null; + } + + public static Gate getOuterCFGate(Gate innerGate) { + if (innerGate == null || !(innerGate.eContainer() instanceof CombinedFragment)) { + return null; + } + CombinedFragment cf = (CombinedFragment) innerGate.eContainer(); + EAnnotation ann = innerGate.getEAnnotation(CF_GATE_DATA); + if (ann != null) { + String name = ann.getDetails().get(CF_GATE_OUTSIDE); + return findGate(cf, name); + } + return null; + } + + public static boolean isInnerCFGate(Gate gate) { + if (gate == null || !(gate.eContainer() instanceof CombinedFragment)) { + return false; + } + CombinedFragment cf = (CombinedFragment) gate.eContainer(); + EAnnotation ann = gate.getEAnnotation(CF_GATE_DATA); + if (ann != null) { + String name = ann.getDetails().get(CF_GATE_OUTSIDE); + return findGate(cf, name) != null; + } + return false; + } + + public static Message getOuterCFMessage(Message message) { + if (message == null) { + return null; + } + MessageEnd sendEvent = message.getSendEvent(); + if (sendEvent instanceof Gate) { + Gate gate = (Gate) sendEvent; + if (!isInnerCFGate(gate)) { + return null; + } + Gate outerGate = getOuterCFGate(gate); + if (outerGate != null) { + return outerGate.getMessage(); + } + } + MessageEnd receiveEvent = message.getReceiveEvent(); + if (receiveEvent instanceof Gate) { + Gate gate = (Gate) receiveEvent; + if (!isInnerCFGate(gate)) { + return null; + } + Gate outerGate = getOuterCFGate(gate); + if (outerGate != null) { + return outerGate.getMessage(); + } + } + return null; + } + + public static Gate getActualGate(Gate gate) { + if (gate == null || !(gate.eContainer() instanceof Interaction)) { + return null; + } + Interaction interaction = (Interaction) gate.eContainer(); + List<InteractionUse> refersToBy = findRefersToBy(interaction); + for (InteractionUse interactionUse : refersToBy) { + Gate actualGate = interactionUse.getActualGate(gate.getName()); + if (actualGate != null) { + return actualGate; + } + } + return null; + } + + public static List<InteractionUse> findRefersToBy(Interaction interaction) { + if (interaction == null) { + return Collections.emptyList(); + } + List<InteractionUse> refersToBy = new ArrayList<InteractionUse>(); + Resource eResource = interaction.eResource(); + if (eResource != null) { + ResourceSet resourceSet = eResource.getResourceSet(); + if (resourceSet != null) { + EList<Resource> resources = resourceSet.getResources(); + for (Resource resource : resources) { + TreeIterator<EObject> contents = resource.getAllContents(); + while (contents.hasNext()) { + EObject next = contents.next(); + if (next instanceof InteractionUse && interaction == ((InteractionUse) next).getRefersTo()) { + refersToBy.add((InteractionUse) next); + } + } + } + } else { + TreeIterator<EObject> contents = eResource.getAllContents(); + while (contents.hasNext()) { + EObject next = contents.next(); + if (next instanceof InteractionUse && interaction == ((InteractionUse) next).getRefersTo()) { + refersToBy.add((InteractionUse) next); + } + } + } + } else { + EList<InteractionFragment> fragments = interaction.getFragments(); + for (InteractionFragment fragment : fragments) { + if (!(fragment instanceof InteractionUse)) { + continue; + } + if (interaction == ((InteractionUse) fragment).getRefersTo()) { + refersToBy.add(((InteractionUse) fragment)); + } + } + } + return refersToBy; + } + + /** + * Generate name for Gate. + * + * @param container + * @param object + * @param direction + * @return + */ + public static String generateGateName(EObject container, String suffix) { + // Update gate name to hold a global sequence number. + int index = getTotalGatesNumber(container); + String name = suffix; + if (container instanceof Interaction) { + Interaction interaction = (Interaction) container; + if (!"gate".equals(suffix) && interaction.getFormalGate(suffix) == null) { + return suffix; + } + String gateName = name + index; + while (interaction.getFormalGate(gateName = name + index) != null) { + index++; + } + name = gateName; + } else if (container instanceof InteractionUse) { + InteractionUse iUse = (InteractionUse) container; + if (!"gate".equals(suffix) && iUse.getActualGate(suffix) == null) { + return suffix; + } + String gateName = name + index; + while (iUse.getActualGate(gateName = name + index) != null) { + index++; + } + name = gateName; + } else if (container instanceof CombinedFragment) { + CombinedFragment cf = (CombinedFragment) container; + if (!"gate".equals(suffix) && cf.getCfragmentGate(suffix) == null) { + return suffix; + } + String gateName = name + index; + while (cf.getCfragmentGate(gateName = name + index) != null) { + index++; + } + name = gateName; + } + return name; + } + + private static int getTotalGatesNumber(EObject parent) { + // fixed bug: start count for each Interaction. + Interaction interaction = getRootInteraction(parent); + if (interaction == null) { + return 0; + } + int size = 0; + TreeIterator<EObject> allContents = interaction.eAllContents(); + while (allContents.hasNext()) { + if (allContents.next() instanceof Gate) { + size++; + } + } + return size; + } + + private static Interaction getRootInteraction(EObject eObj) { + if (eObj == null) { + return null; + } else if (eObj instanceof Interaction) { + return (Interaction) eObj; + } + return getRootInteraction(eObj.eContainer()); + } + + public static Point computeGateLocation(Point pt, IFigure hostFigure, IFigure gateFigure) { + if (pt == null || hostFigure == null) { + return pt; + } + Point location = pt.getCopy(); + Rectangle bounds = new Rectangle(location, GateEditPart.DEFAULT_SIZE); + hostFigure.translateToRelative(bounds); + GateLocator locator = new GateLocator(hostFigure); + Rectangle validLocation = locator.getValidLocation(bounds, gateFigure); + return validLocation.getLocation().getTranslated(0, -GateEditPart.DEFAULT_SIZE.height / 2 - 1); + } + + public static void updateGateName(TransactionalEditingDomain editingDomain, final Gate gate, final String newName) { + if (gate == null) { + return; + } + String name = gate.getName(); + if (name == null && newName == null) { + return; + } else if (name != null && name.equals(newName)) { + return; + } else { + AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(editingDomain, "", null) { + + @Override + protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { + gate.setName(newName); + return CommandResult.newOKCommandResult(); + } + }; + CommandStack commandStack = editingDomain.getCommandStack(); + GMFtoEMFCommandWrapper command = new GMFtoEMFCommandWrapper(cmd); + if (commandStack instanceof TransactionalCommandStack) { + try { + ((TransactionalCommandStack) commandStack).execute(command, Collections.singletonMap(Transaction.OPTION_UNPROTECTED, Boolean.TRUE)); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (RollbackException e) { + e.printStackTrace(); + } + } else { + commandStack.execute(command); + } + } + } + + public static String getGateLabel(Gate gate) { + Message message = gate.getMessage(); + if (message != null) { + MessageDirection direction = null; + EObject parent = gate.eContainer(); + if (parent instanceof CombinedFragment) { + CombinedFragment cf = ((CombinedFragment) parent); + if (gate == message.getSendEvent()) { + MessageEnd receiveEvent = message.getReceiveEvent(); + Lifeline lifeline = getCoveredBy(receiveEvent); + if (!cf.getCovereds().contains(lifeline)) { + direction = MessageDirection.OUT; + } else { + direction = MessageDirection.IN; + } + } else { + MessageEnd sendEvent = message.getSendEvent(); + Lifeline coveredBy = getCoveredBy(sendEvent); + if (!cf.getCovereds().contains(coveredBy)) { + direction = MessageDirection.IN; + } else { + direction = MessageDirection.OUT; + } + } + } else if (parent instanceof Interaction) { + if (gate == message.getSendEvent()) { + direction = MessageDirection.IN; + } else if (gate == message.getReceiveEvent()) { + direction = MessageDirection.OUT; + } + } + if (direction == null) { + if (gate == message.getSendEvent()) { + direction = MessageDirection.OUT; + } else if (gate == message.getReceiveEvent()) { + direction = MessageDirection.IN; + } + } + if (direction != null) { + StringBuffer buf = new StringBuffer(); + buf.append(direction.getName() + "_"); + buf.append(UMLLabelInternationalization.getInstance().getLabel(message)); + return new String(buf); + } + } + return gate.getLabel(); + } + + private static Lifeline getCoveredBy(MessageEnd messageEnd) { + if (messageEnd == null) { + return null; + } + if (messageEnd instanceof OccurrenceSpecification) { + return ((OccurrenceSpecification) messageEnd).getCovered(); + } + return null; + } + + public static void setVolatile(Gate gate, boolean value) { + if (gate == null) { + return; + } + EAnnotation annotation = gate.getEAnnotation(GATE_NAME_VOLATILE); + if (true == value) { + if (annotation == null) { + annotation = gate.createEAnnotation(GATE_NAME_VOLATILE); + } + annotation.getDetails().put(GATE_NAME_VOLATILE, Boolean.toString(value)); + } else if (annotation != null) { + gate.getEAnnotations().remove(annotation); + } + if (gate.eContainer() instanceof InteractionUse) { + Interaction refersTo = ((InteractionUse) gate.eContainer()).getRefersTo(); + if (refersTo != null) { + Gate formalGate = refersTo.getFormalGate(gate.getName()); + if (formalGate != null) { + setVolatile(formalGate, value); + } + } + } else if (!GateHelper.isInnerCFGate(gate)) { + Gate innerGate = GateHelper.getInnerCFGate(gate); + if (innerGate != null) { + setVolatile(innerGate, value); + } + } + } + + public static boolean isVolatile(Gate gate) { + if (gate == null) { + return false; + } + EAnnotation ann = gate.getEAnnotation(GATE_NAME_VOLATILE); + return ann != null; + } + + public static void updateGateWithMessage(Message message, boolean force) { + if (message == null) { + return; + } + MessageEnd sendEvent = message.getSendEvent(); + MessageEnd receiveEvent = message.getReceiveEvent(); + // Suggest a name for gate with message. + if (sendEvent instanceof Gate) { + Gate gate = (Gate) sendEvent; + updateGateName(gate, force); + } + if (receiveEvent instanceof Gate) { + Gate gate = (Gate) receiveEvent; + updateGateName(gate, force); + } + } + + protected static void updateGateName(Gate gate, boolean force) { + if (gate == null) { + return; + } + if (gate.eContainer() instanceof InteractionUse) { + Interaction refersTo = ((InteractionUse) gate.eContainer()).getRefersTo(); + if (refersTo != null) { + Gate formalGate = refersTo.getFormalGate(gate.getName()); + if (formalGate != null && isVolatile(formalGate)) { + formalGate.setName(GateHelper.getGateLabel(gate)); + if (force) { + setVolatile(formalGate, false); + } + } + } else if (isVolatile(gate)) { + gate.setName(GateHelper.getGateLabel(gate)); + if (force) { + setVolatile(gate, false); + } + } + } else if (!GateHelper.isInnerCFGate(gate)) { + String newName = GateHelper.getGateLabel(gate); + if (isVolatile(gate)) { + gate.setName(newName); + } + Gate innerGate = GateHelper.getInnerCFGate(gate); + if (innerGate != null && (isVolatile(innerGate))) { + innerGate.setName(newName); + if (force) { + setVolatile(innerGate, false); + } + } + if (force) { + setVolatile(gate, false); + } + } + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/LifelineLabelHelper.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/LifelineLabelHelper.java index 8ecb37daed7..b1ae2883c3b 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/LifelineLabelHelper.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/LifelineLabelHelper.java @@ -1,179 +1,193 @@ -package org.eclipse.papyrus.uml.diagram.sequence.util;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.eclipse.gef.GraphicalEditPart;
-import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.IMaskManagedLabelEditPolicy;
-import org.eclipse.papyrus.uml.diagram.common.helper.StereotypedElementLabelHelper;
-import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineNameEditPart;
-import org.eclipse.papyrus.uml.tools.utils.UMLUtil;
-import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil;
-import org.eclipse.uml2.uml.ConnectableElement;
-import org.eclipse.uml2.uml.Expression;
-import org.eclipse.uml2.uml.Lifeline;
-import org.eclipse.uml2.uml.LiteralSpecification;
-import org.eclipse.uml2.uml.OpaqueExpression;
-import org.eclipse.uml2.uml.TimeExpression;
-import org.eclipse.uml2.uml.Type;
-import org.eclipse.uml2.uml.ValueSpecification;
-
-public class LifelineLabelHelper extends StereotypedElementLabelHelper {
-
- public static final String SHOW_REPRESENT_NAME = "representsName";
-
- public static final String SHOW_REPRESENT_SELECTOR = "representsSelector";
-
- public static final String SHOW_REPRESENT_TYPE = "representsType";
-
- public static final String SHOW_UNDEFINED_TYPE = "undefinedType";
-
- public static final String SHOW_LIFELINE_NAME = "lifelineName";
-
- public static final Collection<String> DEFAULT_LABEL_DISPLAY = Arrays.asList(SHOW_REPRESENT_NAME, SHOW_REPRESENT_SELECTOR, SHOW_REPRESENT_TYPE);
-
- /**
- * singelton instance
- */
- private static LifelineLabelHelper labelHelper;
-
- /**
- * Returns the singleton instance of this class
- *
- * @return the singleton instance.
- */
- public static LifelineLabelHelper getInstance() {
- if (labelHelper == null) {
- labelHelper = new LifelineLabelHelper();
- }
- return labelHelper;
- }
-
- /** Map for masks */
- protected final Map<String, String> masks = new HashMap<String, String>();
-
- /**
- * Creates a new Operation label helper.
- */
- protected LifelineLabelHelper() {
- // initialize the map
- masks.put(SHOW_REPRESENT_NAME, "Show represent name");
- masks.put(SHOW_REPRESENT_SELECTOR, "Show represent selector");
- masks.put(SHOW_REPRESENT_TYPE, "Show represent type");
- masks.put(SHOW_UNDEFINED_TYPE, "Always show undefined type");
- masks.put(SHOW_LIFELINE_NAME, "Always show lifeline name");
- }
-
- /**
- * Returns the map of masks used to display
- *
- * @return the map of masks used to display
- */
- public Map<String, String> getMasks() {
- return masks;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Lifeline getUMLElement(GraphicalEditPart editPart) {
- return (Lifeline) UMLUtil.resolveUMLElement(editPart);
- }
-
- @Override
- protected String elementLabel(GraphicalEditPart editPart) {
- if (editPart instanceof LifelineNameEditPart) {
- editPart = (GraphicalEditPart) editPart.getParent();
- }
- Collection<String> displayValue = DEFAULT_LABEL_DISPLAY;
- IMaskManagedLabelEditPolicy policy = (IMaskManagedLabelEditPolicy) editPart.getEditPolicy(IMaskManagedLabelEditPolicy.MASK_MANAGED_LABEL_EDIT_POLICY);
- if (policy != null) {
- displayValue = policy.getCurrentDisplayValue();
- }
- return getCustomLabel(getUMLElement(editPart), displayValue);
- }
-
- private static final String UNAMED = "<Unnamed>";
-
- private static final String UNDEFINED = "<Undefined>";
-
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=383722
- private String getCustomLabel(Lifeline lifeline, Collection<String> displayValue) {
- StringBuilder sb = new StringBuilder();
- appendName(lifeline, displayValue, sb);
- boolean displayType = displayValue.contains(SHOW_REPRESENT_TYPE);
- if (lifeline.getRepresents() == null || displayValue.contains(LifelineLabelHelper.SHOW_LIFELINE_NAME)) {
- displayType = false;
- }
- if (displayType) {
- // handle represent type label
- appendType(lifeline, displayValue, sb);
- }
- return sb.toString();
- }
-
- protected void appendName(Lifeline lifeline, Collection<String> displayValue, StringBuilder sb) {
- ConnectableElement element = lifeline.getRepresents();
- String lifelineName = lifeline.getName();
- if (element == null || displayValue.contains(SHOW_LIFELINE_NAME)) {
- appendString(sb, lifelineName, UNAMED);
- return;
- } else {
- // represents is not null
- if (displayValue.contains(SHOW_REPRESENT_NAME)) {
- appendString(sb, element.getName(), UNAMED);
- boolean displaySelector = displayValue.contains(SHOW_REPRESENT_SELECTOR);
- if (lifeline.getRepresents() == null || displayValue.contains(LifelineLabelHelper.SHOW_LIFELINE_NAME)) {
- displaySelector = false;
- }
- if (displaySelector) {
- ValueSpecification selector = lifeline.getSelector();
- // Add the selector if it is a LiteralSpecification
- if(selector instanceof LiteralSpecification) {
- sb.append("[").append(ValueSpecificationUtil.getSpecificationValue(selector)).append("]");
- }
- // Add the selector if it is an Expression
- if(selector instanceof Expression || selector instanceof OpaqueExpression || selector instanceof TimeExpression) {
- String specificationValue = ValueSpecificationUtil.getSpecificationValue(selector);
- if(specificationValue != null && specificationValue.length() > 0) {
- sb.append("[").append(specificationValue).append("]");
- }
- }
- }
- }
- // if neither <1> or <2> are checked, show lifeline name (or <unnamed> when the lifeline has no name)
- else if (!displayValue.contains(SHOW_REPRESENT_TYPE)) {
- appendString(sb, lifelineName, UNAMED);
- }
- }
- }
-
- protected void appendType(Lifeline lifeline, Collection<String> displayValue, StringBuilder sb) {
- ConnectableElement element = lifeline.getRepresents();
- if (element == null) {
- return;
- }
- Type type = element.getType();
- if (displayValue.contains(SHOW_REPRESENT_TYPE)) {
- if (type == null) {
- if (displayValue.contains(SHOW_UNDEFINED_TYPE)) {
- sb.append(":").append(UNDEFINED);
- }
- } else {
- appendString(sb.append(":"), type.getName(), UNAMED);
- }
- }
- }
-
- private void appendString(StringBuilder sb, String str, String defaultValue) {
- if (str != null) {
- sb.append(str);
- } else {
- sb.append(defaultValue);
- }
- }
-
-}
+/***************************************************************************** + * Copyright (c) 2016 CEA LIST and others. + * + * 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: + * CEA LIST - Initial API and implementation + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.sequence.util; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.gef.GraphicalEditPart; +import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.IMaskManagedLabelEditPolicy; +import org.eclipse.papyrus.uml.diagram.common.helper.StereotypedElementLabelHelper; +import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineNameEditPart; +import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization; +import org.eclipse.papyrus.uml.tools.utils.UMLUtil; +import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil; +import org.eclipse.uml2.uml.ConnectableElement; +import org.eclipse.uml2.uml.Expression; +import org.eclipse.uml2.uml.Lifeline; +import org.eclipse.uml2.uml.LiteralSpecification; +import org.eclipse.uml2.uml.OpaqueExpression; +import org.eclipse.uml2.uml.TimeExpression; +import org.eclipse.uml2.uml.Type; +import org.eclipse.uml2.uml.ValueSpecification; + +public class LifelineLabelHelper extends StereotypedElementLabelHelper { + + public static final String SHOW_REPRESENT_NAME = "representsName"; + + public static final String SHOW_REPRESENT_SELECTOR = "representsSelector"; + + public static final String SHOW_REPRESENT_TYPE = "representsType"; + + public static final String SHOW_UNDEFINED_TYPE = "undefinedType"; + + public static final String SHOW_LIFELINE_NAME = "lifelineName"; + + public static final Collection<String> DEFAULT_LABEL_DISPLAY = Arrays.asList(SHOW_REPRESENT_NAME, SHOW_REPRESENT_SELECTOR, SHOW_REPRESENT_TYPE); + + /** + * singelton instance + */ + private static LifelineLabelHelper labelHelper; + + /** + * Returns the singleton instance of this class + * + * @return the singleton instance. + */ + public static LifelineLabelHelper getInstance() { + if (labelHelper == null) { + labelHelper = new LifelineLabelHelper(); + } + return labelHelper; + } + + /** Map for masks */ + protected final Map<String, String> masks = new HashMap<String, String>(); + + /** + * Creates a new Operation label helper. + */ + protected LifelineLabelHelper() { + // initialize the map + masks.put(SHOW_REPRESENT_NAME, "Show represent name"); + masks.put(SHOW_REPRESENT_SELECTOR, "Show represent selector"); + masks.put(SHOW_REPRESENT_TYPE, "Show represent type"); + masks.put(SHOW_UNDEFINED_TYPE, "Always show undefined type"); + masks.put(SHOW_LIFELINE_NAME, "Always show lifeline name"); + } + + /** + * Returns the map of masks used to display + * + * @return the map of masks used to display + */ + public Map<String, String> getMasks() { + return masks; + } + + /** + * {@inheritDoc} + */ + @Override + public Lifeline getUMLElement(GraphicalEditPart editPart) { + return (Lifeline) UMLUtil.resolveUMLElement(editPart); + } + + @Override + protected String elementLabel(GraphicalEditPart editPart) { + if (editPart instanceof LifelineNameEditPart) { + editPart = (GraphicalEditPart) editPart.getParent(); + } + Collection<String> displayValue = DEFAULT_LABEL_DISPLAY; + IMaskManagedLabelEditPolicy policy = (IMaskManagedLabelEditPolicy) editPart.getEditPolicy(IMaskManagedLabelEditPolicy.MASK_MANAGED_LABEL_EDIT_POLICY); + if (policy != null) { + displayValue = policy.getCurrentDisplayValue(); + } + return getCustomLabel(getUMLElement(editPart), displayValue); + } + + private static final String UNAMED = "<Unnamed>"; + + private static final String UNDEFINED = "<Undefined>"; + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=383722 + private String getCustomLabel(Lifeline lifeline, Collection<String> displayValue) { + StringBuilder sb = new StringBuilder(); + appendName(lifeline, displayValue, sb); + boolean displayType = displayValue.contains(SHOW_REPRESENT_TYPE); + if (lifeline.getRepresents() == null || displayValue.contains(LifelineLabelHelper.SHOW_LIFELINE_NAME)) { + displayType = false; + } + if (displayType) { + // handle represent type label + appendType(lifeline, displayValue, sb); + } + return sb.toString(); + } + + protected void appendName(Lifeline lifeline, Collection<String> displayValue, StringBuilder sb) { + ConnectableElement element = lifeline.getRepresents(); + String lifelineName = UMLLabelInternationalization.getInstance().getLabel(lifeline); + if (element == null || displayValue.contains(SHOW_LIFELINE_NAME)) { + appendString(sb, lifelineName, UNAMED); + return; + } else { + // represents is not null + if (displayValue.contains(SHOW_REPRESENT_NAME)) { + appendString(sb,UMLLabelInternationalization.getInstance().getLabel( element), UNAMED); + boolean displaySelector = displayValue.contains(SHOW_REPRESENT_SELECTOR); + if (lifeline.getRepresents() == null || displayValue.contains(LifelineLabelHelper.SHOW_LIFELINE_NAME)) { + displaySelector = false; + } + if (displaySelector) { + ValueSpecification selector = lifeline.getSelector(); + // Add the selector if it is a LiteralSpecification + if(selector instanceof LiteralSpecification) { + sb.append("[").append(ValueSpecificationUtil.getSpecificationValue(selector, true)).append("]"); + } + // Add the selector if it is an Expression + if(selector instanceof Expression || selector instanceof OpaqueExpression || selector instanceof TimeExpression) { + String specificationValue = ValueSpecificationUtil.getSpecificationValue(selector, true); + if(specificationValue != null && specificationValue.length() > 0) { + sb.append("[").append(specificationValue).append("]"); + } + } + } + } + // if neither <1> or <2> are checked, show lifeline name (or <unnamed> when the lifeline has no name) + else if (!displayValue.contains(SHOW_REPRESENT_TYPE)) { + appendString(sb, lifelineName, UNAMED); + } + } + } + + protected void appendType(Lifeline lifeline, Collection<String> displayValue, StringBuilder sb) { + ConnectableElement element = lifeline.getRepresents(); + if (element == null) { + return; + } + Type type = element.getType(); + if (displayValue.contains(SHOW_REPRESENT_TYPE)) { + if (type == null) { + if (displayValue.contains(SHOW_UNDEFINED_TYPE)) { + sb.append(":").append(UNDEFINED); + } + } else { + appendString(sb.append(":"), UMLLabelInternationalization.getInstance().getLabel(type), UNAMED); + } + } + } + + private void appendString(StringBuilder sb, String str, String defaultValue) { + if (str != null) { + sb.append(str); + } else { + sb.append(defaultValue); + } + } + +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/MessageConnectionHelper.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/MessageConnectionHelper.java index f77bb72df9c..0a7fe86c9a6 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/MessageConnectionHelper.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/MessageConnectionHelper.java @@ -1,247 +1,249 @@ -/*****************************************************************************
- * Copyright (c) 2013 CEA
- *
- *
- * 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:
- * Soyatec - Initial API and implementation
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.diagram.sequence.util;
-
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification;
-import org.eclipse.uml2.uml.ExecutionSpecification;
-import org.eclipse.uml2.uml.Gate;
-import org.eclipse.uml2.uml.InteractionFragment;
-import org.eclipse.uml2.uml.Lifeline;
-import org.eclipse.uml2.uml.Message;
-import org.eclipse.uml2.uml.MessageEnd;
-import org.eclipse.uml2.uml.MessageSort;
-import org.eclipse.uml2.uml.NamedElement;
-import org.eclipse.uml2.uml.OccurrenceSpecification;
-
-/**
- * Helper class for determine the message connections. Both for connecting and reconnecting.
- *
- * @author Jin Liu (jin.liu@soyatec.com)
- */
-public class MessageConnectionHelper {
-
- public static boolean debug = false;
-
- private MessageConnectionHelper() {
- }
-
- public static Element getMessageTarget(Message message) {
- Element target = null;
- MessageEnd receiveEvent = message.getReceiveEvent();
- if (receiveEvent instanceof OccurrenceSpecification) {
- EList<Lifeline> covereds = ((OccurrenceSpecification) receiveEvent).getCovereds();
- if (!covereds.isEmpty()) {
- target = covereds.get(0);
- }
- } else if (receiveEvent instanceof Gate
- // special handling of SyncMessages due to #425666
- && MessageSort.SYNCH_CALL_LITERAL != message.getMessageSort()) {
- target = ((Gate) receiveEvent).getOwner();
- }
- return target;
- }
-
- public static Element getMessageSource(Message message) {
- Element source = null;
- MessageEnd sendEvent = message.getSendEvent();
- if (sendEvent instanceof OccurrenceSpecification) {
- EList<Lifeline> covereds = ((OccurrenceSpecification) sendEvent).getCovereds();
- if (!covereds.isEmpty()) {
- source = covereds.get(0);
- }
- } else if (sendEvent instanceof Gate
- // special handling of SyncMessages due to #425666
- && MessageSort.SYNCH_CALL_LITERAL != message.getMessageSort()) {
- source = ((Gate) sendEvent).getOwner();
- }
- return source;
- }
-
- public static boolean canReorientSource(Message message, Element newSource) {
- if (message == null || newSource == null) {
- return false;
- }
- return canExist(message, newSource, getMessageTarget(message));
- }
-
- public static boolean canReorientTarget(Message message, Element newTarget) {
- if (message == null || newTarget == null) {
- return false;
- }
- return canExist(message, getMessageSource(message), newTarget);
- }
-
- public static boolean canExist(Message message, Element source, Element target) {
- MessageSort messageSort = null;
- if (message != null) {
- messageSort = message.getMessageSort();
- }
- return canExist(message, messageSort, source, target);
- }
-
- public static boolean canExist(Message message, MessageSort messageSort, Element source, Element target) {
- if (debug) {
- print(messageSort, source, target);
- }
- if (MessageSort.ASYNCH_CALL_LITERAL == messageSort) {
- return canExistAsynchMessage(message, source, target);
- } else if (MessageSort.ASYNCH_SIGNAL_LITERAL == messageSort) {
- if (source == null) {
- return canExistFoundMessage(message, target);
- }
- if (target == null) {
- return canExistLostMessage(message, source);
- }
- } else if (MessageSort.SYNCH_CALL_LITERAL == messageSort) {
- return canExistSynchMessage(message, source, target);
- } else if (MessageSort.CREATE_MESSAGE_LITERAL == messageSort) {
- return canExistCreateMessage(message, source, target);
- } else if (MessageSort.DELETE_MESSAGE_LITERAL == messageSort) {
- return canExistDeleteMessage(message, source, target);
- } else if (MessageSort.REPLY_LITERAL == messageSort) {
- return canExistReplyMessage(message, source, target);
- }
- return false;
- }
-
- public static boolean canExist(MessageSort messageSort, Element source, Element target) {
- return canExist(null, messageSort, source, target);
- }
-
- private static void print(MessageSort messageSort, Element source, Element target) {
- StringBuffer buf = new StringBuffer();
- if (messageSort != null) {
- buf.append(messageSort.getName());
- buf.append("[");
- }
- buf.append("Source: ");
- if (source != null) {
- buf.append(source.eClass().getName());
- if (source instanceof NamedElement) {
- buf.append("(");
- buf.append(((NamedElement) source).getName());
- buf.append(")");
- }
- } else {
- buf.append("null");
- }
- buf.append(", Target: ");
- if (target != null) {
- buf.append(target.eClass().getName());
- if (target instanceof NamedElement) {
- buf.append("(");
- buf.append(((NamedElement) target).getName());
- buf.append(")");
- }
- } else {
- buf.append("null");
- }
- buf.append("]");
- System.out.println(new String(buf));
- }
-
- public static boolean canExistReplyMessage(Message message, Element source, Element target) {
- if (target instanceof Message) {
- return false;
- }
- if (source instanceof ExecutionSpecification && target instanceof Lifeline) {
- if (((ExecutionSpecification) source).getCovereds().contains(target)) {
- return false;
- }
- }
- if (target instanceof Gate) {
- Message ownMessage = ((Gate) target).getMessage();
- if (ownMessage == null) {
- return true;
- }
- return ownMessage == message;
- }
- return true;
- }
-
- public static boolean canExistDeleteMessage(Message message, Element source, Element target) {
- return true;
- }
-
- public static boolean canExistCreateMessage(Message message, Element source, Element target) {
- if (target != null) {
- if (false == target instanceof Lifeline) {
- return false;
- }
- if (target == source) {
- return false;
- }
- }
- return true;
- }
-
- public static boolean canExistSynchMessage(Message message, Element source, Element target) {
- if (target instanceof Message) {
- return false;
- }
- if (source != null && !(source instanceof ExecutionSpecification || source instanceof Lifeline || source instanceof ExecutionOccurrenceSpecification || source instanceof MessageEnd)) {
- return false;
- }
- if (target != null && !(target instanceof ExecutionSpecification || target instanceof Lifeline || target instanceof ExecutionOccurrenceSpecification || target instanceof MessageEnd)) {
- return false;
- }
- if (source instanceof Gate) {
- Message ownerMessage = ((Gate) source).getMessage();
- return ownerMessage == null ? true : ownerMessage == message;
- }
- if (target instanceof Gate) {
- return false;
- }
- return true;
- }
-
- public static boolean canExistLostMessage(Message message, Element source) {
- if (source instanceof Gate) {
- Message ownMessage = ((Gate) source).getMessage();
- if (ownMessage == null) {
- return true;
- }
- return ownMessage == message;
- }
- return true;
- }
-
- public static boolean canExistFoundMessage(Message message, Element target) {
- if (target instanceof Gate) {
- return message == null ? ((Gate) target).getMessage() == null : message == ((Gate) target).getMessage();
- }
- return true;
- }
-
- public static boolean canExistAsynchMessage(Message message, Element source, Element target) {
- if (target instanceof Message) {
- return false;
- }
- // Only available for ExecutionSpecification and Lifeline.
- if (target != null && !(target instanceof ExecutionSpecification || target instanceof Lifeline || target instanceof InteractionFragment || target instanceof MessageEnd)) {
- return false;
- }
- if (source instanceof Gate) {
- Message ownerMessage = ((Gate) source).getMessage();
- return ownerMessage == null ? true : ownerMessage == message;
- }
- if (target instanceof Gate) {
- Message ownerMessage = ((Gate) target).getMessage();
- return ownerMessage == null ? true : ownerMessage == message;
- }
- return true;
- }
-}
+/***************************************************************************** + * Copyright (c) 2013 CEA + * + * + * 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: + * Soyatec - Initial API and implementation + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.sequence.util; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification; +import org.eclipse.uml2.uml.ExecutionSpecification; +import org.eclipse.uml2.uml.Gate; +import org.eclipse.uml2.uml.InteractionFragment; +import org.eclipse.uml2.uml.Lifeline; +import org.eclipse.uml2.uml.Message; +import org.eclipse.uml2.uml.MessageEnd; +import org.eclipse.uml2.uml.MessageSort; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.OccurrenceSpecification; + +/** + * Helper class for determine the message connections. Both for connecting and reconnecting. + * + * @author Jin Liu (jin.liu@soyatec.com) + */ +public class MessageConnectionHelper { + + public static boolean debug = false; + + private MessageConnectionHelper() { + } + + public static Element getMessageTarget(Message message) { + Element target = null; + MessageEnd receiveEvent = message.getReceiveEvent(); + if (receiveEvent instanceof OccurrenceSpecification) { + EList<Lifeline> covereds = ((OccurrenceSpecification) receiveEvent).getCovereds(); + if (!covereds.isEmpty()) { + target = covereds.get(0); + } + } else if (receiveEvent instanceof Gate + // special handling of SyncMessages due to #425666 + && MessageSort.SYNCH_CALL_LITERAL != message.getMessageSort()) { + target = ((Gate) receiveEvent).getOwner(); + } + return target; + } + + public static Element getMessageSource(Message message) { + Element source = null; + MessageEnd sendEvent = message.getSendEvent(); + if (sendEvent instanceof OccurrenceSpecification) { + EList<Lifeline> covereds = ((OccurrenceSpecification) sendEvent).getCovereds(); + if (!covereds.isEmpty()) { + source = covereds.get(0); + } + } else if (sendEvent instanceof Gate + // special handling of SyncMessages due to #425666 + && MessageSort.SYNCH_CALL_LITERAL != message.getMessageSort()) { + source = ((Gate) sendEvent).getOwner(); + } + return source; + } + + public static boolean canReorientSource(Message message, Element newSource) { + if (message == null || newSource == null) { + return false; + } + return canExist(message, newSource, getMessageTarget(message)); + } + + public static boolean canReorientTarget(Message message, Element newTarget) { + if (message == null || newTarget == null) { + return false; + } + return canExist(message, getMessageSource(message), newTarget); + } + + public static boolean canExist(Message message, Element source, Element target) { + MessageSort messageSort = null; + if (message != null) { + messageSort = message.getMessageSort(); + } + return canExist(message, messageSort, source, target); + } + + public static boolean canExist(Message message, MessageSort messageSort, Element source, Element target) { + if (debug) { + print(messageSort, source, target); + } + if (MessageSort.ASYNCH_CALL_LITERAL == messageSort) { + return canExistAsynchMessage(message, source, target); + } else if (MessageSort.ASYNCH_SIGNAL_LITERAL == messageSort) { + if (source == null) { + return canExistFoundMessage(message, target); + } + if (target == null) { + return canExistLostMessage(message, source); + } + } else if (MessageSort.SYNCH_CALL_LITERAL == messageSort) { + return canExistSynchMessage(message, source, target); + } else if (MessageSort.CREATE_MESSAGE_LITERAL == messageSort) { + return canExistCreateMessage(message, source, target); + } else if (MessageSort.DELETE_MESSAGE_LITERAL == messageSort) { + return canExistDeleteMessage(message, source, target); + } else if (MessageSort.REPLY_LITERAL == messageSort) { + return canExistReplyMessage(message, source, target); + } + return false; + } + + public static boolean canExist(MessageSort messageSort, Element source, Element target) { + return canExist(null, messageSort, source, target); + } + + private static void print(MessageSort messageSort, Element source, Element target) { + StringBuffer buf = new StringBuffer(); + if (messageSort != null) { + buf.append(messageSort.getName()); + buf.append("["); + } + buf.append("Source: "); + if (source != null) { + buf.append(source.eClass().getName()); + if (source instanceof NamedElement) { + buf.append("("); + buf.append(UMLLabelInternationalization.getInstance().getLabel(((NamedElement) source))); + buf.append(")"); + } + } else { + buf.append("null"); + } + buf.append(", Target: "); + if (target != null) { + buf.append(target.eClass().getName()); + if (target instanceof NamedElement) { + buf.append("("); + buf.append(UMLLabelInternationalization.getInstance().getLabel(((NamedElement) target))); + buf.append(")"); + } + } else { + buf.append("null"); + } + buf.append("]"); + System.out.println(new String(buf)); + } + + public static boolean canExistReplyMessage(Message message, Element source, Element target) { + if (target instanceof Message) { + return false; + } + if (source instanceof ExecutionSpecification && target instanceof Lifeline) { + if (((ExecutionSpecification) source).getCovereds().contains(target)) { + return false; + } + } + if (target instanceof Gate) { + Message ownMessage = ((Gate) target).getMessage(); + if (ownMessage == null) { + return true; + } + return ownMessage == message; + } + return true; + } + + public static boolean canExistDeleteMessage(Message message, Element source, Element target) { + return true; + } + + public static boolean canExistCreateMessage(Message message, Element source, Element target) { + if (target != null) { + if (false == target instanceof Lifeline) { + return false; + } + if (target == source) { + return false; + } + } + return true; + } + + public static boolean canExistSynchMessage(Message message, Element source, Element target) { + if (target instanceof Message) { + return false; + } + if (source != null && !(source instanceof ExecutionSpecification || source instanceof Lifeline || source instanceof ExecutionOccurrenceSpecification || source instanceof MessageEnd)) { + return false; + } + if (target != null && !(target instanceof ExecutionSpecification || target instanceof Lifeline || target instanceof ExecutionOccurrenceSpecification || target instanceof MessageEnd)) { + return false; + } + if (source instanceof Gate) { + Message ownerMessage = ((Gate) source).getMessage(); + return ownerMessage == null ? true : ownerMessage == message; + } + if (target instanceof Gate) { + return false; + } + return true; + } + + public static boolean canExistLostMessage(Message message, Element source) { + if (source instanceof Gate) { + Message ownMessage = ((Gate) source).getMessage(); + if (ownMessage == null) { + return true; + } + return ownMessage == message; + } + return true; + } + + public static boolean canExistFoundMessage(Message message, Element target) { + if (target instanceof Gate) { + return message == null ? ((Gate) target).getMessage() == null : message == ((Gate) target).getMessage(); + } + return true; + } + + public static boolean canExistAsynchMessage(Message message, Element source, Element target) { + if (target instanceof Message) { + return false; + } + // Only available for ExecutionSpecification and Lifeline. + if (target != null && !(target instanceof ExecutionSpecification || target instanceof Lifeline || target instanceof InteractionFragment || target instanceof MessageEnd)) { + return false; + } + if (source instanceof Gate) { + Message ownerMessage = ((Gate) source).getMessage(); + return ownerMessage == null ? true : ownerMessage == message; + } + if (target instanceof Gate) { + Message ownerMessage = ((Gate) target).getMessage(); + return ownerMessage == null ? true : ownerMessage == message; + } + return true; + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OccurrenceSpecificationHelper.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OccurrenceSpecificationHelper.java index 8a03aae6d43..500e5109a5d 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OccurrenceSpecificationHelper.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OccurrenceSpecificationHelper.java @@ -1,160 +1,169 @@ -/*****************************************************************************
- * Copyright (c) 2013, 2014 Soyatec, CEA, and others
- *
- *
- * 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:
- * Soyatec - Initial API and implementation
- * Christian W. Damus (CEA) - bug 426732
- *
- *****************************************************************************/
-package org.eclipse.papyrus.uml.diagram.sequence.util;
-
-import java.util.Collection;
-import java.util.List;
-
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EStructuralFeature;
-import org.eclipse.emf.ecore.EStructuralFeature.Setting;
-import org.eclipse.emf.ecore.util.EcoreUtil;
-import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
-import org.eclipse.uml2.uml.Element;
-import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification;
-import org.eclipse.uml2.uml.ExecutionSpecification;
-import org.eclipse.uml2.uml.Interaction;
-import org.eclipse.uml2.uml.InteractionOperand;
-import org.eclipse.uml2.uml.Lifeline;
-import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
-import org.eclipse.uml2.uml.OccurrenceSpecification;
-import org.eclipse.uml2.uml.UMLPackage;
-
-/**
- * Update execution ends to message ends for Sync and Reply message, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=402975
- *
- * @author Jin Liu (jin.liu@soyatec.com)
- */
-public class OccurrenceSpecificationHelper {
-
- /**
- * Constructor.
- *
- */
- private OccurrenceSpecificationHelper() {
- }
-
- public static boolean resetExecutionStart(ExecutionSpecification execution, Element newStart) {
- if (!(newStart instanceof OccurrenceSpecification)) {
- return false;
- }
- return resetExecutionEnd(execution, (OccurrenceSpecification) newStart, true);
- }
-
- public static boolean resetExecutionFinish(ExecutionSpecification execution, Element newFinish) {
- if (!(newFinish instanceof OccurrenceSpecification)) {
- return false;
- }
- return resetExecutionEnd(execution, (OccurrenceSpecification) newFinish, false);
- }
-
- private static void copyInfo(OccurrenceSpecification fromOS, OccurrenceSpecification toOS) {
- if (fromOS == null || toOS == null) {
- return;
- }
- EList<Lifeline> covereds = fromOS.getCovereds();
- for (Lifeline lifeline : covereds) {
- if (toOS.getCovereds().contains(lifeline)) {
- continue;
- }
- toOS.getCovereds().add(lifeline);
- }
- }
-
- public static boolean resetExecutionEnd(ExecutionSpecification execution, OccurrenceSpecification newEnd, boolean isStart) {
- if (execution == null || newEnd == null) {
- return false;
- }
- OccurrenceSpecification oldEnd = isStart ? execution.getStart() : execution.getFinish();
- if (newEnd.eContainer() == null) {
- EObject eContainer = oldEnd != null ? oldEnd.eContainer() : execution.eContainer();
- if (eContainer instanceof Interaction) {
- newEnd.setEnclosingInteraction((Interaction) eContainer);
- } else if (eContainer instanceof InteractionOperand) {
- newEnd.setEnclosingOperand((InteractionOperand) eContainer);
- }
- }
- if (newEnd.getName() == null) {
- if (isStart) {
- newEnd.setName(execution.getName() + "Start");
- } else {
- newEnd.setName(execution.getName() + "Finish");
- }
- }
- copyInfo(oldEnd, newEnd);
- if (newEnd instanceof ExecutionOccurrenceSpecification) {
- ((ExecutionOccurrenceSpecification) newEnd).setExecution(execution);
- }
- if (isStart) {
- execution.setStart(newEnd);
- } else {
- execution.setFinish(newEnd);
- }
- if (canBeRemoved(oldEnd, newEnd, isStart)) {
- // "coveredBy" is bidirectional so must be cleaned prior to deletion of element itself
- // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=451320
- oldEnd.getCovereds().clear();
-
- EcoreUtil.remove(oldEnd);
- }
- return true;
- }
-
- /**
- * The given <code>Occurrence Specification</code> object can be removed without any references.
- *
- * @param isStart
- */
- private static boolean canBeRemoved(OccurrenceSpecification os, OccurrenceSpecification copy, boolean isStart) {
- if (os == null || copy == null || os instanceof MessageOccurrenceSpecification) {
- return false;
- }
- Collection<Setting> usages = EMFHelper.getUsages(os);
- for (Setting setting : usages) {
- Object osValue = setting.get(true);
- if (osValue instanceof List<?> && ((List<?>) osValue).contains(copy)) {
- continue;
- }
- EStructuralFeature feature = setting.getEStructuralFeature();
- if (isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Start() == feature) {
- continue;
- } else if (!isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Finish() == feature) {
- continue;
- }
- }
- return true;
- }
-
- /**
- * Find an Execution with the given end.
- */
- public static ExecutionSpecification findExecutionWith(OccurrenceSpecification end, boolean isStart) {
- if (end instanceof ExecutionOccurrenceSpecification) {
- return ((ExecutionOccurrenceSpecification) end).getExecution();
- }
- Collection<Setting> usages = EMFHelper.getUsages(end);
- for (Setting setting : usages) {
- EObject eObject = setting.getEObject();
- EStructuralFeature feature = setting.getEStructuralFeature();
- if (isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Start() == feature) {
- return (ExecutionSpecification) eObject;
- } else if (!isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Finish() == feature) {
- return (ExecutionSpecification) eObject;
- }
- }
- return null;
- }
-}
+/***************************************************************************** + * Copyright (c) 2013, 2014 Soyatec, CEA, and others + * + * + * 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: + * Soyatec - Initial API and implementation + * Christian W. Damus (CEA) - bug 426732 + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.sequence.util; + +import java.util.Collection; +import java.util.List; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.EStructuralFeature.Setting; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; +import org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils; +import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification; +import org.eclipse.uml2.uml.ExecutionSpecification; +import org.eclipse.uml2.uml.Interaction; +import org.eclipse.uml2.uml.InteractionOperand; +import org.eclipse.uml2.uml.Lifeline; +import org.eclipse.uml2.uml.MessageOccurrenceSpecification; +import org.eclipse.uml2.uml.OccurrenceSpecification; +import org.eclipse.uml2.uml.UMLPackage; + +/** + * Update execution ends to message ends for Sync and Reply message, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=402975 + * + * @author Jin Liu (jin.liu@soyatec.com) + */ +public class OccurrenceSpecificationHelper { + + /** + * Constructor. + * + */ + private OccurrenceSpecificationHelper() { + } + + public static boolean resetExecutionStart(ExecutionSpecification execution, Element newStart) { + if (!(newStart instanceof OccurrenceSpecification)) { + return false; + } + return resetExecutionEnd(execution, (OccurrenceSpecification) newStart, true); + } + + public static boolean resetExecutionFinish(ExecutionSpecification execution, Element newFinish) { + if (!(newFinish instanceof OccurrenceSpecification)) { + return false; + } + return resetExecutionEnd(execution, (OccurrenceSpecification) newFinish, false); + } + + private static void copyInfo(OccurrenceSpecification fromOS, OccurrenceSpecification toOS) { + if (fromOS == null || toOS == null) { + return; + } + EList<Lifeline> covereds = fromOS.getCovereds(); + for (Lifeline lifeline : covereds) { + if (toOS.getCovereds().contains(lifeline)) { + continue; + } + toOS.getCovereds().add(lifeline); + } + } + + public static boolean resetExecutionEnd(ExecutionSpecification execution, OccurrenceSpecification newEnd, boolean isStart) { + if (execution == null || newEnd == null) { + return false; + } + OccurrenceSpecification oldEnd = isStart ? execution.getStart() : execution.getFinish(); + if (newEnd.eContainer() == null) { + EObject eContainer = oldEnd != null ? oldEnd.eContainer() : execution.eContainer(); + if (eContainer instanceof Interaction) { + newEnd.setEnclosingInteraction((Interaction) eContainer); + } else if (eContainer instanceof InteractionOperand) { + newEnd.setEnclosingOperand((InteractionOperand) eContainer); + } + } + copyInfo(oldEnd, newEnd); + if (newEnd instanceof ExecutionOccurrenceSpecification) { + ((ExecutionOccurrenceSpecification) newEnd).setExecution(execution); + } + if (isStart) { + execution.setStart(newEnd); + } else { + execution.setFinish(newEnd); + } + if (newEnd.getName() == null) { + if (isStart) { + newEnd.setName(execution.getName() + "Start"); + if(InternationalizationPreferencesUtils.getInternationalizationPreference(execution) && null != UMLLabelInternationalization.getInstance().getLabelWithoutUML(execution)){ + UMLLabelInternationalization.getInstance().setLabel(newEnd, UMLLabelInternationalization.getInstance().getLabelWithoutUML(execution) + "Start", null); + } + } else { + newEnd.setName(execution.getName() + "Finish"); + if(InternationalizationPreferencesUtils.getInternationalizationPreference(execution) && null != UMLLabelInternationalization.getInstance().getLabelWithoutUML(execution)){ + UMLLabelInternationalization.getInstance().setLabel(newEnd, UMLLabelInternationalization.getInstance().getLabelWithoutUML(execution) + "Finish", null); + } + } + } + if (canBeRemoved(oldEnd, newEnd, isStart)) { + // "coveredBy" is bidirectional so must be cleaned prior to deletion of element itself + // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=451320 + oldEnd.getCovereds().clear(); + + EcoreUtil.remove(oldEnd); + } + return true; + } + + /** + * The given <code>Occurrence Specification</code> object can be removed without any references. + * + * @param isStart + */ + private static boolean canBeRemoved(OccurrenceSpecification os, OccurrenceSpecification copy, boolean isStart) { + if (os == null || copy == null || os instanceof MessageOccurrenceSpecification) { + return false; + } + Collection<Setting> usages = EMFHelper.getUsages(os); + for (Setting setting : usages) { + Object osValue = setting.get(true); + if (osValue instanceof List<?> && ((List<?>) osValue).contains(copy)) { + continue; + } + EStructuralFeature feature = setting.getEStructuralFeature(); + if (isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Start() == feature) { + continue; + } else if (!isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Finish() == feature) { + continue; + } + } + return true; + } + + /** + * Find an Execution with the given end. + */ + public static ExecutionSpecification findExecutionWith(OccurrenceSpecification end, boolean isStart) { + if (end instanceof ExecutionOccurrenceSpecification) { + return ((ExecutionOccurrenceSpecification) end).getExecution(); + } + Collection<Setting> usages = EMFHelper.getUsages(end); + for (Setting setting : usages) { + EObject eObject = setting.getEObject(); + EStructuralFeature feature = setting.getEStructuralFeature(); + if (isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Start() == feature) { + return (ExecutionSpecification) eObject; + } else if (!isStart && UMLPackage.eINSTANCE.getExecutionSpecification_Finish() == feature) { + return (ExecutionSpecification) eObject; + } + } + return null; + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OperationUtil.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OperationUtil.java index 8bdbe346175..15afcc8f7d8 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OperationUtil.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/OperationUtil.java @@ -1,272 +1,286 @@ -package org.eclipse.papyrus.uml.diagram.sequence.util;
-
-import java.util.Collection;
-import java.util.Iterator;
-
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.papyrus.infra.tools.util.StringHelper;
-import org.eclipse.papyrus.uml.tools.utils.ICustomAppearance;
-import org.eclipse.papyrus.uml.tools.utils.MultiplicityElementUtil;
-import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
-import org.eclipse.papyrus.uml.tools.utils.ParameterUtil;
-import org.eclipse.papyrus.uml.tools.utils.TypeUtil;
-import org.eclipse.papyrus.uml.tools.utils.TypedElementUtil;
-import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil;
-import org.eclipse.uml2.uml.Constraint;
-import org.eclipse.uml2.uml.Message;
-import org.eclipse.uml2.uml.Operation;
-import org.eclipse.uml2.uml.Parameter;
-import org.eclipse.uml2.uml.ParameterDirectionKind;
-import org.eclipse.uml2.uml.ValueSpecification;
-
-public class OperationUtil {
-
- public static String getCustomLabel(Message e, int paramIndex, Parameter parameter, Collection<String> displayValue) {
- StringBuffer buffer = new StringBuffer();
- // visibility
- buffer.append(" ");
- if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) {
- buffer.append(NamedElementUtil.getVisibilityAsSign(parameter));
- }
- // direction property
- if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DIRECTION)) {
- buffer.append(" ");
- buffer.append(parameter.getDirection().getLiteral());
- }
- boolean showEqualMark = false;
- // name
- if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_NAME)) {
- buffer.append(" ");
- String name = StringHelper.trimToEmpty(parameter.getName());
- if (name.trim().length() > 0) {
- showEqualMark = true;
- }
- buffer.append(name);
- }
- if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_TYPE)) {
- // type
- if (parameter.getType() != null) {
- buffer.append(": " + StringHelper.trimToEmpty(parameter.getType().getName()));
- } else {
- buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
- }
- showEqualMark = true;
- }
- if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_MULTIPLICITY)) {
- // multiplicity -> do not display [1]
- String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(parameter);
- buffer.append(multiplicity);
- }
- if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) {
- String value = getValue(e, paramIndex, parameter);
- if (value != null) {
- if (showEqualMark) {
- buffer.append(" = ");
- }
- buffer.append(value);
- }
- } else if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DEFAULT)) {
- // default value
- if (parameter.getDefault() != null) {
- if (showEqualMark) {
- buffer.append(" = ");
- }
- buffer.append(parameter.getDefault());
- }
- }
- if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) {
- boolean multiLine = displayValue.contains(ICustomAppearance.DISP_MULTI_LINE);
- // property modifiers
- String modifiers = ParameterUtil.getModifiersAsString(parameter, multiLine);
- if (!modifiers.equals("")) {
- if (multiLine) {
- buffer.append("\n");
- }
- buffer.append(modifiers);
- }
- }
- return buffer.toString();
- }
-
- private static String getValue(Message e, int paramIndex, Parameter parameter) {
- try {
- EList<ValueSpecification> arguments = e.getArguments();
- if (arguments.size() > paramIndex) {
- return ValueSpecificationUtil.getSpecificationValue(arguments.get(paramIndex));
- }
- } catch (Exception e1) {
- }
- return null;
- }
-
- public static String getCustomLabel(Message message, Operation operation, Collection<String> displayValue) {
- StringBuffer buffer = new StringBuffer();
- buffer.append(" "); // adds " " first for correct display considerations
- // visibility
- if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) {
- buffer.append(NamedElementUtil.getVisibilityAsSign(operation));
- }
- // name
- if (displayValue.contains(ICustomAppearance.DISP_NAME)) {
- buffer.append(" ");
- buffer.append(StringHelper.trimToEmpty(operation.getName()));
- }
- //
- // parameters : '(' parameter-list ')'
- buffer.append("(");
- buffer.append(getParametersAsString(message, operation, displayValue));
- buffer.append(")");
- // return type
- if (displayValue.contains(ICustomAppearance.DISP_RT_TYPE) || displayValue.contains(ICustomAppearance.DISP_TYPE)) {
- buffer.append(getReturnTypeAsString(operation, displayValue));
- }
- // modifiers
- if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) {
- String modifiers = getModifiersAsString(operation);
- if (!modifiers.equals("")) {
- buffer.append("{");
- buffer.append(modifiers);
- buffer.append("}");
- }
- }
- return buffer.toString();
- }
-
- /**
- * Returns operation modifiers as string, separated with comma.
- *
- * @return a string containing the modifiers
- */
- private static String getModifiersAsString(Operation operation) {
- StringBuffer buffer = new StringBuffer();
- boolean needsComma = false;
- // Return parameter modifiers
- Parameter returnParameter = OperationUtil.getReturnParameter(operation);
- if (returnParameter != null) {
- // non unique parameter
- if (!returnParameter.isUnique()) {
- buffer.append("nonunique");
- needsComma = true;
- }
- // return parameter has ordered values
- if (returnParameter.isOrdered()) {
- if (needsComma) {
- buffer.append(", ");
- }
- buffer.append("ordered");
- needsComma = true;
- }
- }
- // is the operation a query ?
- if (operation.isQuery()) {
- if (needsComma) {
- buffer.append(", ");
- }
- buffer.append("query");
- needsComma = true;
- }
- // is the operation redefining another operation ?
- Iterator<Operation> it = operation.getRedefinedOperations().iterator();
- while (it.hasNext()) {
- Operation currentOperation = it.next();
- if (needsComma) {
- buffer.append(", ");
- }
- buffer.append("redefines ");
- buffer.append(currentOperation.getName());
- needsComma = true;
- }
- // has the operation a constraint ?
- Iterator<Constraint> it2 = operation.getOwnedRules().iterator();
- while (it2.hasNext()) {
- Constraint constraint = it2.next();
- if (needsComma) {
- buffer.append(", ");
- }
- if (constraint.getSpecification() != null) {
- buffer.append(constraint.getSpecification().stringValue());
- }
- needsComma = true;
- }
- return buffer.toString();
- }
-
- /**
- * Returns return parameter label as a string, string parametrized with a style mask.
- *
- * @param style
- * the mask that indicates which element to display
- * @return a string containing the return parameter type
- */
- private static String getReturnTypeAsString(Operation operation, Collection<String> displayValue) {
- boolean displayType = displayValue.contains(ICustomAppearance.DISP_RT_TYPE) || displayValue.contains(ICustomAppearance.DISP_TYPE);
- boolean displayMultiplicity = displayValue.contains(ICustomAppearance.DISP_RT_MULTIPLICITY) || displayValue.contains(ICustomAppearance.DISP_MULTIPLICITY);
- StringBuffer label = new StringBuffer("");
- // Retrieve the return parameter (assume to be unique if defined)
- Parameter returnParameter = getReturnParameter(operation);
- // Create the string for the return type
- if (returnParameter == null) {
- // no-operation: label = ""
- } else if (!displayType && !displayMultiplicity) {
- // no-operation: label = ""
- } else {
- label.append(": ");
- if (displayType) {
- label.append(TypedElementUtil.getTypeAsString(returnParameter));
- }
- if (displayMultiplicity) {
- label.append(MultiplicityElementUtil.getMultiplicityAsString(returnParameter));
- }
- }
- return label.toString();
- }
-
- /**
- * Gives the return parameter for this operation, or <code>null</code> if none exists.
- *
- * @return the return parameter of the operation or <code>null</code>
- */
- private static Parameter getReturnParameter(Operation operation) {
- // Retrieve the return parameter (assume to be unique if defined)
- Parameter returnParameter = null;
- Iterator<Parameter> it = operation.getOwnedParameters().iterator();
- while ((returnParameter == null) && (it.hasNext())) {
- Parameter parameter = it.next();
- if (parameter.getDirection().equals(ParameterDirectionKind.RETURN_LITERAL)) {
- returnParameter = parameter;
- }
- }
- return returnParameter;
- }
-
- /**
- * Returns operation parameters as a string, the label is customized using a bit mask
- *
- * @param e
- *
- * @return a string containing all parameters separated by commas
- */
- private static String getParametersAsString(Message e, Operation operation, Collection<String> displayValue) {
- StringBuffer paramString = new StringBuffer();
- Iterator<Parameter> paramIterator = operation.getOwnedParameters().iterator();
- boolean firstParameter = true;
- int paramIndex = 0;
- while (paramIterator.hasNext()) {
- Parameter parameter = paramIterator.next();
- // Do not include return parameters
- if (!parameter.getDirection().equals(ParameterDirectionKind.RETURN_LITERAL)) {
- // get the label for this parameter
- String parameterString = getCustomLabel(e, paramIndex, parameter, displayValue).trim();
- paramIndex++;
- if (!parameterString.equals("")) {
- if (!firstParameter) {
- paramString.append(", ");
- }
- paramString.append(parameterString);
- firstParameter = false;
- }
- }
- }
- return paramString.toString();
- }
-}
+/***************************************************************************** + * Copyright (c) 2016 CEA LIST and others. + * + * 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: + * CEA LIST - Initial API and implementation + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.sequence.util; + +import java.util.Collection; +import java.util.Iterator; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.papyrus.infra.tools.util.StringHelper; +import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization; +import org.eclipse.papyrus.uml.tools.utils.ICustomAppearance; +import org.eclipse.papyrus.uml.tools.utils.MultiplicityElementUtil; +import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil; +import org.eclipse.papyrus.uml.tools.utils.ParameterUtil; +import org.eclipse.papyrus.uml.tools.utils.TypeUtil; +import org.eclipse.papyrus.uml.tools.utils.TypedElementUtil; +import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil; +import org.eclipse.uml2.uml.Constraint; +import org.eclipse.uml2.uml.Message; +import org.eclipse.uml2.uml.Operation; +import org.eclipse.uml2.uml.Parameter; +import org.eclipse.uml2.uml.ParameterDirectionKind; +import org.eclipse.uml2.uml.ValueSpecification; + +public class OperationUtil { + + public static String getCustomLabel(Message e, int paramIndex, Parameter parameter, Collection<String> displayValue) { + StringBuffer buffer = new StringBuffer(); + // visibility + buffer.append(" "); + if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) { + buffer.append(NamedElementUtil.getVisibilityAsSign(parameter)); + } + // direction property + if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DIRECTION)) { + buffer.append(" "); + buffer.append(parameter.getDirection().getLiteral()); + } + boolean showEqualMark = false; + // name + if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_NAME)) { + buffer.append(" "); + String name = StringHelper.trimToEmpty(UMLLabelInternationalization.getInstance().getLabel(parameter)); + if (name.trim().length() > 0) { + showEqualMark = true; + } + buffer.append(name); + } + if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_TYPE)) { + // type + if (parameter.getType() != null) { + buffer.append(": " + StringHelper.trimToEmpty(UMLLabelInternationalization.getInstance().getLabel(parameter.getType()))); + } else { + buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME); + } + showEqualMark = true; + } + if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_MULTIPLICITY)) { + // multiplicity -> do not display [1] + String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(parameter); + buffer.append(multiplicity); + } + if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) { + String value = getValue(e, paramIndex, parameter); + if (value != null) { + if (showEqualMark) { + buffer.append(" = "); + } + buffer.append(value); + } + } else if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DEFAULT)) { + // default value + if (parameter.getDefault() != null) { + if (showEqualMark) { + buffer.append(" = "); + } + buffer.append(parameter.getDefault()); + } + } + if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) { + boolean multiLine = displayValue.contains(ICustomAppearance.DISP_MULTI_LINE); + // property modifiers + String modifiers = ParameterUtil.getModifiersAsString(parameter, multiLine); + if (!modifiers.equals("")) { + if (multiLine) { + buffer.append("\n"); + } + buffer.append(modifiers); + } + } + return buffer.toString(); + } + + private static String getValue(Message e, int paramIndex, Parameter parameter) { + try { + EList<ValueSpecification> arguments = e.getArguments(); + if (arguments.size() > paramIndex) { + return ValueSpecificationUtil.getSpecificationValue(arguments.get(paramIndex), true); + } + } catch (Exception e1) { + } + return null; + } + + public static String getCustomLabel(Message message, Operation operation, Collection<String> displayValue) { + StringBuffer buffer = new StringBuffer(); + buffer.append(" "); // adds " " first for correct display considerations + // visibility + if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) { + buffer.append(NamedElementUtil.getVisibilityAsSign(operation)); + } + // name + if (displayValue.contains(ICustomAppearance.DISP_NAME)) { + buffer.append(" "); + buffer.append(StringHelper.trimToEmpty(UMLLabelInternationalization.getInstance().getLabel(operation))); + } + // + // parameters : '(' parameter-list ')' + buffer.append("("); + buffer.append(getParametersAsString(message, operation, displayValue)); + buffer.append(")"); + // return type + if (displayValue.contains(ICustomAppearance.DISP_RT_TYPE) || displayValue.contains(ICustomAppearance.DISP_TYPE)) { + buffer.append(getReturnTypeAsString(operation, displayValue)); + } + // modifiers + if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) { + String modifiers = getModifiersAsString(operation); + if (!modifiers.equals("")) { + buffer.append("{"); + buffer.append(modifiers); + buffer.append("}"); + } + } + return buffer.toString(); + } + + /** + * Returns operation modifiers as string, separated with comma. + * + * @return a string containing the modifiers + */ + private static String getModifiersAsString(Operation operation) { + StringBuffer buffer = new StringBuffer(); + boolean needsComma = false; + // Return parameter modifiers + Parameter returnParameter = OperationUtil.getReturnParameter(operation); + if (returnParameter != null) { + // non unique parameter + if (!returnParameter.isUnique()) { + buffer.append("nonunique"); + needsComma = true; + } + // return parameter has ordered values + if (returnParameter.isOrdered()) { + if (needsComma) { + buffer.append(", "); + } + buffer.append("ordered"); + needsComma = true; + } + } + // is the operation a query ? + if (operation.isQuery()) { + if (needsComma) { + buffer.append(", "); + } + buffer.append("query"); + needsComma = true; + } + // is the operation redefining another operation ? + Iterator<Operation> it = operation.getRedefinedOperations().iterator(); + while (it.hasNext()) { + Operation currentOperation = it.next(); + if (needsComma) { + buffer.append(", "); + } + buffer.append("redefines "); + buffer.append(UMLLabelInternationalization.getInstance().getLabel(currentOperation)); + needsComma = true; + } + // has the operation a constraint ? + Iterator<Constraint> it2 = operation.getOwnedRules().iterator(); + while (it2.hasNext()) { + Constraint constraint = it2.next(); + if (needsComma) { + buffer.append(", "); + } + if (constraint.getSpecification() != null) { + buffer.append(constraint.getSpecification().stringValue()); + } + needsComma = true; + } + return buffer.toString(); + } + + /** + * Returns return parameter label as a string, string parametrized with a style mask. + * + * @param style + * the mask that indicates which element to display + * @return a string containing the return parameter type + */ + private static String getReturnTypeAsString(Operation operation, Collection<String> displayValue) { + boolean displayType = displayValue.contains(ICustomAppearance.DISP_RT_TYPE) || displayValue.contains(ICustomAppearance.DISP_TYPE); + boolean displayMultiplicity = displayValue.contains(ICustomAppearance.DISP_RT_MULTIPLICITY) || displayValue.contains(ICustomAppearance.DISP_MULTIPLICITY); + StringBuffer label = new StringBuffer(""); + // Retrieve the return parameter (assume to be unique if defined) + Parameter returnParameter = getReturnParameter(operation); + // Create the string for the return type + if (returnParameter == null) { + // no-operation: label = "" + } else if (!displayType && !displayMultiplicity) { + // no-operation: label = "" + } else { + label.append(": "); + if (displayType) { + label.append(TypedElementUtil.getTypeAsString(returnParameter)); + } + if (displayMultiplicity) { + label.append(MultiplicityElementUtil.getMultiplicityAsString(returnParameter)); + } + } + return label.toString(); + } + + /** + * Gives the return parameter for this operation, or <code>null</code> if none exists. + * + * @return the return parameter of the operation or <code>null</code> + */ + private static Parameter getReturnParameter(Operation operation) { + // Retrieve the return parameter (assume to be unique if defined) + Parameter returnParameter = null; + Iterator<Parameter> it = operation.getOwnedParameters().iterator(); + while ((returnParameter == null) && (it.hasNext())) { + Parameter parameter = it.next(); + if (parameter.getDirection().equals(ParameterDirectionKind.RETURN_LITERAL)) { + returnParameter = parameter; + } + } + return returnParameter; + } + + /** + * Returns operation parameters as a string, the label is customized using a bit mask + * + * @param e + * + * @return a string containing all parameters separated by commas + */ + private static String getParametersAsString(Message e, Operation operation, Collection<String> displayValue) { + StringBuffer paramString = new StringBuffer(); + Iterator<Parameter> paramIterator = operation.getOwnedParameters().iterator(); + boolean firstParameter = true; + int paramIndex = 0; + while (paramIterator.hasNext()) { + Parameter parameter = paramIterator.next(); + // Do not include return parameters + if (!parameter.getDirection().equals(ParameterDirectionKind.RETURN_LITERAL)) { + // get the label for this parameter + String parameterString = getCustomLabel(e, paramIndex, parameter, displayValue).trim(); + paramIndex++; + if (!parameterString.equals("")) { + if (!firstParameter) { + paramString.append(", "); + } + paramString.append(parameterString); + firstParameter = false; + } + } + } + return paramString.toString(); + } +} diff --git a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/SignalUtil.java b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/SignalUtil.java index 63ab69a46b4..ed215584545 100644 --- a/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/SignalUtil.java +++ b/plugins/uml/diagram/org.eclipse.papyrus.uml.diagram.sequence/custom-src/org/eclipse/papyrus/uml/diagram/sequence/util/SignalUtil.java @@ -1,155 +1,169 @@ -package org.eclipse.papyrus.uml.diagram.sequence.util;
-
-import java.util.Collection;
-
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.papyrus.infra.tools.util.StringHelper;
-import org.eclipse.papyrus.uml.tools.utils.ICustomAppearance;
-import org.eclipse.papyrus.uml.tools.utils.MultiplicityElementUtil;
-import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
-import org.eclipse.papyrus.uml.tools.utils.PropertyUtil;
-import org.eclipse.papyrus.uml.tools.utils.TypeUtil;
-import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil;
-import org.eclipse.uml2.uml.Message;
-import org.eclipse.uml2.uml.Property;
-import org.eclipse.uml2.uml.Signal;
-import org.eclipse.uml2.uml.ValueSpecification;
-
-public class SignalUtil {
-
- private static String getCustomPropertyLabel(Message e, Property property, Collection<String> displayValue) {
- StringBuffer buffer = new StringBuffer();
- // visibility
- buffer.append(" ");
- if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) {
- buffer.append(NamedElementUtil.getVisibilityAsSign(property));
- }
- // derived property
- if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) {
- if (property.isDerived()) {
- buffer.append("/");
- }
- }
- boolean showEqualMark = false;
- // name
- if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_NAME)) {
- buffer.append(" ");
- String name = StringHelper.trimToEmpty(property.getName());
- if (name.trim().length() > 0) {
- showEqualMark = true;
- }
- buffer.append(name);
- }
- if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_TYPE)) {
- // type
- if (property.getType() != null) {
- buffer.append(": " + StringHelper.trimToEmpty(property.getType().getName()));
- } else {
- buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
- }
- showEqualMark = true;
- }
- if (displayValue.contains(ICustomAppearance.DISP_MULTIPLICITY)) {
- // multiplicity -> do not display [1]
- String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(property);
- buffer.append(multiplicity);
- }
- if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) {
- String value = getValue(e, property);
- if (value != null) {
- if (showEqualMark) {
- buffer.append(" = ");
- }
- buffer.append(value);
- }
- } else if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DEFAULT)) {
- // default value
- if (property.getDefaultValue() != null) {
- if (showEqualMark) {
- buffer.append(" = ");
- }
- buffer.append(ValueSpecificationUtil.getSpecificationValue(property.getDefaultValue()));
- }
- }
- if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) {
- boolean multiLine = displayValue.contains(ICustomAppearance.DISP_MULTI_LINE);
- // property modifiers
- String modifiers = PropertyUtil.getModifiersAsString(property, multiLine);
- if (!modifiers.equals("")) {
- if (multiLine) {
- buffer.append("\n");
- }
- if (!buffer.toString().endsWith(" ")) {
- buffer.append(" ");
- }
- buffer.append(modifiers);
- }
- }
- return buffer.toString();
- }
-
- private static String getValue(Message e, Property property) {
- try {
- Signal signal = (Signal) property.getOwner();
- int index = signal.getOwnedAttributes().indexOf(property);
- EList<ValueSpecification> arguments = e.getArguments();
- if (arguments.size() > index) {
- return ValueSpecificationUtil.getSpecificationValue(arguments.get(index));
- }
- } catch (Exception e1) {
- }
- return null;
- }
-
- /**
- * return the custom label of the signal, given UML2 specification and a custom style.
- *
- * @param message
- *
- * @param style
- * the integer representing the style of the label
- *
- * @return the string corresponding to the label of the signal
- */
- public static String getCustomLabel(Message message, Signal signal, Collection<String> displayValue) {
- StringBuffer buffer = new StringBuffer();
- buffer.append(" "); // adds " " first for correct display considerations
- // visibility
- if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) {
- buffer.append(NamedElementUtil.getVisibilityAsSign(signal));
- }
- // name
- if (displayValue.contains(ICustomAppearance.DISP_NAME)) {
- buffer.append(" ");
- buffer.append(StringHelper.trimToEmpty(signal.getName()));
- }
- //
- // parameters : '(' parameter-list ')'
- buffer.append("(");
- buffer.append(getPropertiesAsString(message, signal, displayValue));
- buffer.append(")");
- return buffer.toString();
- }
-
- /**
- * Returns signal properties as a string, the label is customized using a bit mask
- *
- * @return a string containing all properties separated by commas
- */
- private static String getPropertiesAsString(Message e, Signal signal, Collection<String> displayValue) {
- StringBuffer propertiesString = new StringBuffer();
- boolean firstProperty = true;
- for (Property property : signal.getOwnedAttributes()) {
- // get the label for this property
- String propertyString = getCustomPropertyLabel(e, property, displayValue).trim();
- if (!propertyString.equals("")) {
- if (!firstProperty) {
- propertiesString.append(", ");
- }
- propertiesString.append(propertyString);
- firstProperty = false;
- }
- }
- return propertiesString.toString();
- }
-}
+/***************************************************************************** + * Copyright (c) 2016 CEA LIST and others. + * + * 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: + * CEA LIST - Initial API and implementation + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905 + * + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.sequence.util; + +import java.util.Collection; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.papyrus.infra.tools.util.StringHelper; +import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization; +import org.eclipse.papyrus.uml.tools.utils.ICustomAppearance; +import org.eclipse.papyrus.uml.tools.utils.MultiplicityElementUtil; +import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil; +import org.eclipse.papyrus.uml.tools.utils.PropertyUtil; +import org.eclipse.papyrus.uml.tools.utils.TypeUtil; +import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil; +import org.eclipse.uml2.uml.Message; +import org.eclipse.uml2.uml.Property; +import org.eclipse.uml2.uml.Signal; +import org.eclipse.uml2.uml.ValueSpecification; + +public class SignalUtil { + + private static String getCustomPropertyLabel(Message e, Property property, Collection<String> displayValue) { + StringBuffer buffer = new StringBuffer(); + // visibility + buffer.append(" "); + if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) { + buffer.append(NamedElementUtil.getVisibilityAsSign(property)); + } + // derived property + if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) { + if (property.isDerived()) { + buffer.append("/"); + } + } + boolean showEqualMark = false; + // name + if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_NAME)) { + buffer.append(" "); + String name = StringHelper.trimToEmpty(UMLLabelInternationalization.getInstance().getLabel(property)); + if (name.trim().length() > 0) { + showEqualMark = true; + } + buffer.append(name); + } + if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_TYPE)) { + // type + if (property.getType() != null) { + buffer.append(": " + StringHelper.trimToEmpty(property.getType().getName())); + } else { + buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME); + } + showEqualMark = true; + } + if (displayValue.contains(ICustomAppearance.DISP_MULTIPLICITY)) { + // multiplicity -> do not display [1] + String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(property); + buffer.append(multiplicity); + } + if (displayValue.contains(ICustomAppearance.DISP_DERIVE)) { + String value = getValue(e, property); + if (value != null) { + if (showEqualMark) { + buffer.append(" = "); + } + buffer.append(value); + } + } else if (displayValue.contains(ICustomAppearance.DISP_PARAMETER_DEFAULT)) { + // default value + if (property.getDefaultValue() != null) { + if (showEqualMark) { + buffer.append(" = "); + } + buffer.append(ValueSpecificationUtil.getSpecificationValue(property.getDefaultValue(), true)); + } + } + if (displayValue.contains(ICustomAppearance.DISP_MODIFIERS)) { + boolean multiLine = displayValue.contains(ICustomAppearance.DISP_MULTI_LINE); + // property modifiers + String modifiers = PropertyUtil.getModifiersAsString(property, multiLine); + if (!modifiers.equals("")) { + if (multiLine) { + buffer.append("\n"); + } + if (!buffer.toString().endsWith(" ")) { + buffer.append(" "); + } + buffer.append(modifiers); + } + } + return buffer.toString(); + } + + private static String getValue(Message e, Property property) { + try { + Signal signal = (Signal) property.getOwner(); + int index = signal.getOwnedAttributes().indexOf(property); + EList<ValueSpecification> arguments = e.getArguments(); + if (arguments.size() > index) { + return ValueSpecificationUtil.getSpecificationValue(arguments.get(index), true); + } + } catch (Exception e1) { + } + return null; + } + + /** + * return the custom label of the signal, given UML2 specification and a custom style. + * + * @param message + * + * @param style + * the integer representing the style of the label + * + * @return the string corresponding to the label of the signal + */ + public static String getCustomLabel(Message message, Signal signal, Collection<String> displayValue) { + StringBuffer buffer = new StringBuffer(); + buffer.append(" "); // adds " " first for correct display considerations + // visibility + if (displayValue.contains(ICustomAppearance.DISP_VISIBILITY)) { + buffer.append(NamedElementUtil.getVisibilityAsSign(signal)); + } + // name + if (displayValue.contains(ICustomAppearance.DISP_NAME)) { + buffer.append(" "); + buffer.append(StringHelper.trimToEmpty(UMLLabelInternationalization.getInstance().getLabel(signal))); + } + // + // parameters : '(' parameter-list ')' + buffer.append("("); + buffer.append(getPropertiesAsString(message, signal, displayValue)); + buffer.append(")"); + return buffer.toString(); + } + + /** + * Returns signal properties as a string, the label is customized using a bit mask + * + * @return a string containing all properties separated by commas + */ + private static String getPropertiesAsString(Message e, Signal signal, Collection<String> displayValue) { + StringBuffer propertiesString = new StringBuffer(); + boolean firstProperty = true; + for (Property property : signal.getOwnedAttributes()) { + // get the label for this property + String propertyString = getCustomPropertyLabel(e, property, displayValue).trim(); + if (!propertyString.equals("")) { + if (!firstProperty) { + propertiesString.append(", "); + } + propertiesString.append(propertyString); + firstProperty = false; + } + } + return propertiesString.toString(); + } +} |