gpt4 book ai didi

java - 如何使用Nebula NatTable的PreserveSelectionModel?

转载 作者:行者123 更新时间:2023-12-01 11:45:08 25 4
gpt4 key购买 nike

我正在开发一个 RCP 应用程序,并为此使用 Nebula 的 NatTable。当谈到选择时,我不明白我应该如何使用它。

我想要的是:

  • 我想选择整行。我可以使用 RowOnlySelectionConfigurationRowOnlySelectionBindings 来做到这一点。
  • 如果我选择一行,我希望选择保留在那里,并且当该行中的某些数据更新时不会被清除。我该怎么做?
  • 如果选择一行,并且该行中元素的位置发生变化(例如,删除了先前的元素之一,并且位置更改为索引 - 1),我希望选择更改元素的位置,以便在更改后选择相同的元素。我该怎么做?

我已经看到文档讨论了可用于此目的的 PreserveSelectionModel:

If you used the PreserveSelectionStructuralChangeEventHandler workaround in previous versions for not clearing the selection on structural changes, you will notice that this workaround will not work anymore. If you still need that behavior, you are now able to achieve the same by configuring and setting a SelectionModel instance like this:

SelectionModel model = new SelectionModel(selectionLayer); 
// configure to not clear the selection on structural changes
model.setClearSelectionOnChange(false);
selectionLayer.setSelectionModel(model);

If you expect that the selection should update and move with structural changes (e.g. sorting), try to use the PreserveSelectionModel.

https://www.eclipse.org/nattable/nandn/nandn_120.php

所以我想我必须使用PreserveSelectionModel?但我无法调用 setClearSelectionOnChange(false)。默认情况下会这样做吗?

如何使用 PreserveSelectionModel?我在构造函数中传递什么?

我在一个名为 TableBodyLayerStack 的类中实现了自己的 BodyLayerStack,我在构造函数中尝试了以下操作:

public TableBodyLayerStack(IUniqueIndexLayer underlyingLayer) {
super(underlyingLayer);
columnReorderLayer = new ColumnReorderLayer(underlyingLayer);
columnHideShowLayer = new ColumnHideShowLayer(columnReorderLayer);
selectionLayer = new SelectionLayer(columnHideShowLayer, null, true, false);
PreserveSelectionModel<?> selectionModel = new PreserveSelectionModel<>(
selectionLayer, null, null);
selectionLayer.setSelectionModel(selectionModel);
selectionLayer.registerEventHandler(new SelectEventHandler(selectionLayer));
viewportLayer = new ViewportLayer(selectionLayer);
setUnderlyingLayer(viewportLayer);
registerCommandHandler(new CopyDataCommandHandler(selectionLayer));
}

然后,在 GridLayer 实现的构造函数中,我执行以下操作:

// ...

bodyLayer = new TableBodyLayerStack(eventLayer);
// register different selection move command handler that always moves by row
bodyLayer.getSelectionLayer().addConfiguration(new RowOnlySelectionConfiguration<T>());

// register selection bindings that will perform row selections instead of cell selections
// registering the bindings on a layer that is above the SelectionLayer will consume the
// commands before they are handled by the SelectionLayer
bodyLayer.addConfiguration(new RowOnlySelectionBindings());

// ...

但这在 PreserveSelectionModel 中给了我 NullPointerExceptions。

Error while painting table: null
java.lang.NullPointerException
at org.eclipse.nebula.widgets.nattable.selection.preserve.PreserveSelectionModel.getRowPositionByRowObject(PreserveSelectionModel.java:520)
at org.eclipse.nebula.widgets.nattable.selection.preserve.PreserveSelectionModel.createMarkerPoint(PreserveSelectionModel.java:559)
at org.eclipse.nebula.widgets.nattable.selection.preserve.PreserveSelectionModel.getSelectionAnchor(PreserveSelectionModel.java:531)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.getSelectionAnchor(SelectionLayer.java:276)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.getConfigLabelsByPosition(SelectionLayer.java:415)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getConfigLabelsByPosition(AbstractLayerTransform.java:316)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getConfigLabelsByPosition(AbstractIndexLayerTransform.java:318)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.getConfigLabelsByPosition(CompositeLayer.java:553)
at org.eclipse.nebula.widgets.nattable.layer.cell.AbstractLayerCell.getConfigLabels(AbstractLayerCell.java:48)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayer.getCellPainter(AbstractLayer.java:354)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getCellPainter(AbstractIndexLayerTransform.java:340)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getCellPainter(AbstractIndexLayerTransform.java:340)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.getCellPainter(CompositeLayer.java:586)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintCell(CellLayerPainter.java:171)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintLayer(CellLayerPainter.java:81)
at org.eclipse.nebula.widgets.nattable.painter.layer.GridLineCellLayerPainter.paintLayer(GridLineCellLayerPainter.java:106)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayerPainter.paintLayer(SelectionLayerPainter.java:95)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer$CompositeLayerPainter.paintLayer(CompositeLayer.java:913)
at org.eclipse.nebula.widgets.nattable.painter.layer.NatLayerPainter.paintLayer(NatLayerPainter.java:43)
at org.eclipse.nebula.widgets.nattable.NatTable.paintNatTable(NatTable.java:408)
at org.eclipse.nebula.widgets.nattable.NatTable.paintControl(NatTable.java:403)
...

我猜这是因为我在 PreserveSelectionModel 的构造函数中传递了 null 值。但我该如何使用它呢?我必须将什么作为构造函数的参数传递?我从哪里获取这些值?

感谢任何帮助。

最佳答案

您在实现目标方面走错了路。首先我回答一下大家的问题:

  1. So I guess I have to use the PreserveSelectionModel?

不, PreserveSelectionModel旨在保留单元格选择的选择。您想要保留整行的选择。所以你需要使用 RowSelectionModel .

  • But there I can't call setClearSelectionOnChange(false). Does it do that by default?

  • 是的

  • But this is giving me NullPointerExceptions in the PreserveSelectionModel. I guess it is because I pass null values in the constructor of my PreserveSelectionModel.

  • 是的

  • What do I have to pass as arguments for the constructor? Where do I get the values from?

  • 第二个参数是 IRowDataProvider<T> ,所以它是 IDataProvider为了 body 。 第三个参数是 IRowIdAccessor<T> 。您需要创建一个提供唯一 ID 的实现,以便在不知道基础集合中的位置或索引的情况下识别行。

    所以你需要做的是这样的:

    selectionLayer.setSelectionModel(new RowSelectionModel<Person>(
    selectionLayer, bodyDataProvider, new IRowIdAccessor<Person>() {

    @Override
    public Serializable getRowId(Person rowObject) {
    return rowObject.getId();
    }

    }));

    但是您当然需要提供IDataProvider还有IRowIdAccessor给您TableBodyLayerStack如果你想保持通用。

    另请注意,您不必自己调用 SelectionLayer#registerEventHandler()!这是通过调用 SelectionLayer#setSelectionModel() 在内部完成的。

    您可以在 NatTable 示例应用程序中找到多个示例,网址为 https://www.eclipse.org/nattable/ (右侧的“尝试一下!”按钮)。对于您的问题,教程示例 -> 图层 -> 选择 -> RowSelectionExample 似乎是值得一看的。

    关于java - 如何使用Nebula NatTable的PreserveSelectionModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212455/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com