Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f720df0b5b11baf46d4e258b2425eb823e0465a3 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*****************************************************************************
 * Copyright (c) 2013 Cedric Dumoulin.
 *
 *
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.integrationtests.editor;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;


/**
 * This class allows to create a Papyrus Editor programatically.
 * The creation require the name of a project and the name of the model to create.
 * This class is for use in tests.
 *
 * @author cedric dumoulin
 *
 */
public class ProgramaticPapyrusEditor {

	protected static final String DEFAULT_PROJECT_NAME = "org.eclipse.papyrus.integrationtests.editor";
	protected static final String DEFAULT_MODEL_NAME = "papyrusModelForIntegration";

	/**
	 * Name of the project that will contains Model
	 */
	// protected String projectName = DEFAULT_PROJECT_NAME;

	/**
	 * Name of the model to create.
	 */
	protected String modelName = DEFAULT_MODEL_NAME;

	/** The di resource set. */
	// protected ModelSet modelSet;

	/** The file. */
	// protected IFile file;

	/** The page. */
	protected IWorkbenchPage page;

	/** The papyrus editor. */
	protected IMultiDiagramEditor papyrusEditor;

	/** The project. */
	// protected IProject project;

	/**
	 * An object representing an EclipseProject.
	 */
	protected EclipseProject eclipseProject;


	/** The root. */
	// protected IWorkspaceRoot root;


	/**
	 * Constructor.
	 *
	 * @param projectName
	 * @param modelName
	 * @throws EditorCreationException
	 *             If the creation fails.
	 */
	public ProgramaticPapyrusEditor(String projectName, String modelName) throws ExecutionException {
		this.modelName = modelName;

		// Create an Eclipse project
		eclipseProject = new EclipseProject(projectName);
		// Create a suitable EclipseProject
		initResources(false);

		// Create editor
		createEditor();
	}

	/**
	 * Constructor.
	 *
	 * @param projectName
	 * @param modelName
	 * @throws EditorCreationException
	 *             If the creation fails.
	 */
	public ProgramaticPapyrusEditor(EclipseProject eclipseProject, String modelName) throws ExecutionException {
		this.modelName = modelName;

		if (eclipseProject == null) {
			throw new ExecutionException("The argument 'EclipseProject' should be initialized.");
		}
		this.eclipseProject = eclipseProject;
		// Create a suitable EclipseProject
		initResources(false);

		// Create editor
		createEditor();
	}


	/**
	 *
	 * Constructor.
	 *
	 * @throws EditorCreationException
	 *             If the creation fails.
	 *
	 */
	public ProgramaticPapyrusEditor() throws ExecutionException {

		// Create an Eclipse project
		eclipseProject = new EclipseProject(DEFAULT_PROJECT_NAME);
		// Create a suitable EclipseProject
		initResources(true);

		// Create editor
		createEditor();
	}


	/**
	 * Init the resources.
	 * The {@link #eclipseProject} should be initialized.
	 *
	 * @throws ExecutionException
	 * @throws CoreException
	 * @throws IOException
	 */
	private void initResources(boolean deleteFileIfExists) throws ExecutionException {
		try {
			// Ensure path to specified resource
			eclipseProject.createFolders(modelName);

			// delete existing files, and then create a new model
			IFile file = eclipseProject.getProject().getFile(modelName + ".di");
			if (deleteFileIfExists && file.exists()) {
				file.delete(true, new NullProgressMonitor());
			}

			if (!file.exists()) {
				// Create the model
				createModel(file);
			}
		} catch (Exception e) {
			throw new ExecutionException("Can't init Project and Resources", e);
		}
	}


	/**
	 * Create a model suitable for Papyrus uml.
	 * Save it in the provided file.
	 *
	 * @param file
	 * @throws CoreException
	 * @throws IOException
	 */
	protected void createModel(IFile file) throws CoreException, IOException {

		// Create ModelSet and initialize it with models declared in eclipse extensions
		ModelSet modelSet = new ModelSet();
		ModelsReader reader = new ModelsReader();
		reader.readModel(modelSet);

		file.create(new ByteArrayInputStream(new byte[0]), true, new NullProgressMonitor());
		modelSet.createsModels(file);
		// populate the model
		UmlModel umlModel = UmlUtils.getUmlModel(modelSet);
		umlModel.initializeEmptyModel();

		// ICreationCommand command = getDiagramCommandCreation();
		// command.createDiagram(modelSet, null, "DiagramToTest");
		modelSet.save(new NullProgressMonitor());

	}

	/**
	 * Create a papyrus editor.
	 *
	 * @return
	 * @throws EditorCreationException
	 */
	protected IMultiDiagramEditor createEditor() throws EditorCreationException {
		// IWorkspace workspace = ResourcesPlugin.getWorkspace();
		// root = workspace.getRoot();
		// project = root.getProject(projectName);
		IFile file = eclipseProject.getProject().getFile(modelName + ".di");

		try {
			// //at this point, no resources have been created
			// if(!project.exists()) {
			// project.create(null);
			// }
			// if(!project.isOpen()) {
			// project.open(null);
			// }
			//
			// if(file.exists()) {
			// file.delete(true, new NullProgressMonitor());
			// }
			//
			// if(!file.exists()) {
			// // Create the model
			// createModel(file);
			// }
			//
			// Create the editor
			page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
			papyrusEditor = (IMultiDiagramEditor) page.openEditor(new FileEditorInput(file), desc.getId());
			return papyrusEditor;
		} catch (PartInitException e) {
			e.printStackTrace();
			throw new EditorCreationException("Can't create editor", e);
		} catch (CoreException e) {
			throw new EditorCreationException("Can't create editor", e);
			// } catch (IOException e) {
			// throw new EditorCreationException("Can't create editor", e);
		}

	}

	/**
	 * Dispose the editor.
	 */
	public void dispose() {

		if (papyrusEditor == null) {
			return;
		}

		// Save model, to avoid popup
		papyrusEditor.doSave(new NullProgressMonitor());
		papyrusEditor = null;

		// Dispose done from closeAllEditors ?
		// papyrusEditor.dispose();
		page.closeAllEditors(true);
		try {
			eclipseProject.getProject().delete(true, new NullProgressMonitor());
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 *
	 * @return
	 */
	public IMultiDiagramEditor getEditor() {
		return papyrusEditor;
	}

	/**
	 * Get the {@link ServicesRegistry} associated to the editor.
	 *
	 * @return The service registry.
	 */
	public ServicesRegistry getServiceRegistry() {
		return papyrusEditor.getServicesRegistry();
	}


	/**
	 * Cached value of modelSet.
	 */
	private ModelSet modelSet;

	public ModelSet getModelSet() throws ServiceException {
		if (modelSet == null) {
			modelSet = papyrusEditor.getServicesRegistry().getService(ModelSet.class);
		}
		return modelSet;
	}

	public void undo() throws ServiceException {
		TransactionalEditingDomain domain = getServiceRegistry().getService(TransactionalEditingDomain.class);

		if (domain.getCommandStack().canUndo()) {
			domain.getCommandStack().undo();
		}
	}

	public void redo() throws ServiceException {
		TransactionalEditingDomain domain = getServiceRegistry().getService(TransactionalEditingDomain.class);

		if (domain.getCommandStack().canRedo()) {
			domain.getCommandStack().redo();
		}

	}

	public IOperationHistory getIOperationHistory() {
		return papyrusEditor.getSite().getWorkbenchWindow().getWorkbench().getOperationSupport().getOperationHistory();
	}
}

Back to the top