Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Updating selection when removing lines from table
Updating selection when removing lines from table [message #810323] Wed, 29 February 2012 19:53 Go to next message
John Lussmyer is currently offline John Lussmyer
Messages: 96
Registered: December 2010
Member
When my code has to delete something from the table, it usually is the selected row.
So I have this code to move the selection to the previous row:

 int selIdx = mTableView.getTable().getSelectionIndex();
 mTableView.getTable().setSelection((selIdx > 0) ? (selIdx - 1) : 0);
 mTableView.getTable().notifyListeners(SWT.Selection, new Event());


This works fine - EXCEPT if the currently selected item is row 0.
After the delete, and the above code executes. (reselecting row 0)
the table is left with no selection.

Am I missing something?
Re: Updating selection when removing lines from table [message #810565 is a reply to message #810323] Thu, 01 March 2012 03:56 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten Schlathölter
Messages: 208
Registered: February 2012
Location: Düsseldorf
Senior Member
I did not get into details why the selection does not work for selIdx = 0 but I would propose to
place the selection in an asyncExec and it will probably work:

e.g.
deleteButton.addSelectionListener(new SelectionAdapter() {
	@Override
	public void widgetSelected(SelectionEvent e) {
		IStructuredSelection selection = (IStructuredSelection) mTableView.getSelection();
		if (selection!=null && !selection.isEmpty() && selection.size()==1)
		{
			final int selIdx = mTableView.getTable().getSelectionIndex();
			
			List<MyModel> model = (List<MyModel>) mTableView.getInput();
			model.remove(selection.getFirstElement());
			
			mTableView.getTable().getDisplay().asyncExec(new Runnable()
			{
				public void run() {
						 mTableView.getTable().setSelection((selIdx > 0) ? (selIdx - 1) : 0);
						 mTableView.getTable().notifyListeners(SWT.Selection, new Event());
					}
			});
		}
		
		mTableView.refresh();
	}
});
Re: Updating selection when removing lines from table [message #810895 is a reply to message #810565] Thu, 01 March 2012 11:51 Go to previous message
John Lussmyer is currently offline John Lussmyer
Messages: 96
Registered: December 2010
Member
Thanks, that does fix the problem.
Previous Topic:Difference between RCP and E4
Next Topic:Eclipse Templates: Escape < and > ?
Goto Forum:
  


Current Time: Tue Jul 29 00:39:42 EDT 2014

Powered by FUDForum. Page generated in 0.03036 seconds