Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » treeviewer selects first item in tree on open
treeviewer selects first item in tree on open [message #491278] Tue, 13 October 2009 22:40 Go to next message
Natasha D'Silva is currently offline Natasha D'Silva
Messages: 25
Registered: July 2009
Junior Member
I have a View that uses a sashform with a treeviewer/textviewer as its control.

The treeviewer has columns, and I notice that when the view is activated, the first item in the view is selected. If I click anywhere in the treeviewer, even in an empty row, the first item is still selected.
Debugging has shown that just clicking anywhere in the treeviewer generates a selection event. I have looked at other eclipse views with a treeviewer with columns and this does not happen. I am wondering if there is a special style bit I am missing on my tree.

Does it matter when I populate the viewer with model elements by calling setInput for the first time?
Here is the code to create the view:

		fSashForm = new SashForm(parent, SWT.VERTICAL);
		TreeViewer viewer = new TreeViewer(fSashForm, SWT.FULL_SELECTION | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL  | SWT.BORDER );
		//		TreeViewer viewer = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.HIDE_SELECTION | SWT.BORDER);
		viewer.setContentProvider(fContentProvider);
		viewer.setLabelProvider(labelProvider);

		initializeActions(viewer);

		viewer.getTree().setHeaderVisible(true);
		viewer.getTree().setLinesVisible(true);
...setup columns..
		TableLayout layout = new TableLayout();

		ColumnLayoutData c0Data = new ColumnWeightData(20);
		ColumnLayoutData c1Data = new ColumnWeightData(30);
		ColumnLayoutData c2Data = new ColumnWeightData(50);
		layout.addColumnData(c0Data);
		layout.addColumnData(c1Data);
		layout.addColumnData(c2Data);
		viewer.getTree().setLayout(layout);

		viewer.setColumnProperties(COLUMN_PROPERTIES);
		viewer.addSelectionChangedListener(getSelectionChangedListener());
		viewer.addDoubleClickListener(this);

		fDetailText = new TextViewer(fSashForm, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		fDetailText.setEditable(false);
		fDetailText.setDocument(new Document());
		fDetailText.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		fSashForm.setWeights(new int[]{85,15});
		fSashForm.layout(true);


Experimenting with SWT.HIDE_SELECTION did not work, which is expected since its supposed to be for tables only.
Any help/tips would be appreciated,
thanks.
Re: treeviewer selects first item in tree on open [message #491425 is a reply to message #491278] Wed, 14 October 2009 14:00 Go to previous messageGo to next message
saurav sarkar is currently offline saurav sarkar
Messages: 424
Registered: July 2009
Senior Member
have you got any SelectionChangedListener attached to the tree viewer?....it may be the event handler code selects the first element for you.

Re: treeviewer selects first item in tree on open [message #491738 is a reply to message #491425] Thu, 15 October 2009 15:53 Go to previous messageGo to next message
Natasha D'Silva is currently offline Natasha D'Silva
Messages: 25
Registered: July 2009
Junior Member
I do have a selection listener. Disabling it (i.e. not adding it to the viewer) does not make a difference, neither does disabling the double click listener I have.
Re: treeviewer selects first item in tree on open [message #491827 is a reply to message #491738] Fri, 16 October 2009 03:52 Go to previous messageGo to next message
saurav sarkar is currently offline saurav sarkar
Messages: 424
Registered: July 2009
Senior Member
Somewhere your tree is selected.Put a breakpoint on the setselection of the treeviewer and find out where it is selected.

Re: treeviewer selects first item in tree on open [message #491903 is a reply to message #491738] Fri, 16 October 2009 13:51 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2151
Registered: July 2009
Senior Member
Hi,

As an experiment, create your TreeViewer with style SWT.MULTI instead of
SWT.SINGLE. On some platforms (eg.- win32) the first item is natively
selected when a single-select Tree receives focus either by keyboard
traversal or by clicking on its whitespace. Clicking on another item should
not select the first item, so if you're seeing this then this part is
strange. Regardless, try the SWT.MULTI style to at least determine whether
the style is causing some of the behaviour you're describing.

Grant


"Natasha D'Silva" <ndsilva@ca.ibm.com> wrote in message
news:hb7gh6$pol$1@build.eclipse.org...
> I do have a selection listener. Disabling it (i.e. not adding it to the
viewer) does not make a difference, neither does disabling the double click
listener I have.
Re: treeviewer selects first item in tree on open [message #492253 is a reply to message #491278] Mon, 19 October 2009 15:25 Go to previous messageGo to next message
Natasha D'Silva is currently offline Natasha D'Silva
Messages: 25
Registered: July 2009
Junior Member
Hi,
Thanks for the suggestions. I tried using the SWT.MULTI bit instead of single, and that seems to have gotten rid of my problem. The tree's selection wasn't being set explictly using setSelection, and I was having the problem described by Grant. I found a workaround to simulate single selection behavior, so I'll use the MULTI bit flag for now. Are there plans to fix this issue in a newer release? Should I open an eclipse bug?

Thanks again for all your help!
Re: treeviewer selects first item in tree on open [message #492453 is a reply to message #492253] Tue, 20 October 2009 13:42 Go to previous messageGo to next message
Grant Gayed is currently offline Grant Gayed
Messages: 2151
Registered: July 2009
Senior Member
Hi,

This is the native behaviour for single-select Lists/Tables/Trees, so there
isn't a plan to change it. I notice that GTK has the same behaviour. Is it
really a problem in your app if an item is auto-selected the first time your
Tree gets focus?

Grant


"Natasha D'Silva" <ndsilva@ca.ibm.com> wrote in message
news:hbi0d3$pj5$1@build.eclipse.org...
> Hi,
> Thanks for the suggestions. I tried using the SWT.MULTI bit instead of
single, and that seems to have gotten rid of my problem. The tree's
selection wasn't being set explictly using setSelection, and I was having
the problem described by Grant. I found a workaround to simulate single
selection behavior, so I'll use the MULTI bit flag for now. Are there plans
to fix this issue in a newer release? Should I open an eclipse bug?
>
> Thanks again for all your help!
Re: treeviewer selects first item in tree on open [message #492496 is a reply to message #491278] Tue, 20 October 2009 15:49 Go to previous message
Natasha D'Silva is currently offline Natasha D'Silva
Messages: 25
Registered: July 2009
Junior Member
If this is the native behaviour, then I suppose that isn't likely to change. I have been able to work around it, so I have no concerns.
Thanks.
Previous Topic:[TableViewer] how to get the selected column?
Next Topic:Image on jface Dialog
Goto Forum:
  


Current Time: Fri Oct 24 12:18:53 GMT 2014

Powered by FUDForum. Page generated in 0.02245 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software