Good practices with selection listeners [message #520829] |
Mon, 15 March 2010 14:05 |
Olivier Thierry Messages: 11 Registered: January 2010 Location: Nantes, France |
Junior Member |
|
|
Hi,
I have a view part containing a tree. When the user selects a node in the tree, I want to display details for this node in three other views, depending on the node type (imagine a tree view with continent, country and cities). These three views are in a folder layout, and I use showView method on the page to send the right view to the front.
I could make it work by setting the view with the tree as a selection provider, then setting each of my three details views as a selection listener for the tree. I did this in the createPartControl method of the details views. It looks like this :
public void createPartControl(Composite parent) {
PlanTransportView planTransportView = (PlanTransportView) getViewSite().getPage().findView(PlanTransportView.ID);
if (planTransportView != null) {
planTransportView.addSelectionListener(this);
}
}
But I can see two problems here :
- The createPartControl method is executed only when the view is sent to front. But when it is added to a folder, the listeners for the hidden details views are not added until they are sent to front. So selection of corresponding elements in the tree has no effect until the details view is sent to front by the user, which I don't want to do
- Imagine the selection provider (PlanTransportView here) doesn't exist when the listener is created (it has been closed in previous session for example). Since createPartControl method is called once, I see no way how the listener could be added
So I ask myself about the good practices about where to add listeners :
- Is createPartControl method really the right place to do this ?
- Should the listeners be added outside the concerned views, in a application class ?
- Maybe there's a way to force createPartControl method to be called on app startup ?
I have plenty of such problems, when users close views, and open them again, so any help will be very welcome !
Thanks in advance,
Olivier
|
|
|
|
|
|
Re: Good practices with selection listeners [message #521045 is a reply to message #521025] |
Tue, 16 March 2010 10:25 |
|
I dont know why you want to register the view before it is acutally shown. This turns off the lazy loading principle.
I think your problem is, that when you open the view, it shows nothing, because the selectionListener has just registered, and so it is not informed about the actual selection?!?
A proper solution would be to override the partOpened() method and get the actual selection there.
Or do you have another problem?
Greetz
Thomas
|
|
|
Re: Good practices with selection listeners [message #521049 is a reply to message #521045] |
Tue, 16 March 2010 10:37 |
Olivier Thierry Messages: 11 Registered: January 2010 Location: Nantes, France |
Junior Member |
|
|
T. Wilhelm wrote on Tue, 16 March 2010 06:25 | I dont know why you want to register the view before it is acutally shown. This turns off the lazy loading principle.
|
Well, imagine you have a treeview with three kind of nodes : continent, country and city. Then you have three details views for each kind of node : continentDetails, countryDetails and cityDetails. All of these details views are in a folder, so only one is visible, the others are hidden. If you select a continent, I want to fill continentDetails view and show it. If you select a country, I want to fill countryDetails view and show it. And if you select a city I want to fill cityDetails view and show it.
This is done in selectionChanged method of the views with the following piece of code :
getSite().getPage().showView(ID, null, IWorkbenchPage.VIEW_VISIBLE);
But for this piece of code to run, I of course need listener to be started. These listeners are configured in createPartControl method, but this method is not called when the view is added to the workbench window but when it is shown. So if the user doesn't click on the view, it is not shown and the listener is not started. So I need to force call for createPartControl. Hope you understand what I mean
[Updated on: Tue, 16 March 2010 10:40] Report message to a moderator
|
|
|
Re: Good practices with selection listeners [message #521059 is a reply to message #521049] |
Tue, 16 March 2010 11:12 |
|
Here is a code example for my advice before: Just override the partOpenend() Method, so the fist time you click on the part, it will get the actual selection and then you can show the details.
Afterwards your listener is registered and informs about changes.
@Override
public void partOpened( IWorkbenchPart part) {
ISelection selection = getSite().getWorkbenchWindow()
.getSelectionService()
.getSelection();
Country country = (Country) selection;
}
I think that is the better way then forcing the listener to be activated. If you still want to go that way, use an activator to register the listener.
Greetz
Thomas
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04431 seconds