Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 69bd34952b0719cf330161e0a585a6cc3a41440a (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) 2013, 2014 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 434302
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 435995
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.service;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.infra.core.services.EditorLifecycleEventListener;
import org.eclipse.papyrus.infra.core.services.EditorLifecycleManager;
import org.eclipse.papyrus.infra.core.services.IService;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.modelrepair.service.IStereotypeRepairService;
import org.eclipse.papyrus.uml.profile.Activator;
import org.eclipse.papyrus.uml.profile.service.ui.RefreshProfileDialog;
import org.eclipse.papyrus.uml.profile.validation.ProfileValidationHelper;
import org.eclipse.papyrus.uml.tools.commands.ApplyProfileCommand;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.uml.tools.utils.ProfileUtil;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;

/**
 * If a local profile is applied on this model, and this profile has been
 * redefined recently, the user will be asked whether the profile should
 * be reapplied.
 *
 *
 * @author Camille Letavernier
 *
 */
public class ReapplyProfilesService implements IService, EditorLifecycleEventListener {

	private ServicesRegistry servicesRegistry;

	private org.eclipse.uml2.uml.Package rootPackage;

	private IStereotypeRepairService stereotypeRepairService;

	/**
	 * {@inheritDoc}
	 */
	public void init(ServicesRegistry servicesRegistry) throws ServiceException {
		this.servicesRegistry = servicesRegistry;
	}

	/**
	 * {@inheritDoc}
	 */
	public void startService() throws ServiceException {
		try {
			EditorLifecycleManager lifecyleManager = servicesRegistry.getService(EditorLifecycleManager.class);
			lifecyleManager.addEditorLifecycleEventsListener(this);
		} catch (ServiceException ex) {
			return; // If the EditorLifecycleManager is not present, do nothing
		}

		try {
			stereotypeRepairService = servicesRegistry.getService(IStereotypeRepairService.class);
		} catch (ServiceException ex) {
			// If there's no stereotype repair service, then we don't have to worry about waiting for it
		}
	}

	protected void checkProfiles(IMultiDiagramEditor editor) {
		ModelSet modelSet;
		try {
			modelSet = servicesRegistry.getService(ModelSet.class);
		} catch (ServiceException ex) {
			return;
		}

		UmlModel umlModel = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
		if (umlModel == null) {
			return;
		}

		rootPackage = getRootPackage(umlModel);

		if (rootPackage == null) {
			return;
		}

		checkAndRefreshProfiles(rootPackage, editor);
	}

	protected Package getRootPackage(UmlModel umlModel) {
		try {
			EObject root = umlModel.lookupRoot();
			if (root instanceof Package) {
				return (Package) root;
			}
		} catch (NotFoundException ex) {
			// Ignore the exception: On diagram creation, the root isn't defined yet.
			// There's not profile application, and nothing to do.
			// Activator.log.error(ex);
		}
		return null;
	}

	protected boolean checkAndRefreshProfiles(Package currentPackage, IMultiDiagramEditor editor) {
		if (Display.getCurrent() == null) {
			return false;
		}

		for (Profile profile : currentPackage.getAppliedProfiles()) {
			if (ProfileUtil.isDirty(currentPackage, profile)) {
				RefreshProfileDialog dialog = new RefreshProfileDialog(editor.getSite().getShell(), this.rootPackage);
				dialog.setCallback(getCallback(dialog));
				dialog.open();
				return true;
			}
		}

		for (Package nestedPackage : currentPackage.getNestedPackages()) {
			if (checkAndRefreshProfiles(nestedPackage, editor)) {
				return true;
			}
		}

		return false;
	}

	protected Runnable getCallback(final RefreshProfileDialog dialog) {
		return new Runnable() {

			public void run() {
				Map<Package, Collection<Profile>> profilesToReapply = dialog.getProfilesToReapply();
				EditingDomain domain = EMFHelper.resolveEditingDomain(rootPackage);

				if (domain instanceof TransactionalEditingDomain) {

					// Create a flat list of profiles, for validation
					Collection<Profile> allProfiles = new LinkedList<Profile>();
					for (Collection<Profile> profiles : profilesToReapply.values()) {
						allProfiles.addAll(profiles);
					}

					// Validate and apply
					if (ProfileValidationHelper.checkApplicableProfiles(Display.getCurrent().getActiveShell(), allProfiles)) {
						CompoundCommand command = new CompoundCommand();
						for (Map.Entry<Package, Collection<Profile>> profiles : profilesToReapply.entrySet()) {
							command.append(new ApplyProfileCommand(profiles.getKey(), profiles.getValue(), (TransactionalEditingDomain) domain));
						}

						domain.getCommandStack().execute(command);
					}

				} else {
					Activator.log.error(new IllegalArgumentException("Cannot reapply profiles on Package " + rootPackage.getQualifiedName() + ". The EditingDomain cannot be found"));
				}

			}

		};
	}

	/**
	 * {@inheritDoc}
	 */
	public void disposeService() throws ServiceException {
		this.rootPackage = null;
		this.stereotypeRepairService = null;
		this.servicesRegistry = null;
	}

	public void postInit(IMultiDiagramEditor editor) {
		// Nothing
	}

	public void postDisplay(final IMultiDiagramEditor editor) {
		if (stereotypeRepairService == null) {
			// Just check profiles, now
			checkProfiles(editor);
		} else {
			// Ensure that we only kick in the profile migration after any pending repair has completed
			stereotypeRepairService.getPostRepairExecutor().execute(new Runnable() {

				public void run() {
					checkProfiles(editor);
				}
			});
		}
	}

	public void beforeClose(IMultiDiagramEditor editor) {
		// Nothing
	}

}

Back to the top