Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7dc6d9f3c9cb3d6990d39a5ba955378bf3ed9cc0 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*****************************************************************************
 * Copyright (c) 2011 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.properties.databinding;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.tools.databinding.AggregatedObservable;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.IMaskManagedLabelEditPolicy;
import org.eclipse.papyrus.uml.properties.Activator;
import org.eclipse.papyrus.uml.tools.databinding.AbstractUMLAggregatedObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.CommandBasedObservableValue;
import org.eclipse.papyrus.uml.tools.utils.UMLUtil;
import org.eclipse.papyrus.uml.tools.utils.ui.command.AddMaskManagedLabelDisplayCommand;
import org.eclipse.papyrus.uml.tools.utils.ui.command.SetNameLabelIconCommand;
import org.eclipse.papyrus.uml.tools.utils.ui.command.SetQualifiedNameDepthCommand;
import org.eclipse.papyrus.uml.tools.utils.ui.command.SetShadowFigureCommand;
import org.eclipse.papyrus.uml.tools.utils.ui.helper.NameLabelIconHelper;
import org.eclipse.papyrus.uml.tools.utils.ui.helper.QualifiedNameHelper;
import org.eclipse.papyrus.uml.tools.utils.ui.helper.ShadowFigureHelper;
import org.eclipse.uml2.uml.Element;

/**
 * An IObservableValue for custom Papyrus properties.
 * This enables to edit a few preferences-based values.
 * 
 * @author Camille Letavernier
 */
public class ElementCustomizationObservableValue extends AbstractUMLAggregatedObservableValue implements CommandBasedObservableValue, AggregatedObservable {

	private EditPart sourceElement;

	private Property property;

	private TransactionalEditingDomain domain;

	private View notationElement;

	private Element semanticElement;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param sourceElement
	 *        The selected EditPart
	 * @param property
	 *        The Property to edit
	 */
	public ElementCustomizationObservableValue(EditPart sourceElement, Property property) {
		super(UMLUtil.resolveEditingDomain(sourceElement));
		this.sourceElement = sourceElement;
		this.property = property;
		semanticElement = UMLUtil.resolveUMLElement(sourceElement);
		notationElement = (View)sourceElement.getModel();
		domain = (TransactionalEditingDomain)UMLUtil.resolveEditingDomain(semanticElement);
	}

	//TODO : The value is not correctly refreshed when someone else edits it
	//Some listeners need to be added
	public Object getValueType() {
		switch(property) {
		case LABEL_CUSTOMIZATION:
		case QUALIFIED_NAME:
			return Integer.class;
		case ELEMENT_ICON:
		case SHADOW:
			return Boolean.class;
		}

		return Object.class;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Object doGetValue() {
		switch(property) {
		case LABEL_CUSTOMIZATION:
			return getEditPolicy().getCurrentDisplayValue();
		case ELEMENT_ICON:
			return NameLabelIconHelper.showLabelIcon(notationElement);
		case SHADOW:
			return ShadowFigureHelper.getShadowFigureValue(notationElement);
		case QUALIFIED_NAME:
			return QualifiedNameHelper.getQualifiedNameDepth(notationElement);
		}

		return null;
	}

	/**
	 * 
	 * @return the {@link IMaskManagedLabelEditPolicy#MASK_MANAGED_LABEL_EDIT_POLICY} edit policy
	 */
	protected IMaskManagedLabelEditPolicy getEditPolicy() {
		return (IMaskManagedLabelEditPolicy)sourceElement.getEditPolicy(IMaskManagedLabelEditPolicy.MASK_MANAGED_LABEL_EDIT_POLICY);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void doSetValue(Object value) {
		Command command = getCommand(value);
		domain.getCommandStack().execute(command);
	}

	/**
	 * {@inheritDoc}
	 */
	public Command getCommand(Object value) {
		switch(property) {
		case ELEMENT_ICON:
			if(value instanceof Boolean) {
				return new SetNameLabelIconCommand(domain, notationElement, (Boolean)value);
			} else {
				Activator.log.warn(value + " is not a valid value for ElementIcon ; need a Boolean"); //$NON-NLS-1$
			}
			break;
		case SHADOW:
			if(value instanceof Boolean) {
				return new SetShadowFigureCommand(domain, notationElement, (Boolean)value);
			} else {
				Activator.log.warn(value + " is not a valid value for Shadow ; need a Boolean"); //$NON-NLS-1$
			}
			break;
		case QUALIFIED_NAME:
			if(value instanceof Integer) {
				return new SetQualifiedNameDepthCommand(domain, notationElement, (Integer)value);
			} else {
				Activator.log.warn(value + " is not a valid value for QualifiedNameDepth ; need an Integer"); //$NON-NLS-1$
			}
			break;
		case LABEL_CUSTOMIZATION:
			if(value instanceof Integer) {
				return new AddMaskManagedLabelDisplayCommand(domain, notationElement, (Integer)value);
			} else {
				Activator.log.warn(value + " is not a valid value for LabelCustomization ; need an Integer"); //$NON-NLS-1$
			}
			break;
		}

		return UnexecutableCommand.INSTANCE;
	}

	/**
	 * The list of valid properties for {@link ElementCustomizationObservableValue}
	 * 
	 * @author Camille Letavernier
	 * 
	 */
	public enum Property {
		/**
		 * A UML Property or Operation label customization
		 */
		LABEL_CUSTOMIZATION,

		/**
		 * Whether and how the element icon should be displayed
		 */
		ELEMENT_ICON,

		/**
		 * Whether the shadow should be displayed or not
		 */
		SHADOW,

		/**
		 * Whether and how the qualified name should be displayed
		 */
		QUALIFIED_NAME
	}

}

Back to the top