capint
11/1/2015 - 9:28 AM

Eclipse >> JFace >> TableViewer >> Column Editing

Eclipse >> JFace >> TableViewer >> Column Editing

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

public class FirstNameEditingSupport extends EditingSupport {

  private final TableViewer viewer;
  private final CellEditor editor;

  public FirstNameEditingSupport(TableViewer viewer) {
    super(viewer);
    this.viewer = viewer;
    //*** JFace provides the following default implementations for cell editors:
    //*** TextCellEditor, ColorCellEditor, CheckboxCellEditor, DialogCellEditor
  ComboBoxViewerCellEditor  this.editor = new TextCellEditor(viewer.getTable());
  }

  @Override
  protected CellEditor getCellEditor(Object element) {
    return editor;
  }

  @Override
  protected boolean canEdit(Object element) {
    return true;
  }

  @Override
  protected Object getValue(Object element) {
    return ((Person) element).getFirstName();
  }

  @Override
  protected void setValue(Object element, Object userInputValue) {
    ((Person) element).setFirstName(String.valueOf(userInputValue));
    viewer.update(element, null);
  }
}