|
Re: Using graphiti diagrams without GUI [message #753511 is a reply to message #753469] |
Thu, 27 October 2011 14:22 |
Hernan Gonzalez Messages: 188 Registered: October 2010 Location: Buenos Aires, Argentina |
Senior Member |
|
|
That's certainly possible. You just need to load the diagram (EMF) from a input (file or whatever). But bear in mind that this depends on how you store the diagram : the most-simple way (default, as in the tutorial) uses a xml file to store the diagram as first element, and after it the business objects.
You can look inside the Graphiti sources to load it yourself. I've been modifiying the general Graphiti load/save architecture, I copy below some methods from my own code, in case you find them useful:
/** Loads diagram from input, assuming diagram is the first object
You can pass a null domain, then it will be created */
public DiagramWithDomain loadDiagram(Object input, TransactionalEditingDomain domain) {
// try to build the diagram uri
URI uri = convertInputToUri(input);
if (uri == null)
throw new RuntimeException("don't know how to open input " + input);
URI uridiagram = uri.appendFragment("/0"); // diagram is first object
if (domain == null) // create domain
domain = DiagramWithDomainHelper.createResourceSetAndEditingDomain(false);
EObject diagram = domain.getResourceSet().getEObject(uridiagram, false);
if (diagram == null) {
diagram = domain.getResourceSet().getEObject(uridiagram, true);
}
if (!(diagram instanceof Diagram))
throw new RuntimeException("Couldn't find a Diagram as first object in " + input + " : " + diagram);
return new DiagramWithDomain((Diagram) diagram, domain);
}
/**
* utility method: creates a new TransactionalEditingDomain with empty
* resourceset, using the GFWorkspaceCommandStackImpl
* (unless darkprocessing==true)
* */
public static TransactionalEditingDomain createResourceSetAndEditingDomain(boolean darkprocessing) {
final ResourceSet resourceSet = new ResourceSetImpl();
TransactionalEditingDomain editingDomain = null;
if (darkprocessing) {
editingDomain = TransactionUtil.getEditingDomain(resourceSet);
if (editingDomain == null) // Not yet existing, create one
editingDomain = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain(resourceSet);
} else {
final IWorkspaceCommandStack workspaceCommandStack = new GFWorkspaceCommandStackImpl(
new DefaultOperationHistory());
editingDomain = new TransactionalEditingDomainImpl(new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE), workspaceCommandStack, resourceSet);
WorkspaceEditingDomainFactory.INSTANCE.mapResourceSet(editingDomain);
}
return editingDomain;
}
public static URI convertInputToUri(final Object input) {
if (input instanceof URI)
return (URI) input;
URI uri = null;
IFile file = null;
if (input instanceof IFile)
file = (IFile) input;
if (input instanceof URIEditorInput)
uri = ((URIEditorInput) input).getURI();
if (uri == null && input instanceof IAdaptable) {
uri = (URI) ((IAdaptable) input).getAdapter(URI.class);
if (uri == null && file == null)
file = (IFile) ((IAdaptable) input).getAdapter(IFile.class);
}
if (uri == null && file != null)
uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
if (uri == null && (input instanceof File || input instanceof CharSequence)) {
uri = URI.createFileURI(input.toString());
}
return uri;
}
/** Wraps a Diagram and a TransactionalEditingDomain */
public class DiagramWithDomain {
public final Diagram diagram;
public final TransactionalEditingDomain domain;
public DiagramWithDomain(Diagram diagram, TransactionalEditingDomain domain) {
this.diagram = diagram;
this.domain = domain;
}
/**
* Warning: this is the resource tha contains the diagram (normally a file) The actual URL to the diagram is typically
* getDiagramResourceURI().append("/0")
*
* @return
*/
public URI getDiagramResourceURI() {
return diagram.eResource().getURI();
}
/** should be only use for "dark" processing, eg for diagram creation */
public void saveplain() throws IOException {
saveplain(Collections.EMPTY_MAP);
}
public void saveplain(final Map<Resource, Map<?, ?>> options) throws IOException {
for(Resource r : domain.getResourceSet().getResources())
r.save(options.get(r));
}
}
Perhaps in you case you want to change
DiagramWithDomainHelper.createResourceSetAndEditingDomain(false);
to
DiagramWithDomainHelper.createResourceSetAndEditingDomain(true);
so that the a simple CommandStack is used (instead of a GFWorkspaceCommandStack)
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03415 seconds