Community
Participate
Working Groups
I am using a TableCursor in conjunction with a TableViewer. This is working fine apart from one issue, which arises when the table 'model' is updated underneath the cursor. When this occurs the rest of the table refreshes OK, but the content selected by the cursor remains unchanged. The following snippet demonstrates the problem: CUT==================== import java.util.ArrayList; import java.util.Date; import java.util.List; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ControlEditor; import org.eclipse.swt.custom.TableCursor; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; public class TableCursorBug { private static int counter = 0; private static ArrayList data = new ArrayList(); private static Table table; private static TableViewer viewer; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); table = new Table(shell, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION); table.setLayoutData(new GridData(GridData.FILL_BOTH)); TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setWidth(200); TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setWidth(100); TableColumn column3 = new TableColumn(table, SWT.NONE); column3.setWidth(200); viewer = new TableViewer(table); viewer.setContentProvider(new ArrayContentProvider()); viewer.setLabelProvider(new ArrayLabelProvider()); populateTable(); viewer.setInput(data); Button button = new Button(shell, SWT.PUSH); button.setText("Update Model"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { populateTable(); viewer.refresh(); } }); TableCursor cursor = new TableCursor(table, SWT.NONE); ControlEditor editor = new ControlEditor(cursor); editor.grabHorizontal = true; editor.grabVertical = true; shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } private static void populateTable() { counter++; // cause each redraw to be different Date now = new Date(); // different date too data.clear(); for (int i = 0; i < 10; i++) { data.add(now.toString() + " #" + counter + '/' + i); } } private static class ArrayContentProvider implements IStructuredContentProvider { public ArrayContentProvider() {} public void dispose() {} public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} public Object[] getElements(Object inputElement) { return ((List)inputElement).toArray(); } } private static class ArrayLabelProvider implements ITableLabelProvider { public ArrayLabelProvider() {} public boolean isLabelProperty(Object element, String property) { return false; } public Image getColumnImage(Object element, int columnIndex) { return null; } public void addListener(ILabelProviderListener listener) { } public void removeListener(ILabelProviderListener listener) { } public void dispose() {} public String getColumnText(Object element, int columnIndex) { return element.toString() + " col" + columnIndex; } } } CUT==================== The problem occurs both in the case when the model changes its content and when a totally new model is specified, i.e. by calling viewer.setInput() with a new input value.
I believe this is a dup but don't have the strength to find it.
1) TableCursor has no knowledge of TableViewer and its model. The TableViewer is in JFace which is a layer on top of SWT and TableCursor is in SWT. 2) There is no event when the text in the table changes. It is not SWT policy to send event notification for things that are changed programmatically (with some exceptions). So TableCursor can not update itself from a Table event. I think this is not an SWT bug but rather a request for support in JFace for TableCursor in TableViewer. Moving to the UI team.
I understand your point about not adding events; all that is really required is some way of being able to programmatically inform the cursor to 'reset itself'. However, I have not been able to find any way of achieving this.
*** This bug has been marked as a duplicate of 75557 ***