Bug 213628 - CCombo getSelectionIndex always returns -1
Summary: CCombo getSelectionIndex always returns -1
Status: RESOLVED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Duong Nguyen CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 172793
  Show dependency tree
 
Reported: 2007-12-20 14:43 EST by Curtis Windatt CLA
Modified: 2008-01-04 10:50 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Curtis Windatt CLA 2007-12-20 14:43:35 EST
I20071213-0010

I am modifying the PDE product editor (bug 213542) and am having trouble getting the combo boxes to work correctly.  My combo boxes are ComboParts a wrapper around a CCombo.

Originally items were added to the combo using setItems(String[]) and the selection was checked using getSelection().  After my changes, I was adding items using add(String) and checking the selection using getSelectionIndex().

getSelectionIndex is always returning -1.  If I select different things in the combo, getSelection() will return the proper String, but the selection index does not change.

I compared against similar code in Debug and the same steps work fine if a Combo is used instead.  I'm guessing that this has something to do with the text/list combination where I'm getting the text selection index.  However, the javadoc says that it will return the selection from the List.  

Debugging the editor I saw that add(String) appeared to work correctly as the item count in the List inside the CCombo is updated.  However, the list.getSelectionIndex() call inside of CCombo always returns -1.
Comment 1 Curtis Windatt CLA 2007-12-20 14:45:19 EST
Sorry, too many bugs open, this is causing me problems on bug 172793.
Comment 2 Grant Gayed CLA 2008-01-02 17:04:12 EST
The steps you describe work in the snippet below, so there must be more context required to reproduce the problem.  Are you able to modify the snippet below or provide a new one from scratch that shows the problem happening?

public static void main(String[] args) {
	final Display display = new Display();
	final Shell shell = new Shell(display);
	shell.setBounds(10,10,200,200);
	final CCombo combo = new CCombo(shell, SWT.NONE);
	combo.setBounds(10,10,100,30);
	combo.add("item 0");
	combo.add("item 1");
	combo.add("item 2");
	combo.add("item 3");
	combo.add("item 4");
	combo.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			System.out.println("index: " + combo.getSelectionIndex());
		}
	});
	shell.open();
	while(!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose();
}
Comment 3 Curtis Windatt CLA 2008-01-02 17:11:45 EST
Snippet works for me as well, so there must be something else affecting it.  I will investigate further.
Comment 4 Curtis Windatt CLA 2008-01-02 17:36:52 EST
The difference causing the problem is that the Product Editor I was modifying uses modify listeners instead of selection listeners.  With a selection listener the index is correct, but when in a modify listener, the index is always -1.

 combo.addModifyListener(new ModifyListener() {
        	public void modifyText(ModifyEvent e) {
        		 System.out.println("index: " + combo.getSelectionIndex());
        	}
        });

I'm not sure why modify listeners are being added, as the combos are set to read only.  Hopefully this means fixing my problem is as simple as changing the listeners.

This bug can probably be closed.  It wasn't immediately obvious to me that the modification listener would always change the selection index to -1 (even if I hadn't changed the text).  However, after figuring it out, it sounds like reasonable behaviour.
Comment 5 Curtis Windatt CLA 2008-01-04 10:50:27 EST
I get the correct behaviour when using SelectionListener, marking as WORKSFORME.