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

Bug 322468

Summary: [Combo] setText invokes widgetSelected event unexpectedly.
Product: [Eclipse Project] Platform Reporter: Kejia Ye <christie.ykj>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, eclipse.felipe, mukund, niebomin, raji, Silenio_Quarti, sujithjob
Version: 3.5.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
An example snippet to show the defect.
none
Run code in Comments 7. Fail under Arabic locale, WinXP. none

Description Kejia Ye CLA 2010-08-12 02:46:19 EDT
Build Identifier: M20090211-1700

For Combo, clicking the drop-down arrow will send out a widgetSelected event unexpectedly, only if setText(string) is called previously and the string's contained in the list. If we try to move the focus out of the combo each time its selected, this wired behavior will cause a uncomfortable flash when clicking the arrow. See example code.

Reproducible: Always

Steps to Reproduce:
1.Add items to combo,e.g.combo.add("ZoomChange").
2.Call combo.setText("ZoomChange"), the string should already been added to the combo in step 1.
3.Click the dropdown arrow of the combo.

Defect: widgetSelected() is invoked, and the text("ZoomChange") set in setp 2 is selected.
Comment 1 Kejia Ye CLA 2010-08-12 02:54:07 EDT
Created attachment 176438 [details]
An example snippet to show the defect.
Comment 2 Raji Akella CLA 2010-08-12 12:00:09 EDT
Kejia, what version of SWT are you seeing this issue on and what version are you needing a fix for?
Comment 3 Kejia Ye CLA 2010-08-13 01:22:20 EDT
It's found on SWT 3.4.2.v3452b, and I also need a fix for this version.

> Kejia, what version of SWT are you seeing this issue on and what version are
> you needing a fix for?
Comment 4 Felipe Heidrich CLA 2010-08-26 11:40:51 EDT
The selection event has to be sent. See bug 93918.

The problem also happens when setText(string) is not called. In your example, type "z" and click the dropdown arrow of the combo.

Even if I fix setText(string) you would still have the problem when user types something in the edit are that matches an item in the list.

The "uncomfortable flash when clicking the arrow" is caused by the call to button.setFocus(). Why do you call setFocus() on a different control from the selection handler of the combo ? This seems wrong to me.

The native combo can select an item when the list is shown. This is platform behaviour. I vote to close this bug as WONTFIX.
Comment 5 Kejia Ye CLA 2010-08-31 01:36:45 EDT
I tried to replace combo.setText() with combo.select(), no flash anymore. So I think I can use this method to solve my problem. 

It seems if an item's been added to a combo, calling combo.select() not combo.setText() is the right way to init the edit area, according to the platform behavior.

Besides, to confirm the purpose of focus change, the combo in my application is used to zoom an editor. Each time after the zoom value is changed,I'd like to set the focus back to the editor. The process is:
(1) Add items into a combo, like "10%","20%",....,"Entire Page";
(2) Use combo.setText to "Entire Page" as an initialization;
(3) Click the drop-down arrow to change zoom;
Here, the selection event is triggered, and a flash occurred. This flash only happen once due to the step (2).
Comment 6 Kejia Ye CLA 2010-08-31 07:00:09 EDT
I find another issue of BIDI support if use combo.select as a replacement. 

Try as below:
(1)Use below code to contribute a sampleview to Eclipse;
(2)Start the platform with args: -nl ar. Also set the computer locale to Arabic.
 
See the text in edit area(XX%) is inconsistent with the one in the drop-down list(%XX). The "%" should be kept at the front according to BIDI requirment.

public class SampleView extends ViewPart {
	private Combo combo;
	private Text test;

	public SampleView() {
	}

	public void createPartControl(Composite parent) {
		test = new Text(parent, 0);
		combo = new Combo(parent, SWT.DROP_DOWN);
		String[] array = { "%25", "%55", "%85" };
		combo.setItems(array);

		combo.select(0);

		combo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				test.setFocus();
				test.setText(combo.getItem(combo.getSelectionIndex()));
			}
		});
	}

	public void setFocus() {
		test.setFocus();
	}
}
Comment 7 Felipe Heidrich CLA 2010-08-31 10:32:53 EDT
I don't believe this is the same problem.

Anyway, it works for me here:
public static void main123(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.RIGHT_TO_LEFT);
	shell.setLayout(new GridLayout(1, false));
	final Text test = new Text(shell, 0);
	final Combo combo = new Combo(shell, SWT.DROP_DOWN);
	String[] array = { "%25", "%55", "%85" };
	combo.setItems(array);
	combo.select(0);
	combo.addSelectionListener(new SelectionAdapter() {
	    public void widgetSelected(SelectionEvent e) {
	        test.setFocus();
	        test.setText(combo.getItem(combo.getSelectionIndex()));
	    }
	});
	shell.pack();
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	display.dispose();
}


Does the above fails for you ?
Will it work if you replace the call to select() by setText() ?
Comment 8 Kejia Ye CLA 2010-08-31 23:20:28 EDT
Created attachment 177910 [details]
Run code in Comments 7. Fail under Arabic locale, WinXP.
Comment 9 Kejia Ye CLA 2010-08-31 23:21:28 EDT
Yes, I think this may be a different issue, but it do impact how we initialize the combo. 

Actually, there are two requirements in our application:
(1) Switch focus after selection's changed.
   - this may causes a flash if use setText() to initialize the combo.
   - replace it with select() can fix it.

(2) Also need bidi support.
   - select() fails here, the text shown in edit area is inconsistent with the one in drop_down list.
   - Code in Comment 7 also fails for me. (See screen shot)
   - To reproduce the issue, need to set the locale first. e.g. on windows, Control Panel-> Regional and Language Options->Regional Option->Standards and formats->Arabic.

May I treat this bidi issue as another bug?
Comment 10 Felipe Heidrich CLA 2010-09-02 10:26:03 EDT
(In reply to comment #9)
> May I treat this bidi issue as another bug?
Yes