Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnsgar Radermacher2014-02-09 06:11:23 -0500
committerAnsgar Radermacher2014-02-09 06:11:23 -0500
commit3576f568fed6c2423e5ff302eda3afce541adcdf (patch)
treeee091864bc1c6331ad0e1bafc24588597fe3add1 /plugins
parent1ed7967ae48aafa33a95415b089039ea04064976 (diff)
downloadorg.eclipse.papyrus-3576f568fed6c2423e5ff302eda3afce541adcdf.tar.gz
org.eclipse.papyrus-3576f568fed6c2423e5ff302eda3afce541adcdf.tar.xz
org.eclipse.papyrus-3576f568fed6c2423e5ff302eda3afce541adcdf.zip
Bug 427740 - [QDesginer] Refactoring - move common utils to papyrus utils plugin
Diffstat (limited to 'plugins')
-rw-r--r--plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/ConnectorUtil.java180
-rw-r--r--plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/PackageUtil.java41
2 files changed, 217 insertions, 4 deletions
diff --git a/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/ConnectorUtil.java b/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/ConnectorUtil.java
new file mode 100644
index 00000000000..ffef9d79f2c
--- /dev/null
+++ b/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/ConnectorUtil.java
@@ -0,0 +1,180 @@
+/*****************************************************************************
+ * Copyright (c) 2013 CEA LIST.
+ *
+ *
+ * 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:
+ * Ansgar Radermacher ansgar.radermacher@cea.fr
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.uml.tools.utils;
+
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.ConnectableElement;
+import org.eclipse.uml2.uml.Connector;
+import org.eclipse.uml2.uml.ConnectorEnd;
+import org.eclipse.uml2.uml.Port;
+import org.eclipse.uml2.uml.Property;
+
+public class ConnectorUtil {
+
+ /**
+ * Returns true, is a port of a composite has an internal delegation to
+ * another port
+ *
+ * @param composite
+ * @param port
+ * @return
+ */
+ public static boolean isDelegation(Class composite, Port port) {
+ return (getDelegation(composite, port) != null);
+ }
+
+ /**
+ * Return the connector end of a delegation originating from a given port
+ * or null, if not such delegation exists
+ *
+ * @param composite
+ * the composite component
+ * @param port
+ * a port (may be inherited) of that implementation
+ * @return a connector end to which the port delegates or null
+ */
+ public static ConnectorEnd getDelegation(Class composite, Port port) {
+ for(Connector connector : composite.getOwnedConnectors()) {
+ if(connectsPort(connector, port)) {
+ ConnectorEnd otherEnd = connEndNotPart(connector, null);
+ if(otherEnd != null) {
+ return otherEnd;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * check if a connector connects the port that is passed as parameter
+ */
+ public static boolean connectsPort(Connector connection, Port port) {
+ // check roles of end points
+ // String qPortName = port.getQualifiedName();
+ for(ConnectorEnd end : connection.getEnds()) {
+ if(end.getRole() == port) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Simple helper function: return a connector end that is referencing
+ * the passed parameter "part" either directly (role) or via a port
+ *
+ * @param part
+ * a property within a composite
+ * @param connection
+ * @return the first connector end that is referencing a certain part
+ * passed as parameter
+ */
+ public static ConnectorEnd connEndForPart(Connector connection, Property part) {
+ // look for the end with same part (connectedPart == part)
+ for(ConnectorEnd end : connection.getEnds()) {
+ ConnectableElement role = end.getRole();
+ Property connectedPart = null;
+ if(role instanceof Port) {
+ connectedPart = end.getPartWithPort();
+ } else if(role instanceof Property) {
+ // role is not a Port but also not null => connected directly to a part
+ connectedPart = (Property)role;
+ }
+
+ if(connectedPart == part) {
+ return end;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Simple helper function: return a connector end that is *not* referencing
+ * with the passed parameter "part" either directly or via a port
+ * (useful, to get the "other" end of a connector).
+ *
+ * @param part
+ * a property within a composite
+ * @param connection
+ * @return the first connector end that is not referencing to the
+ * part passed as parameter.
+ */
+ public static ConnectorEnd connEndNotPart(Connector connection, Property part) {
+ // look for the other end (connectedPart != part)
+ for(ConnectorEnd end : connection.getEnds()) {
+ ConnectableElement role = end.getRole();
+ Property connectedPart = null;
+
+ if(role instanceof Port) {
+ connectedPart = end.getPartWithPort();
+ } else if(role instanceof Property) {
+ // role is not a Port but also not null => connected directly to a part
+ connectedPart = (Property)role;
+ }
+
+ if(connectedPart != part) {
+ return end;
+ }
+
+ }
+ return null;
+ }
+
+ /**
+ * check if a connector connects the part that is passed
+ * as parameter
+ */
+ public static boolean connectsPart(Connector connection, Property part) {
+ return (connEndForPart(connection, part) != null);
+ }
+
+ /**
+ * Return true, if passed connector is an assembly connector
+ *
+ * @param connection
+ * @return true, if assembly connection
+ */
+ public static boolean isAssembly(Connector connection) {
+ // no "null" part found => all ends are parts
+ return (connEndForPart(connection, null) == null);
+ }
+
+ /**
+ * @param composite A composite class
+ * @param partA A part within the composite
+ * @param partB Another part within the composite
+ * @return a connector, if it connects the parts A and B could within the passed composite, or null if no
+ * such connector exists
+ */
+ public static Connector getConnector(Class composite, Property partA, Property partB) {
+ for (Connector connector : composite.getOwnedConnectors()) {
+ if (ConnectorUtil.connectsPart(connector, partA) && ConnectorUtil.connectsPart(connector, partB)) {
+ return connector;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @param composite A composite class
+ * @param partA A part within the composite
+ * @param partB Another part within the composite
+ * @return true, if a connector between the parts A and B could be
+ * found within the passed composite
+ */
+ public static boolean existsConnector(Class composite, Property partA, Property partB) {
+ return getConnector(composite, partA, partB) != null;
+ }
+}
diff --git a/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/PackageUtil.java b/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/PackageUtil.java
index 45a9370a0d5..81ce8faf333 100644
--- a/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/PackageUtil.java
+++ b/plugins/uml/tools/org.eclipse.papyrus.uml.tools.utils/src/org/eclipse/papyrus/uml/tools/utils/PackageUtil.java
@@ -24,15 +24,19 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
+import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
+import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers;
import org.eclipse.uml2.uml.Collaboration;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
@@ -62,7 +66,7 @@ public class PackageUtil {
*/
public static boolean applyProfile(org.eclipse.uml2.uml.Package package_, org.eclipse.uml2.uml.Profile profileToApply, boolean withSubProfiles) {
- // Returns true if the model was modified
+ // Returns�true if the model was modified
boolean isChanged = false;
// if profile is not defined abort treatment
@@ -580,10 +584,39 @@ public class PackageUtil {
* {@inheritDoc}
*/
public int compare(Type o1, Type o2) {
- final String o1Name = ((o1.getName() != null) ? o1.getName() : "");
- final String o2Name = ((o2.getName() != null) ? o2.getName() : "");
+ final String o1Name = ((o1.getName() != null) ? o1.getName() : ""); //$NON-NLS-1$
+ final String o2Name = ((o2.getName() != null) ? o2.getName() : ""); //$NON-NLS-1$
return o1Name.compareTo(o2Name);
}
-
+ }
+
+ /**
+ * Return the top element of the model that is currently edited. This function is based on the
+ * assumption that the user model is the first resource that is loaded into the model set.
+ * Use this function instead of Utils.getTop (or getModel) if you want to avoid navigating to the
+ * root of an imported model.
+ *
+ * @return the top level package of the model currently loaded into an editor.
+ */
+ public static Package getUserModel(ExecutionEvent event) {
+ ServiceUtilsForHandlers serviceUtils = ServiceUtilsForHandlers.getInstance();
+ try {
+ // IPath fn = serviceUtils.getModelSet().getFilenameWithoutExtension();
+ EList<Resource> resources = serviceUtils.getModelSet(event).getResources();
+ if(resources.size() >= 3) {
+ // check first three resources (di, notation, uml)
+ for(int i = 0; i < 3; i++) {
+ Resource userResource = resources.get(i);
+ if(userResource.getContents().size() > 0) {
+ EObject topEObj = userResource.getContents().get(0);
+ if((topEObj instanceof Package) && (!(topEObj instanceof Profile))) {
+ return (Package)topEObj;
+ }
+ }
+ }
+ }
+ } catch (ServiceException e) {
+ }
+ return null;
}
}

Back to the top