Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c10c739166aba3416ed0b80664a230e9d524999 (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
/**
 * Copyright (c) 2017, 2019 CEA LIST.
 *
 *  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:
 *    Maged Elaasar - Initial API and implementation
 *	  Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Bug 550569
 *
 */
package org.eclipse.papyrus.infra.ui.architecture.handlers;

import java.util.Arrays;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.ui.architecture.dialogs.ArchitectureContextDialog;
import org.eclipse.papyrus.infra.ui.architecture.messages.Messages;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * A command handler for changing architecture context in a model set
 *
 * @since 1.0
 */
public class ChangeArchitectureContextHandler extends AbstractHandler {

	/**
	 * Constructor.
	 */
	public ChangeArchitectureContextHandler() {
		// nothing by default
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		EObject selection = getSelection(event);
		if (selection == null) {
			return new IContributionItem[0];
		}
		ResourceSet resourceSet = selection.eResource().getResourceSet();
		if (!(resourceSet instanceof ModelSet)) {
			return new IContributionItem[0];
		}
		final ArchitectureDescriptionUtils helper = new ArchitectureDescriptionUtils((ModelSet) resourceSet);
		String[] contextIds = new String[] { helper.getArchitectureContextId() };
		String[] viewpointIds = helper.getArchitectureViewpointIds().toArray(new String[0]);

		final Shell shell = Display.getCurrent().getActiveShell();
		ArchitectureContextDialog dialog = new ArchitectureContextDialog(shell, Messages.ChangeArchitectureContextHandler_dialogTitle, Messages.ChangeArchitectureContextHandler_dialogLabel);
		dialog.setSelectedContexts(contextIds);
		dialog.setSelectedViewpoints(viewpointIds);
		dialog.create();

		if (dialog.open() == Window.OK) {
			final TransactionalEditingDomain dom = helper.getModelSet().getTransactionalEditingDomain();
			final CompoundCommand cmd = new CompoundCommand("Change Architecture Context"); //$NON-NLS-1$
			if (!Arrays.equals(dialog.getSelectedContextIds(), contextIds)) {
				cmd.append(helper.switchArchitectureContextId(dialog.getSelectedContextIds()[0]));
			}
			if (!Arrays.equals(dialog.getSelectedViewpointIds(), viewpointIds)) {
				cmd.append(helper.switchArchitectureViewpointIds(dialog.getSelectedViewpointIds()));
			}
			if (!cmd.isEmpty()) {
				dom.getCommandStack().execute(cmd);
			}
		}
		return null;
	}

	/**
	 * Gets the current selection as an EObject
	 *
	 * @return The current selection, or <code>null</code> if it is not an EObject
	 */
	protected EObject getSelection(ExecutionEvent event) {
		Object selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection struct = (IStructuredSelection) selection;
			Object obj = struct.getFirstElement();
			return EMFHelper.getEObject(obj);
		}
		return null;
	}

}

Back to the top