Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce4e4e399eb7f235dbb09294f98c202fb2c6f7d9 (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
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 429242
 *   Eike Stepper (CEA) - bug 466520
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.wizards;

import org.eclipse.core.resources.IContainer;
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.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.explorer.CDOExplorerUtil;
import org.eclipse.emf.cdo.explorer.checkouts.CDOCheckout;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.cdo.core.exporter.IModelExporter;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.internal.core.CDOUtils;
import org.eclipse.papyrus.cdo.internal.ui.l10n.Messages;
import org.eclipse.papyrus.cdo.internal.ui.views.DIModel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.statushandlers.StatusManager;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.eventbus.EventBus;

/**
 * This is the ModelExportWizard type. Enjoy.
 */
public class ModelExportWizard extends Wizard implements IWorkbenchWizard {

	private final LocalRepositoryView localView = new LocalRepositoryView();

	private ModelReferencesPage referencesPage;

	private ModelExportMappingsPage mappingsPage;

	private IStructuredSelection selection;

	private IModelTransferConfiguration exportConfig;

	private CDOCheckout checkout;

	private IContainer initialDestination;

	public ModelExportWizard() {
		super();
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = localView.translate(selection);

		setWindowTitle(Messages.ModelExportWizard_0);
		setNeedsProgressMonitor(true);
		setHelpAvailable(false);
	}

	public void setInitialDestination(IContainer container) {
		this.initialDestination = container;
	}

	@Override
	public void addPages() {
		exportConfig = IModelTransferConfiguration.Factory.EXPORT.create(new WizardOperationContext(getShell().getDisplay(), this), getResourceSet(selection));

		final EventBus bus = new EventBus("exportWizard"); //$NON-NLS-1$

		referencesPage = new ModelReferencesPage(bus, false);
		addPage(referencesPage);

		mappingsPage = new ModelExportMappingsPage(bus);
		addPage(mappingsPage);

		if (initialDestination != null) {
			mappingsPage.setInitialDestination(initialDestination);
		}

		// start analyzing the selected models' dependencies after the wizard
		// has been presented
		Display.getCurrent().asyncExec(new Runnable() {

			@Override
			public void run() {
				for (DIModel next : getSelection()) {
					exportConfig.addModelToTransfer(next.getResource().getURI());
				}

				bus.post(exportConfig);
				bus.post(checkout);
			}
		});
	}

	Iterable<DIModel> getSelection() {
		Multimap<CDOCheckout, DIModel> result = ArrayListMultimap.create();

		if (selection != null) {
			for (Object next : selection.toList()) {
				DIModel model = CDOUtils.adapt(next, DIModel.class);

				if (model != null) {
					URI uri = model.getResource().getURI();
					CDOCheckout checkout = CDOExplorerUtil.getCheckout(uri);
					if (this.checkout == null) {
						this.checkout = checkout;
					}

					if (checkout != null) {
						result.put(checkout, model);
					}
				}
			}
		}

		if (result.keySet().size() > 1) {
			MessageDialog.openInformation(getShell(), Messages.ModelExportWizard_2, NLS.bind(Messages.ModelExportWizard_3, checkout.getLabel()));
		}

		return result.get(checkout);
	}

	@Override
	public boolean performFinish() {
		boolean result = true;

		IModelExporter exporter = IModelExporter.Factory.DEFAULT.create();
		Diagnostic problems = exporter.exportModels(mappingsPage.getMapping());

		if (problems.getSeverity() > Diagnostic.INFO) {
			StatusManager.getManager().handle(BasicDiagnostic.toIStatus(problems), StatusManager.BLOCK);
		}

		return result;
	}

	@Override
	public void dispose() {
		if (exportConfig != null) {
			// it actually takes a while to dispose this, unloading all of the
			// resources potentially covering all of the Papyrus models in the
			// workspace in order to clean up the UML CacheAdapter
			final IModelTransferConfiguration configuration = exportConfig;
			new Job(Messages.ModelExportWizard_4) {

				{
					setSystem(true);
				}

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					try {
						configuration.dispose();
					} finally {
						localView.dispose();
					}
					return Status.OK_STATUS;
				}
			}.schedule();
			exportConfig = null;
		}

		super.dispose();
	}

	ResourceSet getResourceSet(IStructuredSelection selection) {
		ResourceSet result = null;

		for (Object next : selection.toList()) {
			CDOResource resource = CDOUtils.adapt(next, CDOResource.class);
			if (resource != null) {
				result = resource.getResourceSet();
				break;
			}
		}

		return result;
	}
}

Back to the top