file Selected/selecting elements, Navigation Listener in jython scripts

8 years 9 months ago - 8 years 9 months ago #2173 by jmfavre
Bonsoir,
I've started writing a model/metamodel explorer in jython, and I want to add a feature so that it can follow the selection, and conversion have a feature "select in explorer". All in all I need to access the selection/change it.

(1) Is there in the API a way to get directly the current selection? I've not found this function and I'm wondering why. May be it is somewhere but searching for "select*" did not give me the answer ;-)
Yes, I know that the script engine is started with three objects available ("selection", "Modelio", and "SelectedElements"), so in a regular script one can use these variables (at least to read them), but I'm using python modules to improve the code structure and these variables are not available there. In any case, I guess that there is a way to get the set of all selected elements.

(2) There is a navigation service, but it gives only the last selected element. In any case I've a much more important concern here: I do not understand how to catch the event with jython as the following code do not give any result.
# define the listener
class SimpleNavigationListener(INavigationListener):
  def navigateTo(self,target):
    print "selected element is ",target.getName()
# create a listener
myListener = SimpleNavigationListener()
# register it
navservice = Modelio.getInstance().getNavigationService()
navservice.addNavigationListener(myListener)
# from here something should be displayed if as we select some elements
# but nothing actually happens :-(

# the code below works. We can select an element (here the root). 
navservice.fireNavigate(Modelio.getInstance().getModelingSession().getModel().getRoot())
Is there a limitation of jython scripts in terms of listening events because of running in a different thread? I've created SWT components for the browser and have no problem with listener there. Do I miss something here?

Thanks for your help.
Jean-marie

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2175 by chm
Hi jmfavre,

The navigation service you found should have worked, but it seems it is badly initalized in Modelio 2.2.x and only 'fireNavigate' is working... :(
However, it would not have been the appropriate answer for you, as you only get the result of our 'navigation event', i.e. when we set a specific selection programmaticaly.

There is no service in Modelio giving the current selection, but it is possible to rely on the RCP platform it is based on. Here is your code sample:
from org.eclipse.ui import ISelectionListener
from org.eclipse.ui import PlatformUI

# define the listener
class SimpleNavigationListener(ISelectionListener):
  def selectionChanged(self,part,selection):
    print "eclipse selection is ",selection
    # warning: this selection often contains IAdaptable instead of IElement

# create a listener
myListener = SimpleNavigationListener()
# register it
selectionservice = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
selectionservice.addSelectionListener(myListener)
# from here something should be displayed if as we select some elements

# the code below works. We can select an element (here the root).
navservice = Modelio.getInstance().getNavigationService() 
navservice.fireNavigate(Modelio.getInstance().getModelingSession().getModel().getRoot())

Hope this helps,


CHM
The following user(s) said Thank You: jmfavre

Please Log in or Create an account to join the conversation.

8 years 9 months ago - 8 years 9 months ago #2177 by jmfavre
Thank you very much for this detailed answer. With eclipse selection this is working properly now, although it returns adapters as mentionned. I will check how to filter elements and post the answer here ;)

Just for the record, it seems that modelio selection listener interface does not work either in Modelio 3.0. Here is the code adapted.
from org.modelio.api.app.navigation import INavigationListener
from org.modelio.api.modelio import Modelio
# define the listener
class SimpleNavigationListener(INavigationListener):
  def navigateTo(self,target):
    print "selected element is ",target.getName()
# create a listener
myListener = SimpleNavigationListener()
# register it
navservice = Modelio.getInstance().getNavigationService()
navservice.addNavigationListener(myListener)
imports are added (see next thread)

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2180 by chm
Thank you for the script, I'll check it on Modelio 3 as soon as possible.

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2192 by jmfavre
Bonjour,
I've just realized that the "fireNavigate" method do not work either on modelio 3.
Just create a class "MyClass" and try the code below:
print "Assuming that there is a class MyClass in the project"
from org.modelio.metamodel.uml.statik import Class
from org.modelio.api.modelio import Modelio
x = Modelio.getInstance().getModelingSession().findByAtt(Class,"Name","MyClass")
Modelio.getInstance().getNavigationService().fireNavigate(x[0])
print "the class MyClass should be selected now (but is not...)"

I've finished to port the "AdvancedSearch" script to V3, but the select in model explorer feature do not work :dry:

Please Log in or Create an account to join the conversation.

8 years 9 months ago #2195 by chm
Hello,

you were right, both navigation listener and fire were broken in 3.0.00. :(

However, they will be working in 3.0.01, which should be out soon.

Sorry for the inconvenience.
The following user(s) said Thank You: andy

Please Log in or Create an account to join the conversation.

Moderators: chmcma
Time to create page: 0.109 seconds
^ Back to Top