Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e52615587ce2af8a9b486d90ef2509a03865c8a2 (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
205
206
207
208
209
210
211
212
213
214
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST and others.
 *
 *    
 * 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 386118
 *
 *****************************************************************************/
package org.eclipse.papyrus.junit.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPage;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPageBookView;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.part.IPage;
import org.junit.Assert;

/**
 * Useful methods for the ModelExplorer view
 */
public class ModelExplorerUtils {

	/**
	 * the ID of the ModelExplorerView
	 */
	private static final String ModelExplorerViewId = "org.eclipse.papyrus.views.modelexplorer.modelexplorer"; //$NON-NLS-1$

	private ModelExplorerUtils() {
		// to prevent instanciation
	}

	/**
	 * 
	 * @return
	 *         the opened modelexplorer. Warning, it should be better that Papyrus was opened yet
	 * @throws PartInitException
	 */
	public static ModelExplorerView openModelExplorerView() throws PartInitException {
		IViewPart modelexplorer = null;
		modelexplorer = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ModelExplorerViewId);
		final ModelExplorerPageBookView view = (ModelExplorerPageBookView)modelexplorer;
		final IPage currentPage = view.getCurrentPage();
		final ModelExplorerPage page = (ModelExplorerPage)currentPage;
		final IViewPart viewer = page.getViewer();
		Assert.assertNotNull(viewer);
		viewer.setFocus();
		return (ModelExplorerView)viewer;
	}

	/**
	 * 
	 * @param view
	 *        the modelexplorer to manipulate
	 * @param elements
	 *        the elements to select
	 */
	public static void setSelectionInTheModelexplorer(final ModelExplorerView view, List<?> elements) {
		view.revealSemanticElement(elements);
		final List<?> currentSelection = getCurrentSelectionInTheModelExplorer();
		Assert.assertTrue("The current selection is not the wanted selection", elements.containsAll(currentSelection)); //$NON-NLS-1$
		Assert.assertTrue("The current selection is not the wanted selection", currentSelection.containsAll(elements)); //$NON-NLS-1$
	}

	/**
	 * 
	 * @return
	 *         the object selected in the ModelExplorer
	 *         //TODO : should be moved in the ModelExplorer
	 */
	public static List<?> getCurrentSelectionInTheModelExplorer() {
		final List<Object> selection = new ArrayList<Object>();
		IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (activeWorkbenchWindow!=null){
			final IStructuredSelection currentSelection = (IStructuredSelection)activeWorkbenchWindow.getSelectionService().getSelection(ModelExplorerViewId);
			final Iterator<?> iter = currentSelection.iterator();
			while(iter.hasNext()) {
				final Object current = iter.next();
				EObject eObject = EMFHelper.getEObject(current);
				if(eObject != null) {
					selection.add(eObject);
				} else {
					selection.add(current);
				}
			}			
		}
		return selection;
	}

	/**
	 * 
	 * @param view
	 *        the ModelExplorerView
	 * @return
	 *         the root of the Model
	 *         //TODO : should be moved in the ModelExplorer
	 */
	public static final EObject getRootInModelExplorer(final ModelExplorerView view) {
		view.getCommonViewer().expandToLevel(2);

		// store the root of the model
		final Object[] visibleElement = view.getCommonViewer().getVisibleExpandedElements();
		EObject modelRoot = null;
		if(visibleElement.length > 0) {
			modelRoot = EMFHelper.getEObject(visibleElement[0]);
		}
		Assert.assertNotNull(modelRoot);
		while(modelRoot.eContainer() != null) {
			modelRoot = modelRoot.eContainer();
		}
		return modelRoot;
	}

	/**
	 * 
	 * @param actionContext
	 *        the creation context
	 * @param wantedResult
	 *        the wanted result
	 */
	public static final void testHandlerStatusInModelExplorer(final ModelExplorerView view, final String commandToTest, final EObject actionContext, boolean wantedResult) {
		setSelectionInTheModelexplorer(view, Collections.singletonList(actionContext));
		ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
		Command cmd = commandService.getCommand(commandToTest);
		IHandler handler = cmd.getHandler();
		if(handler instanceof AbstractHandler) {
			((AbstractHandler)handler).setEnabled(commandToTest);
		}
		boolean res = handler.isEnabled();
		Assert.assertEquals(wantedResult, res);
	}

	/**
	 * Execute an editor command creation and returns the current papyrus nested editor (you must verify that it is the correct editor to be sure of
	 * the command execution)
	 * 
	 * @param currentPapyrusEditor
	 *        the current PapyrusEditor
	 * @param view
	 *        the model explorer view
	 * @param commandToExecute
	 *        the command to execute
	 * @param actionContext
	 *        the context used for the commadn (the selected elements)
	 * @param bundelID
	 *        the bundle id
	 * 
	 * @return
	 *         the current papyrus nested editor (you must verify that it is the correct editor to be sure of
	 *         the command execution)
	 */
	public static final Object executeCreateNestedEditorHandlerInModelExplorer(final IMultiDiagramEditor currentPapyrusEditor, final ModelExplorerView view, final String commandToExecute, final EObject actionContext, final String bundelID) {
		setSelectionInTheModelexplorer(view, Collections.singletonList(actionContext));
		ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
		final Command cmd = commandService.getCommand(commandToExecute);
		final IHandler handler = cmd.getHandler();
		if(handler instanceof AbstractHandler) {
			((AbstractHandler)handler).setEnabled(commandToExecute);
		}
		final RunnableWithResult<?> runnableWithResult = new RunnableWithResult.Impl<Object>() {

			public void run() {
				try {
					handler.execute(new ExecutionEvent(cmd, Collections.emptyMap(), null, null));
				} catch (ExecutionException e) {
					setStatus(new Status(IStatus.ERROR, bundelID, e.getMessage()));
				}

				IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				IEditorPart activeEditor = activePage.getActiveEditor();
				if(currentPapyrusEditor != activeEditor) {
					setStatus(new Status(IStatus.ERROR, bundelID, "The current active editor is not the wanted Papyrus Editor")); //$NON-NLS-1$
				}

				setResult(currentPapyrusEditor.getActiveEditor());
				setStatus(Status.OK_STATUS);
			}
		};
		Display.getDefault().syncExec(runnableWithResult);
		Assert.assertEquals(runnableWithResult.getStatus().getMessage(), IStatus.OK, runnableWithResult.getStatus().getSeverity());
		Object result = runnableWithResult.getResult();
		Assert.assertNotNull(result);
		return result;
	}
}

Back to the top