Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab7aa91d87b2aa9838a030a157627ed8446aa93c (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*****************************************************************************
 * Copyright (c) 2008 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:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.profile.ui.compositesformodel;

import java.util.ArrayList;
import java.util.Collections;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.profile.Message;
import org.eclipse.papyrus.profile.tree.ProfileElementContentProvider;
import org.eclipse.papyrus.profile.tree.ProfileElementLabelProvider;
import org.eclipse.papyrus.profile.tree.objects.AppliedStereotypePropertyTreeObject;
import org.eclipse.papyrus.profile.tree.objects.AppliedStereotypeTreeObject;
import org.eclipse.papyrus.profile.tree.objects.StereotypedElementTreeObject;
import org.eclipse.papyrus.profile.tree.objects.ValueTreeObject;
import org.eclipse.papyrus.profile.ui.section.AppliedStereotypePropertyEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;

/**
 * The goal of this composite is make properties of applied stereotype editable see class AppliedStereotypeEditor
 */
public class PropertyComposite extends DecoratedTreeComposite {

	public TransactionalEditingDomain getDomain() {
		// return domain;
		return EditorUtils.getTransactionalEditingDomain();
	}

	public void setDomain(TransactionalEditingDomain domain) {
		this.domain = domain;
	}

	protected TransactionalEditingDomain domain;

	/**
	 * Creates a new PropertyComposite.
	 * 
	 * @param parent
	 *        the composite parent
	 */
	public PropertyComposite(Composite parent) {
		super(parent, SWT.NONE, "Property values", false);
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cea.papyrus.ui.composites.DecoratedTableComposite#createContent(org.eclipse.swt.widgets.Composite,
	 * org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
	 */
	@Override
	public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
		super.createContent(parent, factory);
		createPropTree();

		return this;
	}

	/**
	 * Creates the prop tree.
	 */
	protected void createPropTree() {

		// List of applied profiles
		treeViewer.setContentProvider(new ProfileElementContentProvider());
		treeViewer.setLabelProvider(new ProfileElementLabelProvider());
	}

	/**
	 * Refresh.
	 */
	@Override
	public void refresh() {

		if(treeViewer.getTree() != null && !(treeViewer.getTree().isDisposed())) {
			treeViewer.refresh();

			AppliedStereotypePropertyTreeObject pTO = (AppliedStereotypePropertyTreeObject)treeViewer.getInput();
			if(pTO == null) {
				this.setVisible(false);
			} else if((pTO.getProperty() != null) && pTO.getProperty().isReadOnly()) {
				upButton.setEnabled(false);
				downButton.setEnabled(false);
				removeButton.setEnabled(false);
				addButton.setEnabled(false);
			} else {
				upButton.setEnabled(true);
				downButton.setEnabled(true);
				removeButton.setEnabled(true);
				addButton.setEnabled(true);

				// If the property is Multivalued show Up - Down
				if((pTO.getChildren() != null) && (pTO.getChildren().length > 1)) {
					upButton.setEnabled(true);
					downButton.setEnabled(true);
				} else {
					upButton.setEnabled(false);
					downButton.setEnabled(false);
				}

				if((pTO.getChildren() != null) && (pTO.getChildren().length == pTO.getProperty().getLower())) {
					removeButton.setEnabled(false);
				} else {
					removeButton.setEnabled(true);
				}

				if((pTO.getChildren() != null) && (pTO.getChildren().length == pTO.getProperty().getUpper())) {
					addButton.setEnabled(false);
				} else {
					addButton.setEnabled(true);
				}

				this.setVisible(true);
			}
		}

	}

	/**
	 * Action triggered when the add button is pressed.
	 */
	@Override
	public void addButtonPressed() {

		// Retrieve selections
		AppliedStereotypePropertyTreeObject pTO = (AppliedStereotypePropertyTreeObject)treeViewer.getInput();
		Property property = pTO.getProperty();
		Stereotype selectedSt = ((AppliedStereotypeTreeObject)pTO.getParent()).getStereotype();
		Element selectedElt = ((StereotypedElementTreeObject)pTO.getParent().getParent()).getElement();

		if((property == null) || (selectedSt == null) || (selectedElt == null)) {
			// Nothing selected
			return;
		}

		// Retrieve property related info
		int lower = property.getLower();
		int upper = property.getUpper();
		Type type = property.getType();
		String typeName = type.getName();

		// if lower multiplicity is equal to upper multiplicity : cannot add
		if(lower == upper && selectedElt.getValue(selectedSt, property.getName()) != null) {
			if(selectedElt.getValue(selectedSt, property.getName()) instanceof EList) {
				if(((EList)selectedElt.getValue(selectedSt, property.getName())).size() >= upper) {
					Message.warning("Multiplicity of this property is " + property.getLower() + ".." + property.getUpper() + "\n" + "Impossible to add a new value.");
					return;
				}
			} else {
				Message.warning("Multiplicity of this property is " + property.getLower() + ".." + property.getUpper() + "\n" + "Impossible to add a new value.");
				return;
			}
		}

		// Retrieve current value
		ArrayList currentPropertyValues = new ArrayList();
		Object currentValue = null;
		if(selectedElt.hasValue(selectedSt, property.getName())) {
			currentValue = selectedElt.getValue(selectedSt, property.getName());

			if(upper == 1) {
				currentPropertyValues.add(currentValue);

			} else { // if (upper != 1) {
				EList currentValues = (EList)currentValue;
				for(int i = 0; i < currentValues.size(); i++) {
					currentPropertyValues.add(currentValues.get(i));
				}
			}
		}

		if(property.isMultivalued() || (currentPropertyValues.size() < upper)) {
			Object newValue = null;

			// get a new value for the property
			newValue = AppliedStereotypePropertyEditor.getNewValueForProperty(this.getShell(), property, selectedElt, type, currentPropertyValues);
			// new value entered ?
			if(newValue == null) {
				// quit
				return;
			}

			// Update property value(s)
			if(property.isMultivalued()) {
				// If newValue was entered, add to tempValues (future values list)
				currentPropertyValues.add (newValue);
				setPropertiesValue(selectedElt, selectedSt, property, currentPropertyValues);
			}
			else {
				// otherwise ([0..1] case)
				setPropertiesValue(selectedElt, selectedSt, property, newValue);
			}
		}
		else {
			Message.warning("Upper multiplicity of " + property.getName() + " is " + property.getUpper());
		}
		// Update tree && refresh
		pTO.reInitChilds ();
	}

	/**
	 * Action triggered when the remove button is pressed.
	 */
	@Override
	public void removeButtonPressed() {
		int nbrOfSelection = getTree().getSelectionCount();
		if(nbrOfSelection == 0) {
			return;
		}

		TreeItem[] items = getTree().getSelection();
		for(int i = 0; i < nbrOfSelection; i++) {
			ValueTreeObject vTO = (ValueTreeObject)items[i].getData();
			// vTO.removeMe();
			AppliedStereotypePropertyTreeObject pTO = (AppliedStereotypePropertyTreeObject)treeViewer.getInput();
			Property property = pTO.getProperty();
			Stereotype stereotype = ((AppliedStereotypeTreeObject)pTO.getParent()).getStereotype();
			Element selectedElt = ((StereotypedElementTreeObject)pTO.getParent().getParent()).getElement();

			int lower = property.getLower();
			int upper = property.getUpper();

			// if lower multiplicity is equal to upper multiplicity
			if(lower == upper) {
				Message.warning("Multiplicity of this property is" + lower + ".." + upper + "\n" + "Impossible to remove a value.");
				return;
			}

			Object currentVal = pTO.getValue();
			ArrayList tempValues = new ArrayList();

			if(((lower == 0) && (upper == 1))) {
				if(currentVal != null) {
					tempValues.add(currentVal);
				}

			} else if(upper != 1) {
				EList currentValues = (EList)currentVal;

				for(int j = 0; j < currentValues.size(); j++) {
					tempValues.add(currentValues.get(j));
				}
			}

			if((lower == 0) || (tempValues.size() > lower)) {
				tempValues.remove(vTO.getValue());

				if(property.isMultivalued()) {
					setPropertiesValue(selectedElt, stereotype, property, tempValues);
				} else {
					setPropertiesValue(selectedElt, stereotype, property, null);
				}

				// Force model change
				// Util.touchModel(element);

			} else {
				Message.warning("Lower multiplicity of " + property.getName() + " is " + lower);
			}
			pTO.reInitChilds ();
		}
	}

	/**
	 * Action triggered when the up button is pressed.
	 */
	@Override
	public void upButtonPressed() {
		int nbrOfSelection = getTree().getSelectionCount();
		if(nbrOfSelection < 1) {
			return;
		}

		TreeItem[] items = getTree().getSelection();
		int indexFirst = getTree().indexOf(items[0]);
		if(indexFirst == 0) {
			// do nothing
			return;
		}

		for(int i = 0; i < nbrOfSelection; i++) {
			ValueTreeObject vTO = (ValueTreeObject)items[i].getData();
			int index = getTree().indexOf(items[i]);
			vTO.moveMeUp(index);
		}
	}

	/**
	 * Action triggered when the down button is pressed.
	 */
	@Override
	public void downButtonPressed() {
		int nbrOfSelection = getTree().getSelectionCount();
		if(nbrOfSelection < 1) {
			return;
		}

		TreeItem[] items = getTree().getSelection();
		// Get last value index
		AppliedStereotypePropertyTreeObject pTO = (AppliedStereotypePropertyTreeObject)treeViewer.getInput();
		int indexLastValue = -1;
		if(pTO.getChildren() != null) {
			indexLastValue = pTO.getChildren().length - 1;
		}
		// Get last selection index
		int indexLastSelection = getTree().indexOf(items[nbrOfSelection - 1]);
		if((indexLastValue == -1) || (indexLastSelection == indexLastValue)) {
			// do nothing
			return;
		}

		for(int i = 0; i < nbrOfSelection; i++) {
			ValueTreeObject vTO = (ValueTreeObject)items[nbrOfSelection - 1 - i].getData();
			int index = getTree().indexOf(items[nbrOfSelection - 1 - i]);
			vTO.moveMeDown(index);
		}
	}

	/**
	 * Sets the input.
	 * 
	 * @param element
	 *        the element
	 */
	public void setInput(AppliedStereotypePropertyTreeObject element) {
		treeViewer.setInput(element);
		if (element != null) {
			element.reInitChilds ();
		}
		refresh();
	}

	/**
	 * Gets the tree.
	 * 
	 * @return the tree
	 */
	public Tree getTree() {
		return treeViewer.getTree();
	}

	/**
	 * 
	 * 
	 * @param item
	 */
	@Override
	public void editItem(TreeItem item) {
		// do nothing
	}

	protected void setPropertiesValue(final Element element, final Stereotype stereotype, final Property property, final Object newValue) {
		// bugfix: a selected element is not necessary a diagram element (ex: selection in the outline)
		IOperationHistory history = OperationHistoryFactory.getOperationHistory();
		try {
			history.execute ( new AbstractTransactionalCommand (getDomain (), "Apply stereotype property", Collections.EMPTY_LIST) {
				public CommandResult doExecuteWithResult (IProgressMonitor dummy, IAdaptable info) {
					element.setValue(stereotype, property.getName(), newValue);
	                return CommandResult.newOKCommandResult();
				}
			}, null, null);
		}
		catch (ExecutionException e) {
			e.printStackTrace ();
		}
	}

}

Back to the top