Home » Modeling » Graphiti » Adding Root Domain Object on Diagram Creation
| |
Re: Adding Root Domain Object on Diagram Creation [message #1001985 is a reply to message #1001973] |
Thu, 17 January 2013 16:52 |
Dan Simkins Messages: 38 Registered: December 2012 |
Member |
|
|
Hi Michael,
That's what I'm trying to do, but failing miserably - I seem to have 'code' blindness!!
I am trying to add it (as per above) in the performFinish method. I have tried in a number of places. For instance, after creating the diagram (using - Diagram diagram = Graphiti.getPeCreateService().createDiagram( diagramTypeId, diagramName, true ) ), if I attempt to create and add the root domain at that point, I get a null pointer exception on execution at:
diagram.eResource().getContents().add( model );
I'm sure I must be doing something really dumb!
Dan
[Updated on: Thu, 17 January 2013 18:03] Report message to a moderator
|
|
|
Re: Adding Root Domain Object on Diagram Creation [message #1002080 is a reply to message #1001985] |
Thu, 17 January 2013 22:23 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
On 17.01.13 08.52, Dan Simkins wrote:
> Hi Michael,
>
> That's what I'm trying to do, but failing miserably - I seem to have
> 'code' blindness!!
>
> I am trying to add it (as per above) in the performFinish method. I have
> tried in a number of places. For instance, after creating the diagram
> (using - Diagram diagram = Graphiti.getPeCreateService().createDiagram(
> diagramTypeId, diagramName, true ) ), if I attempt to create and add the
> root domain at that point, I get a null pointer exception on execution of:
>
> diagram.eResource().getContents().add( model );
It seems diagram is not inside a resource, hence eResource() returns
null. Have you created a ResourceSet and a Resource and added the
diagram? Below I've pasted code from
org.eclipse.graphiti.examples.common.FileService that does that. Insert
a call to it before the line that gives an NPE.
Hallvard
package org.eclipse.graphiti.examples.common;
public class FileService {
/**
* @since 0.9
*/
public static void createEmfFileForDiagram(URI diagramResourceUri,
final Diagram diagram) {
// Create a resource set and EditingDomain
final TransactionalEditingDomain editingDomain =
GraphitiUiInternal.getEmfService()
.createResourceSetAndEditingDomain();
final ResourceSet resourceSet = editingDomain.getResourceSet();
// Create a resource for this file.
final Resource resource = resourceSet.createResource(diagramResourceUri);
final CommandStack commandStack = editingDomain.getCommandStack();
commandStack.execute(new RecordingCommand(editingDomain) {
@Override
protected void doExecute() {
resource.setTrackingModification(true);
resource.getContents().add(diagram);
}
});
save(editingDomain, Collections.<Resource, Map<?, ?>> emptyMap());
editingDomain.dispose();
}
Hallvard
|
|
|
Re: Adding Root Domain Object on Diagram Creation [message #1002090 is a reply to message #1002080] |
Thu, 17 January 2013 22:59 |
Dan Simkins Messages: 38 Registered: December 2012 |
Member |
|
|
Hi Hallvard,
Thanks for your response.
In my initial post, I had tried to add the root domain object after the call to createEmfFileForDiagram. My apologies - my subsequent post was misleading in that I tried adding the root domain object before the call to createEmfFileForDiagram and as you correctly noted, no resource had been created and thus I got a NPE.
My problem still remains the same - the essential sequence is as below, however, the resultant diagram file (which is fine in all other respects) does not contain my root domain object:
Diagram diagram = Graphiti.getPeCreateService().createDiagram( diagramTypeId, diagramName, true );
IFile diagramFile = diagramFolder.getFile( diagramName + "." + editorExtension );
URI uri = URI.createPlatformResourceURI( diagramFile.getFullPath().toString(), true );
FileService.createEmfFileForDiagram( uri, diagram );
String providerId = GraphitiUi.getExtensionManager().getDiagramTypeProviderId( diagram.getDiagramTypeId() );
DiagramEditorInput editorInput = new DiagramEditorInput( EcoreUtil.getURI( diagram ), providerId );
try
{
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor( editorInput, editorID );
ModelPackageImpl.init();
Model model = ControlFactory.eINSTANCE.createModel();
diagram.eResource().getContents().add( model );
}
...
Apologies for the misleading NPE post.
I would really appreciate any ideas.
Thanks
Dan
|
|
|
Re: Adding Root Domain Object on Diagram Creation [message #1002522 is a reply to message #1002090] |
Fri, 18 January 2013 19:01 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
Dan,
You must link the diagram to it, too.
Hallvard
On 17.01.13 14.59, Dan Simkins wrote:
> Hi Hallvard,
>
> Thanks for your response.
>
> In my initial post, I had tried to add the root domain object after the
> call to createEmfFileForDiagram. My apologies - my subsequent post was
> misleading in that I tried adding the root domain object before the call
> to createEmfFileForDiagram and as you correctly noted, no resource had
> been created and thus I got a NPE.
>
> My problem still remains the same - the essential sequence is as below,
> however, the resultant diagram file (which is fine in all other
> respects) does not contain my root domain object:
>
> Diagram diagram = Graphiti.getPeCreateService().createDiagram(
> diagramTypeId, diagramName, true );
>
> IFile diagramFile = diagramFolder.getFile( diagramName + "." +
> editorExtension );
> URI uri = URI.createPlatformResourceURI(
> diagramFile.getFullPath().toString(), true );
>
> FileService.createEmfFileForDiagram( uri, diagram );
>
> String providerId =
> GraphitiUi.getExtensionManager().getDiagramTypeProviderId(
> diagram.getDiagramTypeId() );
>
> DiagramEditorInput editorInput = new DiagramEditorInput(
> EcoreUtil.getURI( diagram ), providerId );
>
> try
> {
>
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(
> editorInput, editorID );
>
> ModelPackageImpl.init();
> Model model = ControlFactory.eINSTANCE.createModel();
> diagram.eResource().getContents().add( model );
> }
> ..
>
> Apologies for the misleading NPE post.
>
> I would really appreciate any ideas.
>
> Thanks
>
> Dan
>
>
|
|
| |
Goto Forum:
Current Time: Sun Sep 22 21:38:29 GMT 2024
Powered by FUDForum. Page generated in 0.04155 seconds
|