听众听不到 NatTable 单元格点击

NatTable cell click not heard by listener

以下 SSCCE:

我感到困惑的一点是在 START OF CONFUSIONEND OF CONFUSION 注释行之间 - 剩下的就是设置示例的内容。我试图按照提供的 Rendering_cells_as_a_link_and_button 示例进行操作,但显然我在这里搞砸了。

NatTable的性能和美感真的很赞;很高兴有人在这里指出我的错误,这样我就可以将其添加到我的项目中:-)

import org.eclipse.nebula.widgets.nattable.*;
import org.eclipse.nebula.widgets.nattable.config.*;
import org.eclipse.nebula.widgets.nattable.data.*;
import org.eclipse.nebula.widgets.nattable.grid.layer.*;
import org.eclipse.nebula.widgets.nattable.hideshow.*;
import org.eclipse.nebula.widgets.nattable.layer.*;
import org.eclipse.nebula.widgets.nattable.layer.cell.*;
import org.eclipse.nebula.widgets.nattable.painter.cell.*;
import org.eclipse.nebula.widgets.nattable.selection.*;
import org.eclipse.nebula.widgets.nattable.ui.action.*;
import org.eclipse.nebula.widgets.nattable.ui.binding.*;
import org.eclipse.nebula.widgets.nattable.ui.matcher.*;
import org.eclipse.nebula.widgets.nattable.viewport.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class NatTableTest extends Dialog implements IDataProvider, IConfigLabelAccumulator {

    @Override protected Control createDialogArea(Composite parent) {
        Composite toReturn = (Composite) super.createDialogArea(parent);
        toReturn.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        IDataProvider dataProvider = this; 
        DataLayer bodyDataLayer = new DataLayer(dataProvider);
        bodyDataLayer.setConfigLabelAccumulator(this);
        RowHideShowLayer rowHideShowLayer = new RowHideShowLayer(bodyDataLayer);
        SelectionLayer selectionLayer = new SelectionLayer(rowHideShowLayer);
        ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
        DataLayer rowDataLayer = new DefaultRowHeaderDataLayer(dataProvider);
        ILayer rowLayer = new RowHeaderLayer(rowDataLayer, viewportLayer, selectionLayer);
        DataLayer headerDataLayer = new DefaultColumnHeaderDataLayer(dataProvider);
        ILayer headerLayer = new ColumnHeaderLayer(headerDataLayer, viewportLayer, selectionLayer);
        DataLayer cornerDataLayer = new DataLayer(dataProvider);
        ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowLayer, headerLayer);
        GridLayer gridLayer = new GridLayer(viewportLayer, headerLayer, rowLayer, cornerLayer);
        NatTable nattable = new NatTable(toReturn, gridLayer, false);
        nattable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        nattable.addConfiguration(new DefaultNatTableStyleConfiguration());

        // ================================================== START OF CONFUSION

        ButtonCellPainter buttonPainter = new ButtonCellPainter(new TextPainter());

        buttonPainter.addClickListener(new IMouseAction(){
            @Override public void run(NatTable table, MouseEvent event) {
                System.err.println("YOU ARE HERE");
            }
        });     

        nattable.addConfiguration(new AbstractUiBindingConfiguration(){
            @Override public void configureUiBindings(UiBindingRegistry registry) {
                CellLabelMouseEventMatcher mouseEventMatcher = new CellLabelMouseEventMatcher("BODY",1,"BUTTON");
                registry.registerMouseDownBinding(mouseEventMatcher, buttonPainter);
            }
        });

        IConfigRegistry configRegistry = new ConfigRegistry();

        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, buttonPainter, "NORMAL","BUTTON");

        nattable.setConfigRegistry(configRegistry);

        // ================================================== END OF CONFUSION

        nattable.configure();
        return toReturn;
    }

    public NatTableTest(Shell parentShell) {super(parentShell);}

    // IDataProvider methods
    @Override public int getRowCount() {return 1;}
    @Override public int getColumnCount() {return 1;}
    @Override public Object getDataValue(int var1, int var2) {return "X";}
    @Override public void setDataValue(int var1, int var2, Object var3) {}

    // IConfigLabelAccumulator methods
    @Override public void accumulateConfigLabels(LabelStack labels, int col, int row) {labels.addLabel("BUTTON");}

}

编辑: 在调试中似乎在下面(来自 NatTable 的 UiBindingRegistry)regionLabels 只包含 BODY :

    private IMouseAction getMouseEventAction(MouseEventTypeEnum mouseEventType, MouseEvent event) {
        try {
            LinkedList<MouseBinding> mouseEventBindings = (LinkedList) this.mouseBindingsMap.get(mouseEventType);
            if (mouseEventBindings != null) {
                LabelStack regionLabels = this.natTable.getRegionLabelsByXY(event.x, event.y);
                Iterator var6 = mouseEventBindings.iterator();

                while (var6.hasNext()) {
                    MouseBinding mouseBinding = (MouseBinding) var6.next();
                    if (mouseBinding.getMouseEventMatcher().matches(this.natTable, event, regionLabels)) {
                        return mouseBinding.getAction();
                    }
                }
            }
        } catch (Exception var7) {
            log.error("Exception on retrieving a mouse event action", var7);
        }

        return null;
    }

... BUTTON 不应该在这里返回,因为标签已添加到该单元格(我可以告诉该区域已正确添加,因为该单元格确实呈现为按钮)?

NatTable 是 2.0.0.201910310701

问题是单击操作是独占的。所以第一场比赛获胜。在您的情况下,SelectCellAction 被触发,由 DefaultSelectionBindings 注册。由于 selection 被触发,按钮点击被跳过。

在您的代码中,您只需使用 registerFirstMouseDownBinding()

即可解决此问题
nattable.addConfiguration(new AbstractUiBindingConfiguration(){
        @Override public void configureUiBindings(UiBindingRegistry registry) {
            CellLabelMouseEventMatcher mouseEventMatcher = new CellLabelMouseEventMatcher("BODY",1,"BUTTON");
            registry.registerFirstMouseDownBinding(mouseEventMatcher, buttonPainter);
        }
    });

通过这种方式,您的绑定会在注册表顶部注册,因此会首先进行检查。由于我们的检查有更多条件(区域标签、状态掩码、鼠标按钮和单元格标签),它仅在 BODY 区域中带有 BUTTON 标签的单元格上触发。然后不能 select 编辑纽扣电池,这是正确的恕我直言。但是您仍然可以 select 其他没有 BUTTON 单元格标签的单元格。

关于你的第二个问题:不,BUTTON 标签不应该是区域标签的一部分。是单元格标签,不是区域标签。