Nattable添加行命令

时间:2018-10-31 10:01:34

标签: eclipse-rcp nattable

一个E4 RCP应用程序包含几个nattables。

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

Nattable

材料可移植表包括过滤器列。

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

这是通过创建命令和命令处理程序来处理添加事件来完成的,如下所示: 链接(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()));

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

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

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

Wrong rendering

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

1 个答案:

答案 0 :(得分:2)

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