Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8c47f03f2c91d9f828513acf12deed3a8100b9a4 (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
/*****************************************************************************
 * Copyright (c) 2013, 2014 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.migration.rsa.handler;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.migration.rsa.Activator;
import org.eclipse.papyrus.migration.rsa.RSAToPapyrusParameters.Config;
import org.eclipse.papyrus.migration.rsa.RSAToPapyrusParameters.RSAToPapyrusParametersFactory;
import org.eclipse.papyrus.migration.rsa.transformation.ImportTransformationLauncher;
import org.eclipse.papyrus.views.properties.creation.PropertyEditorFactory;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;


public class ImportHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection == null || selection.isEmpty()) {
			return null;
		}

		Set<IFile> filesToImport = new HashSet<IFile>();

		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			Iterator<?> selectionIterator = structuredSelection.iterator();
			while (selectionIterator.hasNext()) {
				Object selectedElement = selectionIterator.next();
				if (selectedElement instanceof IAdaptable) {
					IFile selectedFile = (IFile) ((IAdaptable) selectedElement).getAdapter(IFile.class);
					if (selectedFile == null) {
						continue;
					}

					// EFX files can be selected (Makes it easier to select a set of files),
					// but they will be imported by their parent model
					String fileExtension = selectedFile.getFileExtension();
					if ("epx".equals(fileExtension) || "emx".equals(fileExtension)) { //$NON-NLS-1$ //$NON-NLS-2$
						filesToImport.add(selectedFile);
					}
				}
			}
		}

		if (filesToImport.isEmpty()) {
			Activator.log.warn("The selection doesn't contain any *.epx nor *.emx file");
		} else {
			importFiles(filesToImport, event);
		}

		return null;
	}

	public void importFiles(Set<IFile> selectedFiles, ExecutionEvent event) {
		Config config = getTransformationParameters(event);

		if (config == null) {
			return;
		}

		List<URI> urisToImport = new LinkedList<URI>();

		for (IFile selectedFile : selectedFiles) {
			URI uri = URI.createPlatformResourceURI(selectedFile.getFullPath().toString(), true);

			urisToImport.add(uri);
		}

		// The Event's control is (or may be) a Context Menu, which will be disposed soon: retrieve its own parent instead (The main Window), if it has one.
		Control baseControl = HandlerUtil.getActiveShell(event);
		if (baseControl != null && !baseControl.isDisposed() && baseControl.getParent() != null) {
			baseControl = baseControl.getParent();
		}

		// On some platforms, it seems that the ActiveShell (Context Menu) may already be disposed (Bug 455011). Use the Active Workbench Window directly
		if (baseControl == null || baseControl.isDisposed()) {
			baseControl = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		}

		ImportTransformationLauncher launcher = new ImportTransformationLauncher(config, baseControl);
		launcher.run(urisToImport);
	}

	public Config getTransformationParameters(ExecutionEvent event) {
		Config config = RSAToPapyrusParametersFactory.eINSTANCE.createConfig();

		Shell activeShell = HandlerUtil.getActiveShell(event);

		final AtomicBoolean okPressed = new AtomicBoolean(true);
		PropertyEditorFactory factory = new PropertyEditorFactory() {
			@Override
			public String getEditionDialogTitle(Object objectToEdit) {
				return "Transformation parameters";
			}

			@Override
			protected void handleEditCancelled(Control widget, Object source) {
				okPressed.set(false);
				super.handleEditCancelled(widget, source);
			}
		};

		Object result = factory.edit(activeShell, config);

		if (!okPressed.get()) {
			return null;
		}

		// Result can be null, the source config, or a new config
		if (result instanceof Config) {
			config = (Config) result;
		}

		return config;
	}



}

Back to the top