|
Re: Managing separate model files [message #991207 is a reply to message #991176] |
Mon, 17 December 2012 18:18 |
Hallvard Traetteberg Messages: 673 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
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04064 seconds