Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 326e894de720e3de94b238c32767ca6fc960ef20 (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 323802
 *  
 *****************************************************************************/
package org.eclipse.papyrus.uml.properties.modelelement;

import static org.eclipse.papyrus.uml.tools.databinding.OwnerObservableValue.ASSOCIATION;
import static org.eclipse.papyrus.uml.tools.databinding.OwnerObservableValue.CLASSIFIER;
import static org.eclipse.papyrus.uml.tools.util.MultiplicityParser.ANY;
import static org.eclipse.papyrus.uml.tools.util.MultiplicityParser.ONE;
import static org.eclipse.papyrus.uml.tools.util.MultiplicityParser.ONE_OR_MORE;
import static org.eclipse.papyrus.uml.tools.util.MultiplicityParser.OPTIONAL;

import java.util.List;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.StaticContentProvider;
import org.eclipse.papyrus.uml.properties.Activator;
import org.eclipse.papyrus.uml.tools.databinding.ExtensionEndMultiplicityObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.MultiplicityObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.NavigationObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.OwnerObservableValue;
import org.eclipse.papyrus.views.properties.modelelement.AbstractModelElement;
import org.eclipse.uml2.uml.ExtensionEnd;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * A Model Element for manipulating MemberEnd properties
 * 
 * @author Camille Letavernier
 */
public class MemberEndModelElement extends AbstractModelElement {

	private EObject source;

	private EditingDomain domain;

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

	/**
	 * The "owner" virtual property
	 */
	public static String OWNER = "owner"; //$NON-NLS-1$

	/**
	 * The "navigable" virtual property
	 */
	public static String NAVIGABLE = "navigable"; //$NON-NLS-1$

	/**
	 * 
	 * Constructor.
	 * 
	 * @param source
	 *        The EObject being edited
	 * @param domain
	 *        The Editing domain on which the commands will be executed
	 */
	public MemberEndModelElement(EObject source, EditingDomain domain) {
		this.source = source;
		this.domain = domain;
	}

	@Override
	public IObservable doGetObservable(String propertyPath) {
		if(propertyPath.equals(MULTIPLICITY)) {
			if(source instanceof ExtensionEnd) {
				return new ExtensionEndMultiplicityObservableValue((ExtensionEnd)source, domain);
			}
			return new MultiplicityObservableValue(source, domain);
		} else if(propertyPath.equals(OWNER)) {
			return new OwnerObservableValue(source, domain);
		} else if(propertyPath.equals(NAVIGABLE)) {
			return new NavigationObservableValue(source, domain);
		}
		Activator.log.warn("The property " + propertyPath + " doesn't exist"); //$NON-NLS-1$ //$NON-NLS-2$
		return null;
	}

	@Override
	public IStaticContentProvider getContentProvider(String propertyPath) {
		if(propertyPath.equals(MULTIPLICITY)) {
			if(source instanceof ExtensionEnd) {
				return new StaticContentProvider(new String[]{ ONE, OPTIONAL });
			}
			return new StaticContentProvider(new String[]{ ANY, ONE_OR_MORE, OPTIONAL, ONE });
		} else if(propertyPath.equals(OWNER)) {
			return new StaticContentProvider(new String[]{ ASSOCIATION, CLASSIFIER });
		}
		return super.getContentProvider(propertyPath);
	}

	@Override
	public boolean isMandatory(String propertyPath) {
		return true;
	}

	@Override
	public boolean isEditable(String propertyPath) {
		if(propertyPath.equals(OWNER)) {
			List<Property> memberEnds = ((Property)source).getAssociation().getMemberEnds();
			if(memberEnds.size() == 2) {
				//Association between two associations : this doesn't make sense ?
				if(isAssociation(memberEnds.get(0)) && isAssociation(memberEnds.get(1))) {
					return false;
				}
			}
			return (((Property)source).getAssociation().getMemberEnds().size() <= 2) && !EMFHelper.isReadOnly(source);
		}
		return !EMFHelper.isReadOnly(source);
	}

	private boolean isAssociation(Property property) {
		return property.getType().eClass() == UMLPackage.eINSTANCE.getAssociation();
	}

	@Override
	public boolean forceRefresh(String propertyPath) {
		return propertyPath.equals(NAVIGABLE) || propertyPath.equals(OWNER);
	}
}

Back to the top