Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c12c824aa2bcb3e48584abc8cead1a8c8253a375 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*****************************************************************************
 * Copyright (c) 2009 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Celine Janssens (ALL4TEC) celine.janssens@all4tec.net - Bug 455311 : Refactor Stereotypes Display
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.parser;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.AbstractCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus;
import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.diagram.common.stereotype.StereotypeDisplayHelper;
import org.eclipse.papyrus.uml.diagram.common.stereotype.StereotypeLocationEnum;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * this is a parser to display a slot in the diagram
 *
 */
public class StereotypePropertyParser implements IParser, ISemanticParser {

	private static final String DEFAULT_VALUE = "<UNDEFINED>";

	/**
	 *
	 * @see org.eclipse.gmf.runtime.common.ui.services.parser.IParser#getCompletionProcessor(org.eclipse.core.runtime.IAdaptable)
	 *
	 * @param element
	 * @return
	 */
	@Override
	public IContentAssistProcessor getCompletionProcessor(IAdaptable element) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 *
	 * @see org.eclipse.gmf.runtime.common.ui.services.parser.IParser#getEditString(org.eclipse.core.runtime.IAdaptable, int)
	 *
	 * @param element
	 * @param flags
	 * @return
	 */
	@Override
	public String getEditString(IAdaptable element, int flags) {
		if (element instanceof IAdaptable) {
			final Property property = ((Property) EMFHelper.getEObject(element));
			final View view = ((View) element.getAdapter(View.class));
			final EObject stereotypeApplication = ((View) view.eContainer()).getElement();
			final Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
			final Element umlElement = UMLUtil.getBaseElement(stereotypeApplication);


			String result = StereotypeUtil.displayPropertyValue(stereotype, property, umlElement, "");
			if (result.contains("=")) {
				result = result.substring(property.getName().length() + 1);
				return result;
			}
			else {
				return "";
			}

		}
		return DEFAULT_VALUE;
	}

	/**
	 *
	 * @see org.eclipse.gmf.runtime.common.ui.services.parser.IParser#getParseCommand(org.eclipse.core.runtime.IAdaptable, java.lang.String, int)
	 *
	 * @param element
	 * @param newString
	 * @param flags
	 * @return
	 */
	@Override
	public ICommand getParseCommand(IAdaptable element, final String newString, int flags) {
		if (element instanceof IAdaptable) {
			final Property property = ((Property) (EMFHelper.getEObject(element)));
			final View view = ((View) element.getAdapter(View.class));
			final EObject stereotypeApplication = ((View) view.eContainer()).getElement();
			final Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
			final Element umlElement = UMLUtil.getBaseElement(stereotypeApplication);
			final Object oldValue = umlElement.getValue(stereotype, property.getName());
			ICommand cmd = new AbstractCommand("AppliedStereotypeProperty") {

				@Override
				protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
					umlElement.setValue(stereotype, property.getName(), oldValue);
					return null;
				}

				@Override
				protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
					umlElement.setValue(stereotype, property.getName(), newString);
					return null;
				}

				@Override
				protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
					umlElement.setValue(stereotype, property.getName(), newString);
					return null;

				}
			};
			return cmd;
		}
		// TO Modify
		return UnexecutableCommand.INSTANCE;
	}

	/**
	 *
	 * @see org.eclipse.gmf.runtime.common.ui.services.parser.IParser#getPrintString(org.eclipse.core.runtime.IAdaptable, int)
	 *
	 * @param element
	 * @param flags
	 * @return
	 */
	@Override
	public String getPrintString(IAdaptable element, int flags) {

		StereotypeDisplayHelper helper = StereotypeDisplayHelper.getInstance();
		if (element instanceof IAdaptable) {
			final Property property = ((Property) (EMFHelper.getEObject(element)));
			final View view = ((View) element.getAdapter(View.class));

			StereotypeLocationEnum location;
			if (helper.isInStereotypeComment((Node) view)) {
				location = StereotypeLocationEnum.IN_COMMENT_COMPARTMENT;
			} else {
				location = StereotypeLocationEnum.IN_COMPARTMENT;
			}

			if (view != null && property != null) {
				return helper.getStereotypePropertyToDisplay(view, property, location);
			}
		}
		return DEFAULT_VALUE;
	}

	/**
	 *
	 * @see org.eclipse.gmf.runtime.common.ui.services.parser.IParser#isAffectingEvent(java.lang.Object, int)
	 *
	 * @param event
	 * @param flags
	 * @return
	 */
	@Override
	public boolean isAffectingEvent(Object event, int flags) {
		// TODO Auto-generated method stub
		return true;
	}

	/**
	 *
	 * @see org.eclipse.gmf.runtime.common.ui.services.parser.IParser#isValidEditString(org.eclipse.core.runtime.IAdaptable, java.lang.String)
	 *
	 * @param element
	 * @param editString
	 * @return
	 */
	@Override
	public IParserEditStatus isValidEditString(IAdaptable element, String editString) {
		return new ParserEditStatus("Papyrus Edition for property of stereotype", IStatus.OK, "");
	}

	@Override
	@SuppressWarnings("rawtypes")
	public List getSemanticElementsBeingParsed(EObject element) {
		return new ArrayList();
	}

	@Override
	public boolean areSemanticElementsAffected(EObject listener, Object notification) {
		return true;
	}


}

Back to the top