How to get the right value in tableviewer [message #1235218] |
Thu, 23 January 2014 15:58 |
Eclipse User |
|
|
|
I created tableviewer ( jface ).
All the columns are editable ( using EditingSupport )
I want to save the changes in the table.
I Added a button of save.
The issue that I tried change the value in the cell ( the focus is still on the cell ) I press save and I want to read the value that changed.The issue that I always see the old value and not the new value.
If I changed the focus of the cell then it is working.
Do I need to add event for the editing in the cell ?
This is my code
//logic for the save button
saveButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
TableItem item = this.getTable().getItem(selectionIndex);
//The gridViewer Class
public class MyGridViewer extends TableViewer {
public MyGridViewer (Composite parent) {
super(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
final Table table = this.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
this.setContentProvider(new MyModelProvider());
}
}
@Override
protected void inputChanged(Object input, Object oldInput) {
removeColumn();
tableCol = new TableViewerColumn(this, SWT.NONE);
column = tableCol.getColumn();
column.setText(dataColumnHeader.getName());
column.setWidth(100);
column.setResizable(true);
column.setMoveable(true);
tableCol.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
DataRow r = (DataRow) element;
DataCell c = r.getDataCellByName(dataColumnHeader.getName());
if (c != null && c.getValue() != null) {
return c.getValue().toString();
}
return null;
}
});
editingSupport = new StringCellEditingSupport(this, dataColumnHeader);
tableCol.setEditingSupport(editingSupport);
super.inputChanged(input, oldInput);
}
//The edit cell Class
public class StringCellEditingSupport extends EditingSupport {
private final TableViewer tableViewer;
private final DataColumnHeader column;
private final StringCellEditor cellEditor;
public StringCellEditingSupport(TableViewer viewer, DataColumnHeader dataColumnHeader) {
super(viewer);
this.tableViewer = viewer;
this.cellEditor = new StringCellEditor(tableViewer.getTable(), SWT.BORDER);
this.column = dataColumnHeader;
}
@Override
protected StringCellEditor getCellEditor(Object element) {
return cellEditor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
if(element instanceof DataRow) {
DataRow dataRow = (DataRow) element;
DataCell cell = dataRow.getDataCellByName(this.column.getName());
return cell.getValue();
}
return null;
}
@Override
protected void setValue(Object element, Object value) {
if (element instanceof DataRow && value instanceof String) {
DataRow dataRow = (DataRow) element;
DataCell cell = dataRow.getDataCellByName(this.column.getName());
cell.setValue(value);
tableViewer.update(element, null);
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.04219 seconds