Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » TablkeCursor fails to update when model changes
TablkeCursor fails to update when model changes [message #448629] Wed, 12 January 2005 15:18
Andy Doddington is currently offline Andy DoddingtonFriend
Messages: 17
Registered: July 2009
Junior Member
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.

Any advice would be most appreciated!
Previous Topic:How can I put TableViewer and TableCursor work together?
Next Topic:table drawing question....
Goto Forum:
  


Current Time: Tue Apr 13 20:50:45 GMT 2021

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

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

Back to the top