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 14: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 18:58]

Report message to a moderator

Re: Open Diagram programmatically [message #1228068 is a reply to message #1227183] Mon, 06 January 2014 05:46 Go to previous messageGo to next message
Alex Lagarde is currently offline Alex Lagarde
Messages: 177
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] Mon, 03 February 2014 23: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 03:44 Go to previous messageGo to next message
Alex Lagarde is currently offline Alex Lagarde
Messages: 177
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 06:18 Go to previous message
Christian Pontesegger is currently offline Christian Pontesegger
Messages: 55
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.
Previous Topic:Unable to create view point selection
Next Topic:Create double click action for diagram background?
Goto Forum:
  


Current Time: Wed Aug 20 16:42:39 EDT 2014

Powered by FUDForum. Page generated in 0.01955 seconds