Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d6df1edfc4deea38a3d8e6fe54d1ca4055c7845 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*****************************************************************************
 * Copyright (c) 2012 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *  Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr
 *
 *****************************************************************************/
package org.eclipse.papyrus.texteditor.cdt.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.AbstractEMFOperation;
import org.eclipse.papyrus.codegen.extensionpoints.ILangCodegen;
import org.eclipse.papyrus.codegen.extensionpoints.LanguageCodegen;
import org.eclipse.papyrus.commands.CheckedOperationHistory;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ISashWindowsContentProvider;
import org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.DiSashModelManager;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashPanel;
import org.eclipse.papyrus.infra.core.sashwindows.di.TabFolder;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers;
import org.eclipse.papyrus.texteditor.cdt.Activator;
import org.eclipse.papyrus.texteditor.cdt.TextEditorConstants;
import org.eclipse.papyrus.texteditor.cdt.editor.PapyrusCDTEditor;
import org.eclipse.papyrus.texteditor.cdt.modelresource.TextEditorModelSharedResource;
import org.eclipse.papyrus.texteditor.model.texteditormodel.TextEditorModel;
import org.eclipse.papyrus.texteditor.model.texteditormodel.TextEditorModelFactory;
import org.eclipse.papyrus.uml.diagram.common.handlers.CmdHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.UMLPackage;


/**
 * The handler creates a new CDT editor
 */
public class PapyrusCDTEditorHandler extends CmdHandler {


	public PapyrusCDTEditorHandler() {
	}

	/**
	 *
	 * @see org.eclipse.infra.core.commands.AbstractHandler#isEnabled()
	 *
	 * @return
	 */
	@Override
	public boolean isEnabled() {
		updateSelectedEObject();
		// Filter Classes (including Behaviors, since Behavior inherits from Class), Operation and Transition
		if (selectedEObject instanceof Class ||
				selectedEObject instanceof Operation ||
				selectedEObject instanceof Transition)
		{
			URI uri = selectedEObject.eResource().getURI();

			// URIConverter uriConverter = resource.getResourceSet().getURIConverter();
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			if (uri.segmentCount() < 2) {
				return false;
			}
			IProject modelProject = root.getProject(uri.segment(1));
			return modelProject.exists();
		}
		return false;
	}

	/**
	 * @see org.eclipse.infra.core.commands.IHandler#execute(org.eclipse.infra.core.commands.ExecutionEvent)
	 *
	 * @param event
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		try {
			final ServicesRegistry serviceRegistry = ServiceUtilsForHandlers.getInstance().getServiceRegistry(event);
			TransactionalEditingDomain domain = ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);

			// Create the transactional command
			AbstractEMFOperation command = new AbstractEMFOperation(domain, "Create CDT editor") {

				@Override
				protected IStatus doExecute(final IProgressMonitor monitor, final IAdaptable info) throws ExecutionException {
					try {
						PapyrusCDTEditorHandler.this.doExecute(serviceRegistry);
					} catch (ServiceException e) {
						Activator.log.error(e);
						return Status.CANCEL_STATUS;
					} catch (NotFoundException e) {
						Activator.log.error(e);
						return Status.CANCEL_STATUS;
					}
					return Status.OK_STATUS;
				}
			};

			// Execute the command
			CheckedOperationHistory.getInstance().execute(command, new NullProgressMonitor(), null);
		} catch (ExecutionException e) {
			Activator.log.error("Can't create a CDT editor", e);
		} catch (ServiceException e) {
			Activator.log.error("Service exception during creation of CDT editor", e);
		}
		return null;
	}


	/**
	 * Do the execution of the command.
	 *
	 * @param serviceRegistry
	 * @throws ServiceException
	 * @throws NotFoundException
	 */
	public void doExecute(final ServicesRegistry serviceRegistry) throws ServiceException, NotFoundException {
		// Get the page manager allowing to add/open an editor.
		final IPageManager pageMngr = ServiceUtils.getInstance().getIPageManager(serviceRegistry);

		Classifier classifierToEdit = getClassifierToEdit();
		TextEditorModel editorModel = getEditorModel(serviceRegistry, classifierToEdit);
		if (editorModel == null) {
			// no editor exist for the given file => create
			editorModel = createEditorModel(serviceRegistry, classifierToEdit);
			if (editorModel == null) {
				return;
			}
		}
		ILangCodegen codegen = LanguageCodegen.getGenerator(TextEditorConstants.CPP, editorModel.getGeneratorID());

		if (codegen.getTargetProject(classifierToEdit, true) == null) {
			return;
		}
		
		if (selectedEObject instanceof Transition) {
			Transition transition = (Transition) selectedEObject;
			if (transition.getEffect() == null) {
				Behavior effect = transition.createEffect("effectOf" + transition.getName(), UMLPackage.eINSTANCE.getOpaqueBehavior()); //$NON-NLS-1$
			}
		}
		// add the new editor model to the sash.
		editorModel.setSelectedObject(selectedEObject);

		final TextEditorModel editorModelFinal = editorModel;
		// open asynchronously to prevent handler cycles, see bug 434484
		Display.getDefault().asyncExec(new Runnable() {

			@Override
			public void run() {
				if (pageMngr.isOpen(editorModelFinal)) {
					// select existing editor
					pageMngr.selectPage(editorModelFinal);
				}
				else {
					pageMngr.openPage(editorModelFinal);
				}
				try {
					// move page to the RIGHT
					DiSashModelManager modelMngr = ServiceUtils.getInstance().getService(DiSashModelManager.class, serviceRegistry);
					ISashWindowsContentProvider sashContentProvider = modelMngr.getISashWindowsContentProvider();
					Object rootModel = sashContentProvider.getRootModel();

					if (rootModel instanceof TabFolder) {
						// root = tabFolder, i.e. there is a single folder
						ISashWindowsContainer sashContainer = ServiceUtils.getInstance().getISashWindowsContainer(serviceRegistry);
						int index = lookupIndex((TabFolder) rootModel, editorModelFinal);
						if (index != -1) {
							sashContentProvider.createFolder(sashContainer.getSelectedTabFolderModel(), index, sashContainer.getSelectedTabFolderModel(), SWT.RIGHT);
						}
					}
					else if (rootModel instanceof SashPanel) {
						// multiple tab-folders exist. Find existing one and move editorModel to other
						// TODO
						// ISashWindowsContainer sashContainer = ServiceUtils.getInstance().getISashWindowsContainer(serviceRegistry);
						// sashContentProvider.movePage(sashContainer.getSelectedTabFolderModel(), lookupIndex(sourceTab, editorModel), targetTabModel, -1);
					}
				} catch (ServiceException e) {

				}
			}

		});

	}

	/**
	 * Create a model identifying the editor. This model will be saved with the sash
	 *
	 * @return
	 * @throws ServiceException
	 * @throws NotFoundException
	 *             The model where to save the TableInstance is not found.
	 */
	protected TextEditorModel createEditorModel(final ServicesRegistry serviceRegistry, Classifier classifierToEdit) throws ServiceException, NotFoundException {
		TextEditorModel editorModel = TextEditorModelFactory.eINSTANCE.createTextEditorModel();

		editorModel.setEditedObject(classifierToEdit);
		editorModel.setType(PapyrusCDTEditor.EDITOR_TYPE);
		editorModel.setName("CDT " + classifierToEdit.getName()); //$NON-NLS-1$
		ILangCodegen codegen = LanguageCodegen.chooseGenerator(TextEditorConstants.CPP, classifierToEdit);
		if (codegen == null) {
			return null;
		}
		editorModel.setGeneratorID(LanguageCodegen.getID(codegen));
		TextEditorModelSharedResource model = (TextEditorModelSharedResource)
				ServiceUtils.getInstance().getModelSet(serviceRegistry).getModelChecked(TextEditorModelSharedResource.MODEL_ID);
		model.addTextEditorModel(editorModel);

		return editorModel;
	}

	/**
	 * The classifier to edit - corresponding to the selected object.
	 *
	 * @return
	 */
	protected Classifier getClassifierToEdit() {
		if (selectedEObject instanceof Operation) {
			return ((Operation) selectedEObject).getFeaturingClassifiers().get(0);
		}
		else if (selectedEObject instanceof Transition) {
			return ((Transition) selectedEObject).getContainer().getStateMachine().getContext();
		}
		else if (selectedEObject instanceof Behavior) {
			Element owner = (Behavior) selectedEObject;
			while (owner != null) {
				owner = owner.getOwner();
				if ((owner instanceof Classifier) && !(owner instanceof Behavior)) {
					return (Classifier) owner;
				}
			}
			return null;
		}
		else if (selectedEObject instanceof Classifier) {
			// must be class or datatype
			return (Classifier) selectedEObject;
		}
		return null;
	}

	/**
	 * return the editor model corresponding to an EObject
	 *
	 * @param serviceRegistry
	 *            the service registry
	 * @param classifierToEdit
	 *            The classifier for which a CDT editor should be opened
	 * @return
	 * @throws ServiceException
	 * @throws NotFoundException
	 */
	protected TextEditorModel getEditorModel(final ServicesRegistry serviceRegistry, Classifier classifierToEdit) throws ServiceException, NotFoundException {
		TextEditorModelSharedResource model = (TextEditorModelSharedResource)
				ServiceUtils.getInstance().getModelSet(serviceRegistry).getModelChecked(TextEditorModelSharedResource.MODEL_ID);
		return model.getTextEditorModel(classifierToEdit);
	}

	/**
	 * Recursively search in sash models for a FolderModel.
	 * Return the first encountered folder.
	 *
	 * @param panelModel
	 * @return
	 */
	public static int lookupIndex(TabFolder folder, Object model) {

		int index = 0;
		for (PageRef pr : folder.getChildren()) {
			if (pr.getPageIdentifier() == model) {
				return index;
			}
			index++;
		}
		return -1;
	}
}

Back to the top