Nebula NatTable 在使用 GridLayer 时没有显示正确的单元格编辑器。

Nebula NatTable didn't display correct cell editor when use GridLayer.

我编辑了一个 Nebula 示例 (_301_CustomDataProviderExample) 来测试按 tab 键时的单元格编辑功能(按 tab 键启用下一个单元格编辑)。当我使用 GridLayer 时出现问题:单元格编辑器显示不正确。我认为这是因为行和列 headers 但我不知道如何调试或修复。你能帮我或给我一些提示吗?这是我编辑或添加的代码:

 @Override
public Control createExampleControl(Composite parent) {

    String[][] testData = new String[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            testData[i][j] = "" + i + "/" + j;
        }
    }

    IDataProvider bodyDataProvider = new TwoDimensionalArrayDataProvider(testData);
    final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);

    SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
    ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
    viewportLayer.addConfiguration(new DefaultEditConfiguration());
    viewportLayer.addConfiguration(new DefaultEditBindings());

    IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
    DataLayer rowHeaderDataLayer = new DataLayer(rowHeaderDataProvider, 40, DataLayer.DEFAULT_ROW_HEIGHT);
    rowHeaderDataLayer.setColumnsResizableByDefault(true);
    ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer);

    IDataProvider columnHeaderDataProvider = new LetterColumnHeaderDataProvider(20);
    ILayer columnHeaderLayer = new ColumnHeaderLayer(
            new DefaultColumnHeaderDataLayer(columnHeaderDataProvider), viewportLayer, selectionLayer);

    IDataProvider conerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
    ILayer cornerLayer = new CornerLayer(new DataLayer(conerDataProvider), rowHeaderLayer, columnHeaderLayer);

    GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer, false);

    final NatTable natTable = new NatTable(parent, false);
    natTable.setLayer(/* viewportLayer */ gridLayer);

    natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {
        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            super.configureRegistry(configRegistry);
            configRegistry.registerConfigAttribute(
                    EditConfigAttributes.CELL_EDITABLE_RULE,
                    IEditableRule.ALWAYS_EDITABLE);
        }
    });

    natTable.configure();

    return natTable;
}

已添加:

class LetterColumnHeaderDataProvider implements IDataProvider {

private int columns = 0;

public LetterColumnHeaderDataProvider(int columns) {
    this.columns = columns;
}

@Override
public Object getDataValue(int columnIndex, int rowIndex) {
    return (char) ('A' + columnIndex);
    // TODO: support column header AA-AZ, ZA-ZZ, ZZZ...
}

@Override
public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
    throw new UnsupportedOperationException();
}

@Override
public int getColumnCount() {
    return this.columns;
}

@Override
public int getRowCount() {
    return 1;
}
}

此问题与在 ViewportLayer 上注册 DefaultEditConfiguration 有关。这样做将导致将按键识别的编辑器放置在相对于主体层堆栈而不是 GridLayer。使用 GridLayer 应用的默认配置已经包含必要的编辑配置。但是您通过 GridLayer 构造函数中的最后一个参数禁用了它。

简而言之,从您的示例中删除以下两行,只有在没有 GridLayer.

的组合中才需要它们
viewportLayer.addConfiguration(new DefaultEditConfiguration());
viewportLayer.addConfiguration(new DefaultEditBindings());

并将 GridLayer 的创建更改为此

GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);

作为提示,在不理解其用途的情况下从不同示例复制代码时要小心。 NatTable 示例通常包含代码解释以更详细地解释它们。