Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Programatically Fire a Selection Event
Programatically Fire a Selection Event [message #328232] Mon, 19 May 2008 16:05 Go to next message
Maurice O'Connor is currently offline Maurice O'ConnorFriend
Messages: 86
Registered: July 2009
Member
Hi there,

I have a bunch of ViewParts created, all implementing the
ISelectionListener interface and registered as selectionListeners. I would
like to programatically fire a selection event from one ViewPart to update
the others, based on a value selected in a ListViewer.

From reading around it appears there are two schools of thought on this -

1. Call Widget.notifyListeners() with an Event as a parameter

or

2. Call Display.post() with an Event as a parameter

I've tried both of these, but my ISelectionListeners are not registering
the selection event. Here is the code I'm currently using:

// Trying to fire a selection event
listViewer.getList().setSelection(0);
Event event = new Event();
event.type = SWT.Selection;
event.widget = listViewer.getList();
listViewer.getList().notifyListeners(SWT.Selection, event);

Has anyone got any idea how to get this working correctly?

I understand I can call setSelection on the listViewer itself, but I don't
have the list of objects at hand to create a meaningful selection here. I
just want to ensure the first item is selected, hence the use of getList().

Thanks!

Maurice
Re: Programatically Fire a Selection Event [message #328238 is a reply to message #328232] Mon, 19 May 2008 17:22 Go to previous messageGo to next message
Maurice O'Connor is currently offline Maurice O'ConnorFriend
Messages: 86
Registered: July 2009
Member
Apologies for reposting, but somebody else had exactly this problem here:

http://dev.eclipse.org/newslists/news.eclipse.platform.rcp/m sg05204.html

Any ideas would be greatly appreciated!

Thanks

Maurice
Re: Programatically Fire a Selection Event [message #328240 is a reply to message #328232] Mon, 19 May 2008 18:02 Go to previous messageGo to next message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
Tell the view's site to setSelectionProvider to your ListViewer.
Something like:

getSite().setSelectionProvider(myListViewer);

You follow up post cites an message that suggests this already. In what
way is setSelectionProvider *not* doing what you need?

You might also consider setting the selection provider to another
implementation of ISelectionProvider. Say, a custom one that suits your
purposes...

Wayne

On Mon, 2008-05-19 at 16:05 +0000, Maurice O\'Connor wrote:
> Hi there,
>
> I have a bunch of ViewParts created, all implementing the
> ISelectionListener interface and registered as selectionListeners. I would
> like to programatically fire a selection event from one ViewPart to update
> the others, based on a value selected in a ListViewer.
>
> From reading around it appears there are two schools of thought on this -
>
> 1. Call Widget.notifyListeners() with an Event as a parameter
>
> or
>
> 2. Call Display.post() with an Event as a parameter
>
> I've tried both of these, but my ISelectionListeners are not registering
> the selection event. Here is the code I'm currently using:
>
> // Trying to fire a selection event
> listViewer.getList().setSelection(0);
> Event event = new Event();
> event.type = SWT.Selection;
> event.widget = listViewer.getList();
> listViewer.getList().notifyListeners(SWT.Selection, event);
>
> Has anyone got any idea how to get this working correctly?
>
> I understand I can call setSelection on the listViewer itself, but I don't
> have the list of objects at hand to create a meaningful selection here. I
> just want to ensure the first item is selected, hence the use of getList().
>
> Thanks!
>
> Maurice
>
Re: Programatically Fire a Selection Event [message #328252 is a reply to message #328240] Tue, 20 May 2008 09:29 Go to previous messageGo to next message
Maurice O'Connor is currently offline Maurice O'ConnorFriend
Messages: 86
Registered: July 2009
Member
Wayne Beaton wrote:
> Tell the view's site to setSelectionProvider to your ListViewer.
> Something like:
>
> getSite().setSelectionProvider(myListViewer);
>
> You follow up post cites an message that suggests this already. In what
> way is setSelectionProvider *not* doing what you need?
>
> You might also consider setting the selection provider to another
> implementation of ISelectionProvider. Say, a custom one that suits your
> purposes...


Heya, thanks for the help!

setSelectionProvider works for me. I add the listViewer as a provider,
then the user clicks the items in the list to update other ViewParts, each
of those implementing ISelectionListener and registered using the
Selection Service. eg.

In one ViewPart:

getSite().setSelectionProvider(listViewer);

In the others:

getSite().getPage().addSelectionListener(new
ListISelectionListener());

The ISelectionListeners update correctly except when an item is selected
on the list programmatically. Eg.

listViewer.getList().setSelection(0);

In that case I need to fire my own event to trigger the selectionChanged
method in the ISelectionListeners - my problem is figuring out how to do
this.

Maurice
Re: Programatically Fire a Selection Event [message #328256 is a reply to message #328252] Tue, 20 May 2008 12:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Maurice,

I think you'd have to create your own SelectionEvent and call
notifyListeners with it.


Maurice O'Connor wrote:
> Wayne Beaton wrote:
>> Tell the view's site to setSelectionProvider to your ListViewer.
>> Something like:
>>
>> getSite().setSelectionProvider(myListViewer);
>>
>> You follow up post cites an message that suggests this already. In what
>> way is setSelectionProvider *not* doing what you need?
>>
>> You might also consider setting the selection provider to another
>> implementation of ISelectionProvider. Say, a custom one that suits your
>> purposes...
>
>
> Heya, thanks for the help!
>
> setSelectionProvider works for me. I add the listViewer as a provider,
> then the user clicks the items in the list to update other ViewParts,
> each of those implementing ISelectionListener and registered using the
> Selection Service. eg.
>
> In one ViewPart:
>
> getSite().setSelectionProvider(listViewer);
>
> In the others:
>
> getSite().getPage().addSelectionListener(new
> ListISelectionListener());
>
> The ISelectionListeners update correctly except when an item is
> selected on the list programmatically. Eg.
>
> listViewer.getList().setSelection(0);
>
> In that case I need to fire my own event to trigger the
> selectionChanged method in the ISelectionListeners - my problem is
> figuring out how to do this.
>
> Maurice
>
Re: Programatically Fire a Selection Event [message #328268 is a reply to message #328252] Tue, 20 May 2008 14:40 Go to previous messageGo to next message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
What are you really trying to do?

Do listeners get properly notified when you go through the ListViewer's
API:

listViewer.setSelection(StructuredSelection.EMPTY);

Wayne

On Tue, 2008-05-20 at 09:29 +0000, Maurice O\'Connor wrote:
> Wayne Beaton wrote:
> > Tell the view's site to setSelectionProvider to your ListViewer.
> > Something like:
> >
> > getSite().setSelectionProvider(myListViewer);
> >
> > You follow up post cites an message that suggests this already. In what
> > way is setSelectionProvider *not* doing what you need?
> >
> > You might also consider setting the selection provider to another
> > implementation of ISelectionProvider. Say, a custom one that suits your
> > purposes...
>
>
> Heya, thanks for the help!
>
> setSelectionProvider works for me. I add the listViewer as a provider,
> then the user clicks the items in the list to update other ViewParts, each
> of those implementing ISelectionListener and registered using the
> Selection Service. eg.
>
> In one ViewPart:
>
> getSite().setSelectionProvider(listViewer);
>
> In the others:
>
> getSite().getPage().addSelectionListener(new
> ListISelectionListener());
>
> The ISelectionListeners update correctly except when an item is selected
> on the list programmatically. Eg.
>
> listViewer.getList().setSelection(0);
>
> In that case I need to fire my own event to trigger the selectionChanged
> method in the ISelectionListeners - my problem is figuring out how to do
> this.
>
> Maurice
>
Re: Programatically Fire a Selection Event [message #328280 is a reply to message #328268] Tue, 20 May 2008 16:34 Go to previous messageGo to next message
Maurice O'Connor is currently offline Maurice O'ConnorFriend
Messages: 86
Registered: July 2009
Member
Wayne Beaton wrote:
> What are you really trying to do?
>
> Do listeners get properly notified when you go through the ListViewer's
> API:
>
> listViewer.setSelection(StructuredSelection.EMPTY);
>
> Wayne

This will notify ISelectionChangedListeners of the above change. However,
my listeners implement ISelectionListener. I need to notify the
ISelectionListeners by firing a new selection event through the workbench
selection service.

The listViewer is already registered as a SelectionProvider:

getSite().setSelectionProvider(listViewer);

Now I must create my own Event and notify its listeners:

listViewer.getList().notifyListeners(SWT.Selection, new Event());

That doesn't work though. There's a nice tutorial here:

http://www.eclipse.org/articles/Article-WorkbenchSelections/ article.html

It says:

"Each workbench window has its own selection service instance. The service
keeps track of the selection in the currently active part and propagates
selection changes to all registered listeners. Such selection events occur
when the selection in the current part is changed or when a different part
is activated. Both can be triggered by user interaction or
programmatically."

I'm trying to work out how to do this programmatically, with little
success so far ;)

Maurice
Re: Programatically Fire a Selection Event [message #328283 is a reply to message #328280] Tue, 20 May 2008 16:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Maurice,

I think maybe it needs to be a SelectionEvent. You should also have to
look closely at all the fields that are normally populated and populate
them properly.

Wayne,

Programmatically changing the selection of a widget (or anything else
you do with it) doesn't fire the same events as when the user does that
same thing. This is by design.


Maurice O'Connor wrote:
> Wayne Beaton wrote:
>> What are you really trying to do?
>>
>> Do listeners get properly notified when you go through the ListViewer's
>> API:
>>
>> listViewer.setSelection(StructuredSelection.EMPTY);
>>
>> Wayne
>
> This will notify ISelectionChangedListeners of the above change.
> However, my listeners implement ISelectionListener. I need to notify
> the ISelectionListeners by firing a new selection event through the
> workbench selection service.
>
> The listViewer is already registered as a SelectionProvider:
>
> getSite().setSelectionProvider(listViewer);
>
> Now I must create my own Event and notify its listeners:
>
> listViewer.getList().notifyListeners(SWT.Selection, new Event());
>
> That doesn't work though. There's a nice tutorial here:
>
> http://www.eclipse.org/articles/Article-WorkbenchSelections/ article.html
>
> It says:
> "Each workbench window has its own selection service instance. The
> service keeps track of the selection in the currently active part and
> propagates selection changes to all registered listeners. Such
> selection events occur when the selection in the current part is
> changed or when a different part is activated. Both can be triggered
> by user interaction or programmatically."
>
> I'm trying to work out how to do this programmatically, with little
> success so far ;)
>
> Maurice
>
Re: Programatically Fire a Selection Event [message #328319 is a reply to message #328283] Wed, 21 May 2008 13:42 Go to previous messageGo to next message
Maurice O'Connor is currently offline Maurice O'ConnorFriend
Messages: 86
Registered: July 2009
Member
Ed Merks wrote:

> I think maybe it needs to be a SelectionEvent. You should also have to
> look closely at all the fields that are normally populated and populate
> them properly.

notifyListeners(Event event) & getDisplay().post(Event event)

These take only a plain Event as a parameter. So I tried both of these. A
comparison of both a generated event and one of my own creation is below:

Event {type=13 List {} time=32740484 data=null x=0 y=0 width=0 height=0
detail=0}
Event {type=13 List {} time=29613453 data=null x=0 y=0 width=0 height=0
detail=0}

Still not getting through to the SelectionListeners with this. Perhaps I
have registered the listeners incorrectly?

getSite().getPage().addSelectionListener(new ChangeListener(listViewer));

The listViewer is registered in the ViewPart like so:

getSite().setSelectionProvider(listViewer);

Maurice
Maurice
Re: Programatically Fire a Selection Event [message #328326 is a reply to message #328319] Wed, 21 May 2008 16:00 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Maurice,

I've never done it so tracing through the calls in each case with the
debugger would be my approach...


Maurice O'Connor wrote:
> Ed Merks wrote:
>
>> I think maybe it needs to be a SelectionEvent. You should also have
>> to look closely at all the fields that are normally populated and
>> populate them properly.
>
> notifyListeners(Event event) & getDisplay().post(Event event)
>
> These take only a plain Event as a parameter. So I tried both of
> these. A comparison of both a generated event and one of my own
> creation is below:
>
> Event {type=13 List {} time=32740484 data=null x=0 y=0 width=0
> height=0 detail=0}
> Event {type=13 List {} time=29613453 data=null x=0 y=0 width=0
> height=0 detail=0}
>
> Still not getting through to the SelectionListeners with this. Perhaps
> I have registered the listeners incorrectly?
>
> getSite().getPage().addSelectionListener(new ChangeListener(listViewer));
>
> The listViewer is registered in the ViewPart like so:
>
> getSite().setSelectionProvider(listViewer);
>
> Maurice
> Maurice
>
Re: Programatically Fire a Selection Event [message #328919 is a reply to message #328252] Fri, 06 June 2008 17:41 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Don't get the SWT list ... just call setSelection(*) on your ListViewer

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Previous Topic:Extrange behaviours in Ganymedes M7 workbench
Next Topic:How to listen to ActiveEditor changes and is IEditorActionDelegate deprecated?
Goto Forum:
  


Current Time: Thu Jul 18 03:39:10 GMT 2024

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

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

Back to the top