Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 70594a78fdc5fae9985a68588c57c052e550097f (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 *  Thomas Daniellou (CEA LIST) - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.adl4eclipsetool.assistant;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.papyrus.adl4eclipse.org.ADL4Eclipse_Stereotypes;
import org.eclipse.papyrus.adltool.ADL4EclipseUtils;
import org.eclipse.papyrus.adltool.assistant.wizard.BundleSelectionPage;
import org.eclipse.papyrus.adltool.command.CompleteArchitectureSnapshotCommand;
import org.eclipse.papyrus.adltool.designer.ReverseSettings;
import org.eclipse.papyrus.adltool.reversible.project.ReversibleProject;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource;
import org.eclipse.papyrus.osgi.profile.OSGIStereotypes;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ModelEditPart;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;

/**
 * This abstract class specifies the default functionality of the import wizard
 * used to launch a reverse on a set of
 * {@link org.eclipse.papyrus.adltool.reversible.project.ReversibleProject
 * ReversibleProject}.
 *
 * <p>
 * <b>Note</b>: In order to extend this abstract class, the user needs to
 * initialize the {@link #reversibleList} field and implement the
 * {@link #performFinish()} method.
 * </p>
 */
public abstract class AbstractImportWizard extends Wizard implements IImportWizard {

	/**
	 * The list of reversible projects to be displayed in the
	 * {@link org.eclipse.papyrus.adltool.assistant.wizard.BundleSelectionPage
	 * BundleSelectionPage}.
	 */
	protected Set<ReversibleProject> reversibleList;

	/**
	 * Whether the import wizard is in "Advanced mode" or not.
	 */
	protected boolean advanced;

	/**
	 * The Wizard page that will help selecting bundles or features to import
	 * for the reverse engineering.
	 */
	protected BundleSelectionPage bundleSelectionPage;

	/**
	 * The object used to perform transactions on the models.
	 */
	protected TransactionalEditingDomain transactionalEditingDomain;

	/**
	 * The service used to access the Papyrus' models.
	 */
	protected ModelSet modelSet;

	/**
	 * The command used to launch the reverse.
	 */
	protected RecordingCommand command;

	/**
	 * Constructor.
	 *
	 * @param advanced
	 *            true to launch the wizard in advanced mode.
	 */
	public AbstractImportWizard(boolean advanced) {
		super();
		setNeedsProgressMonitor(true);

		this.advanced = advanced;
	}

	@Override
	public void addPages() {
		addPage(bundleSelectionPage);
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		reversibleList = getReversibleList();
		bundleSelectionPage = new BundleSelectionPage(reversibleList, advanced);

		Package rootModel = null;
		Object selectedModel = selection.getFirstElement();

		if (selectedModel instanceof ModelEditPart) {
			// Get the root model from the diagram view
			EObject element = ((ModelEditPart) selectedModel).getDiagramView().getElement();

			if (element instanceof Model) {
				rootModel = (Model) element;
			}
		} else {
			// Get the root model from the selection
			List<Element> selectionSet = getSelectionSet();

			if (!selectionSet.isEmpty()) {
				Element selectedElement = selectionSet.get(0);

				rootModel = ADL4EclipseUtils.getRootModel(selectedElement);
			} else {
				bundleSelectionPage.setErrorMessage("You must select a Papyrus Model before running the import.");
			}
		}

		if (rootModel != null && !rootModel.isModelLibrary()) {
			// Check if the required profiles are applied to the rootModel to
			// initialize the modelSet
			Profile adlProfile = rootModel.getAppliedProfile(ADL4Eclipse_Stereotypes.ADL4ECLIPSE);
			Profile osgiProfile = rootModel.getAppliedProfile(OSGIStereotypes.OSGI);

			if (adlProfile == null && osgiProfile == null) {
				bundleSelectionPage
						.setMessage("Info: The selected model does not have the ADL and OSGI profiles applied.");
			}

			try {
				// Initialize the modelSet and the TransactionalEditingDomain
				modelSet = ServiceUtilsForResource.getInstance().getModelSet(rootModel.eResource());
				transactionalEditingDomain = modelSet.getTransactionalEditingDomain();
				bundleSelectionPage.setSelectedModel(rootModel);
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * Gets the selected element in the diagram or in the model explorer.
	 *
	 * @return Element or null
	 */
	protected List<Element> getSelectionSet() {
		List<Element> selectedSet = new ArrayList<>();
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();

		if (selection instanceof IStructuredSelection) {
			Iterator<?> selectedobjectIteractor = ((IStructuredSelection) selection).iterator();

			while (selectedobjectIteractor.hasNext()) {
				Object currentSelection = selectedobjectIteractor.next();
				EObject selectedEObject = EMFHelper.getEObject(currentSelection);

				if (selectedEObject instanceof Element) {
					selectedSet.add((Element) selectedEObject);
				}
			}
		}

		return selectedSet;
	}

	@Override
	public boolean canFinish() {
		return bundleSelectionPage.isPageComplete();
	}

	/**
	 * Gets the selected reversible project from the
	 * {@link org.eclipse.papyrus.adltool.assistant.wizard.BundleSelectionPage
	 * BundleSelectionPage} and launches the architecture snapshot command that
	 * will reverse the projects.
	 *
	 * @return true if the command has launched, false if not
	 */
	@Override
	public boolean performFinish() {
		final Set<ReversibleProject> selectedBundles = bundleSelectionPage.getResult();

		// One bundle must be selected
		if (selectedBundles.size() > 0) {
			final Package selection = bundleSelectionPage.getSelectedModel();

			if (selection != null) {
				final ReverseSettings reverseSettings = bundleSelectionPage.getReverseSettings();
				Job importJob = new Job("Import") {

					@Override
					protected IStatus run(IProgressMonitor monitor) {
						// Launch the advanced reverse engineering
						command = new CompleteArchitectureSnapshotCommand(transactionalEditingDomain, selection,
								selectedBundles, reverseSettings);
						monitor.worked(1);
						transactionalEditingDomain.getCommandStack().execute(command);
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								MessageDialog.openInformation(getShell(), "Import finished", "Import is finished on "+ selectedBundles.size() +" bundles.");
							}
						});
						return Status.OK_STATUS;
					}
				};
				importJob.schedule();

				return true;
			}
		}

		return false;
	}

	protected abstract Set<ReversibleProject> getReversibleList();
}

Back to the top