Nattable custom label accumulator

275 Views Asked by At

A NatTable requires to set cells of column 3 with BACKGROUND_COLOR = GUIHelper.COLOR_GREEN when the property of the Material is of type "Composite".

Material list is the source of data (the DataProvider)

NatTable is configured with StackingTableConfiguration.class

NatTable uses a custom label to set the cell style by means of StackingTableLabelAccumulator.class

As suggested in https://www.eclipse.org/forums/index.php/t/781508/ I have set the cellLabelAccumulator body data layer.

I used AggregateConfigLabelAccumulator since I have the custom label accumulator (AggregateConfigLabelAccumulator) and a columnLabelAccumulator.

When the Material is of type Composite, the label is added to the config labels, but the green color is nor render.

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

The configuration registry:

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

The custom label accumulator:

//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

There are 0 best solutions below