Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1d082f3603ceb8a5b484c6592153fc43bb4a85c (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*****************************************************************************
 * Copyright (c) 2012 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.uml.profile.properties.constraints;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.papyrus.infra.constraints.SimpleConstraint;
import org.eclipse.papyrus.infra.constraints.constraints.AbstractConstraint;
import org.eclipse.papyrus.infra.constraints.constraints.Constraint;
import org.eclipse.papyrus.uml.profile.properties.Activator;
import org.eclipse.papyrus.uml.profile.structure.AppliedStereotypeProperty;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;

public class IsAppliedStereotypePropertyConstraint extends AbstractConstraint {

	/**
	 * The "propertyType" property
	 */
	public final static String PROPERTY_TYPE = "propertyType"; //$NON-NLS-1$

	/**
	 * The "multiplicity" property
	 */
	public final static String MULTIPLICITY = "multiplicity"; //$NON-NLS-1$

	private PropertyType type;

	@Override
	protected void setDescriptor(SimpleConstraint descriptor) {
		String propertyType = getValue(PROPERTY_TYPE);
		try {
			type = PropertyType.valueOf(propertyType);
		} catch (Exception ex) {
			Activator.log.error("The property type is invalid : \"" + propertyType + "\"", ex);
		}

		int propertyMultiplicity = 1;

		String multiplicityValue = getValue(MULTIPLICITY);
		if(multiplicityValue != null) {
			try {
				propertyMultiplicity = Integer.parseInt(multiplicityValue);
			} catch (NumberFormatException ex) {
				Activator.log.error("The property multiplicity is invalid : \"" + multiplicityValue + "\"", ex);
			}
		}

		if(propertyMultiplicity != 1) {
			type = getMultivaluedType(type);
		}
	}

	public boolean match(Object selection) {

		AppliedStereotypeProperty property = null;

		if(selection instanceof AppliedStereotypeProperty) {
			property = (AppliedStereotypeProperty)selection;
		}

		if(selection instanceof IAdaptable) {
			Object adapter = ((IAdaptable)selection).getAdapter(AppliedStereotypeProperty.class);
			if(adapter != null) {
				property = (AppliedStereotypeProperty)adapter;
			}
		}

		if(property == null || type == null) {
			return false;
		}

		PropertyType propertyType = findType(property.getStereotypeProperty());

		return type == propertyType;
	}

	protected PropertyType findType(Property property) {
		Type type = property.getType();
		if(type instanceof PrimitiveType) {
			try {
				return PropertyType.valueOf(type.getName());
			} catch (Exception ex) {
				//Unknown primitive type
				return PropertyType.String;
			}
		}

		if(type instanceof Enumeration) {
			return PropertyType.Reference;
		}

		if(type instanceof DataType) {
			return PropertyType.String;
		}

		if(type instanceof Classifier) {
			return PropertyType.Reference;
		}

		return PropertyType.String;
	}

	protected PropertyType getMultivaluedType(PropertyType type) {
		switch(type) {
		case Boolean:
			return PropertyType.MultivaluedBoolean;
		case String:
			return PropertyType.MultivaluedString;
		case Real:
			return PropertyType.MultivaluedReal;
		case Reference:
			return PropertyType.MultivaluedReference;
		case Integer:
			return PropertyType.MultivaluedInteger;
		}

		return type;
	}

	@Override
	protected boolean equivalent(Constraint constraint) {
		if(!(constraint instanceof IsAppliedStereotypePropertyConstraint)) {
			return false;
		}

		if(type == null) {
			return false;
		}

		PropertyType otherType = ((IsAppliedStereotypePropertyConstraint)constraint).type;
		return type == otherType;
	}

	public static enum PropertyType {
		String, Integer, Real, Boolean, Reference, MultivaluedString, MultivaluedInteger, MultivaluedReal, MultivaluedBoolean, MultivaluedReference
	}
}

Back to the top