Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Event bubbling
Event bubbling [message #457939] Thu, 07 July 2005 21:17 Go to next message
Eclipse UserFriend
Originally posted by: ondrej.kucera.centrum.cz

Hello,

I'm sort of new to SWT (and definitely new to this newsgroup), although
after reading some material, writing a few code samples and going through
almost all the official snippets I think I know the basics. But I can't
figure out something about the event handling and can't find a decent
article or something that would go deeper then simple telling how to add a
listener (typed or untyped) and how to write a simple one.
I'm mostly familiar with the event model of web browsers as stated in DOM
2 Events specification where there's possible either event capturing or
event bubbling - for example if there is a list item (LI) inside an
unordered list (UL) inside the body of the document and user clicks over
the list item, this event can be caught on all of these elements (LI, UL,
BODY), some of them or none of them (bubbling vs. capturing only differs
in the order of the call of the appropriate event handlers).
I desperately need to do something like that in SWT, but I don't know how.
For example I need to catch every MouseDown over o Composite, whether it
is an empty Composite or it is full of Labels. If I try the code below and
I click somewhere in the Label, only the Listener for the Label is called
(and not the ones for Group, Composite and Shell). I thought the the rest
would be called automatically but they're obviously not, so I was
wandering that maybe I should "do something" to the event in the Listener
to pass the event to the widget's parent but just from looking to the
javadoc documentation I couldn't figure out what would do the trick. Or
maybe I'm going completely wrong direction but I really need to solve this.

Regards, Ondřej


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;

public class EventExample {

public static void main (String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Composite composite = new Composite(shell, SWT.BORDER);
Group group = new Group(composite, SWT.NONE);
group.setText("group");
Label label = new Label(group, SWT.BORDER);
label.setText("label");
label.pack();
label.setLocation(10, 100);
group.setSize(100, 200);
group.setLocation(10, 100);
composite.setSize(200, 300);
composite.setLocation(10, 100);
shell.setSize(300, 450);

Listener listener = new Listener() {
public void handleEvent(Event event) {
if (event.type == SWT.MouseDown) {
System.out.println("MouseDown: " + event.widget.toString());
}
}
};

shell.addListener(SWT.MouseDown, listener);
composite.addListener(SWT.MouseDown, listener);
group.addListener(SWT.MouseDown, listener);
label.addListener(SWT.MouseDown, listener);

shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Re: Event bubbling [message #457987 is a reply to message #457939] Fri, 08 July 2005 17:29 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Events are captured in SWT, not bubbled. However, you can use
Display.addFilter() to see every event for every widget.

"Ondrej Kucera" <ondrej.kucera@centrum.cz> wrote in message
news:op.stkar7icvhco6m@daria.lawndale...
> Hello,
>
> I'm sort of new to SWT (and definitely new to this newsgroup), although
> after reading some material, writing a few code samples and going through
> almost all the official snippets I think I know the basics. But I can't
> figure out something about the event handling and can't find a decent
> article or something that would go deeper then simple telling how to add a
> listener (typed or untyped) and how to write a simple one.
> I'm mostly familiar with the event model of web browsers as stated in DOM
> 2 Events specification where there's possible either event capturing or
> event bubbling - for example if there is a list item (LI) inside an
> unordered list (UL) inside the body of the document and user clicks over
> the list item, this event can be caught on all of these elements (LI, UL,
> BODY), some of them or none of them (bubbling vs. capturing only differs
> in the order of the call of the appropriate event handlers).
> I desperately need to do something like that in SWT, but I don't know how.
> For example I need to catch every MouseDown over o Composite, whether it
> is an empty Composite or it is full of Labels. If I try the code below and
> I click somewhere in the Label, only the Listener for the Label is called
> (and not the ones for Group, Composite and Shell). I thought the the rest
> would be called automatically but they're obviously not, so I was
> wandering that maybe I should "do something" to the event in the Listener
> to pass the event to the widget's parent but just from looking to the
> javadoc documentation I couldn't figure out what would do the trick. Or
> maybe I'm going completely wrong direction but I really need to solve
this.
>
> Regards, Ondrej
>
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.*;
> import org.eclipse.swt.widgets.*;
>
> public class EventExample {
>
> public static void main (String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> Composite composite = new Composite(shell, SWT.BORDER);
> Group group = new Group(composite, SWT.NONE);
> group.setText("group");
> Label label = new Label(group, SWT.BORDER);
> label.setText("label");
> label.pack();
> label.setLocation(10, 100);
> group.setSize(100, 200);
> group.setLocation(10, 100);
> composite.setSize(200, 300);
> composite.setLocation(10, 100);
> shell.setSize(300, 450);
>
> Listener listener = new Listener() {
> public void handleEvent(Event event) {
> if (event.type == SWT.MouseDown) {
> System.out.println("MouseDown: " + event.widget.toString());
> }
> }
> };
>
> shell.addListener(SWT.MouseDown, listener);
> composite.addListener(SWT.MouseDown, listener);
> group.addListener(SWT.MouseDown, listener);
> label.addListener(SWT.MouseDown, listener);
>
> shell.open ();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
> }
Re: Event bubbling [message #458091 is a reply to message #457987] Sat, 09 July 2005 18:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ondrej.kucera.centrum.cz

> However, you can use Display.addFilter() to see every event for every
> widget.
Can you be more specific about this? Because either I don't get what it
should do (neither from javadoc, nor from trying) or I don't see how it
could help me with what I'm trying to do.

> Events are captured in SWT, not bubbled.
OK, but in that case I would expect something like Event.passToParent()
method for manual bubbling. Or is there a way create my own click event at
such coordinates of the Group (for example) where the Label is situated? I
tried it right now (calling Display.post() from the Listener in case I
clicked on the Label) and I ended in an indefinite cycle of simulated
clicking on the Label (which makes sense from some point of view).
Is there a way of rewriting my example to do what I'd like it to do?
Because in the real application I will have probably only two simple
Labels on a Composite so I can imagine catching events on those Labels and
calling my own method which will know that the event is from one of these
Labels and I actually need to handle them as if they were caught on their
parent Composite, but I can't really imagine doing something like this in
a case of some more complicated structure of widgets (couple of nested
Composites containing who knows what) - it would feel like writing part of
the whole event model myself - catching the event, recalculating the
coordinates for its parent and passing the information about the event up,
there doing the same, until I found the desired predecessor...

Regards, Ondrej


> Events are captured in SWT, not bubbled. However, you can use
> Display.addFilter() to see every event for every widget.
>
> "Ondrej Kucera" <ondrej.kucera@centrum.cz> wrote in message
> news:op.stkar7icvhco6m@daria.lawndale...
>> Hello,
>>
>> I'm sort of new to SWT (and definitely new to this newsgroup), although
>> after reading some material, writing a few code samples and going
>> through
>> almost all the official snippets I think I know the basics. But I can't
>> figure out something about the event handling and can't find a decent
>> article or something that would go deeper then simple telling how to
>> add a
>> listener (typed or untyped) and how to write a simple one.
>> I'm mostly familiar with the event model of web browsers as stated in
>> DOM
>> 2 Events specification where there's possible either event capturing or
>> event bubbling - for example if there is a list item (LI) inside an
>> unordered list (UL) inside the body of the document and user clicks over
>> the list item, this event can be caught on all of these elements (LI,
>> UL,
>> BODY), some of them or none of them (bubbling vs. capturing only differs
>> in the order of the call of the appropriate event handlers).
>> I desperately need to do something like that in SWT, but I don't know
>> how.
>> For example I need to catch every MouseDown over o Composite, whether it
>> is an empty Composite or it is full of Labels. If I try the code below
>> and
>> I click somewhere in the Label, only the Listener for the Label is
>> called
>> (and not the ones for Group, Composite and Shell). I thought the the
>> rest
>> would be called automatically but they're obviously not, so I was
>> wandering that maybe I should "do something" to the event in the
>> Listener
>> to pass the event to the widget's parent but just from looking to the
>> javadoc documentation I couldn't figure out what would do the trick. Or
>> maybe I'm going completely wrong direction but I really need to solve
> this.
>>
>> Regards, Ondrej
Re: Event bubbling [message #458262 is a reply to message #458091] Tue, 12 July 2005 13:55 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Rather than do all that, what exactly are you trying to do that requires
event bubbling? Can you post some code?

"Ondrej Kucera" <ondrej.kucera@centrum.cz> wrote in message
news:op.stntb5b7vhco6m@daria.lawndale...
> > However, you can use Display.addFilter() to see every event for every
> > widget.
> Can you be more specific about this? Because either I don't get what it
> should do (neither from javadoc, nor from trying) or I don't see how it
> could help me with what I'm trying to do.
>
> > Events are captured in SWT, not bubbled.
> OK, but in that case I would expect something like Event.passToParent()
> method for manual bubbling. Or is there a way create my own click event at
> such coordinates of the Group (for example) where the Label is situated? I
> tried it right now (calling Display.post() from the Listener in case I
> clicked on the Label) and I ended in an indefinite cycle of simulated
> clicking on the Label (which makes sense from some point of view).
> Is there a way of rewriting my example to do what I'd like it to do?
> Because in the real application I will have probably only two simple
> Labels on a Composite so I can imagine catching events on those Labels and
> calling my own method which will know that the event is from one of these
> Labels and I actually need to handle them as if they were caught on their
> parent Composite, but I can't really imagine doing something like this in
> a case of some more complicated structure of widgets (couple of nested
> Composites containing who knows what) - it would feel like writing part of
> the whole event model myself - catching the event, recalculating the
> coordinates for its parent and passing the information about the event up,
> there doing the same, until I found the desired predecessor...
>
> Regards, Ondrej
>
>
> > Events are captured in SWT, not bubbled. However, you can use
> > Display.addFilter() to see every event for every widget.
> >
> > "Ondrej Kucera" <ondrej.kucera@centrum.cz> wrote in message
> > news:op.stkar7icvhco6m@daria.lawndale...
> >> Hello,
> >>
> >> I'm sort of new to SWT (and definitely new to this newsgroup), although
> >> after reading some material, writing a few code samples and going
> >> through
> >> almost all the official snippets I think I know the basics. But I can't
> >> figure out something about the event handling and can't find a decent
> >> article or something that would go deeper then simple telling how to
> >> add a
> >> listener (typed or untyped) and how to write a simple one.
> >> I'm mostly familiar with the event model of web browsers as stated in
> >> DOM
> >> 2 Events specification where there's possible either event capturing or
> >> event bubbling - for example if there is a list item (LI) inside an
> >> unordered list (UL) inside the body of the document and user clicks
over
> >> the list item, this event can be caught on all of these elements (LI,
> >> UL,
> >> BODY), some of them or none of them (bubbling vs. capturing only
differs
> >> in the order of the call of the appropriate event handlers).
> >> I desperately need to do something like that in SWT, but I don't know
> >> how.
> >> For example I need to catch every MouseDown over o Composite, whether
it
> >> is an empty Composite or it is full of Labels. If I try the code below
> >> and
> >> I click somewhere in the Label, only the Listener for the Label is
> >> called
> >> (and not the ones for Group, Composite and Shell). I thought the the
> >> rest
> >> would be called automatically but they're obviously not, so I was
> >> wandering that maybe I should "do something" to the event in the
> >> Listener
> >> to pass the event to the widget's parent but just from looking to the
> >> javadoc documentation I couldn't figure out what would do the trick. Or
> >> maybe I'm going completely wrong direction but I really need to solve
> > this.
> >>
> >> Regards, Ondrej
Re: Event bubbling [message #458430 is a reply to message #458262] Wed, 13 July 2005 15:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ondrej.kucera.centrum.cz

Well, right now I can't post the code but for example I wanted to be
notified wherever mouse is moving over a composite - let's just say that
in such case I wanted to change background of the composite or do some
other visual changes for the user to see (I'm not sure what exactly yet).
Since luckily there are not many children on that particular composite
(and none of them has its own children) I ended up with catching the
MouseMove event on that composite and those couple of children (instead of
only on that composite which was what I was hoping would be enough). So my
problem in this particular case is solved (unless I find out later that I
need much more children on that composite). I was just wondering if the
composite was full of other controls (which would possibly contain other
controls) that it would become much harder to do such kind of trivial task
as to be notified any time a mouse is moving somewhere over the composite.

Regards, Ondrej


> Rather than do all that, what exactly are you trying to do that requires
> event bubbling? Can you post some code?
Re: Event bubbling [message #458518 is a reply to message #458430] Thu, 14 July 2005 13:38 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
In that case, you might want Display.addFilter() to see every mouse move in
every widget on a display.

"Ondrej Kucera" <ondrej.kucera@centrum.cz> wrote in message
news:op.stuxw2pyvhco6m@daria.lawndale...
> Well, right now I can't post the code but for example I wanted to be
> notified wherever mouse is moving over a composite - let's just say that
> in such case I wanted to change background of the composite or do some
> other visual changes for the user to see (I'm not sure what exactly yet).
> Since luckily there are not many children on that particular composite
> (and none of them has its own children) I ended up with catching the
> MouseMove event on that composite and those couple of children (instead of
> only on that composite which was what I was hoping would be enough). So my
> problem in this particular case is solved (unless I find out later that I
> need much more children on that composite). I was just wondering if the
> composite was full of other controls (which would possibly contain other
> controls) that it would become much harder to do such kind of trivial task
> as to be notified any time a mouse is moving somewhere over the composite.
>
> Regards, Ondrej
>
>
> > Rather than do all that, what exactly are you trying to do that requires
> > event bubbling? Can you post some code?
Re: Event bubbling [message #458702 is a reply to message #457939] Tue, 19 July 2005 15:59 Go to previous message
Dave Orme is currently offline Dave OrmeFriend
Messages: 424
Registered: July 2009
Senior Member
Ondřej Kučera wrote:
> I'm mostly familiar with the event model of web browsers as stated in
> DOM 2 Events specification where there's possible either event
> capturing or event bubbling - for example if there is a list item (LI)
> inside an unordered list (UL) inside the body of the document and user
> clicks over the list item, this event can be caught on all of these
> elements (LI, UL, BODY), some of them or none of them (bubbling vs.
> capturing only differs in the order of the call of the appropriate
> event handlers).

Sorry, SWT doesn't do what you're asking for. It's really convenient,
but it's also really expensive in terms of performance.

In addition to adding a filter to a Display like Steve suggested, you
could also have the Composite explicitly register itself as a listener
to each child control on the events you need.

Or if the child control doesn't need to be interactive itself (ie:
labels, if you setEnabled(false) the containing Composite, then the
events for the Composite and its children (the labels) will go to the
Composite's parent.

See:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet46.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

for an example.


Best regards,

Dave Orme
--
Visual Editor Project lead
http://www.db4o.com -- The Open-source Java Object Database
http://xswt.sf.net -- XML-based SWT page description language
Previous Topic:SashForm/Scroll Bars problem
Next Topic:SearchDialog overrides user set layout data
Goto Forum:
  


Current Time: Mon Oct 19 21:40:34 GMT 2020

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

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

Back to the top