如何使用星云NatTable的PreserveSelectionModel? [英] How to use Nebula NatTable's PreserveSelectionModel?

查看:101
本文介绍了如何使用星云NatTable的PreserveSelectionModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发RCP应用程序,并正在使用Nebula的NatTable. 关于选择,我无法理解应该如何使用它.

I am developing an RCP Application, and am using Nebula's NatTable for that. When it comes to selection, I am failing to understand how I am supposed to use it.

我想要的是:

  • 我希望选择整个行.我可以使用RowOnlySelectionConfigurationRowOnlySelectionBindings来做到这一点.
  • 如果我选择一行,我希望所选内容保留在那里并且在该行中的某些数据更新时不清除.我该怎么办?
  • 如果选择了一行,并且该行中元素的位置发生了变化(例如,删除了先前的元素之一,并且位置更改为索引-1),我希望选择项可以更改该元素的位置,以便在更改后选择相同的元素.我该怎么办?
  • I want to have entire Rows selected. I was able do that using the RowOnlySelectionConfiguration and the RowOnlySelectionBindings.
  • If I select a row, I want the selection to stay there and not be cleared when some data in that row gets updated. How do I do that?
  • If a row is selected, and the position of the element in that row changes (e.g. one of the previous elements is removed, and the position changes to index - 1), I want the selection to change the position with the element, so that the same element is selected after the change. How do I do that?

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

I have seen that the documentation talks about a PreserveSelectionModel that can be used for that:

如果您在以前的版本中使用了PreserveSelectionStructuralChangeEventHandler解决方法,而不是清除有关结构更改的选择,则您会注意到此解决方法不再起作用.如果仍然需要该行为,则现在可以通过配置和设置如下的SelectionModel实例来实现相同的目的:

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);

如果您希望所选内容随结构变化(例如排序)而更新和移动,请尝试使用PreserveSelectionModel.

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).默认情况下会这样做吗?

So I guess I have to use the PreserveSelectionModel? But there I can't call setClearSelectionOnChange(false). Does it do that by default?

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

And how do I use the PreserveSelectionModel? What do I pass in the constructor?

我在名为TableBodyLayerStack的类中实现了自己的BodyLayerStack,在该类中的构造函数中进行了尝试:

I implement my own BodyLayerStack, in a class called TableBodyLayerStack, where I tried this in the constructor:

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的构造函数中,我这样做:

Then, in the contructor of my implementation of the GridLayer, I do this:

// ...

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.

But this is giving me NullPointerExceptions in the PreserveSelectionModel.

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的构造函数中传递了空值.但是,我该如何使用它呢?我必须作为构造函数的参数传递什么?我从哪里得到这些值?

I guess it is because I pass null values in the constructor of my PreserveSelectionModel. But how do I use it instead? What do I have to pass as arguments for the constructor? Where do I get the values from?

感谢您的帮助.

推荐答案

您在实现目标上走了错误的路.首先,我会回答您的问题:

you are on the wrong track to achieve your goals. First I will answer your questions:

所以我想我必须使用PreserveSelectionModel?

不,PreserveSelectionModel旨在保留单元格选择的选择.您要保留整个行的选择.因此,您需要使用RowSelectionModel.

No, the PreserveSelectionModel is intended to preserve the selection for cell selections. You want to preserve the selection for whole rows. So you need to use the RowSelectionModel.

但是我不能调用setClearSelectionOnChange(false).默认情况下会这样做吗?

是的

但这在PreserveSelectionModel中给了我NullPointerExceptions.我猜是因为我在PreserveSelectionModel的构造函数中传递了空值.

是的

我必须作为构造函数的参数传递什么?我从哪里得到这些值?

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

The second parameter is IRowDataProvider<T>, so it is the IDataProvider for the body. The third parameter is IRowIdAccessor<T>. You need to create an implementation that provides an unique id, so a row can be identified without knowing the position or index in the underlying collection.

所以您需要做的是这样的事情:

So what you need to do is something like this:

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

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

    }));

但是,如果要使其通用,您当然需要为TableBodyLayerStack提供IDataProviderIRowIdAccessor.

But of course you need to provide the IDataProvider and also the IRowIdAccessor to your TableBodyLayerStack if you want to keep it generic.

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

Also note that you don't have to call SelectionLayer#registerEventHandler() yourself! This is done internally by calling SelectionLayer#setSelectionModel().

您可以在NatTable示例应用程序中的 https://www.eclipse.org/nattable/找到一些示例. (右侧的尝试!"按钮). 对于您的问题,教程示例->图层->选择-> RowSelectionExample似乎是要看的一个.

You can find several examples in the NatTable Examples Application at https://www.eclipse.org/nattable/ (the Try it! button on the right side). For your question the Tutorial Examples -> Layers -> Selection -> RowSelectionExample seems to be the one to look at.

这篇关于如何使用星云NatTable的PreserveSelectionModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆