Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Managing separate model files
Managing separate model files [message #991176] Mon, 17 December 2012 15:39 Go to next message
Dan Simkins is currently offline Dan Simkins
Messages: 38
Registered: December 2012
Member
Hi,
I have created an EMF meta-model and associated editor in Graphiti.

I would now like to create a separate resource file (xmi) to store the associated model.

I have basic java experience but have no real experience in Eclipse GMF or Plugins.

When a new diagram is created, I would to be able to create a new (associated) model file. I can then add created objects as per tutorial.

Additionally, when an existing diagram is opened, I would like to load the associated model file.

I would really appreciate some guidance on how to achieve the above.

Many thanks,

Dan

Re: Managing separate model files [message #991207 is a reply to message #991176] Mon, 17 December 2012 18:18 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard Traetteberg
Messages: 601
Registered: July 2009
Location: Trondheim, Norway
Senior Member
On 17.12.12 07.39, Dan Simkins wrote:
> Hi,
> I have created an EMF meta-model and associated editor in Graphiti.
> I would now like to create a separate resource file (xmi) to store the
> associated model.
> I have basic java experience but have no real experience in Eclipse GMF
> or Plugins.
>
> When a new diagram is created, I would to be able to create a new
> (associated) model file. I can then add created objects as per tutorial.
>
> Additionally, when an existing diagram is opened, I would like to load
> the associated model file.
>
> I would really appreciate some guidance on how to achieve the above.

Based on advice on this forum, I made a custom persistence behavior
class, along these lines:

public class ActorDiagramPersistencyBehavior extends
DefaultPersistencyBehavior {

public Diagram loadDiagram(URI uri) {
// load diagram
diagram = super.loadDiagram(uri);
// is it properly linked or not?
if (diagram != null) {
PictogramLink link = diagram.getLink();
if (link == null || link.getBusinessObjects().isEmpty())
ensureModelResource(diagram);
}
}

// create model with root object corresponding to diagram
protected Resource ensureModelResource(Diagram diagram) {
Resource diagramResource = diagram.eResource();
URI modelUri =
diagramResource.getURI().trimFileExtension().appendFileExtension("xactor");
ResourceSet resourceSet =
diagramEditor.getEditingDomain().getResourceSet();
Resource modelResource = ensureModelResource(modelUri, resourceSet);
Nameable modelRoot = ensureModelRoot(modelResource);
// try {
// modelResource.save(Collections.EMPTY_MAP);
// } catch (IOException e) {
// e.printStackTrace();
// }
PictogramLink link = diagram.getLink();
if (link == null) {
diagram.setLink(link = pictogramsFactory.createPictogramLink());
}
link.getBusinessObjects().add(modelRoot);
return modelResource;
}

private Nameable ensureModelRoot(Resource modelResource) {
// create the root object
String name = getModelRootName(modelResource.getURI());
Nameable modelRoot = null;
if (modelResource.getContents().isEmpty()) {
modelRoot = createModelRoot();
modelRoot.setName(name);
modelResource.getContents().add(modelRoot);
} else {
modelRoot = (Nameable) modelResource.getContents().get(0);
}
return modelRoot;
}

Note that since this is considered a change to the model, it needs to be
done in a write transaction, so I have actually created a Command object
as follows, that is executed the the editing domain's command stack:

private Command createModelRootCommand(final Diagram diagram) {
return new AbstractCommand() {
@Override
public boolean canExecute() {
if (diagram != null) {
PictogramLink link = diagram.getLink();
return (link == null || link.getBusinessObjects().isEmpty());
}
return false;
}
@Override
public void execute() {
ensureModelResource(diagram);
}
@Override
public void redo() {
}
@Override
public boolean canUndo() {
return false;
}
};
}


Hallvard
Re: Managing separate model files [message #991282 is a reply to message #991207] Tue, 18 December 2012 10:32 Go to previous messageGo to next message
Dan Simkins is currently offline Dan Simkins
Messages: 38
Registered: December 2012
Member
Hi Hallvard et al,

Many thanks for your response. This is precisely what I was looking for. I understand the need for the creation of the custom persistence class. Whilst I am happy with the creation of such a class, I am struggling understanding how to ensure that this gets invoked.

I have looked at the Graphiti help where it states that I need to use DiagramEditor.createPersistencyBehavior() to modify the persistence behaviour. As this method is protected, does this imply that I need to also create my own custom DiagramEditor class? I am hoping not and would appreciate any additional help.

Many thanks

Dan
Re: Managing separate model files [message #991301 is a reply to message #991282] Tue, 18 December 2012 12:25 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1618
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Dan,

actually that would mean it. But that should not be too much overhead:
- subclass DiagramEditor
- override that single method and create your behavior instance
- register your editor class with the org.eclipse.ui.editors extension point
(probably for your own diagram file extension)

There's an enhancement request to make the behavior objects injectable but
that is not yet implemented:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=394315

Michael
Re: Managing separate model files [message #991305 is a reply to message #991301] Tue, 18 December 2012 12:42 Go to previous messageGo to next message
Dan Simkins is currently offline Dan Simkins
Messages: 38
Registered: December 2012
Member
Hi Michael,

I shall do as you suggest!

Many thanks for your response,

Dan
Re: Managing separate model files [message #991468 is a reply to message #991305] Wed, 19 December 2012 11:15 Go to previous messageGo to next message
Dan Simkins is currently offline Dan Simkins
Messages: 38
Registered: December 2012
Member
Hi Michael,

I have successfully created my own DiagramEditor subclass and registered this as an extension point in eclipse. My subclass gets invoked when I open a diagram file (I registered "diagram" as the extension).

I am trying to understand the life-cycle of DiagramEditor. My observations (under debugger) are that if I open a file, the constructor of my subclass gets invoked as does the init method (and loadDiagram as expected). However, when I create a new diagram, neither are invoked although a new diagram file is created and written to disk.

Is it possible to inject my own code on creation of a new diagram?

Many thanks

Dan

Re: Managing separate model files [message #991658 is a reply to message #991468] Wed, 19 December 2012 23:35 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard Traetteberg
Messages: 601
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Hi,

I assume you are using a wizard to create the new diagram. Are you sure
that wizard opens your editor and not the default/generic one?

Usually you want to create your own wizard that opens your own editor
(remember to register it in plugin.xml).

Hallvard

On 19.12.12 03.15, Dan Simkins wrote:
> Hi Michael,
>
> I have successfully created my own DiagramEditor subclass and registered
> this as an extension point in eclipse. My subclass gets invoked when I
> open a diagram file (I registered "diagram" as the extension).
>
> I am trying to understand the life-cycle of DiagramEditor. My
> observations (under debugger) are that if I open a file, the constructor
> of my subclass gets invoked as does the init method (and loadDiagram as
> expected). However, when I create a new diagram, neither are invoked
> although a new diagram file is created and written to disk.
> Is it possible to inject my own code on creation of a new diagram?
Re: Managing separate model files [message #992334 is a reply to message #991658] Fri, 21 December 2012 11:34 Go to previous message
Dan Simkins is currently offline Dan Simkins
Messages: 38
Registered: December 2012
Member
Hi Hallvard,

Thank you - you were correct; it was the fact that the wizard was launching the default DiagramEditor.

I have created and registered my own wizard which now launches my custom diagram editor.

Many thanks to Michael and yourself for assistance.

Kind regards

Dan
Previous Topic:JFace Error
Next Topic:Digram Editor loses focus on closing of MessageDialog in Graphiti
Goto Forum:
  


Current Time: Fri Oct 24 16:23:04 GMT 2014

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

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