Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 22594

Summary: Tree does not fire SelectionChanged event
Product: [Eclipse Project] Platform Reporter: Randy Hudson <hudsonr>
Component: SWTAssignee: Mike Wilson <Mike_Wilson>
Status: RESOLVED WORKSFORME QA Contact:
Severity: normal    
Priority: P3    
Version: 2.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows 2000   
Whiteboard:

Description Randy Hudson CLA 2002-08-20 16:22:10 EDT
If I right click on a TreeItem, I will receive a menuAboutToShow() callback 
without any notification that the Selection has changed.  Yet, if I query the 
tree, it's selection did change.  After the menu is done showing, then a 
selection changed event occurs, but it is too late at this point.  the context 
menu may be built based on objects that listen to selection events.
Comment 1 Steve Northover CLA 2002-08-21 11:43:24 EDT
The following code shows that SWT.Selection for Tree comes in before SWT.Show 
for Menu when the right button is pressed:

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;

public class PR_22594 {

public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell (display);
	final Tree tree = new Tree (shell, SWT.BORDER);
	for (int i=0; i<16; i++) {
		TreeItem item = new TreeItem (tree, SWT.NONE);
		item.setText ("Item " + i);
	}
	Menu menu = new Menu (shell, SWT.POP_UP);
	for (int i=0; i<16; i++) {
		MenuItem item = new MenuItem (menu, SWT.NONE);
		item.setText ("Item " + i);
	}
	tree.setMenu (menu);
	Listener listener = new Listener () {
		public void handleEvent (Event event) {
			String name = event.type == 
SWT.Show ? "Show" : "Selection";
			System.out.print (name + ": {");
			TreeItem [] selection = tree.getSelection ();
			for (int i=0; i<selection.length; i++) {
				System.out.print (selection [i] + " ");
			}
			
			System.out.println ("}");
		}
	};
	menu.addListener (SWT.Show, listener);
	tree.addListener (SWT.Selection, listener);
	
	shell.setLayout (new FillLayout ());
	tree.pack ();
	shell.pack ();
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}

}
Comment 2 Randy Hudson CLA 2002-08-21 15:28:45 EDT
Sorry, my recollection was incorrect.  Next time I'll write the testcase.

The situation in which selection used to not occur was on DragStart, not 
menuPopup.  It appears that this has been fixed in 2.0.0.