ekjcfn3902039
ekjcfn3902039

Reputation: 1839

Dynamically change row color in NatTable

I am trying to change the background row color based on the rows criteria. I am very close but there's something off I can't quite put my finger on. (I believe it's because I'm pulling an Object from an underlying list vs getting the data dynamically. I marked this section of the code below)

In the example below, each row color is based off of an Object (MyObj) which has a success or failure value. If myObj has a success value the row should be green. If myObj has a failure value the row should be red. If myObj has no value, the default row color should be used.

When I run the code, the row colors display as expected. However, if I sort the columns the original rows index maintains that color while the data moves to a new row index. I would expect that the row color would move with the object instead of always being fixed at that row index.

Example:
 Row 1 - "SUCCESS" - Shows Green
 Row 2 - "FAIL" - Shows Red

If I sort on that column alphabetically I get:

 Row 1 - "FAIL - Shows Green
 Row 2 - "SUCCESS" - Shows Red

Below is the code snippet I use to generate the example:

void example() {
    getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            Style cellStyleSuccess = new Style();
            cellStyleSuccess.setAttributeValue(
                                CellStyleAttributes.BACKGROUND_COLOR,
                                COLOR_SUCCESS);
            configRegistry.registerConfigAttribute(
                                CellConfigAttributes.CELL_STYLE, 
                                cellStyleSuccess,
                                DisplayMode.NORMAL, "SUCCESS");

            Style cellStyleFail = new Style();
            cellStyleFail.setAttributeValue(
                                CellStyleAttributes.BACKGROUND_COLOR, 
                                COLOR_FAILURE);
            configRegistry.registerConfigAttribute(
                                CellConfigAttributes.CELL_STYLE, 
                                cellStyleFail,
                                DisplayMode.NORMAL, "FAIL");
        }
    });
    DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
    IConfigLabelAccumulator cellLabelAccumulator = 
      new IConfigLabelAccumulator() {
        @Override
        public void accumulateConfigLabels(LabelStack configLabels, 
                        int columnPosition, int rowPosition) {
            configLabels.getLabels().clear();
            // TODO Is this the issue? Is there a better way to 
            // pull MyObj here?
            MyObj myObj = getEventList().get(rowPosition);
            if (myObj.getFoo().equals("SUCCESS")) {
                configLabels.addLabel("SUCCESS");
            } else if (myObj.getFoo().equals("FAIL"))) {
                configLabels.addLabel("FAIL");
            } else {
                // default color
            }

        }
    };

    dl.setConfigLabelAccumulator(cellLabelAccumulator);
    getNatTable().configure();
}

Upvotes: 1

Views: 467

Answers (1)

Dirk Fauth
Dirk Fauth

Reputation: 4231

The important part that probably causes the issue is missing. Which list is returned by getEventList()? If it is the basic EventList you always get the object at the original index. When you sort a transformation is applied via SortedList. So your issue should be solved if getEventList() returns the top most GlazedLists collection (SortedList or FilterList dependent on which features you are using).

Upvotes: 2

Related Questions