Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/infra/org.eclipse.papyrus.infra.elementtypesconfigurations.emf/src/org/eclipse/papyrus/infra/elementtypesconfigurations/emf/invariantcontainerconfiguration/InvariantContainerRule.java')
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.elementtypesconfigurations.emf/src/org/eclipse/papyrus/infra/elementtypesconfigurations/emf/invariantcontainerconfiguration/InvariantContainerRule.java153
1 files changed, 82 insertions, 71 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.elementtypesconfigurations.emf/src/org/eclipse/papyrus/infra/elementtypesconfigurations/emf/invariantcontainerconfiguration/InvariantContainerRule.java b/plugins/infra/org.eclipse.papyrus.infra.elementtypesconfigurations.emf/src/org/eclipse/papyrus/infra/elementtypesconfigurations/emf/invariantcontainerconfiguration/InvariantContainerRule.java
index a5b37908ac8..f5b855e2b2f 100644
--- a/plugins/infra/org.eclipse.papyrus.infra.elementtypesconfigurations.emf/src/org/eclipse/papyrus/infra/elementtypesconfigurations/emf/invariantcontainerconfiguration/InvariantContainerRule.java
+++ b/plugins/infra/org.eclipse.papyrus.infra.elementtypesconfigurations.emf/src/org/eclipse/papyrus/infra/elementtypesconfigurations/emf/invariantcontainerconfiguration/InvariantContainerRule.java
@@ -12,103 +12,114 @@
*****************************************************************************/
package org.eclipse.papyrus.infra.elementtypesconfigurations.emf.invariantcontainerconfiguration;
-import java.util.ArrayList;
import java.util.Arrays;
-import java.util.List;
import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EReference;
-import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
+import org.eclipse.gmf.runtime.emf.type.core.IElementMatcher;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
+import org.eclipse.gmf.runtime.emf.type.core.IMetamodelType;
+import org.eclipse.gmf.runtime.emf.type.core.ISpecializationType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
-import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
+import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.elementtypesconfigurations.emf.Activator;
import org.eclipse.papyrus.infra.elementtypesconfigurations.emf.invariantcontainerruleconfiguration.HierarchyPermission;
import org.eclipse.papyrus.infra.elementtypesconfigurations.emf.invariantcontainerruleconfiguration.InvariantContainerRuleConfiguration;
+import org.eclipse.papyrus.infra.elementtypesconfigurations.impl.ConfiguredHintedSpecializationElementType;
import org.eclipse.papyrus.infra.elementtypesconfigurations.invarianttypes.invarianttypeconfiguration.AbstractInvariantRule;
+import org.eclipse.papyrus.infra.services.edit.internal.context.TypeContext;
+@SuppressWarnings("restriction")
public class InvariantContainerRule extends AbstractInvariantRule<InvariantContainerRuleConfiguration> {
- protected boolean isValid(IElementType typeToAdd, boolean baseIsValid) {
- boolean isValid = baseIsValid;
- for (HierarchyPermission permission : invariantRuleConfiguration.getPermissions()) {
- boolean isPermitted = permission.isIsPermitted();
- String childType = permission.getChildType();
- boolean isStrict = permission.isIsStrict();
- IElementType type = ElementTypeRegistry.getInstance().getType(childType);
- if (type != null) {
- if (isStrict) {
- if (typeToAdd.equals(type)) {
- if (isPermitted) {
- isValid = true;
- }
- } else if (!isPermitted) {
- isValid = false;
- }
- } else {
- // not strict. The super types of typeToCreate should contain the permission type if permitted, or not contain the permission type if not permitted
- List<IElementType> allTypes = Arrays.asList(typeToAdd.getAllSuperTypes());
- allTypes.add(typeToAdd);
- if (allTypes.contains(type)) {
- if (isPermitted) {
- isValid = true;
- }
- } else if (!isPermitted) {
- isValid = false;
- }
- }
- }
- }
- return isValid;
+ protected boolean approveCreationRequest(ConfiguredHintedSpecializationElementType typeToCreate, CreateElementRequest request) {
+ EObject newContainer = request.getContainer();
+ return canContain(newContainer);
}
+ protected boolean approveSetRequest(ConfiguredHintedSpecializationElementType typeToMove, SetRequest request) {
+ EObject newContainer = request.getElementToEdit();
+ return canContain(newContainer);
+ }
- protected boolean canContain(SetRequest request) {
-
- List<Object> toEvaluate;
- // multivalue or simple value ?
- if (request.getValue() instanceof List<?>) {
- toEvaluate = (List<Object>) request.getValue();
+ protected boolean approveMoveRequest(ConfiguredHintedSpecializationElementType typeToMove, MoveRequest request) {
+ EObject newContainer = request.getTargetContainer();
+ return canContain(newContainer);
+ }
- } else {
- toEvaluate = new ArrayList<Object>();
- toEvaluate.add(request.getValue());
+ protected boolean canContain(EObject container) {
+ if (container == null) {
+ return false;
}
+ boolean isValid = false;
+ try {
+ IElementType newContainerElementType = ElementTypeRegistry.getInstance().getElementType(container, TypeContext.getContext());
+
+ for (HierarchyPermission permission : invariantRuleConfiguration.getPermissions()) { // for each permission, get the matcher and matches directly
+ boolean isPermitted = permission.isPermitted();
+ String permissionContainerTypeID = permission.getContainerType();
+ boolean isStrict = permission.isStrict();
+ IElementType permissionContainerType = ElementTypeRegistry.getInstance().getType(permissionContainerTypeID);
- for (Object value : toEvaluate) {
- if (value instanceof EObject) {
- IElementType type = ElementTypeRegistry.getInstance().getElementType((EObject) value, request.getClientContext());
- if (type != null) {
- if (!isValid(type, false)) {
- return false;
+ // check is the permission type is a specialization type or a metamodel type, to enhance performances
+ // if this is a metamodel type and strict, a simple equals can match or not if it is permitted or not
+ // if this is a metamodel type and not strict, we only compare the list of supertypes of the eObject type with the permission Type
+ if (permissionContainerType instanceof IMetamodelType) {
+ if (isStrict) {
+ isValid = permissionContainerType.equals(newContainerElementType) ? isPermitted : !isPermitted;
+ } else {
+ // this is not strict
+ // so any super metamodeltype of permission type that matches newContainerElementType is OK
+ isValid = Arrays.asList(newContainerElementType.getAllSuperTypes()).contains(permissionContainerType) ? isPermitted : !isPermitted;
+ }
+ } else if (permissionContainerType instanceof ISpecializationType) {
+ IElementMatcher matcher = ((ISpecializationType) permissionContainerType).getMatcher();
+ if (matcher != null) {
+ boolean matchesSpecialization = matcher.matches(container) && newContainerElementType.equals(((ISpecializationType) permissionContainerType).getMetamodelType()); // the eObject matches the specializations type that is permitted.
+ if (matchesSpecialization) {
+ // test the isStrict now... It will be false if it matches one of the sub-specialization type
+ if (isStrict) {
+ ISpecializationType[] subtypes = ElementTypeRegistry.getInstance().getSpecializationsOf(permissionContainerTypeID);
+ if (subtypes != null && subtypes.length > 0) {
+ for (ISpecializationType subType : subtypes) {
+ isValid = subType.getMatcher().matches(container) ? isPermitted : !isPermitted; // the isStrict is not verified (one of the subtypes is matched whereas it should not)
+ }
+ } else { // there are no further sub specialization types.
+ isValid = isPermitted;
+ }
+ } else {
+ // not strict
+ isValid = isPermitted;
+ }
+ } else { // eObject type does not match the permission type
+ isValid = !isPermitted;
+
+ }
}
- } else {
- return false;
}
}
+
+ } catch (ServiceException e) {
+ Activator.log.error(e);
}
- return true;
+
+ return isValid;
}
+ /**
+ * @see org.eclipse.papyrus.infra.elementtypesconfigurations.invarianttypes.invarianttypeconfiguration.IInvariantRule#matches(org.eclipse.emf.ecore.EObject)
+ *
+ * @param eObject
+ * @return
+ */
@Override
- public boolean approveRequest(IEditCommandRequest request) {
- if (request instanceof SetRequest) {
- SetRequest setRequest = ((SetRequest) request);
- EStructuralFeature structuralFeature = setRequest.getFeature();
- if (structuralFeature instanceof EReference) {
- EReference reference = ((EReference) structuralFeature);
- if (reference.isContainment() && setRequest.getValue() != null) {
- // check new value
- return canContain(setRequest);
- }
- }
- } else if (request instanceof CreateElementRequest) {
- CreateElementRequest createElementRequest = ((CreateElementRequest) request);
- IElementType typeToCreate = createElementRequest.getElementType();
- return isValid(typeToCreate, false);
- }
- return true;
+ public boolean matches(EObject eObject) {
+ EObject container = eObject.eContainer();
+
+ return canContain(container);
}
+
}

Back to the top