Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Access to selected object on the diagram editor from Java code
Access to selected object on the diagram editor from Java code [message #689912] Tue, 28 June 2011 11:21 Go to next message
Denis Makartetskiy is currently offline Denis Makartetskiy
Messages: 5
Registered: June 2011
Junior Member
Dear All,

i was trying to get access to an object on the SysML requirement diagram and coult'd manage to do that.
I figured out what selection listener has to implemented for that, so I did it. Further, every view has to provide selection by implementing ISelectionProvider interface.

Please, could you please tell me if SelectionProvider exists for Papyrus editor?
If not, which is the easier way to get an access to selected object/class (namely to its name and to its attributes) from Java code?

Thank you all.

Denis
Re: Access to selected object on the diagram editor from Java code [message #691298 is a reply to message #689912] Fri, 01 July 2011 03:34 Go to previous messageGo to next message
JM Gauthier is currently offline JM Gauthier
Messages: 25
Registered: June 2011
Junior Member
Hi Denis,

I don't know if my answer is totally in the scope of your problem but I think it could help. I am actually working on a plugin for Papyrus and the following is how I catch the current project with a class implementing ISelectionListener.

When my plugin is active, I add a listener to the editor of Papyrus:

EditorUtils.getWorkbenchWindow().getSelectionService().addPostSelectionListener(new MyListener());


With this, I always know what is the selection (StruturedSelection, Tree viewer, ....).

Then the code of MyListener Object (implements ISelectionListener) :

public void selectionChanged(IWorkbenchPart part, ISelection selection){
 
 if(selection instanceof StructuredSelection){
  
  StructuredSelection ss = (StructuredSelection) selection;
  Iterator it = ss.iterator();

  while(it.hasNext()){
   Object current  = it.next();

   //Then you just have to check the instance of the current object and do some treatment.
   if(current instance of OperationEditPart){ //for example
    ....
   }
  }
 }
}


Hope it helps!
Re: Access to selected object on the diagram editor from Java code [message #691849 is a reply to message #691298] Sat, 02 July 2011 11:21 Go to previous messageGo to next message
Denis Makartetskiy is currently offline Denis Makartetskiy
Messages: 5
Registered: June 2011
Junior Member
Jm.gauthier25.free.fr, thank you for your answer. Yes, it might be what I need.
There is one thing I didn't get.
Where do you add a listener to Papyrus editor? I mean this line:

EditorUtils.getWorkbenchWindow().getSelectionService().addPostSelectionListener(new MyListener());


Which is appropriate papyrus package for doing that?

Thank you.
Denis
Re: Access to selected object on the diagram editor from Java code [message #692341 is a reply to message #691849] Mon, 04 July 2011 05:07 Go to previous messageGo to next message
JM Gauthier is currently offline JM Gauthier
Messages: 25
Registered: June 2011
Junior Member
Hi,

Actually, this line :

EditorUtils.getWorkbenchWindow().getSelectionService().addPostSelectionListener(new MyListener());


is called by a commands (handler) when I click on a button (which contribute to the papyrus tool bar). To summerize, you write this line to the "enter point" of your plugin. I don't know if it's clear... Razz

Re: Access to selected object on the diagram editor from Java code [message #699500 is a reply to message #692341] Thu, 21 July 2011 12:40 Go to previous messageGo to next message
Denis Makartetskiy is currently offline Denis Makartetskiy
Messages: 5
Registered: June 2011
Junior Member
Hi again,
now I get the point, but in order to register such a listener I need to get an access to EditorUtils. How do I get it? Should I use some Papyrus Extension Point or import corresponding Papyrus package and recompile it?

Thanks.
Denis
Re: Access to selected object on the diagram editor from Java code [message #699536 is a reply to message #699500] Thu, 21 July 2011 13:47 Go to previous messageGo to next message
Miles Parker is currently offline Miles Parker
Messages: 1338
Registered: July 2009
Senior Member
Hi Denis,

Here's a really nice article on the selection service http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html but what it boils down to is that all you need is something like:

			ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
			service.addPostSelectionListener(listener);


Of course you want to do it in a nice way -- there are more elegant ways than this -- make sure that the active window is available when you need it and clean up after yourself.
Re: Access to selected object on the diagram editor from Java code [message #699798 is a reply to message #689912] Fri, 22 July 2011 06:19 Go to previous message
Cedric Dumoulin is currently offline Cedric Dumoulin
Messages: 34
Registered: July 2009
Member
Hi,

There is a HowTo in the Papyrus wiki:
http://wiki.eclipse.org/Papyrus_Developer_Guide/How_To_Code_Examples#How_to_Get_the_Current_Selection_from_Java_code

Hope this help,

Cedric

Denis Makartetskiy a écrit :
> Dear All,
> i was trying to get access to an object on the SysML requirement diagram
> and coult'd manage to do that.
> I figured out what selection listener has to implemented for that, so I
> did it. Further, every view has to provide selection by implementing
> ISelectionProvider interface.
> Please, could you please tell me if SelectionProvider exists for Papyrus
> editor?
> If not, which is the easier way to get an access to selected
> object/class (namely to its name and to its attributes) from Java code?
>
> Thank you all.
>
> Denis
Previous Topic:Customize a Dialog Window
Next Topic:Error exporting a papyrus plugin from svn
Goto Forum:
  


Current Time: Thu Jul 31 01:31:13 EDT 2014

Powered by FUDForum. Page generated in 0.02296 seconds