简体   繁体   English

听者听不到 NatTable 单元格点击

[英]NatTable cell click not heard by listener

The following SSCCE:以下 SSCCE:

  • creates a 1x1 NatTable with the label "BUTTON" applied to the single contained cell创建一个 1x1 NatTable,标签“BUTTON”应用于单个包含的单元格
  • adds a ButtonCellPainter which works (the cell looks like a button)添加一个有效的 ButtonCellPainter(单元格看起来像一个按钮)
  • tries to add a click listener which doesn't work (else I'd see "YOU ARE HERE" in stderr)尝试添加一个不起作用的点击监听器(否则我会在标准错误中看到“你在这里”)

The bit I'm confused about is between the START OF CONFUSION & END OF CONFUSION comment lines - the rest is stuff to get the example set up.我感到困惑的一点是在START OF CONFUSIONEND OF CONFUSION注释行之间 - 其余的是设置示例的内容。 I've tried to follow along with the Rendering_cells_as_a_link_and_button example provided, but obviously there's something I'm messing up here.我试图遵循提供的 Rendering_cells_as_a_link_and_button 示例,但显然我在这里搞砸了一些事情。

The performance & aesthetics of NatTable really are amazing; NatTable 的性能和美学真的很棒; it'd be nice to have someone point out my error here so that I can add this to my project :-)很高兴有人在这里指出我的错误,以便我可以将其添加到我的项目中:-)

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

}

edit: in debugging it seems that in the below (from NatTable's UiBindingRegistry) regionLabels only contains BODY :编辑:在调试中似乎在下面(来自 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;
    }

... should BUTTON not also be returned here, since the label is added to that cell (I can tell the region is added correctly since the cell does get rendered as a button)? ... 不应该在这里返回BUTTON ,因为标签已添加到该单元格(我可以告诉该区域已正确添加,因为该单元格确实呈现为按钮)?

NatTable is 2.0.0.201910310701 NatTable 是 2.0.0.201910310701

The issue is that single click actions are exclusive.问题是单击操作是排他的。 So the first match wins.所以第一场比赛获胜。 In your case the SelectCellAction gets triggered, which is registered by the DefaultSelectionBindings .在您的情况下, SelectCellAction被触发,由DefaultSelectionBindings注册。 And since the selection is triggered, the button click is skipped.并且由于选择被触发,按钮点击被跳过。

In your code you can simply fix this by using registerFirstMouseDownBinding()在您的代码中,您可以简单地使用registerFirstMouseDownBinding()解决此问题

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

This way your binding is registered on top of the registry and therefore checked first.这样,您的绑定就在注册表顶部注册,因此首先进行检查。 As our check has more conditions (region label, state mask, mouse button AND cell label) it is only triggered on the cells with the BUTTON label in the BODY region.由于我们的检查有更多条件(区域标签、状态掩码、鼠标按钮和单元格标签),它仅在 BODY 区域中带有 BUTTON 标签的单元格上触发。 The button cell can then not be selected, which is correct IMHO.然后无法选择纽扣电池,恕我直言,这是正确的。 But you could still select other cells that do not have the BUTTON cell label.但是您仍然可以选择没有 BUTTON 单元格标签的其他单元格。

Regarding your second question: No, the BUTTON label should NOT be part of the region labels.关于你的第二个问题:不,按钮标签不应该是区域标签的一部分。 It is a cell label, not a region label.它是一个单元格标签,而不是一个区域标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM