Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to change the diagram's model at runtime?
How to change the diagram's model at runtime? [message #662167] Tue, 29 March 2011 15:24 Go to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 7
Registered: March 2011
Junior Member
Hello everybody,

how can I edit a diagram at runtime? The following plug-in code opens an editor at startup and tries to add an EClass to the ECore-Diagram. But it does not work :-/
What's going wrong here?

package testecorediagrameditor;
 
import java.io.File;
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.edit.command.AddCommand;
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.notation.Diagram;
import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;
 
public class StartUp implements IStartup {
 
    ResourceSet resourceSet = new ResourceSetImpl();
 
    @Override
    public void earlyStartup() {
        final IWorkbench workbench = PlatformUI.getWorkbench();
        workbench.getDisplay().asyncExec(new TestECoreDiagramEditor());
    }
 
    private class TestECoreDiagramEditor implements Runnable {
 
        @Override
        public void run() {
            try {
                // create an EPackage
                URI ePackageURI = URI.createFileURI("temp_model.ecore");
                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
                URI ePackageDiagramURI = URI.createFileURI("temp_model.ecore_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);
                ePackageDiagramResource.save(Collections.EMPTY_MAP);
                new File(ePackageDiagramURI.toFileString()).deleteOnExit();
 
                // start the editor
                IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                String editorId = EcoreDiagramEditor.ID;
                IEditorPart editorPart = IDE.openEditor(page, new URIEditorInput(ePackageDiagramURI), editorId);
 
                // try to add an element to the diagram
                DiagramDocumentEditor editor = (DiagramDocumentEditor) editorPart;
                TransactionalEditingDomain domain = editor.getEditingDomain();
                AddCommand.create(domain, ePackage, EcorePackage.eINSTANCE.getEPackage_EClassifiers(),
                        EcoreFactory.eINSTANCE.createEClass()).execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


best regards
Re: How to change the diagram's model at runtime? [message #662176 is a reply to message #662167] Tue, 29 March 2011 15:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33258
Registered: July 2009
Senior Member
I think you need to ensure that you have an absolute path there:

URI ePackageURI = URI.createFileURI("temp_model.ecore");

I'm not sure if GMF can deal with file: URIs though.


sricht10@htwm.de wrote:
> Hello everybody,
>
> how can I edit a diagram at runtime? The following plug-in code opens
> an editor at startup and tries to add an EClass to the ECore-Diagram.
> But it does not work :-/
> What's going wrong here?
>
>
> package testecorediagrameditor;
>
> import java.io.File;
> 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.edit.command.AddCommand;
> 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.Di agramDocumentEditor;
>
> import org.eclipse.gmf.runtime.notation.Diagram;
> import org.eclipse.ui.*;
> import org.eclipse.ui.ide.IDE;
>
> public class StartUp implements IStartup {
>
> ResourceSet resourceSet = new ResourceSetImpl();
>
> @Override
> public void earlyStartup() {
> final IWorkbench workbench = PlatformUI.getWorkbench();
> workbench.getDisplay().asyncExec(new TestECoreDiagramEditor());
> }
>
> private class TestECoreDiagramEditor implements Runnable {
>
> @Override
> public void run() {
> try {
> // create an EPackage
> URI ePackageURI = URI.createFileURI("temp_model.ecore");
> 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
> URI ePackageDiagramURI =
> URI.createFileURI("temp_model.ecore_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);
> ePackageDiagramResource.save(Collections.EMPTY_MAP);
> new
> File(ePackageDiagramURI.toFileString()).deleteOnExit();
>
> // start the editor
> IWorkbenchPage page =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage();
> String editorId = EcoreDiagramEditor.ID;
> IEditorPart editorPart = IDE.openEditor(page, new
> URIEditorInput(ePackageDiagramURI), editorId);
>
> // try to add an element to the diagram
> DiagramDocumentEditor editor = (DiagramDocumentEditor)
> editorPart;
> TransactionalEditingDomain domain =
> editor.getEditingDomain();
> AddCommand.create(domain, ePackage,
> EcorePackage.eINSTANCE.getEPackage_EClassifiers(),
> EcoreFactory.eINSTANCE.createEClass()).execute();
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
> }
>
>
> best regards


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to change the diagram's model at runtime? [message #662182 is a reply to message #662176] Tue, 29 March 2011 15:50 Go to previous message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 7
Registered: March 2011
Junior Member
The relative path is no problem for GMF.

I solved my problem by myself Wink The following code works for me.

package testecorediagrameditor;

import java.io.*;
import java.util.*;

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.SetValueCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
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 extends TimerTask implements Runnable {

		private IEditorPart editorPart = null;
		private Timer timer = new Timer();

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

				if (this.editorPart == null) {
					createTempECoreModelWithDiagram(ePackageURI, ePackageDiagramURI);

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

					this.timer.scheduleAtFixedRate(this, 1000, 1000);
				}

				// try to add an element to the diagram
				DiagramDocumentEditor editor = (DiagramDocumentEditor) this.editorPart;
				TransactionalEditingDomain domain = editor.getEditingDomain();

				EPackage ePackage = (EPackage) editor.getDiagram().getElement();

				SetRequest setReq = new SetRequest(domain, ePackage, EcorePackage.eINSTANCE.getEPackage_EClassifiers(),
						EcoreFactory.eINSTANCE.createEClass());

				new SetValueCommand(setReq).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();
		}
	}
}
Previous Topic:GMF Developer Guide eclipse 3.6
Next Topic:Selected Node - Which function is called?
Goto Forum:
  


Current Time: Thu Dec 05 22:39:48 GMT 2024

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

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

Back to the top