Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Using graphiti diagrams without GUI
Using graphiti diagrams without GUI [message #753469] Thu, 27 October 2011 12:06 Go to next message
Julien Mising name is currently offline Julien Mising name
Messages: 16
Registered: October 2011
Junior Member
Hello,

I made a graphiti-based editor so that I am able to edit graphical component and associate other objects with them. Now, I would like to be able to process the graphical model elements from other programs (some eclipse plugin) and so, get the graphiti objects in a regular java program. So, I would like to know if this is possible. In particular, would it be possible to retrieve the list of PictogramElement from a diagram just by using the diagram file ? Has someone an access of such a piece of code ?

Thanks for any help or suggestion,

Best regards,
Re: Using graphiti diagrams without GUI [message #753511 is a reply to message #753469] Thu, 27 October 2011 14:22 Go to previous messageGo to next message
Hernan Gonzalez is currently offline Hernan Gonzalez
Messages: 181
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)
Re: Using graphiti diagrams without GUI [message #753576 is a reply to message #753469] Thu, 27 October 2011 20:14 Go to previous messageGo to next message
Julien Mising name is currently offline Julien Mising name
Messages: 16
Registered: October 2011
Junior Member
Whow, seems overkilled for simply retrieveing the objects ! There is no standard API in the graphiti API to do that ?
Re: Using graphiti diagrams without GUI [message #753580 is a reply to message #753576] Thu, 27 October 2011 20:32 Go to previous messageGo to next message
Hernan Gonzalez is currently offline Hernan Gonzalez
Messages: 181
Registered: October 2010
Location: Buenos Aires, Argentina
Senior Member
Julien wrote on Thu, 27 October 2011 17:14
Whow, seems overkilled for simply retrieveing the objects ! There is no standard API in the graphiti API to do that ?


Did you take a glimpse at the code? It's just a loading of a EMF resource, you can simplify it to a few lines, depending on your scenario (in which format did you save it? do you want to load the diagram inside some existing/transactional editing domain? etc). Graphiti does not need to "provide a standard API" for this.

Here goes a strimed down version (untested)

  public Diagram loadDiagram(File input) {
    URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
    final ResourceSet resourceSet = new ResourceSetImpl();
    TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(resourceSet);
    URI uridiagram = uri.appendFragment("/0");   // diagram is first object
    EObject diagram = domain.getResourceSet().getEObject(uridiagram, false);
    if (diagram == null) 
      diagram = domain.getResourceSet().getEObject(uridiagram, true);
    return (Diagram) diagram:
  }
Re: Using graphiti diagrams without GUI [message #754020 is a reply to message #753580] Mon, 31 October 2011 14:12 Go to previous message
Julien Mising name is currently offline Julien Mising name
Messages: 16
Registered: October 2011
Junior Member
My apologies, I tried again the code and it works ! The problem was to retrieve the editing domain.

Thanks a lot !
Previous Topic:Display workspace-located image
Next Topic:Create Connection Programatically
Goto Forum:
  


Current Time: Mon Oct 20 08:19:02 GMT 2014

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

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