Skip to main content


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 AlvarezFriend
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 LagardeFriend
Messages: 193
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 AlvarezFriend
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 LagardeFriend
Messages: 193
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 PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior 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 RedorFriend
Messages: 300
Registered: July 2009
Senior 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


Laurent Redor - Obeo

Need training or 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 PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior 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äferFriend
Messages: 2
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 messageGo to next message
VVasanth Kumar is currently offline VVasanth KumarFriend
Messages: 13
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!
Re: Open Diagram programmatically [message #1429404 is a reply to message #1427080] Tue, 23 September 2014 07:33 Go to previous messageGo to next message
Verena Käfer is currently offline Verena KäferFriend
Messages: 2
Registered: September 2014
Junior Member
Hi,

I assume that in your Eclipse instance you have a modeling project (or java project with modeling nature) with a model, a selected viewpoint and a representation? You can create these programatically. Let me know if you need help with this.
Now you want to open them via open... with, correct?
What I did was using the Eclipse editor extension point and connecting the following class as launcher. But you can use the code at every point in your code.
I used the code from Alex Lagarde above.

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.diagram.business.internal.metamodel.spec.DSemanticDiagramSpec;
import org.eclipse.sirius.diagram.sequence.business.internal.metamodel.SequenceDDiagramSpec;
import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;
import org.eclipse.sirius.ui.tools.api.project.ModelingProjectManager;
import org.eclipse.sirius.viewpoint.DAnalysis;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.DView;
import org.eclipse.ui.IEditorLauncher;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;

public class GraphicalEditor implements IEditorLauncher {

	protected AdapterFactoryEditingDomain editingDomain;

	@Override
	public void open(IPath filePath) {

		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		IStructuredSelection selection = (IStructuredSelection) window
				.getSelectionService().getSelection();
		if (selection.getFirstElement() instanceof IFile) {
			IFile file = (IFile) selection.getFirstElement();

			IProject project = file.getProject();

			
			IFile airdFile = project.getFile("representations.aird");

			URI representationsFileURI = URI.createPlatformResourceURI(airdFile
					.getFullPath().toOSString(), true);

			URI fFileURI = URI.createPlatformResourceURI(file.getFullPath()
					.toOSString(), true);

			Session session = SessionManager.INSTANCE.getSession(
					representationsFileURI, new NullProgressMonitor());

			// get representation
			DAnalysis root = (DAnalysis) session.getSessionResource()
					.getContents().get(0);
			EList<DView> views = root.getOwnedViews();

			EList<DRepresentation> representations = new BasicEList<DRepresentation>();
			
			for(DView view : views){
				representations.addAll(view.getOwnedRepresentations());
			}



			DRepresentation representation = null;
			EObject rootObject = null;
			for (DRepresentation currentRep : representations) {
				if (currentRep instanceof SequenceDDiagramSpec) {
					rootObject = ((SequenceDDiagramSpec) currentRep)
							.getTarget();
				} else if (currentRep instanceof DSemanticDiagramSpec) {
					rootObject = ((DSemanticDiagramSpec) currentRep)
							.getTarget();
				}

				Resource eResource = rootObject.eResource();
				URI eUri = eResource.getURI();

				if (eUri.equals(fFileURI)) {
					representation = currentRep;
					break;
				}

			}

			DialectUIManager.INSTANCE.openEditor(session, representation,
					new NullProgressMonitor());
		}

Re: Open Diagram programmatically [message #1780995 is a reply to message #1429404] Wed, 31 January 2018 07:01 Go to previous message
Muhammad Abbas is currently offline Muhammad AbbasFriend
Messages: 5
Registered: January 2018
Junior Member
Question
Hello!, I m trying something very similar (to load aird file standalone). What I want to do is to load an aird file and display its different ViewPoints in the console.
Case 1:
When I try to create a session for the representation File using the following code:
Session session=null;
//model is the uri to the representation file
			try {
				session = SiriusSessionFactory.INSTANCE.createSession(model, new NullProgressMonitor());
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DAnalysis root =(DAnalysis) session.getSessionResource().getContents().get(0);
			DView dView = root.getOwnedViews().get(0);
			System.out.println("Loaded:"+dView.getClass());

I get the following Exception:
org.eclipse.core.runtime.CoreException: Error while loading representations file
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.loadSessionModelResource(SessionFactoryImpl.java:130)
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.createSession(SessionFactoryImpl.java:102)
	at mainpkg.ModelLoader.loadModel(ModelLoader.java:140)
	at mainpkg.MainController.loadModel(MainController.java:135)
	at mainpkg.MainController.main(MainController.java:30)
Caused by: org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.IllegalValueException: Value 'CUAV.afm' is not legal. (file:///C:/Users/Sony/workspace-capella/MBT/Models/CUAV.aird, 4, 52)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$ResourceLocator.demandLoadHelper(ResourceSetImpl.java:804)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$MappedResourceLocator.getResource(ResourceSetImpl.java:1204)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:352)
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.loadSessionModelResource(SessionFactoryImpl.java:118)
	... 4 more
Caused by: org.eclipse.emf.ecore.xmi.IllegalValueException: Value 'CUAV.afm' is not legal. (file:///C:/Users/Sony/workspace-capella/MBT/Models/CUAV.aird, 4, 52)

Please note that the .aird file is a Capella project file.

Case 2:
I tried the following LOC too:
Session siriusSession = SessionManager.INSTANCE.getSession(model , new NullProgressMonitor());

Which caused the following exception:
java.lang.IllegalStateException: Workspace is closed.
	at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:399)
	at org.eclipse.sirius.business.api.query.URIQuery.getCorrespondingResource(URIQuery.java:71)
	at org.eclipse.sirius.business.internal.session.SessionManagerImpl.getSession(SessionManagerImpl.java:251)
	at mainpkg.ModelLoader.loadModel(ModelLoader.java:134)
	at mainpkg.MainController.loadModel(MainController.java:135)
	at mainpkg.MainController.main(MainController.java:30)


Is there any other way of reading the .aird file standalone? Am I doing something wrong?

Previous Topic:Semantic Element of the variable ContainerView
Next Topic:How to run Java code from a Sirius diagram ?
Goto Forum:
  


Current Time: Fri Apr 19 21:21:51 GMT 2024

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

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

Back to the top