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

Bug 131696

Summary: [Widgets] ExpandBar 'expanded' field negated AFTER firing ExpandEvent
Product: [Eclipse Project] Platform Reporter: Graham Briggs <graham>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WONTFIX QA Contact: Felipe Heidrich <eclipse.felipe>
Severity: normal    
Priority: P3 CC: mccarl, Michael.Valenta, veronika_irvine
Version: 3.2Keywords: triaged
Target Milestone: ---   
Hardware: Power PC   
OS: Windows All   
Whiteboard: stalebug

Description Graham Briggs CLA 2006-03-14 08:31:46 EST
In methods:

LRESULT WM_KEYDOWN (...) {...}
LRESULT WM_LBUTTONUP (...) {...}

the ordering in the win32 codebase is:

  sendEvent (item.expanded ? SWT.Collapse : SWT.Expand, event);
  item.expanded = !item.expanded;

this means that in your event handler code, if you try to get the status of the ExpandItem, or try to do resizing of components containing the ExpandBar, you will get an inconsistent result compared to other platforms.

The ordering of the commands should be:

  item.expanded = !item.expanded;
  sendEvent (item.expanded ? SWT.Collapse : SWT.Expand, event);

so that you get consistent results with other platforms, and what is naturally expected when you've received an ExpandEvent (namely that if it is an SWT.Expand event, then the relevant ExpandItem should already have expanded set to true).
Comment 1 Felipe Heidrich CLA 2006-03-14 12:21:50 EST
Veronika has fixed this for me.

Note: during the collapse the item has expand==true, and during the expand callback expand==false. This consistency with Tree widget.
Comment 2 Veronika Irvine CLA 2006-03-14 13:06:12 EST
All platforms send the Expand and Collapse events before the state change occurs.  This is consistent with the way the OS messages are sent and the way that the Tree Expand/Collapse Events behave.

The difference between Tree and ExpandBar is that in Tree, if you change the state during the event, the state change is respected (i.e. if in the Expand event you all TreeItem.setExpanded(true) the item remains expanded).  On ExpandBar the state change is flippped (i.e. if in the Expand event you call ExpandItem.setExpanded(true) then the item will not be expanded when the event is finished)

See this example:

public static void main (String [] args) {
	Display display = new Display ();
	Shell shell = new Shell (display);
	shell.setLayout(new FillLayout());
	Tree tree = new Tree(shell, SWT.BORDER);
	for (int i = 0; i < 10; i++) {
		TreeItem item = new TreeItem(tree, SWT.NONE);
		item.setText("item "+i);
		for (int j = 0; j < 10; j++) {
			TreeItem subItem = new TreeItem(item, SWT.NONE);
			subItem.setText("item "+j);
			for (int k = 0; k < 10; k++) {
				TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
				subsubItem.setText("item "+k);
			}
		}
	}
	tree.addListener(SWT.Expand, new Listener() {
		public void handleEvent(Event e) {
			TreeItem item = (TreeItem)e.item;
			System.out.println("Expand Tree "+item+" "+item.getExpanded());
			item.setExpanded(true);
			System.out.println("Expand Tree 2 "+item+" "+item.getExpanded());
		}
	});
	tree.addListener(SWT.Collapse, new Listener() {
		public void handleEvent(Event e) {
			TreeItem item = (TreeItem)e.item;
			System.out.println("Collapse Tree "+item+" "+item.getExpanded());
			item.setExpanded(false);
			System.out.println("Collapse Tree 2 "+item+" "+item.getExpanded());
		}
	});
	ExpandBar bar = new ExpandBar(shell, SWT.NONE);
	ExpandItem item = new ExpandItem(bar, SWT.NONE);
	item.setText("Item 1");
	item = new ExpandItem(bar, SWT.NONE);
	item.setText("Item 2");
	bar.addListener(SWT.Expand, new Listener() {
		public void handleEvent(Event e) {
			ExpandItem item = (ExpandItem)e.item;
			System.out.println("Expand ExpandBar "+item+" "+item.getExpanded());
			item.setExpanded(true);
			System.out.println("Expand ExpandBar 2 "+item+" "+item.getExpanded());
		}
	});
	bar.addListener(SWT.Collapse, new Listener() {
		public void handleEvent(Event e) {
			ExpandItem item = (ExpandItem)e.item;
			System.out.println("Collapse ExpandBar "+item+" "+item.getExpanded());
			item.setExpanded(false);
			System.out.println("Collapse ExpandBar 2 "+item+" "+item.getExpanded());
		}
	});
	shell.open ();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	display.dispose ();
}
Comment 3 Felipe Heidrich CLA 2006-03-16 10:57:05 EST
*** Bug 132021 has been marked as a duplicate of this bug. ***
Comment 4 Clayton McCarl CLA 2009-01-08 12:03:02 EST
SelectionEvent.doit does not seem to work for ExpandEvent
http://help.eclipse.org/stable/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/events/class-use/ExpandEvent.html

I use the workaround described in Comment #2, but sometimes I get a screen flicker while the state is temporarily set to collapsed.

bar.addExpandListener(new ExpandListener(){
	@Override
	public void itemCollapsed(ExpandEvent e) {
		if(boolExpandItemsLockedOpen){
			//I would prefer this, but it doesn't work
			e.doit=false;

			//This is what I use so the state is toggled back to open
			((ExpandItem)e.item).setExpanded(false);
		}
	}
}
Comment 5 Felipe Heidrich CLA 2009-08-20 09:40:20 EDT
Your bug has been moved to triage, visit http://www.eclipse.org/swt/triage.php for more info.
Comment 6 Leo Ufimtsev CLA 2017-08-03 12:26:58 EDT
This is a one-off bulk update. (The last one in the triage migration).

Moving bugs from swt-triaged@eclipse to platform-swt-inbox@eclipse.org and adding "triaged" keyword as per new triage process:
https://wiki.eclipse.org/SWT/Devel/Triage

See Bug 518478 for details.

Tag for notification/mail filters:
@TriageBulkUpdate
Comment 7 Eclipse Genie CLA 2020-07-09 17:41:27 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.