Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » SelectionChangedListener never gets called
SelectionChangedListener never gets called [message #268238] Mon, 23 August 2004 11:23 Go to next message
Eclipse UserFriend
Originally posted by: mvsteenbergen.eljakim.scratch-this.nl

In my TextEditor's constructor (Eclipse 3.0 plugin), I do the following:

getSelectionProvider().addSelectionChangedListener(
new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
System.out.println("selectionChanged");
}
}
);

Since getSelectionProvider().setSelection() changes the selection of the
text and the position of the caret, I expect my selectionChanged() to be
called whenever the text selection or caret position in my text editor
changes, but it doesn't. Not when I type, not when I use my mouse to
change the selection, and not even when I programmatically call
getSelectionProvider().setSelection() myself. What am I doing wrong? Are
my expectations wrong? Is there another way to listen for caret position
changes?

What I'd like to achieve eventually is that my outline view (which
displays a tree based on my text editor's text) follows the caret,
selecting the appropriate (most specific) tree node, just like in the
Java editor. The code for determining and selecting the appropriate node
I already have, and at the moment only gets called whenever I reparse my
text.

Thank you very much in advance,

Martijn van Steenbergen.
Re: SelectionChangedListener never gets called [message #268367 is a reply to message #268238] Tue, 24 August 2004 04:04 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daniel.megert.gmx.net

Martijn van Steenbergen wrote:

> In my TextEditor's constructor (Eclipse 3.0 plugin), I do the following:
>
> getSelectionProvider().addSelectionChangedListener(
> new ISelectionChangedListener() {
> public void selectionChanged(SelectionChangedEvent event) {
> System.out.println("selectionChanged");
> }
> }
> );
>
> Since getSelectionProvider().setSelection() changes the selection of
> the text and the position of the caret, I expect my selectionChanged()
> to be called whenever the text selection or caret position in my text
> editor changes, but it doesn't. Not when I type, not when I use my
> mouse to change the selection, and not even when I programmatically
> call getSelectionProvider().setSelection() myself. What am I doing
> wrong? Are my expectations wrong?

Yes. The selection changed event is only sent if the selection changes
but not if the selection remains empty (e.g. while you type or simply
set the caret).

> Is there another way to listen for caret position changes?

Yes, use a post-selection listener. Note: this will not be triggered
immediately i.e. while typing but after some timeout when the user
stopped typing.

HTH
Dani

>
> What I'd like to achieve eventually is that my outline view (which
> displays a tree based on my text editor's text) follows the caret,
> selecting the appropriate (most specific) tree node, just like in the
> Java editor. The code for determining and selecting the appropriate
> node I already have, and at the moment only gets called whenever I
> reparse my text.
>
> Thank you very much in advance,
>
> Martijn van Steenbergen.
Re: SelectionChangedListener never gets called [message #268458 is a reply to message #268367] Tue, 24 August 2004 13:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mvsteenbergen.eljakim.scratch-this.nl

I now have this in my TextEditor's constructor:

ISelectionProvider sp = getSelectionProvider();
if (sp instanceof IPostSelectionProvider) {
IPostSelectionProvider psp = (IPostSelectionProvider) sp;
psp
.addPostSelectionChangedListener(new ISelectionChangedListener() {
{
System.out
.println("Post selection listener created.");
}

public void selectionChanged(SelectionChangedEvent e) {
System.out.println("Post event!");
}
});
}

I do see the "Post selection listener created." when I launch my plugin,
but I never see the "Post event!". I've tried both moving the cursor and
making text selections using both mouse and keyboard, waiting several
seconds each time for something to happen, but to no avail. What am I
doing wrong? Do I perhaps need to set the timeout myself?

Thank you for your time,

Martijn van Steenbergen.


Daniel Megert wrote:
>> Are my expectations wrong?
>
>
> Yes. The selection changed event is only sent if the selection changes
> but not if the selection remains empty (e.g. while you type or simply
> set the caret).
>
>> Is there another way to listen for caret position changes?
>
>
> Yes, use a post-selection listener. Note: this will not be triggered
> immediately i.e. while typing but after some timeout when the user
> stopped typing.
Re: SelectionChangedListener never gets called [message #268602 is a reply to message #268458] Wed, 25 August 2004 06:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daniel.megert.gmx.net

Martijn van Steenbergen wrote:

> I now have this in my TextEditor's constructor:
>
> ISelectionProvider sp = getSelectionProvider();
> if (sp instanceof IPostSelectionProvider) {
> IPostSelectionProvider psp = (IPostSelectionProvider) sp;
> psp
> .addPostSelectionChangedListener(new ISelectionChangedListener() {
> {
> System.out
> .println("Post selection listener created.");
> }
>
> public void selectionChanged(SelectionChangedEvent e) {
> System.out.println("Post event!");
> }
> });
> }
>
> I do see the "Post selection listener created." when I launch my
> plugin, but I never see the "Post event!". I've tried both moving the
> cursor and making text selections using both mouse and keyboard,
> waiting several seconds each time for something to happen, but to no
> avail. What am I doing wrong? Do I perhaps need to set the timeout
> myself?

No. Debug the code: check which instance is the post-selection provider
then debug that instance to see whether it sends out post-selection events.

Dani

>
> Thank you for your time,
>
> Martijn van Steenbergen.
>
>
> Daniel Megert wrote:
>
>>> Are my expectations wrong?
>>
>>
>>
>> Yes. The selection changed event is only sent if the selection
>> changes but not if the selection remains empty (e.g. while you type
>> or simply set the caret).
>>
>>> Is there another way to listen for caret position changes?
>>
>>
>>
>> Yes, use a post-selection listener. Note: this will not be triggered
>> immediately i.e. while typing but after some timeout when the user
>> stopped typing.
>
Re: SelectionChangedListener never gets called [message #268615 is a reply to message #268458] Wed, 25 August 2004 06:45 Go to previous messageGo to next message
Eclipse UserFriend
Martijn van Steenbergen wrote:
> I now have this in my TextEditor's constructor:
>
> ISelectionProvider sp = getSelectionProvider();
> if (sp instanceof IPostSelectionProvider) {
> IPostSelectionProvider psp = (IPostSelectionProvider) sp;
> psp
> .addPostSelectionChangedListener(new ISelectionChangedListener() {
> {
> System.out
> .println("Post selection listener created.");
> }
>
> public void selectionChanged(SelectionChangedEvent e) {
> System.out.println("Post event!");
> }
> });
> }
>
> I do see the "Post selection listener created." when I launch my plugin,
> but I never see the "Post event!". I've tried both moving the cursor and
> making text selections using both mouse and keyboard, waiting several
> seconds each time for something to happen, but to no avail. What am I
> doing wrong? Do I perhaps need to set the timeout myself?

I think you can't register the listener in the editor's constructor, since
the source viewer is not yet created at that time and the
AbstractTextEditor's SelectionProvider throws listeners away when the source
viewer is not there.

Try registering you listener later (e.g. in createPartControl(), after
super.createPartControl()), or file a PR if you don't like this restriction.

Markus
Re: SelectionChangedListener never gets called [message #268657 is a reply to message #268615] Wed, 25 August 2004 09:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: daniel.megert.gmx.net

Markus Keller wrote:

> Martijn van Steenbergen wrote:
>
>> I now have this in my TextEditor's constructor:
>>
>> ISelectionProvider sp = getSelectionProvider();
>> if (sp instanceof IPostSelectionProvider) {
>> IPostSelectionProvider psp = (IPostSelectionProvider) sp;
>> psp
>> .addPostSelectionChangedListener(new ISelectionChangedListener() {
>> {
>> System.out
>> .println("Post selection listener created.");
>> }
>> public void selectionChanged(SelectionChangedEvent e) {
>> System.out.println("Post event!");
>> }
>> });
>> }
>>
>> I do see the "Post selection listener created." when I launch my
>> plugin, but I never see the "Post event!". I've tried both moving the
>> cursor and making text selections using both mouse and keyboard,
>> waiting several seconds each time for something to happen, but to no
>> avail. What am I doing wrong? Do I perhaps need to set the timeout
>> myself?
>
>
> I think you can't register the listener in the editor's constructor,
> since the source viewer is not yet created at that time and the
> AbstractTextEditor's SelectionProvider throws listeners away when the
> source viewer is not there.

See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=72587

Dani

>
> Try registering you listener later (e.g. in createPartControl(), after
> super.createPartControl()), or file a PR if you don't like this
> restriction.
>
> Markus
Re: SelectionChangedListener never gets called [message #268663 is a reply to message #268615] Wed, 25 August 2004 09:44 Go to previous message
Eclipse UserFriend
Originally posted by: mvsteenbergen.eljakim.scratch-this.nl

Yes! Adding the listener in createPartControl() works fine. Thank you
both very much. :)

Martijn.

P.S. Dani, I got the code folding working too, thanks to your hints and
a cached Google page at:
http://66.102.11.104/search?q=cache:PPLVowtm4PQJ:oss.linux-o nline.ru/eclipse/downloads/drops/S-3.0RC2-200406111814/build notes/buildnotes_text.html+eclipse+projection+viewer&hl= en


Markus Keller wrote:
> I think you can't register the listener in the editor's constructor,
> since the source viewer is not yet created at that time and the
> AbstractTextEditor's SelectionProvider throws listeners away when the
> source viewer is not there.
>
> Try registering you listener later (e.g. in createPartControl(), after
> super.createPartControl()), or file a PR if you don't like this
> restriction.
>
> Markus
Previous Topic:Crash on big binary file
Next Topic:Building a jar file with all dependencies
Goto Forum:
  


Current Time: Tue Jul 15 16:36:01 EDT 2025

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

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

Back to the top