Nattable添加行命令 [英] Nattable add row command

查看:128
本文介绍了Nattable添加行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个E4 RCP应用程序由几个nattables组成.

An E4 RCP application consists on several nattables.

材料表可以创建用户定义的新材料.

Material nattable allows to create new user defined materials.

材料nattable包含过滤器列.

Material nattable includes filter columns.

EventList<AncolabMaterial> eventList = GlazedLists.eventList(input_values_list);
TransformedList<?, ?> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
SortedList<?> sortedList = new SortedList<>(rowObjectsGlazedList, null);
this.filterList = new FilterList<AncolabMaterial>((EventList<AncolabMaterial>) sortedList);
this.bodyDataProvider = new ListDataProvider<AncolabMaterial>(this.filterList, columnPropertyAccessor);
DataLayer bodyDataLayer = new DataLayer(getBodyDataProvider());
bodyDataLayer.registerCommandHandler(new DeleteRowCommandHandler<>(((ListDataProvider<AncolabMaterial>) bodyDataProvider).getList()));
bodyDataLayer.registerCommandHandler(new AddRowCommandHandler(((ListDataProvider<Object>) bodyDataProvider).getList()));
// layer for event handling of GlazedLists and PropertyChanges
GlazedListsEventLayer<?> glazedListsEventLayer =
        new GlazedListsEventLayer<AncolabMaterial>(bodyDataLayer, this.filterList);
this.selectionLayer = new SelectionLayer(glazedListsEventLayer, false);
selectionLayer.addConfiguration(new DefaultRowSelectionLayerConfiguration());
ViewportLayer viewportLayer = new ViewportLayer(getSelectionLayer());
setUnderlyingLayer(viewportLayer);
/...

这是通过创建一个命令和命令处理程序来处理添加事件来完成的,如下所示: 链接(从Nattable中删除行)

This is done by creating a command and command handler to process add events as in link (Delete rows from Nattable)

  1. 创建命令以添加行

    public class AddRowCommand extends AbstractRowCommand {
        private Object item;
        public AddRowCommand(ILayer layer, Object item) {
            super(layer, 0);
            this.setItem(item);
        }
        protected AddRowCommand(AddRowCommand command) {
           super(command);
        }
        @Override
        public ILayerCommand cloneCommand() {
            return new AddRowCommand(this);
        }
        public Object getItem() {
            return this.item;
        }
        private void setItem(Object item) {
            this.item = item;
        }
    }

  1. 创建命令处理程序

    public class AddRowCommandHandler implements ILayerCommandHandler<AddRowCommand> {
        private List<Object> bodyData;
        public AddRowCommandHandler(List<Object> bodyData) {
            this.bodyData = bodyData;
        }
        @Override
        public Class<AddRowCommand> getCommandClass() {
            return AddRowCommand.class;
        }
        @Override
        public boolean doCommand(ILayer targetLayer,  AddRowCommand command) {
                this.bodyData.add(0, (AncolabMaterial)command.getItem() );
                targetLayer.fireLayerEvent(new RowInsertEvent(targetLayer, command.getRowPosition()));
                return true;
        }  
     }

  1. 将命令处理程序注册到主体DataLayer

    bodyDataLayer.registerCommandHandler(new DeleteRowCommandHandler<>(((ListDataProvider<AncolabMaterial>) bodyDataProvider).getList()));
    bodyDataLayer.registerCommandHandler(new AddRowCommandHandler(((ListDataProvider<Object>) bodyDataProvider).getList()));

注意:用于删除行的命令处理程序工作正常

Note: command handler for deleting rows works fine

  1. 在菜单配置中添加一个菜单项,以触发该命令

    //At SWT Button mouseUp event:
    AddUserMaterialDialog dialog = new AddUserMaterialDialog(shell);
    dialog.create();
    if (dialog.open() == Window.OK) {
        AncolabMaterial matToAdd = dialog.getUserMaterial();
        natTableMaterials.doCommand(new AddRowCommand(natTableMaterials, matToAdd));
    }

创建新材质后,将更新材质列表,但是在渲染表格时会引发以下错误,并且表格无法正确渲染:

After creating new material, the list of materials is updated but when rendering the table the following error is thrown and the table does not render properly:

Error while painting table
java.lang.RuntimeException: java.lang.NullPointerException
at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getDataValue(ReflectiveColumnPropertyAccessor.java:74)
at org.eclipse.nebula.widgets.nattable.data.ListDataProvider.getDataValue(ListDataProvider.java:51)
at org.eclipse.nebula.widgets.nattable.layer.DataLayer.getDataValue(DataLayer.java:164)
at org.eclipse.nebula.widgets.nattable.layer.DataLayer.getDataValueByPosition(DataLayer.java:447)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getDataValueByPosition(AbstractLayerTransform.java:351)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getDataValueByPosition(AbstractIndexLayerTransform.java:358)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getDataValueByPosition(AbstractLayerTransform.java:351)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getDataValueByPosition(AbstractLayerTransform.java:351)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.getDataValueByPosition(CompositeLayer.java:617)
at org.eclipse.nebula.widgets.nattable.layer.cell.AbstractLayerCell.getDataValue(AbstractLayerCell.java:64)
at org.eclipse.nebula.widgets.nattable.layer.cell.CellDisplayConversionUtils.convertDataType(CellDisplayConversionUtils.java:21)
at org.eclipse.nebula.widgets.nattable.painter.cell.AbstractTextPainter.convertDataType(AbstractTextPainter.java:188)
at org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter.paintCell(TextPainter.java:157)
at org.eclipse.nebula.widgets.nattable.painter.cell.CellPainterWrapper.paintCell(CellPainterWrapper.java:67)
at org.eclipse.nebula.widgets.nattable.painter.cell.decorator.LineBorderDecorator.paintCell(LineBorderDecorator.java:76)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintCell(CellLayerPainter.java:216)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintLayer(CellLayerPainter.java:86)
at org.eclipse.nebula.widgets.nattable.painter.layer.GridLineCellLayerPainter.paintLayer(GridLineCellLayerPainter.java:141)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayerPainter.paintLayer(SelectionLayerPainter.java:111)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer$CompositeLayerPainter.paintLayer(CompositeLayer.java:1055)
at org.eclipse.nebula.widgets.nattable.painter.layer.NatLayerPainter.paintLayer(NatLayerPainter.java:54)
at org.eclipse.nebula.widgets.nattable.NatTable.paintNatTable(NatTable.java:507)
at org.eclipse.nebula.widgets.nattable.NatTable.paintControl(NatTable.java:502)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:231)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4118)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1052)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1076)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
at org.eclipse.swt.widgets.Composite.WM_PAINT(Composite.java:1561)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4822)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:345)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4765)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2303)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3532)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1170)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1059)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:162)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:656)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:592)
at org.eclipse.equinox.launcher.Main.run(Main.java:1498)
at org.eclipse.equinox.launcher.Main.main(Main.java:1471)
Caused by: java.lang.NullPointerException
at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getPropertyDescriptor(ReflectiveColumnPropertyAccessor.java:107)
at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getDataValue(ReflectiveColumnPropertyAccessor.java:69)
... 53 more

推荐答案

当尝试通过对空对象的反射来访问getter方法时,NPE来自ReflectiveColumnPropertyAccessor.我想您在列表转换方面遇到了问题.如果我正确阅读了您的代码,则说明您在命令处理程序中使用了FilterList. FilterList上的添加操作尚未传播或尚未传播到基础输入列表.我建议向EventList而不是FilterList注册命令处理程序.

The NPE is coming from the ReflectiveColumnPropertyAccessor when trying to access a getter method via reflection on a null object. I suppose you are running into issues with the list transformations. If I read your code correctly you are using the FilterList in your command handler. The add operation on the FilterList is not or not yet propagated to the underlying input list. I would suggest to register the command handlers with the EventList and not the FilterList.

这篇关于Nattable添加行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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