TableViewer_TreeViewer

SWT中设置TableViewer和TreeViewer成为双击编辑
博客分类: eclipse plugin


TableViewerEditor.create(viewer, new DoubleClickColumnViewerEditorActivationStrategy(viewer), ColumnViewerEditor.DEFAULT);

class DoubleClickColumnViewerEditorActivationStrategy extends ColumnViewerEditorActivationStrategy {
   public DoubleClickColumnViewerEditorActivationStrategy(ColumnViewer viewer) {
    super(viewer);
   }

   /**
   * 設置編輯器觸發方式
   */
   @Override
   protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
    boolean singleSelect = ((IStructuredSelection) getViewer().getSelection()).size() == 1;
    return singleSelect
      && (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL);
   }
}
覆盖isEditorActivationEvent方法,加上event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION条件。










tableViewerColumn_1.setEditingSupport(new EditingSupport(

tableViewerColumn_1.getViewer()) {



@Override

protected CellEditor getCellEditor(Object element) {

// TODO Auto-generated method stub

if (element instanceof Department) {

Department dep = (Department) element;

TextCellEditor textCell = new TextCellEditor(table);

textCell.setValue(dep.getDmanager());

return textCell;

}

return null;

}



@Override

protected boolean canEdit(Object element) {

// TODO Auto-generated method stub



if (element instanceof Department) {

return true;

}

return false;

}



@Override

protected Object getValue(Object element) {

// TODO Auto-generated method stub

if (element instanceof Department) {

Department dep = (Department) element;

return dep.getDmanager();

}

return null;

}



@Override

protected void setValue(Object element, Object value) {

// TODO Auto-generated method stub

if (element instanceof Department) {

Department dep = (Department) element;

dep.setDmanager(value.toString());

getViewer().refresh();

}

}

});







总结 

关键代码就这两行

1:向viewer绑定一种启动编辑的入口(如:是双击、单机、键盘都可以在这个类中区定义)DoubleClickColumnViewerEditorActivationStrategy

TableViewerEditor.create(viewer, new DoubleClickColumnViewerEditorActivationStrategy(viewer), ColumnViewerEditor.DEFAULT);



这行代码是为 哪列 设置为可编辑的,并设置一个EditingSupport

tableViewerColumn_1.setEditingSupport(new EditingSupport(

你可能感兴趣的:(tableview)