gpt4 book ai didi

java - 监听器听不到 NatTable 单元格单击

转载 作者:行者123 更新时间:2023-12-02 08:55:30 24 4
gpt4 key购买 nike

以下 SSCCE:

  • 创建一个 1x1 NatTable,并将标签“BUTTON”应用于单个包含细胞
  • 添加一个可以运行的 ButtonCellPainter(单元格看起来像一个按钮)
  • 尝试添加一个不起作用的点击监听器(否则我会在 stderr 中看到“YOU ARE HERE”)

我感到困惑的地方是在 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 注册。并且由于触发了选择,因此会跳过按钮单击。

在您的代码中,您可以使用 registerFirstMouseDownBinding() 简单地解决此问题

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

这样您的绑定(bind)就会在注册表顶部注册,因此首先进行检查。由于我们的检查有更多条件(区域标签、状态掩码、鼠标按钮和单元格标签),因此仅在 BODY 区域中具有 BUTTON 标签的单元格上触发。然后就无法选择纽扣电池,恕我直言,这是正确的。但您仍然可以选择没有 BUTTON 单元格标签的其他单元格。

关于您的第二个问题:不,BUTTON 标签不应成为区域标签的一部分。它是单元格标签,而不是区域标签。

关于java - 监听器听不到 NatTable 单元格单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60518351/

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