Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1f83c5e1f160078d67c6281a767011b2da822123 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.constraints.constraints;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.papyrus.infra.constraints.Activator;
import org.eclipse.papyrus.infra.constraints.SimpleConstraint;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;

/**
 * A constraint testing if a Selection is an EObject, instance of the given
 * EClass. The EClass is identified by its nsURI and name.
 *
 * @author Camille Letavernier
 *
 */
public class EMFInstanceOfConstraint extends AbstractConstraint {

	private String className;

	private String nsUri;

	private EPackage metamodel;

	@Override
	protected void setDescriptor(SimpleConstraint descriptor) {
		className = getValue("className"); //$NON-NLS-1$
		nsUri = getValue("nsUri"); //$NON-NLS-1$
		metamodel = EPackage.Registry.INSTANCE.getEPackage(nsUri);
		if (metamodel == null) {
			Activator.log.warn("Metamodel with nsUri " + nsUri + " not found"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * A class constraint overrides its superclass constraints
	 * e.g. : instanceOf(Class) overrides instanceOf(Classifier)
	 */
	@Override
	public boolean overrides(Constraint otherConstraint) {
		if (!(otherConstraint instanceof EMFInstanceOfConstraint)) {
			return false;
		}

		EMFInstanceOfConstraint constraint = (EMFInstanceOfConstraint) otherConstraint;
		EClass thisClass = EMFHelper.getEClass(nsUri, className);
		EClass otherClass = EMFHelper.getEClass(constraint.nsUri, constraint.className);
		boolean result = (!equals(constraint)) && EMFHelper.isSubclass(thisClass, otherClass) && thisClass != otherClass;

		return result || super.overrides(constraint);
	}

	@Override
	public boolean match(Object selection) {
		if (className == null || nsUri == null) {
			return false;
		}

		EObject selectedItem = EMFHelper.getEObject(selection);

		if (selectedItem != null) {
			if (metamodel == null) { // This may be a dynamic, local (non-registered) EPackage
				return EMFHelper.isInstance(selectedItem, className, nsUri);
			} else {
				return EMFHelper.isInstance(selectedItem, className, metamodel);
			}
		}

		return false;
	}

	@Override
	public String toString() {
		return "EMFInstanceOfConstraint (" + nsUri + "/" + className + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	@Override
	protected boolean equivalent(Constraint constraint) {
		if (this == constraint) {
			return true;
		}
		if (constraint == null) {
			return false;
		}
		if (!(constraint instanceof EMFInstanceOfConstraint)) {
			return false;
		}
		EMFInstanceOfConstraint other = (EMFInstanceOfConstraint) constraint;
		if (className == null) {
			if (other.className != null) {
				return false;
			}
		} else if (!className.equals(other.className)) {
			return false;
		}
		if (nsUri == null) {
			if (other.nsUri != null) {
				return false;
			}
		} else if (!nsUri.equals(other.nsUri)) {
			return false;
		}
		return true;
	}

}

Back to the top