向 NatTable 行添加图像和背景

Adding Image and background to NatTable rows

我已阅读文档:https://eclipse.org/nattable/documentation.php?page=styling

我很好奇是否有任何简单的方法可以使用单独的配置添加背景行颜色和图像。我不希望像 CellPainterWrapper 示例那样将它们组合成 1 个配置,因为我想将两者之间的逻辑分开。我当前的代码适用于图像或背景颜色,但我不能两者都做(最顶层的配置覆盖最底层的配置)。以下是我的片段:

    void run(){
           addBackgroundRowColors();
           addImageToColumn();
    }

    void addImageToColumn() {
        getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
            @Override
            public void configureRegistry(IConfigRegistry configRegistry) {
                final Style cellStyleOne = new Style();
                cellStyleOne.setAttributeValue(CellStyleAttributes.IMAGE,
                   myIcon);
                configRegistry.registerConfigAttribute(
                   CellConfigAttributes.CELL_STYLE, cellStyleOne,
                   DisplayMode.NORMAL, myIconLabel);

                final Style cellStyleTwo = new Style();
                cellStyleTwo.setAttributeValue(CellStyleAttributes.IMAGE, 
                   myIcon2);
                configRegistry.registerConfigAttribute(
                   CellConfigAttributes.CELL_STYLE, cellStyleTwo,
                   DisplayMode.NORMAL, myIconLabel2);

                configRegistry.registerConfigAttribute(
                   CellConfigAttributes.CELL_PAINTER,
                   new CellPainterDecorator(new TextPainter(), 
                   CellEdgeEnum.LEFT, 10, new ImagePainter()),
                   DisplayMode.NORMAL);
            }
        });
        DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
        IConfigLabelAccumulator cellLabelAccumulator = (configLabels, 
          columnPosition, rowPosition) -> {
               // Label code here...
        };

        dl.setConfigLabelAccumulator(cellLabelAccumulator);
    }

    void addBackgroundRowColors() {
        getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
            @Override
            public void configureRegistry(IConfigRegistry configRegistry) {
                Style cellStyleOne = new Style();
                cellStyleOne.setAttributeValue(
                  CellStyleAttributes.BACKGROUND_COLOR, myColorOne);
                configRegistry.registerConfigAttribute(
                  CellConfigAttributes.CELL_STYLE, cellStyleOne,
                  DisplayMode.NORMAL, myColorLabel1);

                Style cellStyleTwo = new Style();
                cellStyleTwo.setAttributeValue(
                  CellStyleAttributes.BACKGROUND_COLOR, cellStyleTwo);
                configRegistry.registerConfigAttribute(
                  CellConfigAttributes.CELL_STYLE, cellStyleTwo,
                  DisplayMode.NORMAL, myColorLabel2);
            }
        });
        DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
        IConfigLabelAccumulator cellLabelAccumulator = (configLabels, 
          columnPosition, rowPosition) -> {
               // Label code here...
        };

       dl.setConfigLabelAccumulator(cellLabelAccumulator);
    }

更新

我最终做了类似于以下的事情来让它工作:

  AggregateConfigLabelAccumulator aggregate = 
    new AggregateConfigLabelAccumulator();
  aggregate.add(addImageToColumn());
  aggregate.add(addBackgroundRowColors());

  getGlazedListsGridLayer().getBodyDataLayer().
    setConfigLabelAccumulator(aggregate);

从评论来看,真正的问题是关于如何支持分隔 IConfigLabelAccumulator。由于每层只能注册一个IConfigLabelAccumulator,有两种方法可以实现:

  1. 在不同的图层上注册不同的IConfigLabelAccumulator
  2. 使用一个 AggregateConfigLabelAccumulator,您可以组合多个 IConfigLabelAccumulator

这在入门教程中也有说明: http://www.vogella.com/tutorials/NatTable/article.html