Nattable自定义标签累加器

时间:2018-11-22 11:16:21

标签: nattable

当Material的属性类型为“复合”时,NatTable要求使用Background_COLOR = GUIHelper.COLOR_GREEN设置第3列的单元格。

材料清单是数据的来源(数据提供者)

NatTable配置有StackingTableConfiguration.class

NatTable使用自定义标签通过StackingTableLabelAccumulator.class

设置单元格样式。

https://www.eclipse.org/forums/index.php/t/781508/中的建议 我已经设置了cellLabelAccumulator主体数据层。

我使用了AggregateConfigLabelAccumulator,因为我有自定义标签累加器(AggregateConfigLabelAccumulator)和columnLabelAccumulator

当Material的类型为Composite时,标签会添加到配置标签中,但是绿色不会渲染。

public class StackingTable {

    private NatTable natTable;
    public static IDataProvider bodyDataProvider;

    public static String COLUMN_ONE_LABEL = "ColumnOneLabel";
    public static String COLUMN_TWO_LABEL = "ColumnTwoLabel";
    public static String COLUMN_THREE_LABEL = "ColumnThreeLabel";

    public static String TEST = "Composite_Label";

    public StackingTable(Composite parent, 
    EventList<AncolabStackingLayer> ancolabStackingData, 
    SelectionLayer stackingTableSelectionLayer ) {

        bodyDataProvider = new ListDataProvider<>(ancolabStackingData, colAccessor);
        final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
        final ColumnOverrideLabelAccumulator columnLabelAccumulator =
               new ColumnOverrideLabelAccumulator(bodyDataLayer);
        columnLabelAccumulator.registerColumnOverrides(0, COLUMN_ONE_LABEL);
        columnLabelAccumulator.registerColumnOverrides(1, COLUMN_TWO_LABEL);
        columnLabelAccumulator.registerColumnOverrides(2, COLUMN_THREE_LABEL);
        //Create the gridLayer 
        natTable = new NatTable(parent,  gridLayer, false);

        AggregateConfigLabelAccumulator aggregateConfigLabelAccumulator =
            new AggregateConfigLabelAccumulator();
        aggregateConfigLabelAccumulator.add(columnLabelAccumulator);
        aggregateConfigLabelAccumulator.add(
            new StackingTableLabelAccumulator(bodyDataProvider));

        bodyDataLayer.setConfigLabelAccumulator(aggregateConfigLabelAccumulator);

        bodyDataLayer.addConfiguration(
            new DefaultNatTableStyleConfiguration());
        bodyDataLayer.addConfiguration(
            new StackingTableConfiguration(bodyDataProvider));
        bodyDataLayer.addConfiguration(new DefaultEditConfiguration());
        bodyDataLayer.addConfiguration(new DefaultEditBindings());   

        natTable.configure();
    }

配置注册表:

public class StackingTableConfiguration extends AbstractRegistryConfiguration {
    private IDataProvider bodyDataProvider;
    public StackingTableConfiguration(IDataProvider dp) {
    this.bodyDataProvider = dp;
    @Override
    public void configureRegistry(IConfigRegistry configRegistry) {
        //...some configutarion attributes for other columns  
        Style cellStyle2 = new Style();
        cellStyle2.setAttributeValue(
            CellStyleAttributes.BACKGROUND_COLOR,
            GUIHelper.COLOR_GREEN);

    configRegistry.registerConfigAttribute(
            CellConfigAttributes.CELL_STYLE, cellStyle2,
            DisplayMode.NORMAL, StackingTable.TEST);
    }
}

自定义标签累加器:

//Add a label to cells in column 2 when the material of the row is of type = "Composite"
public class StackingTableLabelAccumulator extends AbstractOverrider {
    IDataProvider dataProvider;
    public StackingTableLabelAccumulator(IDataProvider dataProvider){
        this.dataProvider = dataProvider;
    }
    @Override
    public void accumulateConfigLabels(LabelStack configLabels, 
                                      int columnPosition, int rowPosition) {
        Material mat = 
            (Material) ((IRowDataProvider) dataProvider).getRowObject(rowPosition);
        if(mat.getType().equals("Composite")&& columnPosition == 2) {
            configLabels.addLabel(StackingTable.TEST);
            //When a material of type composite, 
            //the code reachs this point, i.e. the label is added to the labelStack
            System.out.println(configLabels.getLabels().get(1).toString() +
                            "\t" + columnPosition + "\t" + rowPosition);
       }
    }
}

0 个答案:

没有答案