Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0562c83347422903ec4966116421558bc0c387b2 (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
/**
 * Copyright (c) 2013 Mia-Software.
 *
 * 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:
 *     Gregoire Dupe (Mia-Software) - Bug 406570 - Handlers to Save and SaveAs EMF resources
 */
package org.eclipse.papyrus.emf.facet.util.emf.ui.internal.handler;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.emf.facet.util.emf.ui.internal.ResourceUiUtils;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

public class SaveAsHandler implements IHandler {

	@Override
	public void addHandlerListener(final IHandlerListener handlerListener) {
		// Nothing to do
	}

	@Override
	public void dispose() {
		// Nothing to do
	}

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final Shell shell = HandlerUtil.getActiveShell(event);
		for (Object object : getSelection()) {
			if (object instanceof Resource) {
				final Resource resource = (Resource) object;
				ResourceUiUtils.openSaveAsDialog(resource, shell);
			} else if (object instanceof IAdaptable) {
				final IAdaptable adaptable = (IAdaptable) object;
				final Resource resource = (Resource) adaptable
						.getAdapter(Resource.class);
				if (resource != null) {
					ResourceUiUtils.openSaveAsDialog(resource, shell);
				}
			}
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public boolean isHandled() {
		return isEnabled();
	}

	@Override
	public void removeHandlerListener(final IHandlerListener handlerListener) {
		// Nothing to do
	}

	private static <E> List<E> getSelection() {
		final List<E> result = new ArrayList<E>();
		final IWorkbenchWindow wWindow = getActiveWindow();
		final ISelectionService selectService = wWindow.getSelectionService();
		final ISelection selection = selectService.getSelection();
		if (selection instanceof StructuredSelection) {
			final StructuredSelection structS = (StructuredSelection) selection;
			@SuppressWarnings("unchecked")
			// @SuppressWarnings This cast is unsafe but the framework does not
			// provide other way to do
			final List<E> list = structS.toList();
			result.addAll(list);
		}
		return result;
	}

	private static IWorkbenchWindow getActiveWindow() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		return workbench.getActiveWorkbenchWindow();
	}

}

Back to the top