Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b3d05ed21bc0cc56658fbaa25980e306e368ed1 (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 *   Celine JANSSENS (ALL4TEC) celine.janssens@all4tec.net - Initial API and implementation
 *   Celine JANSSENS (ALL4TEC) celine.janssens@all4tec.net - Bug 455311 : Refactor Stereotype Display
 *   Fanch BONNABESSE (ALL4TEC) fanch.bonnabesse@all4tec.net - Bug 493420
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.common.stereotype.migration.editpolicies;

import java.util.Iterator;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.commands.RemoveEAnnotationCommand;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.ConnectionEditPart;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayUtil;
import org.eclipse.papyrus.uml.diagram.common.stereotype.migration.StereotypeDisplayDiagramReconciler_1_2_0;
import org.eclipse.papyrus.uml.diagram.common.stereotype.migration.StereotypeMigrationHelper;
import org.eclipse.papyrus.uml.diagram.common.util.CommandUtil;

/**
 * This Edit Policy is in charge to migrate user preferences from old stereotype display structure
 * (which were using EAnnotation) to the new structure using NamedStyle and additional notation Views
 *
 * @author Céline JANSSENS
 *
 * @deprecated Replaced by {@link StereotypeDisplayDiagramReconciler_1_2_0}
 */
@Deprecated
public abstract class StereotypeEAnnotationMigrationEditPolicy extends AbstractEditPolicy {

	public static final Object STEREOTYPE_RECONCILER = "Stereotype Display Reconciler"; //$NON-NLS-1$

	protected StereotypeMigrationHelper migrationHelper = StereotypeMigrationHelper.getInstance();
	protected StereotypeDisplayUtil helper = StereotypeDisplayUtil.getInstance();

	protected View hostView = null;
	protected EAnnotation eAnnotation = null;
	protected IGraphicalEditPart editPart = null;



	/**
	 * Set the attributes and launch the migration.
	 *
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#activate()
	 *
	 */
	@Override
	public void activate() {

		// set editPart
		if (getHost() instanceof GraphicalEditPart) {
			this.editPart = (GraphicalEditPart) getHost();
		}

		if (getHost() instanceof ConnectionEditPart) {
			this.editPart = (ConnectionEditPart) getHost();
		}

		// set hostView
		if (editPart != null && editPart.getModel() instanceof View) {
			this.hostView = (View) editPart.getModel();
		}

		// set EAnnotation and start the migration process
		if (editPart != null && hostView != null) {
			this.eAnnotation = migrationHelper.getStereotypeEAnnotation(hostView);
			if (eAnnotation != null && !eAnnotation.getDetails().isEmpty()) {
				migrateStereotype();
			}
		}

	}

	/**
	 * Migrate all the Stereotype User preferences
	 *
	 */
	protected void migrateStereotype() {

		if (eAnnotation != null) {
			if (hasEAnnotationDetails(hostView)) {
				// Retrieve the migration Command and execute it.
				ICommand command = getStereotypeMigrationTransactionalCommand(hostView);
				CommandUtil.executeUnsafeCommand(command, editPart);

			}
			// Clean the details associated to each sub edit policies;
			cleanEAnnotationDetails(hostView);
		}
		// Clean the EAnnotation if Empty
		cleanEAnnotation();
	}


	/**
	 * Remove the EAnnotation when necessary
	 */
	private void cleanEAnnotation() {

		// If the EAnnotation is Empty Delete it
		if (eAnnotation != null && eAnnotation.getDetails().size() == 0) {
			RemoveEAnnotationCommand command = new RemoveEAnnotationCommand(editPart.getEditingDomain(), hostView, eAnnotation);
			CommandUtil.executeUnsafeCommand(command, editPart);
		}

		// If No Stereotype Structure is Found on the host but EAnnotation is present, delete the EAnnotation
		if (eAnnotation != null && !helper.hasStereotypeViews(hostView)) {
			RemoveEAnnotationCommand command = new RemoveEAnnotationCommand(editPart.getEditingDomain(), hostView, eAnnotation);
			CommandUtil.executeUnsafeCommand(command, editPart);
		}

		// Delete orphan Comment Node from OldStructure
		Object container = hostView.eContainer();
		if (container instanceof View) {
			View containerView = (View) container;
			Iterator<Object> sibilings = containerView.getChildren().iterator();
			while (sibilings.hasNext()) {
				Object sibiling = sibilings.next();
				if (migrationHelper.isOldComment(sibiling)) {
					if (migrationHelper.isOrphanComment((View) sibiling)) {
						DeleteCommand deleteComment = new DeleteCommand((View) sibiling);
						CommandUtil.executeUnsafeCommand(deleteComment, sibiling);
					}
				}
			}
		}

	}

	/**
	 * Clean EAnnotation Details in the model depending of what has been treated.
	 *
	 * @param hostView
	 *            The view on which the Stereotype has been applied
	 */
	public abstract void cleanEAnnotationDetails(View view);

	/**
	 * Define if the Edit Policy detail for the specific EANnotation Detail is not Empty.
	 *
	 * @param view
	 *            The view on which the Stereotype has been applied
	 */
	public abstract boolean hasEAnnotationDetails(View view);

	/**
	 * Get the command to update the Node visibility related to the EAnnotation.
	 *
	 * @param hostView
	 *            The view on which the Stereotype has been applied
	 */
	public Runnable getStereotypeMigrationCommand(View view) {
		return null;
	}

	/**
	 * Get the command to update the Node visibility related to the EAnnotation.
	 *
	 * @param hostView
	 *            The view on which the Stereotype has been applied
	 */
	public ICommand getStereotypeMigrationTransactionalCommand(final View view) {
		ICommand command = null;
		if (null != getStereotypeMigrationCommand(view)) {
			command = new AbstractTransactionalCommand(migrationHelper.getDomain(view), "Migration Stereotype", null) { //$NON-NLS-1$
				@Override
				protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
					getStereotypeMigrationCommand(view).run();
					return CommandResult.newOKCommandResult();
				}
			};
		}
		return command;
	}
}

Back to the top