Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to exchange the root element of the diagram editor?
How to exchange the root element of the diagram editor? [message #662318] Wed, 30 March 2011 09:02
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 7
Registered: March 2011
Junior Member
Hello gmf experts Smile

Is it possible to exchange the root element which is shown within a diagram editor?
I tried it with the following source code ... but it does not work

package testecorediagrameditor;

import java.util.Collections;

import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.*;
import org.eclipse.emf.ecore.resource.*;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.ecore.part.*;
import org.eclipse.gmf.runtime.emf.type.core.commands.*;
import org.eclipse.gmf.runtime.emf.type.core.requests.*;
import org.eclipse.gmf.runtime.notation.*;
import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;

public class StartUp implements IStartup {

	public void earlyStartup() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		workbench.getDisplay().asyncExec(new TestECoreDiagramEditor());

	private class TestECoreDiagramEditor implements Runnable {

		public void run() {
			try {
				URI ePackageURI = URI.createFileURI("temp_model.ecore");
				URI ePackageDiagramURI = URI.createFileURI("temp_model.ecore_diagram");

				createTempECoreModelWithDiagram(ePackageURI, ePackageDiagramURI);

				// start the editor
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				String editorId = EcoreDiagramEditor.ID;
				IEditorPart editorPart = IDE.openEditor(page, new URIEditorInput(ePackageDiagramURI), editorId);

				// try to exchange the diagram root element
				DiagramDocumentEditor editor = (DiagramDocumentEditor) editorPart;
				TransactionalEditingDomain domain = editor.getEditingDomain();

				// destroy the root element
				EPackage ePackage = (EPackage) editor.getDiagram().getElement();
				DestroyElementRequest delReq = new DestroyElementRequest(domain, ePackage, false);
				new DestroyElementCommand(delReq).execute(null, editor);

				// create the new root element
				EPackage newEPackage = EcoreFactory.eINSTANCE.createEPackage();
				newEPackage.setName("new package");
				SetRequest setReq3 = new SetRequest(domain, editor.getDiagram(),
						NotationPackage.eINSTANCE.getView_Element(), newEPackage);
				new SetValueCommand(setReq3).execute(null, editor);

				// add a new class (EClass) to the new root element (EPackage)
				SetRequest setReq4 = new SetRequest(domain, newEPackage,
						EcorePackage.eINSTANCE.getEPackage_EClassifiers(), EcoreFactory.eINSTANCE.createEClass());
				new SetValueCommand(setReq4).execute(null, editor);
			} catch (Exception e) {

		private void createTempECoreModelWithDiagram(URI ePackageURI, URI ePackageDiagramURI) throws IOException {
			ResourceSet resourceSet = new ResourceSetImpl();

			// create an EPackage
			EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
			ePackage.setName("main package");
			Resource ePackageResource = resourceSet.createResource(ePackageURI);
			new File(ePackageURI.toFileString()).deleteOnExit();

			// create an EPackage diagram
			Diagram diagram = ViewService.createDiagram(ePackage, EPackageEditPart.MODEL_ID,
			Resource ePackageDiagramResource = resourceSet.createResource(ePackageDiagramURI);
			new File(ePackageDiagramURI.toFileString()).deleteOnExit();

best regards
Previous Topic:Connection issue
Next Topic:How to add dependencies to manifest.xpt
Goto Forum:

Current Time: Fri Jan 20 05:51:12 GMT 2017

Powered by FUDForum. Page generated in 0.14636 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software