Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Programmatically create and open a GMF diagram
Programmatically create and open a GMF diagram [message #484510] Mon, 07 September 2009 21:02 Go to next message
Baptiste Mesta is currently offline Baptiste MestaFriend
Messages: 31
Registered: September 2009
Member
Hi,

I'm trying to programmatically create and open a diagram based on an emf model that I have created.

This EObject is contained in an other EObject, i create the EOBject using the factory:

xyzFactory.eINSTANCE.create....

Then I add it to the parent object.
All these objects are in the same xml file (including the parent's diagram).

My problem is that I can't create the diagram in the same file.

I have tried using the same code as in performFinish() from the generated xyzNewDiagramFileWizard class but the diagram is created in an other file, if i give him the uri of the EObject it erase my file (even if the diagram is correctly opening after that)

here is the code i use from performFinish:

List affectedFiles = new LinkedList();
//using an other uri
		String diagramFile  = "/My Processes/"+form.getName()+".form_diagram";
		URI diagramModelURI = URI.createPlatformResourceURI(diagramFile, true);
		ResourceSet resourceSet = getEditingDomain().getResourceSet();
		final Resource diagramResource = resourceSet.createResource(diagramModelURI);
		
		AbstractTransactionalCommand command = new AbstractTransactionalCommand(
				getEditingDomain(),
				"",
				affectedFiles) {

			protected CommandResult doExecuteWithResult(
					IProgressMonitor monitor, IAdaptable info)
					throws ExecutionException {
				int diagramVID = ProcessVisualIDRegistry
						.getDiagramVisualID(form);
				if (diagramVID != FormEditPart.VISUAL_ID) {
					return CommandResult
							.newErrorCommandResult(Messages.ProcessNewDiagramFileWizard_IncorrectRootError);
				}
				Diagram diagram = ViewService.createDiagram(
						form,
						FormEditPart.MODEL_ID,
						FormDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
				diagramResource.getContents().add(diagram);
				diagramResource.getContents().add(diagram.getElement());
				return CommandResult.newOKCommandResult();
			}
		};
		
		try {
			OperationHistoryFactory.getOperationHistory().execute(command,
					new NullProgressMonitor(), null);
			diagramResource.save(ProcessDiagramEditorUtil.getSaveOptions());
			ProcessDiagramEditorUtil.openDiagram(diagramResource);
		} catch (ExecutionException e) {
			FormDiagramEditorPlugin.getInstance().logError(
					"Unable to create model and diagram", e); //$NON-NLS-1$
		} catch (IOException ex) {
			FormDiagramEditorPlugin.getInstance().logError(
					"Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$
		} catch (PartInitException ex) {
			FormDiagramEditorPlugin.getInstance().logError(
					"Unable to open editor", ex); //$NON-NLS-1$
		}


Have you an idea?
Thanks you by advance for your help!

[Updated on: Mon, 07 September 2009 21:05]

Report message to a moderator

Re: Programmatically create and open a GMF diagram [message #484820 is a reply to message #484510] Wed, 09 September 2009 12:46 Go to previous messageGo to next message
Alexander Shatalin is currently offline Alexander ShatalinFriend
Messages: 2928
Registered: July 2009
Senior Member
Hello Baptiste,

> String diagramFile = "/My
> Processes/"+form.getName()+".form_diagram";
You have to open resource where EObject is located here.

> final Resource diagramResource =
> resourceSet.createResource(diagramModelURI);
use resourceSet.getResource(diagramModelURI, true);
here instead of creating new resource.

> diagramResource.getContents().add(diagram.getElement());
do not add diagram element to the resoruce content - it is already there.

-----------------
Alex Shatalin
Re: Programmatically create and open a GMF diagram [message #484950 is a reply to message #484510] Wed, 09 September 2009 21:06 Go to previous messageGo to next message
Baptiste Mesta is currently offline Baptiste MestaFriend
Messages: 31
Registered: September 2009
Member
Thank you it helped a lot

I did this:
// add the form to the task
		task.getForm().add(myForm);

		//create the diagram
		int diagramVID = ProcessVisualIDRegistry.getDiagramVisualID(form);
		if (diagramVID != FormEditPart.VISUAL_ID) {
			// error
		}
		Diagram diagram = ViewService.createDiagram(myForm,
				FormEditPart.MODEL_ID,
				FormDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
		myForm.eResource().getContents().add(diagram);

		// save the ressource
		try {
			task.eResource().save(ProcessDiagramEditorUtil.getSaveOptions());
		} catch (IOException e) {
			FormDiagramEditorPlugin.getInstance().logError(
					"Save operation failed for: " + task.eResource(), e); //$NON-NLS-1$
		}
		


Now i'm trying to open the Diagram but with it open the whole resource file even if I give him the uri with the fragment corresponding to my diagram:

		//open the diagram
		try {
			URI uri = EcoreUtil.getURI(diagram);
			Resource res = editingDomain.getResourceSet().getResource(uri, true);
			FormDiagramEditorUtil.openDiagram(res);
		} catch (PartInitException e) {
			FormDiagramEditorPlugin.getInstance().logError(
					"Unable to open editor", e); //$NON-NLS-1$
		}

this open the parent's diagram in the sub-diagram editor (???)

Thank you again for your help, and if you have an idea it would be great!


Re: Programmatically create and open a GMF diagram [message #485055 is a reply to message #484950] Thu, 10 September 2009 11:53 Go to previous messageGo to next message
Alex Shatalin is currently offline Alex ShatalinFriend
Messages: 141
Registered: July 2009
Senior Member
Hello Baptiste,

> FormDiagramEditorUtil.openDiagram(res);
I do not have FormDiagramEditorUtil in my workspace..

I suggest you using IWorkbenchPage.openEditor(URIEditorInput(), <editorID>);
for openning diagram editor.

-----------------
Alex Shatalin
Re: Programmatically create and open a GMF diagram [message #485642 is a reply to message #484510] Mon, 14 September 2009 09:44 Go to previous messageGo to next message
Baptiste Mesta is currently offline Baptiste MestaFriend
Messages: 31
Registered: September 2009
Member
It worked perfectly,
Thanks a lot! Smile


Re: Programmatically create and open a GMF diagram [message #486522 is a reply to message #484950] Fri, 18 September 2009 00:59 Go to previous messageGo to next message
Vanessa Aline is currently offline Vanessa AlineFriend
Messages: 11
Registered: July 2009
Junior Member
Hi,

I am trying to create a diagram programatically, like you. What is this
task that you used?

Here:
> // add the form to the task
> task.getForm().add(myForm);


Thanks,
--
Vanessa Aline
Bacharelanda em Ciência da Computação UFBA
Re: Programmatically create and open a GMF diagram [message #486547 is a reply to message #484510] Fri, 18 September 2009 07:32 Go to previous messageGo to next message
Baptiste Mesta is currently offline Baptiste MestaFriend
Messages: 31
Registered: September 2009
Member
"task" is one of my EObjects, i juste add an Element to another one.

I've made 2 functions to do the creation and the opening of the diagram


	public Diagram createDiagram(XXX xxx){
		//create the diagram
		int diagramVID = ProcessVisualIDRegistry.getDiagramVisualID(xxx);
		if (diagramVID != FormEditPart.VISUAL_ID) {
			// error
		}
		Diagram diagram = ViewService.createDiagram(xxx,
				XXXEditPart.MODEL_ID,
				XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
		xxx.eResource().getContents().add(diagram);

		// save the ressource
		try {
			xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
		} catch (IOException e) {
			XXXDiagramEditorPlugin.getInstance().logError(
					"Save operation failed for: " + xxx.eResource(), e); //$NON-NLS-1$
		}
		refresh();
		return diagram;
	}
	

	public void openDiagram(XXX xxx){
		//open the diagram

		try {
                //find the diagram in the resource
			Diagram diag = null;
			EList<EObject> resources = xxx.eResource().getContents();
			for (EObject eObject : resources) {
				if(eObject instanceof Diagram){
					if(((Diagram)eObject).getElement()!=null  && ((Diagram)eObject).getElement().equals(xxx)){
						diag = (Diagram)eObject;
						break;
					}
				}
			}
                   //open it
			URI uri = EcoreUtil.getURI(diag);
			IWorkbenchPage page = PlatformUI.getWorkbench()
			.getActiveWorkbenchWindow().getActivePage();
			page.openEditor(new URIEditorInput(uri,xxx.getName()), XXXDiagramEditor.ID);
			
			
		} catch (PartInitException e) {
			XXXDiagramEditorPlugin.getInstance().logError(
					"Unable to open editor", e); //$NON-NLS-1$
		}


It's better to execute them in a command.

tell me if you have troubles with it


Re: Programmatically create and open a GMF diagram [message #491279 is a reply to message #486547] Tue, 13 October 2009 22:41 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 62
Registered: July 2009
Member
Hi Baptiste,

I am currently trying to implement the following behaviour:

Upon the push of a button, a new model and diagram file shall be created
and the selected diagram elements shall be stored in those files.

It looks like that's what you are doing, although I'm not entirely sure.
Can you help me with implementing the things above? I do not know how to
create new files and store things in them from eithin the run method of
my action.

Regards,
Matthias

Baptiste Mesta wrote:
> "task" is one of my EObjects, i juste add an Element to another one.
>
> I've made 2 functions to do the creation and the opening of the diagram
>
>
>
> public Diagram createDiagram(XXX xxx){
> //create the diagram
> int diagramVID = ProcessVisualIDRegistry.getDiagramVisualID(xxx);
> if (diagramVID != FormEditPart.VISUAL_ID) {
> // error
> }
> Diagram diagram = ViewService.createDiagram(xxx,
> XXXEditPart.MODEL_ID,
> XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
> xxx.eResource().getContents().add(diagram);
>
> // save the ressource
> try {
> xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
> } catch (IOException e) {
> XXXDiagramEditorPlugin.getInstance().logError(
> "Save operation failed for: " + xxx.eResource(), e);
> //$NON-NLS-1$
> }
> refresh();
> return diagram;
> }
>
>
> public void openDiagram(XXX xxx){
> //open the diagram
>
> try {
> //find the diagram in the resource
> Diagram diag = null;
> EList<EObject> resources = xxx.eResource().getContents();
> for (EObject eObject : resources) {
> if(eObject instanceof Diagram){
> if(((Diagram)eObject).getElement()!=null &&
> ((Diagram)eObject).getElement().equals(xxx)){
> diag = (Diagram)eObject;
> break;
> }
> }
> }
> //open it
> URI uri = EcoreUtil.getURI(diag);
> IWorkbenchPage page = PlatformUI.getWorkbench()
> .getActiveWorkbenchWindow().getActivePage();
> page.openEditor(new URIEditorInput(uri,xxx.getName()),
> XXXDiagramEditor.ID);
>
>
> } catch (PartInitException e) {
> XXXDiagramEditorPlugin.getInstance().logError(
> "Unable to open editor", e); //$NON-NLS-1$
> }
>
> It's better to execute them in a command.
>
> tell me if you have troubles with it
Re: Programmatically create and open a GMF diagram [message #491666 is a reply to message #491279] Thu, 15 October 2009 12:28 Go to previous messageGo to next message
Romain Bioteau is currently offline Romain BioteauFriend
Messages: 65
Registered: August 2009
Location: Grenoble
Member
Hi Matthias,

Here is a snippet of code doing the thing in the execute method of an
AbstractHandler:

public Object execute(ExecutionEvent event) throws ExecutionException {
try {


IProject project = //retreive your IProject

URI newProcessURI =
URI.createPlatformResourceURI(project.getFile("your file
name").getFullPath().toString(), false);
Resource diagram = XXXDiagramEditorUtil.createDiagram(newProcessURI,
new NullProgressMonitor());
if (diagram != null) {
try {
XXXDiagramEditorUtil.openDiagram(diagram);
} catch (PartInitException e) {
ErrorDialog.openError(Display.getDefault().getActiveShell(),
"Open New Diagram",
null, e.getStatus());
}
}




return null;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

As you can see here you should reuse generated Util functions.
Good Luck

Romain


Matthias Schmeling a écrit :
> Hi Baptiste,
>
> I am currently trying to implement the following behaviour:
>
> Upon the push of a button, a new model and diagram file shall be created
> and the selected diagram elements shall be stored in those files.
>
> It looks like that's what you are doing, although I'm not entirely sure.
> Can you help me with implementing the things above? I do not know how to
> create new files and store things in them from eithin the run method of
> my action.
>
> Regards,
> Matthias
>
> Baptiste Mesta wrote:
>> "task" is one of my EObjects, i juste add an Element to another one.
>>
>> I've made 2 functions to do the creation and the opening of the diagram
>>
>>
>>
>> public Diagram createDiagram(XXX xxx){
>> //create the diagram
>> int diagramVID = ProcessVisualIDRegistry.getDiagramVisualID(xxx);
>> if (diagramVID != FormEditPart.VISUAL_ID) {
>> // error
>> }
>> Diagram diagram = ViewService.createDiagram(xxx,
>> XXXEditPart.MODEL_ID,
>> XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
>> xxx.eResource().getContents().add(diagram);
>>
>> // save the ressource
>> try {
>> xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
>> } catch (IOException e) {
>> XXXDiagramEditorPlugin.getInstance().logError(
>> "Save operation failed for: " + xxx.eResource(),
>> e); //$NON-NLS-1$
>> }
>> refresh();
>> return diagram;
>> }
>>
>> public void openDiagram(XXX xxx){
>> //open the diagram
>>
>> try {
>> //find the diagram in the resource
>> Diagram diag = null;
>> EList<EObject> resources = xxx.eResource().getContents();
>> for (EObject eObject : resources) {
>> if(eObject instanceof Diagram){
>> if(((Diagram)eObject).getElement()!=null &&
>> ((Diagram)eObject).getElement().equals(xxx)){
>> diag = (Diagram)eObject;
>> break;
>> }
>> }
>> }
>> //open it
>> URI uri = EcoreUtil.getURI(diag);
>> IWorkbenchPage page = PlatformUI.getWorkbench()
>> .getActiveWorkbenchWindow().getActivePage();
>> page.openEditor(new URIEditorInput(uri,xxx.getName()),
>> XXXDiagramEditor.ID);
>> } catch (PartInitException e) {
>> XXXDiagramEditorPlugin.getInstance().logError(
>> "Unable to open editor", e); //$NON-NLS-1$
>> }
>>
>> It's better to execute them in a command.
>>
>> tell me if you have troubles with it


R&D Engineer at BonitaSoft
Re: Programmatically create and open a GMF diagram [message #491792 is a reply to message #491666] Thu, 15 October 2009 21:11 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 62
Registered: July 2009
Member
Thanks a lot Romain. I'll try that.

Romain Bioteau wrote:
> Hi Matthias,
>
> Here is a snippet of code doing the thing in the execute method of an
> AbstractHandler:
>
> public Object execute(ExecutionEvent event) throws ExecutionException {
> try {
>
>
> IProject project = //retreive your IProject
>
> URI newProcessURI =
> URI.createPlatformResourceURI(project.getFile("your file
> name").getFullPath().toString(), false);
> Resource diagram =
> XXXDiagramEditorUtil.createDiagram(newProcessURI, new
> NullProgressMonitor());
> if (diagram != null) {
> try {
> XXXDiagramEditorUtil.openDiagram(diagram);
> } catch (PartInitException e) {
>
> ErrorDialog.openError(Display.getDefault().getActiveShell(),
> "Open New Diagram",
> null, e.getStatus());
> }
> }
>
>
>
>
> return null;
> } catch (Exception ex) {
> ex.printStackTrace();
> return null;
> }
> }
>
> As you can see here you should reuse generated Util functions.
> Good Luck
>
> Romain
>
>
> Matthias Schmeling a écrit :
>> Hi Baptiste,
>>
>> I am currently trying to implement the following behaviour:
>>
>> Upon the push of a button, a new model and diagram file shall be
>> created and the selected diagram elements shall be stored in those files.
>>
>> It looks like that's what you are doing, although I'm not entirely
>> sure. Can you help me with implementing the things above? I do not
>> know how to create new files and store things in them from eithin the
>> run method of my action.
>>
>> Regards,
>> Matthias
>>
>> Baptiste Mesta wrote:
>>> "task" is one of my EObjects, i juste add an Element to another one.
>>>
>>> I've made 2 functions to do the creation and the opening of the diagram
>>>
>>>
>>>
>>> public Diagram createDiagram(XXX xxx){
>>> //create the diagram
>>> int diagramVID =
>>> ProcessVisualIDRegistry.getDiagramVisualID(xxx);
>>> if (diagramVID != FormEditPart.VISUAL_ID) {
>>> // error
>>> }
>>> Diagram diagram = ViewService.createDiagram(xxx,
>>> XXXEditPart.MODEL_ID,
>>> XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
>>> xxx.eResource().getContents().add(diagram);
>>>
>>> // save the ressource
>>> try {
>>> xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
>>> } catch (IOException e) {
>>> XXXDiagramEditorPlugin.getInstance().logError(
>>> "Save operation failed for: " + xxx.eResource(),
>>> e); //$NON-NLS-1$
>>> }
>>> refresh();
>>> return diagram;
>>> }
>>> public void openDiagram(XXX xxx){
>>> //open the diagram
>>>
>>> try {
>>> //find the diagram in the resource
>>> Diagram diag = null;
>>> EList<EObject> resources = xxx.eResource().getContents();
>>> for (EObject eObject : resources) {
>>> if(eObject instanceof Diagram){
>>> if(((Diagram)eObject).getElement()!=null &&
>>> ((Diagram)eObject).getElement().equals(xxx)){
>>> diag = (Diagram)eObject;
>>> break;
>>> }
>>> }
>>> }
>>> //open it
>>> URI uri = EcoreUtil.getURI(diag);
>>> IWorkbenchPage page = PlatformUI.getWorkbench()
>>> .getActiveWorkbenchWindow().getActivePage();
>>> page.openEditor(new URIEditorInput(uri,xxx.getName()),
>>> XXXDiagramEditor.ID);
>>> } catch (PartInitException e) {
>>> XXXDiagramEditorPlugin.getInstance().logError(
>>> "Unable to open editor", e); //$NON-NLS-1$
>>> }
>>>
>>> It's better to execute them in a command.
>>>
>>> tell me if you have troubles with it
Re: Programmatically create and open a GMF diagram [message #1793365 is a reply to message #484510] Mon, 06 August 2018 16:48 Go to previous message
Maude Sabourin is currently offline Maude SabourinFriend
Messages: 11
Registered: May 2018
Junior Member
I have a problem at the second line
		CanardModel model = factory.createCanardModel();

		Diagram diagram = ViewService.createDiagram(model, CanardModelEditPart.MODEL_ID, CanardDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);

		URI diagUri = URI.createFileURI("output/out.canard");
		ResourceSet resourceSet = new ResourceSetImpl();
		Resource diagramResource = resourceSet.createResource(diagUri);
		diagramResource.getContents().add(diagram);


It gives me the following error :
Exception in thread "main" java.lang.ExceptionInInitializerError
	at ca.umontreal.iro.geodes.canard.tool.CanardTool.main(CanardTool.java:52)
Caused by: java.lang.NullPointerException
	at org.eclipse.gmf.runtime.diagram.core.internal.DiagramPlugin.getPluginId(DiagramPlugin.java:48)
	at org.eclipse.gmf.runtime.diagram.core.services.ViewService.<clinit>(ViewService.java:136)
	... 1 more


I have tried a lot of variations to make the EMF to GMF but it doesn't work. I don't understand why my Plugin can't get the ID
Previous Topic:Limiting/changing the anchor points for the connections
Next Topic:GMF2 File Format
Goto Forum:
  


Current Time: Fri Apr 19 22:22:44 GMT 2024

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

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

Back to the top