Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a9c42d6db762769f5e4eeaa8f71cf663078eb470 (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
/**
 * Copyright (c) 2017 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:
 *  Maged Elaasar - Initial API and implementation
 *  
 * 
 */
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.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureContext;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.ui.architecture.ArchitectureUIPlugin;
import org.eclipse.papyrus.infra.ui.architecture.widgets.ArchitectureContextComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
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 {

	private final static String DIALOG_SECTION = ChangeArchitectureContextHandler.class.getName();
	
	/**
	 * Constructor.
	 */
	public ChangeArchitectureContextHandler() {
		// nothing by default
	}
	
	@Override
	public Object execute(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);
		dialog.setSelectedContexts(contextIds);
		dialog.setSelectedViewpoints(viewpointIds);
		dialog.create();
		
		if (dialog.open() == Window.OK) {
			TransactionalEditingDomain dom = helper.getModelSet().getTransactionalEditingDomain();
			CompoundCommand cmd = new CompoundCommand("Change Architecture Context");
			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;
	}

	/**
	 * A dialog to allow choosing a new architecture context and viewpoints 
	 */
	private class ArchitectureContextDialog extends Dialog {

		private String[] originalContextIds;
		
		private String[] selectedContextIds;

		private String[] selectedViewpointIds;

		protected ArchitectureContextDialog(Shell parentShell) {
			super(parentShell);
		}
		
		public String[] getSelectedContextIds() {
			return selectedContextIds;
		}

		public String[] getSelectedViewpointIds() {
			return selectedViewpointIds;
		}

		public void setSelectedContexts(String[] selectedContextIds) {
			this.selectedContextIds = selectedContextIds;
			this.originalContextIds = selectedContextIds;
		}

		public void setSelectedViewpoints(String[] selectedViewpointIds) {
			this.selectedViewpointIds = selectedViewpointIds;
		}

		@Override
        protected Control createDialogArea(Composite parent) {
			Composite container = (Composite) super.createDialogArea(parent);
			
			Label label = new Label(container, SWT.NONE);
			label.setText("Switch the architecture context and/or viewpoints applied to the Papyrus model:");
			
			ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
			
			ArchitectureContextComposite acc = new ArchitectureContextComposite(container, 1, 1, GridData.FILL_BOTH, 0, 0);
			acc.setAllowSeveralContexts(false);
			acc.setSelectedContexts(selectedContextIds);
			acc.setSelectedViewpoints(selectedViewpointIds);
			acc.setInput(manager.getVisibleArchitectureContexts().toArray(new MergedArchitectureContext[0]));
			acc.setUpdater(new ArchitectureContextComposite.Updater() {
				@Override
				public void update() {
					selectedContextIds = acc.getSelectedContexts();
					selectedViewpointIds = acc.getSelectedViewpoints();
				}
			});
			
			return container;
		}
		
		@Override
        protected void configureShell(Shell newShell) {
			super.configureShell(newShell);
            newShell.setText("Switch Architecture Context");
        }

        @Override
    	protected boolean isResizable() {
    		return true;
    	}
        
        @Override
        protected IDialogSettings getDialogBoundsSettings() {
    		IDialogSettings globalSettings = ArchitectureUIPlugin.getPlugin().getDialogSettings();
    		IDialogSettings dialogSettings = globalSettings.getSection(DIALOG_SECTION);
    		if (dialogSettings == null) {
    			dialogSettings = globalSettings.addNewSection(DIALOG_SECTION);
    	    }
    		return dialogSettings;
    	}
        
		@Override
		protected void okPressed() {
			if (!Arrays.equals(getSelectedContextIds(), originalContextIds)) {
				MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_WARNING | SWT.YES | SWT.NO);
			    messageBox.setMessage("Changing the architecture context may cause significant changes to the model.\nDo you like to proceed?");
			    messageBox.setText("Warning");
			    int response = messageBox.open();
			    if (response != SWT.YES)
			    	return;
			}
	    	super.okPressed();
		}
	}

}

Back to the top