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 05:02
Missing name Missing name is currently offline Missing name Missing name
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.io.*;
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.edit.parts.EPackageEditPart;
import org.eclipse.gmf.ecore.part.*;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor;
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 {

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

	private class TestECoreDiagramEditor implements Runnable {

		@Override
		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();
				page.closeAllEditors(false);
				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) {
				e.printStackTrace();
			}
		}

		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);
			ePackageResource.getContents().add(ePackage);
			ePackageResource.save(Collections.EMPTY_MAP);
			new File(ePackageURI.toFileString()).deleteOnExit();

			// create an EPackage diagram
			Diagram diagram = ViewService.createDiagram(ePackage, EPackageEditPart.MODEL_ID,
					EcoreDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
			Resource ePackageDiagramResource = resourceSet.createResource(ePackageDiagramURI);
			ePackageDiagramResource.getContents().add(diagram);
			ePackageDiagramResource.save(Collections.EMPTY_MAP);
			new File(ePackageDiagramURI.toFileString()).deleteOnExit();
		}
	}
}


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


Current Time: Wed Jul 30 17:34:33 EDT 2014

Powered by FUDForum. Page generated in 0.01408 seconds