[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Event bubbling

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 ();
    }
}