Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Open Diagram programmatically
Open Diagram programmatically [message #1227183] Fri, 03 January 2014 19:48 Go to next message
Camilo Alvarez is currently offline Camilo Alvarez
Messages: 5
Registered: July 2013
Junior Member
Hi,

How can I open a diagram programmatically?

Thanks,
Camilo.

[Updated on: Fri, 03 January 2014 23:58]

Report message to a moderator

Re: Open Diagram programmatically [message #1228068 is a reply to message #1227183] Mon, 06 January 2014 10:46 Go to previous messageGo to next message
Alex Lagarde is currently offline Alex Lagarde
Messages: 185
Registered: May 2010
Senior Member

Hi Camilo,

you can use:
- the org.eclipse.sirius.business.api.dialect.DialectManager to create,
delete, copy (etc...) representations (e.g. DDiagrams, DTables...)
- the org.eclipse.sirius.ui.business.api.dialect.DialectUIManager to
open and close editors.

So to open a diagram programmatically, use
DialectUIManager.INSTANCE.openEditor(mySession, myDiagram,
myProgressMonitor);

You may use a NullProgressMonitor if you are not interested in giving
progression feedback to end-user.

Best regards, and please let my know if you have follow-up questions,
Alex

Le 03/01/2014 20:48, Camilo Alvarez a écrit :
> Hi,
>
> How can I open a diagram programmatically?
>
> Thanks,
> Camilo.
>
>
Re: Open Diagram programmatically [message #1239387 is a reply to message #1228068] Tue, 04 February 2014 04:17 Go to previous messageGo to next message
Camilo Alvarez is currently offline Camilo Alvarez
Messages: 5
Registered: July 2013
Junior Member
Hi Alex,

Thank you for the answer.
I implemented the functionality looking into the source code of the Model Explorer, using this code:

org.eclipse.sirius.ui.business.api.session.SessionEditorInput sEditor = 
new SessionEditorInput(diagramURI, "New Diagram", null);
IEditorPart openEditor = IDE.openEditor(page, sEditor,"org.eclipse.sirius.diagram.part.SiriusDiagramEditorID", true);


Im trying to use the DialectUIManager
diagramURI = URI.createURI("/" + dFile.getProject().getName() + "/" + dFile.getProjectRelativePath().toOSString());
ResourceSet rs = new ResourceSetImpl(); 
Resource rsc = rs.createResource(diagramURI);
try {
	load(null);
} catch (IOException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
}

DAnalysis root = (DAnalysis) rsc.getContents().get(0);
DView dView = root.getOwnedViews().get(0);
DRepresentation rep = dView.getOwnedRepresentations().get(0); 
DialectUIManager.INSTANCE.openEditor(SessionManager.INSTANCE.getSession(diagramURI, new NullProgressMonitor()), rep, new NullProgressMonitor());


but i get this exception

!ENTRY org.eclipse.sirius 4 0 2014-02-03 22:26:16.667
!MESSAGE Impossible to find an interpreter - Could not find a session for model element : org.eclipse.sirius.viewpoint.impl.DAnalysisImpl@5d73d6f0 (eProxyURI: genCode.mtc#/)
!STACK 0
java.lang.RuntimeException
	at org.eclipse.sirius.tools.api.interpreter.InterpreterRegistry.getInterpreter(InterpreterRegistry.java:111)
	at org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.hookGraphicalViewer(DDiagramEditorImpl.java:649)
	at org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createOriginalGraphicalViewer(DDiagramEditorImpl.java:1444)
	at org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createMainDiagramSection(DDiagramEditorImpl.java:1366)
	at org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createGraphicalViewer(DDiagramEditorImpl.java:1359)
	at org.eclipse.gef.ui.parts.GraphicalEditor.createPartControl(GraphicalEditor.java:171)
	at org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor.createPartControl(DiagramEditor.java:1580)
	at org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditorWithFlyOutPalette.createPartControl(DiagramEditorWithFlyOutPalette.java:328)
	at org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor.createPartControl(DiagramDocumentEditor.java:1514)
	at org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createPartControl(DDiagramEditorImpl.java:431)


What Im trying to do is to have a file per Diagram with my own file extension, i created an editor that invokes the Sirius code to open the representation file.
Is possible to store only a diagram in the archive an open directly with an editor?. It would be nice if i can use a ExtensionPoint that associate my extension with the sirius diagram/table/etc..

Best Regards,
Camilo
Re: Open Diagram programmatically [message #1239455 is a reply to message #1239387] Tue, 04 February 2014 08:44 Go to previous messageGo to next message
Alex Lagarde is currently offline Alex Lagarde
Messages: 185
Registered: May 2010
Senior Member

Hi Camilo,

this is the Sirius "Session" which is in charge of loading models (and
many other things e.g. initializing Interpreters).
Moreover, you should not create an editor input by yourself, and use the
DialectUIManager.openEditor() method (as explained in my previous post).

So to give you a complete example similar to what you wrote:

// Step 1: get the .aird file and the corresponding Sirius Session
URI myRepresentationsFileURI = URI.createURI("/" +
dFile.getProject().getName() + "/" +
dFile.getProjectRelativePath().toOSString());

Session siriusSession =
SessionManager.INSTANCE.getSession(myRepresentationsFileURI , new
NullProgressMonitor());

// Step 2: get the DRepresentation to open
DAnalysis root =siriusSession.getSessionResource().getContents().get(0);
DView dView = root.getOwnedViews().get(0);
DRepresentation myRepresentation = dView.getOwnedRepresentations().get(0);

// Step 3: open representation
DialectUIManager.INSTANCE.openEditor(siriusSession, myRepresentation,
new NullProgressMonitor());

In regards to your last question, I do not understand why you would need
to change the file extension we use by defaul (".aird"), this does not
prevent you from creating a new aird file per representation.
Maybe you should take a look at the DAnalysisSelector interface, that
you can implement to customize the .aird file in which new
representations will be created, and create a new .aird file for each
new representation. You can then plug this custom DAnalysisSelector by
calling siriusSession.setAnalysisSelector().

Please let me know if have any further question,
Alex


Le 04/02/2014 05:17, Camilo Alvarez a écrit :
> Hi Alex,
>
> Thank you for the answer.
> I implemented the functionality looking into the source code of the
> Model Explorer, using this code:
>
>
> org.eclipse.sirius.ui.business.api.session.SessionEditorInput sEditor =
> new SessionEditorInput(diagramURI, "New Diagram", null);
> IEditorPart openEditor = IDE.openEditor(page,
> sEditor,"org.eclipse.sirius.diagram.part.SiriusDiagramEditorID", true);
>
>
> Im trying to use the DialectUIManager
> diagramURI = URI.createURI("/" + dFile.getProject().getName() + "/" +
> dFile.getProjectRelativePath().toOSString());
> ResourceSet rs = new ResourceSetImpl(); Resource rsc =
> rs.createResource(diagramURI);
> try {
> load(null);
> } catch (IOException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
>
> DAnalysis root = (DAnalysis) rsc.getContents().get(0);
> DView dView = root.getOwnedViews().get(0);
> DRepresentation rep = dView.getOwnedRepresentations().get(0);
> DialectUIManager.INSTANCE.openEditor(SessionManager.INSTANCE.getSession(diagramURI,
> new NullProgressMonitor()), rep, new NullProgressMonitor());
>
>
> but i get this exception
>
>
> !ENTRY org.eclipse.sirius 4 0 2014-02-03 22:26:16.667
> !MESSAGE Impossible to find an interpreter - Could not find a session
> for model element :
> org.eclipse.sirius.viewpoint.impl.DAnalysisImpl@5d73d6f0 (eProxyURI:
> genCode.mtc#/)
> !STACK 0
> java.lang.RuntimeException
> at
> org.eclipse.sirius.tools.api.interpreter.InterpreterRegistry.getInterpreter(InterpreterRegistry.java:111)
>
> at
> org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.hookGraphicalViewer(DDiagramEditorImpl.java:649)
>
> at
> org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createOriginalGraphicalViewer(DDiagramEditorImpl.java:1444)
>
> at
> org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createMainDiagramSection(DDiagramEditorImpl.java:1366)
>
> at
> org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createGraphicalViewer(DDiagramEditorImpl.java:1359)
>
> at
> org.eclipse.gef.ui.parts.GraphicalEditor.createPartControl(GraphicalEditor.java:171)
>
> at
> org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor.createPartControl(DiagramEditor.java:1580)
>
> at
> org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditorWithFlyOutPalette.createPartControl(DiagramEditorWithFlyOutPalette.java:328)
>
> at
> org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor.createPartControl(DiagramDocumentEditor.java:1514)
>
> at
> org.eclipse.sirius.diagram.tools.internal.editor.DDiagramEditorImpl.createPartControl(DDiagramEditorImpl.java:431)
>
>
>
> What Im trying to do is to have a file per Diagram with my own file
> extension, i created an editor that invokes the Sirius code to open the
> representation file. Is possible to store only a diagram in the archive
> an open directly with an editor?. It would be nice if i can use a
> ExtensionPoint that associate my extension with the sirius
> diagram/table/etc..
>
> Best Regards,
> Camilo
Re: Open Diagram programmatically [message #1410289 is a reply to message #1239455] Wed, 20 August 2014 10:18 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian Pontesegger
Messages: 65
Registered: July 2009
Location: Graz, Austria
Member
Followup as my question is similar:
I see how I can open an editor programmatically now. Is there a way to register an editor for a given file extension similar to the way text editors are registered?
So if I have a file mydata.ext I would like to use the defined sirius editor as the default editor, while the user still could use the "Open with" context menu for other choices (or even other sirius diagram types).

Double clicking mydata.ext in the resource navigator view should then fire up the sirius editor.
Re: Open Diagram programmatically [message #1410637 is a reply to message #1410289] Thu, 21 August 2014 09:02 Go to previous messageGo to next message
Laurent Redor is currently offline Laurent Redor
Messages: 38
Registered: July 2009
Member
Le 20/08/2014 12:18, Christian Pontesegger a écrit :
> Followup as my question is similar:
> I see how I can open an editor programmatically now. Is there a way to
> register an editor for a given file extension similar to the way text
> editors are registered?
> So if I have a file mydata.ext I would like to use the defined sirius
> editor as the default editor, while the user still could use the "Open
> with" context menu for other choices (or even other sirius diagram types).
>
> Double clicking mydata.ext in the resource navigator view should then
> fire up the sirius editor.

Hi,

There is no specific API for this in Sirius.
It may be possible using the Eclipse API to detect a double click on
mydata.ext and then reuse the code of this post to open the desired
representation.

There is also an option on representation, "Show on Startup", to open
automatically the representations of this kind when a session is opened.
I'm not sure it meets your needs, but I preferred to indicate.

Best regards,

--
Laurent - Obeo

Need professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Open Diagram programmatically [message #1410714 is a reply to message #1410637] Thu, 21 August 2014 12:50 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian Pontesegger
Messages: 65
Registered: July 2009
Location: Graz, Austria
Member
Thanks, not exactly what I was looking for. Adding double click listeners for all Navigator views around seems like a hack to me. Also The Sirius editor would not show up in file context menus that way.

Raised https://bugs.eclipse.org/bugs/show_bug.cgi?id=442261

[Updated on: Thu, 21 August 2014 12:50]

Report message to a moderator

Re: Open Diagram programmatically [message #1426127 is a reply to message #1410714] Thu, 18 September 2014 09:32 Go to previous messageGo to next message
Verena Käfer is currently offline Verena Käfer
Messages: 1
Registered: September 2014
Junior Member
Hi,
I had the same problem trying to open the diagram programatically.
What I did:
- Use the org.eclipse.ui.editor extension point
- Use a launcher (IEditorLauncher), not a class
- In the open method use the code from above to open the sirius editor with DialectUIManager.INSTANCE.openEditor(session, representation, new NullProgressMonitor());
Re: Open Diagram programmatically [message #1427080 is a reply to message #1426127] Fri, 19 September 2014 17:20 Go to previous message
VVasanth Kumar is currently offline VVasanth Kumar
Messages: 6
Registered: July 2012
Junior Member
Hi,

Could you please point me to any reference over github/svn that contains the code to create the diagram programmatically?

I am having tough time in finding the right direction. Following are the issues that I am facing:

1. I am not able to find the sirius jar that I could import into my regular java project (I want to create the model and diagram from the normal java project and would access the generated diagram through Sirius project).
2. To start with, I have added all the jars of Sirius jar in to my classpath of sirius project and I am trying to open a editor. But it displays null pointer exception.

It would be really great, if you point me to the right direction. Thanks in advance!
Previous Topic:Optimize performances on big models
Next Topic:Documentation / Tutorial
Goto Forum:
  


Current Time: Mon Sep 22 06:17:46 GMT 2014

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

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