使用组合查看器选择的数据更新表的列单元格JFace

时间:2016-07-18 07:36:51

标签: java swt jface

我有一个包含4列的表格。 ReferenceID,问题,类别和已审核。 RefrenceId,Question Category是普通文本,而Review是下拉状态,是或空白值。

RefrenceId,Question,Category从名为QuestionType的模型对象中获取值。

对于已审核,它始终是固定值是,空白不是来自模型对象。

我创建了Tabel查看器,创建了列,添加了不同的单元格编辑器,还编辑了对审阅列的支持。

到目前为止,所有专栏都有数据。但是当我选择带有任何选择的已审核列时,值不会出现在所选单元格中

以下是我的代码

private void createCheckListTableUI(Composite parent) {
checkListTableViewer = new TableViewer(tableContainer, SWT.RESIZE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.SINGLE);
    checkListTableViewer.setContentProvider(new ObservableSetContentProvider());
    checkListTableViewer.getTable().setHeaderVisible(true);
    checkListTableViewer.getTable().setLinesVisible(true);
    TableColumnLayout tcl = new TableColumnLayout();
    createColumnsForCheckListTable(checkListTableViewer, tcl);
    CellEditor[] editors = new CellEditor[4];
    editors[0] = new TextCellEditor(checkListTableViewer.getTable());
    editors[1] = new TextCellEditor(checkListTableViewer.getTable());
    editors[2] = new TextCellEditor(checkListTableViewer.getTable());        
    editors[3] = new ComboBoxCellEditor(parent, reviewed, SWT.DROP_DOWN);        
    checkListTableViewer.setCellEditors(editors);
    checkListTableViewer.setInput(checkListModel.getAvailableQuestList());
    tableContainer.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).span(3, 30).create());
    tableContainer.setLayout(tcl);
}

列创建代码

private void createColumnsForCheckListTable(TableViewer checkListTableViewer, TableColumnLayout tcl) {
  //RefID
    TableViewerColumn refIdColumn = createColumn(checkListTableViewer, Constants.CHECKLIST_REFID, 80);
    tcl.setColumnData(refIdColumn.getColumn(), new ColumnWeightData(80));
    refIdColumn.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof QuestionType) {
                QuestionType question = (QuestionType) element;
                return question.getRefID();
            }
            return element.toString();
        }
    });
   ...

TableViewerColumn reviewedColumn = createColumn(checkListTableViewer, Constants.CHECKLIST_REVIEWED, 80);
    ReviewColumnEditorSupport revColEditing = new ReviewColumnEditorSupport(reviewedColumn.getViewer(),checkListModel.getReviewedVal());
    reviewedColumn.setEditingSupport(revColEditing);
    tcl.setColumnData(reviewedColumn.getColumn(), new ColumnWeightData(90));
    reviewedColumn.setLabelProvider(new CellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            if(cell.getElement() instanceof String) {
                cell.setText(((String) cell.getElement()));
            }
        }
    });

}

编辑支持类如下所示

public final class ReviewColumnEditorSupport  extends EditingSupport {
    private String[] reviewed = {"","yes","No"};
    private ComboBoxViewerCellEditor cellEditor = null;
    private WritableValue isReviewedValue;

    public ReviewColumnEditorSupport(ColumnViewer viewer, WritableValue isReviewedValue) {
        super(viewer);
        this.isReviewedValue = isReviewedValue;
        cellEditor = new ComboBoxViewerCellEditor((Composite) getViewer().getControl(), SWT.READ_ONLY);
        cellEditor.setLabelProvider(new LabelProvider());
        cellEditor.setContentProvider(new ArrayContentProvider());
        cellEditor.setInput(reviewed);
        cellEditor.addListener(new ICellEditorListener() {

            @Override
            public void editorValueChanged(boolean oldValidState, boolean newValidState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void cancelEditor() {
                // TODO Auto-generated method stub

            }

            @Override
            public void applyEditorValue() {
                CCombo combo =  (CCombo) cellEditor.getControl();
                isReviewedValue.setValue(combo.getText());
            }
        });

    }

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

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

    @Override
    protected Object getValue(Object element) {

        if (element instanceof QuestionType) {
            CCombo combo =  (CCombo) cellEditor.getControl();
            return combo.getText();
        }
        return null;
    }

    @Override
    protected void setValue(Object element, Object value) {
        if (element instanceof QuestionType && value instanceof String) {
          //  CCombo combo =  (CCombo) cellEditor.getControl();
            getViewer().update(value, null);
            isReviewedValue.setValue((String)value);

        }
    }

任何人都可以帮助我理解出了什么问题。 由于审核列中的数据不是来自模型,是否应对此进行特殊处理?

示例代码段也没问题。

1 个答案:

答案 0 :(得分:0)

combo的getValue()和setValue()应该使用整数值而不是字符串。从getValue()方法返回字符串数组中的值索引,并在setValue()中设置value的索引。可能你会从' value'获得一个int值。尝试调试。 如果它的字符串值然后在数组中找到值的索引。和

isReviewedValue.select(索引);