Menu

Set background color into the special row

Developers
Ira
2012-05-24
2013-05-08
  • Ira

    Ira - 2012-05-24

    Hi,

    is it possible to configure a NatTable so that the special row (e.g. row number 2) gets special background color (e.g. red)?

    Thanks,
    Ira

     
  • Dirk Fauth

    Dirk Fauth - 2012-05-24

    Hi,

    yes this is possible via cell label accumulators.

    See http://nattable.org/drupal/docs/customcellrendering

    Greez,
    Dirk

     
  • Ira

    Ira - 2012-05-30

    Hi Dirk,

    I have had a look at the example _001_Custom_styling_of_specific_cells. I have implemented it in my source code. It works fine. But if I try to use the ColumnGroupBodyLayerStack it doesn't seem to work anymore. Do you know what is wrong with my program?

        public Control createExampleControl(Composite parent) {
            propertyToLabels = new HashMap<String, String>();
            propertyToLabels.put("checkBox", "");
            propertyToLabels.put("fieldName", "Field Name");
            propertyToLabels.put("sourceType", "Source Type");
            propertyToLabels.put("null1", "Null Value");
            propertyToLabels.put("empty1", "Empty Value");
            propertyToLabels.put("distinct1", "Distinct Value");
            propertyToLabels.put("null2", "Null Value");
            propertyToLabels.put("empty2", "Empty Value");
            propertyToLabels.put("distinct2", "Distinct Value");
           
            propertyNames = new String { "checkBox", "fieldName", "sourceType", "null1", "empty1", "distinct1", "null2", "empty2", "distinct2"};
           
             eventList = GlazedLists.eventList(rows1);
            rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
            ConfigRegistry configRegistry = new ConfigRegistry();
            GlazedListsGridLayer<Row> glazedListsGridLayer = new GlazedListsGridLayer<Row>(rowObjectsGlazedList, propertyNames,
                    propertyToLabels, configRegistry);
           
            NatTable natTable = new NatTable(parent, glazedListsGridLayer, false);
           
            natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
            // Custom style for label "FOO"
            natTable.addConfiguration(new AbstractRegistryConfiguration() {
                @Override
                public void configureRegistry(IConfigRegistry configRegistry) {
                    Style cellStyle = new Style();
                    cellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_GREEN);
                    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL, "FOO");
                }
            });
            
            final SelectionLayer selectionLayer = glazedListsGridLayer.getBodyLayerStack().getSelectionLayer();
            final ListDataProvider<Row> bodyDataProvider = glazedListsGridLayer.getBodyDataProvider();
           
            final DataLayer bodyDataLayer =glazedListsGridLayer.getBodyDataLayer();

            ColumnGroupBodyLayerStack columnGroupBodyLayer = new ColumnGroupBodyLayerStack(bodyDataLayer, columnGroupModel);
            // Column header
            DefaultColumnHeaderDataProvider colHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabels);
            DefaultColumnHeaderDataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(colHeaderDataProvider);
            columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, columnGroupBodyLayer, columnGroupBodyLayer.getSelectionLayer());
            ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, columnGroupBodyLayer.getSelectionLayer(), columnGroupModel);
           
            columnGroupHeaderLayer.addColumnsIndexesToGroup("DE", 3, 4, 5);
            columnGroupHeaderLayer.addColumnsIndexesToGroup("FR", 6, 7, 8);
            
            columnHeaderDataLayer.setConfigLabelAccumulator(new ColumnLabelAccumulator());
           
            // Row header   
            final DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
            DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
            ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, columnGroupBodyLayer, columnGroupBodyLayer.getSelectionLayer());

            // Corner
            final DefaultCornerDataProvider cornerDataProvider =
                new DefaultCornerDataProvider(colHeaderDataProvider, rowHeaderDataProvider);
            DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
            ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnGroupHeaderLayer);

           
            glazedListsGridLayer.setBodyLayer(columnGroupBodyLayer);
            glazedListsGridLayer.setColumnHeaderLayer(columnGroupHeaderLayer);
            glazedListsGridLayer.setRowHeaderLayer(rowHeaderLayer);
            glazedListsGridLayer.setCornerLayer(cornerLayer);
           
            final DefaultBodyLayerStack bodyLayer = glazedListsGridLayer.getBodyLayerStack();
           IConfigLabelAccumulator cellLabelAccumulator = new IConfigLabelAccumulator() {
                @Override
                public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
                    //int columnIndex = bodyLayer.getColumnIndexByPosition(columnPosition);
                    int rowIndex = bodyLayer.getRowIndexByPosition(rowPosition);
                    if ( rowIndex == 2) {
                        configLabels.addLabel("FOO");
                    }
                }
            };

            bodyLayer.setConfigLabelAccumulator(cellLabelAccumulator);
           
            // Select complete rows
            RowOnlySelectionConfiguration<Row> selectionConfig = new RowOnlySelectionConfiguration<Row>();
            selectionLayer.addConfiguration(selectionConfig);
            natTable.addConfiguration(new RowOnlySelectionBindings());

            // Preserve selection on updates and sort
            selectionLayer.setSelectionModel(new RowSelectionModel<Row>(selectionLayer, bodyDataProvider, new IRowIdAccessor<Row>() {

                @Override
                public Serializable getRowId(Row rowObject) {
                    return rowObject.getFieldName();
                }
            }));
           
           
            natTable.configure();
           
            return natTable;
    }

    Thanks,
    Ira

     
  • Dirk Fauth

    Dirk Fauth - 2012-05-30

    Hi Ira,

    this one is easy. :) You should set your config label accumulator to the ColumnGroupBodyLayerStack. You can only have one body layer stack (at least everything else wouldn't make sense). You are still building the DefaultBodyLayerStack to add the config label accumulator without using the DefaultBodyLayerStack later as you set the ColumnGroupBodyLayerStack as your body layer in the grid.

    Greez,
    Dirk

     

Log in to post a comment.