Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae9b3fd2193855875cc2874cfc31103573e94275 (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
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST, Christian W. Damus, 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 Displays
 *   Christian W. Damus - bug 466629
 *   
 *****************************************************************************/

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

import java.util.Iterator;
import java.util.StringTokenizer;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.IStereotypeViewProvider;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.StereotypeViewProvider;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayCommandExecution;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayUtil;
import org.eclipse.papyrus.uml.diagram.common.stereotype.migration.StereotypeMigrationHelper;
import org.eclipse.uml2.uml.Stereotype;

/**
 * This Command applies the user preferences for the Stereotype Label
 * In charge of check the visibility, depth and persistence based on the Old EAnnotation
 * 
 * @author Céline JANSSENS
 *
 */
public class StereotypeLabelMigrationCommand implements Runnable {

	// String Constant
	private static final String EANNOTATION_LIST_SEPARATOR = ","; //$NON-NLS-1$
	private static final String QUALIFIED_NAME_DEPTH = "full"; //$NON-NLS-1$


	private View mainView;

	// Static Instances of helpers and providers
	private static StereotypeDisplayUtil helper = StereotypeDisplayUtil.getInstance();
	private static StereotypeMigrationHelper migrationHelper = StereotypeMigrationHelper.getInstance();
	private static StereotypeDisplayCommandExecution commandHelper = StereotypeDisplayCommandExecution.getInstance();
	private IStereotypeViewProvider provider;


	/**
	 * Constructor.
	 *
	 * @param label
	 *            The Label of the Command
	 * @param content
	 *            The View of the element that is migrated.
	 */
	public StereotypeLabelMigrationCommand(String label, View content) {

		this.mainView = content;

	}


	/**
	 * Migrate the Stereotype Label from the old Version.
	 * 
	 * @param View
	 *            The element of the diagram to migrate
	 */
	protected void migrateStereotypeLabel(View view) {
		// Create the provider
		setProvider(view);

		if (migrationHelper.hasStereotypeEAnnotation(view)) {

			// Update Visibility
			String oldStereotype = getOldStereotypeToDisplay(view);
			updateNewStereotypeVisibility(view, oldStereotype);

			// Update Depth
			String oldQNStereotype = getOldQNStereotypeToDisplay(view);
			updateNewStereotypeDepth(view, oldQNStereotype);

		}
	}

	/**
	 * From the Stereotype List to display with they Qualified Name, update the Depth to full
	 * 
	 * @param view
	 *            The view of the object to migrate
	 * @param stereotypeList
	 *            List of Stereotype to display with their Qualified Name
	 */
	protected void updateNewStereotypeDepth(View view, String stereotypeList) {
		StringTokenizer tokenizer = new StringTokenizer(stereotypeList, EANNOTATION_LIST_SEPARATOR);

		while (tokenizer.hasMoreTokens()) {
			String stereotypeName = tokenizer.nextToken();
			View label = helper.getStereotypeLabel(view, stereotypeName);
			if (label != null) {
				commandHelper.setDepth(migrationHelper.getDomain(view), migrationHelper.getStereotypeFromString(view, stereotypeName), view, QUALIFIED_NAME_DEPTH, false);
			}
		}
	}


	/**
	 * From the Stereotype List to display , update the Visibility to true
	 * And hide visible Stereotype Label that should not be shown.
	 * 
	 * @param view
	 *            The view of the object to migrate
	 * @param stereotypeList
	 *            List of Stereotype to display
	 */
	protected void updateNewStereotypeVisibility(View view, String stereotypeList) {

		// Hide all the Label displayed but that should not to be shown
		hideStereotypeLabelNotDisplayed(stereotypeList, view);

	}


	/**
	 * Show each Stereotype from the Stereotype to display list.
	 * 
	 * @param stereotypeList
	 *            The list of stereotype to display
	 * @param view
	 *            The view of the object that is migrated
	 */
	private void showStereotypeLabelToBeDisplayed(String stereotypeList, View view) {
		StringTokenizer tokenizer = new StringTokenizer(stereotypeList, EANNOTATION_LIST_SEPARATOR);
		while (tokenizer.hasMoreTokens()) {
			String stereotypeName = tokenizer.nextToken();
			showStereotypeLabel(view, stereotypeName);
		}

	}

	/**
	 * Show the Label To display.
	 * Updating Visibility and Persistence
	 * 
	 * @param view
	 *            The view of the object to migrate
	 * @param stereotypeList
	 *            List of Stereotype to display
	 */
	protected void showStereotypeLabel(View view, String stereotypeName) {
		View label = provider.getLabel(migrationHelper.getStereotypeFromString(view, stereotypeName));
		if (label != null) {
			migrationHelper.updateVisibilityAndPersistence(label, view, true);
		}
	}


	/**
	 * Hide visible Stereotype Label that should not be shown.
	 * List all the applied Stereotypes, if the applied stereotype is not in the Stereotype to Display list,
	 * hide the Node.
	 * 
	 * @param view
	 *            The view of the object to migrate
	 * @param stereotypeList
	 *            List of Stereotype to display
	 */
	private void hideStereotypeLabelNotDisplayed(String stereotypeList, View view) {
		EList<Stereotype> appliedStereotypes = migrationHelper.getAppliedStereotypesFromView(view);
		Iterator<Stereotype> stereotypes = appliedStereotypes.iterator();
		while (stereotypes.hasNext()) {
			String stereotypeName = stereotypes.next().getQualifiedName();
			if (stereotypeList.indexOf(stereotypeName) == -1) {
				hideStereotypeLabel(view, stereotypeName);
			}
		}
	}




	/**
	 * Hide the Stereotype Label
	 * 
	 * @param view
	 *            The view of the object that is migrated
	 * @param stereotypeName
	 *            Name of the Stereotype of which the Label should be hidden
	 */
	private void hideStereotypeLabel(View view, String stereotypeName) {
		View label = provider.getLabel(migrationHelper.getStereotypeFromString(view, stereotypeName));
		migrationHelper.updateVisibilityAndPersistence(label, mainView, false);

	}


	/**
	 * Get List of Stereotype to display with their Qualified Name from the EAnnotation.
	 * 
	 * @param view
	 *            The view of the object to migrate
	 * @return The List of Stereotype to display with their Qualified Name
	 */
	protected String getOldQNStereotypeToDisplay(View view) {
		return migrationHelper.getStereotypesQNToDisplay(view);
	}


	/**
	 * Get List of Stereotype to display from the EAnnotation.
	 * 
	 * @param view
	 *            The view of the object to migrate
	 * @return The List of Stereotype to display
	 */
	protected String getOldStereotypeToDisplay(View view) {

		return migrationHelper.getStereotypesToDisplay(view);
	}

	/**
	 * Create the provider
	 * 
	 * @param view
	 *            The view on which stereotypes is applied and from which the Stereotype Views will be provided
	 */
	private void setProvider(View view) {
		provider = new StereotypeViewProvider(view);

	}


	/**
	 * @see java.lang.Runnable#run()
	 *
	 */
	@Override
	public void run() {
		migrateStereotypeLabel(mainView);

	}


}

Back to the top