Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28134892207ca1203801fc61f71339a7c6b471c2 (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.service.palette;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.infra.ui.util.EditorUtils;
import org.eclipse.papyrus.uml.appearance.helper.AppliedStereotypeHelper;
import org.eclipse.papyrus.uml.appearance.helper.UMLVisualInformationPapyrusConstant;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.part.PaletteUtil;
import org.eclipse.papyrus.uml.diagram.common.service.IPapyrusPaletteConstant;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Post action that manages the display of applied stereotypes.
 * <p>
 * More precisely, it is in charge of the display of properties of Stereotypes in Compartments
 * </p>
 * 
 * @deprecated Use the commands into oep.uml.diagram.common.stereotype.display.command
 * 
 */
@Deprecated
public class DisplayAppliedStereotypePropertiesInCompartmentPostAction extends GraphicalPostAction {

	/** map of stereotypes / properties to display with their qualified names */
	protected Map<String, String> stereotypePropertiesToDisplay = new HashMap<String, String>();

	/** add icon path */
	protected static final String ICONS_ADD = "/icons/obj16/Add_16x16.gif"; //$NON-NLS-1$

	/** remove icon path */
	protected static final String ICONS_REMOVE = "/icons/delete.gif"; //$NON-NLS-1$

	/** TreeViewer that shows the stereotypes to display and their properties */
	private CheckboxTreeViewer stereotypePropertiesToShowTreeViewer;

	/** HashMap of displayed Properties */
	protected Set<String> listOfDisplayableStereotypeProperties = new HashSet<String>();

	/** Node names and constants */
	private static final String PROPERTIES_NODE_NAME = "propertiesToDisplay";

	private static final String PROPERTY_NODE_NAME = "propertyToDisplay";

	/**
	 * @{inheritDoc
	 */
	@Override
	public void init(Node configurationNode, IAspectActionProvider factory) {
		super.init(configurationNode, factory);
		if (configurationNode == null) {
			return;
		}
		NodeList childNodes = configurationNode.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			String nodeName = childNodes.item(i).getNodeName();
			if (PROPERTIES_NODE_NAME.equals(nodeName)) {
				NodeList propertyNodes = childNodes.item(i).getChildNodes();
				for (int j = 0; j < propertyNodes.getLength(); j++) {
					String propertyNodeName = propertyNodes.item(j).getNodeName();
					if (PROPERTY_NODE_NAME.equals(propertyNodeName)) {
						Node propertyNameNode = propertyNodes.item(j).getAttributes().getNamedItem(IPapyrusPaletteConstant.NAME);
						if (propertyNameNode != null) {
							listOfDisplayableStereotypeProperties.add(propertyNameNode.getNodeValue());
						}
					}
				}
			}
		}
	}

	/**
	 * @{inheritDoc
	 */
	@Override
	public void save(Node parentNode) {
		Element propertiesNode = ((Element) parentNode).getOwnerDocument().createElement(PROPERTIES_NODE_NAME);
		for (String propertyName : listOfDisplayableStereotypeProperties) {
			Element propertyNode = propertiesNode.getOwnerDocument().createElement(PROPERTY_NODE_NAME);
			propertyNode.setAttribute(IPapyrusPaletteConstant.NAME, propertyName);
			propertiesNode.appendChild(propertyNode);
		}
		((Element) parentNode).appendChild(propertiesNode);
	}

	/**
	 * @{inheritDoc
	 */
	@Override
	public ICommand getPostCommand(final IAdaptable viewAdapter) {
		final TransactionalEditingDomain editingDomain = EditorUtils.getTransactionalEditingDomain();

		return new AbstractTransactionalCommand(editingDomain, "Modify Stereotype Properties Display", null) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {

				View view = viewAdapter.getAdapter(View.class);
				String stereotypesStrList = PaletteUtil.convertToCommaSeparatedRepresentation(listOfDisplayableStereotypeProperties);

				AppliedStereotypeHelper.getAddAppliedStereotypePropertiesCommand(editingDomain, view, stereotypesStrList).execute();
				AppliedStereotypeHelper.getAppliedStereotypeToDisplayCommand(editingDomain, view, "", UMLVisualInformationPapyrusConstant.STEREOTYPE_COMPARTMENT_LOCATION).execute();

				return CommandResult.newOKCommandResult();
			}
		};
	}

	/**
	 * @{inheritDoc
	 */
	@Override
	public Control createConfigurationComposite(Composite parent, IPaletteEntryProxy entryProxy, List<Profile> appliedProfiles) {
		this.appliedProfiles = appliedProfiles;
		this.entryProxy = entryProxy;
		// retrieve displayed stereotypes
		List<String> displayedStereotypeNames = new ArrayList<String>();
		if (entryProxy instanceof IPaletteAspectToolEntryProxy) {
			for (IPostAction postAction : ((IPaletteAspectToolEntryProxy) entryProxy).getPostActions()) {
				if (postAction instanceof StereotypePostAction) {
					for (String stereotypeName : ((StereotypePostAction) postAction).getStereotypesToApply()) {
						displayedStereotypeNames.add(stereotypeName);
					}
				}
			}
		}
		// get the stereotypes to display, looking in the list of applied profiles and comparing with the list of stereotype qualified names to be displayed
		List<Stereotype> displayedStereotypes = new ArrayList<Stereotype>();
		for (Profile appliedProfile : appliedProfiles) {
			for (Stereotype stereotype : appliedProfile.getOwnedStereotypes()) {
				if (stereotype != null && displayedStereotypeNames.contains(stereotype.getQualifiedName())) {
					displayedStereotypes.add(stereotype);
				}
			}
		}
		// check list has same size...
		if (displayedStereotypeNames.size() != displayedStereotypes.size()) {
			Activator.log.error("all the stereotypes were not resolved:\n" + displayedStereotypeNames + "\n" + displayedStereotypes, null);
		}
		Composite composite = new Composite(parent, SWT.BORDER);
		composite.setLayout(new GridLayout(1, false));
		Label label = new Label(composite, SWT.None);
		label.setText("Choose applied stereotype Properties to display in compartment: ");
		GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		label.setLayoutData(data);
		stereotypePropertiesToShowTreeViewer = new CheckboxTreeViewer(composite, SWT.BORDER | SWT.CHECK);
		GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
		stereotypePropertiesToShowTreeViewer.getControl().setLayoutData(layoutData);
		stereotypePropertiesToShowTreeViewer.setContentProvider(new ITreeContentProvider() {

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				// nothing to do here
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void dispose() {
				// nothing to do here
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public boolean hasChildren(Object element) {
				if (element instanceof ITreeElement) {
					return ((ITreeElement) element).getChildren().length > 0;
				}
				return false;
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Object getParent(Object element) {
				if (element instanceof ITreeElement) {
					return ((ITreeElement) element).getParent();
				}
				return null;
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Object[] getElements(Object inputElement) {
				if (inputElement instanceof List) {
					List<StereotypeTreeItem> stereotypeTreeItems = new ArrayList<DisplayAppliedStereotypePropertiesInCompartmentPostAction.StereotypeTreeItem>();
					for (Object object : (List<?>) inputElement) {
						if (object instanceof Stereotype) {
							stereotypeTreeItems.add(new StereotypeTreeItem((Stereotype) object));
						}
					}
					return stereotypeTreeItems.toArray();
				}
				return new Object[0];
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public Object[] getChildren(Object parentElement) {
				if (parentElement instanceof ITreeElement) {
					return ((ITreeElement) parentElement).getChildren();
				}
				return new Object[0];
			}
		});
		stereotypePropertiesToShowTreeViewer.setLabelProvider(new TreeItemLabelProvider());
		stereotypePropertiesToShowTreeViewer.setCheckStateProvider(new CheckStateProvider());
		stereotypePropertiesToShowTreeViewer.addCheckStateListener(new CheckStateListener());
		stereotypePropertiesToShowTreeViewer.setInput(displayedStereotypes);
		return composite;
	}

	/**
	 * Listener for the check/uncheck states of the event.
	 */
	public class CheckStateListener implements ICheckStateListener {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void checkStateChanged(CheckStateChangedEvent event) {
			for (TreeItem stereotypeItem : ((CheckboxTreeViewer) event.getCheckable()).getTree().getItems()) {
				Stereotype stereotype = ((StereotypeTreeItem) stereotypeItem.getData()).getStereotype();
				if (stereotype == null) {
					Activator.log.debug("impossible to find stereotype");
					return;
				}
				for (TreeItem propertyItem : stereotypeItem.getItems()) {
					PropertyTreeItem propertyTreeItem = ((PropertyTreeItem) propertyItem.getData());
					if (propertyTreeItem != null && propertyTreeItem.getProperty() != null) {
						if (propertyItem.getChecked()) {
							listOfDisplayableStereotypeProperties.add(getPropertyName(stereotype, ((PropertyTreeItem) propertyItem.getData()).getProperty()));
						} else {
							listOfDisplayableStereotypeProperties.remove(getPropertyName(stereotype, ((PropertyTreeItem) propertyItem.getData()).getProperty()));
						}
					}
				}
			}

		}
	}

	/**
	 * The label provider for the tableviewer : one columns shows the stereotype
	 * name and the other shows the display option
	 */
	class TreeItemLabelProvider extends LabelProvider {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText(Object element) {
			if (element instanceof StereotypeTreeItem) {
				return ((StereotypeTreeItem) element).getStereotype().getQualifiedName();
			} else if (element instanceof PropertyTreeItem) {
				return ((PropertyTreeItem) element).getProperty().getQualifiedName();
			}
			return super.getText(element);
		}
	}

	public class CheckStateProvider implements ICheckStateProvider {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean isChecked(Object element) {
			if (element instanceof PropertyTreeItem) {
				Property property = ((PropertyTreeItem) element).getProperty();
				Stereotype stereotype = ((PropertyTreeItem) element).getParent().getStereotype();
				if (property != null && stereotype != null) {
					String name = getPropertyName(stereotype, property);
					return listOfDisplayableStereotypeProperties.contains(name);
				}
			}
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean isGrayed(Object element) {
			return false;
		}
	}

	public String getPropertyName(Stereotype stereotype, Property property) {
		StringBuilder buffer = new StringBuilder();
		buffer.append(stereotype.getQualifiedName());
		buffer.append(".");
		buffer.append(property.getName());
		return buffer.toString();
	}

	public interface ITreeElement {

		public ITreeElement[] getChildren();

		public ITreeElement getParent();
	}

	/**
	 * Tree Item to manage Stereotypes
	 */
	public class StereotypeTreeItem implements ITreeElement {

		private Stereotype stereotype;

		private PropertyTreeItem[] propertyTreeItems;

		/**
		 * Constructor.
		 *
		 * @param parentItem
		 *            th parent item for this tree item
		 * @param style
		 *            the style of this tree item
		 */
		public StereotypeTreeItem(Stereotype stereotype) {
			this.stereotype = stereotype;
		}

		/**
		 * Returns the Stereotype represented by this tree item
		 */
		public Stereotype getStereotype() {
			return stereotype;
		}

		@Override
		public PropertyTreeItem[] getChildren() {
			if (propertyTreeItems == null) {
				List<PropertyTreeItem> items = new ArrayList<DisplayAppliedStereotypePropertiesInCompartmentPostAction.PropertyTreeItem>();
				for (Property property : stereotype.getAllAttributes()) {
					if (property != null) {
						items.add(new PropertyTreeItem(this, property));
					}
				}
				propertyTreeItems = items.toArray(new PropertyTreeItem[] {});
			}
			return propertyTreeItems;
		}

		@Override
		public ITreeElement getParent() {
			return null;
		}
	}

	/**
	 * Tree Item to manage Stereotypes
	 */
	public class PropertyTreeItem implements ITreeElement {

		private Property property;

		private StereotypeTreeItem stereotypeTreeItem;

		/**
		 * Constructor.
		 *
		 * @param parentItem
		 *            the parent item for this tree item
		 * @param style
		 *            the style of this tree item
		 */
		public PropertyTreeItem(StereotypeTreeItem stereotypeTreeItem, Property property) {
			this.stereotypeTreeItem = stereotypeTreeItem;
			this.property = property;
		}

		/**
		 * Returns the Property represented by this tree item
		 */
		public Property getProperty() {
			return property;
		}

		@Override
		public StereotypeTreeItem getParent() {
			return stereotypeTreeItem;
		}

		@Override
		public ITreeElement[] getChildren() {
			return new ITreeElement[0];
		}
	}
}

Back to the top