Skip to main content



      Home
Home » Eclipse Projects » NatTable » [Resolved] How to apply background to specific column headers
[Resolved] How to apply background to specific column headers [message #1726550] Mon, 14 March 2016 11:17 Go to next message
Eclipse UserFriend
Hi Smile

I'm working with the NatTable examples to try to understand how styling works. I read the examples to apply a custom style to cells based on their label (using a ColumnOverrideLabelAccumulator) and to apply a background to all column headers by extending the DefaultColumnHeaderStyleConfiguration class.

I succeeded in creating a NatTable with a custom background color for each columns using the ColumnOverrideLabelAccumulator on my bodylayerstack and the configRegistry mechanism to define the backgrounds.

Now I'm trying to apply a custom style to some of my column headers. I'm using a ColumnGroupHeaderLayer to group my columns and I know that it has a setConfigLabelAccumulator method so I tried to use another ColumnOverrideLabelAccumulator and a configRegistry to define the styles but it doesn't seem to work.

Is it possible to apply custom style to specific column headers when grouping headers? Should it work with the classes I used or is there another method to defines these custom backgrounds for my column headers and groups?

Thanks Smile

[Updated on: Wed, 16 March 2016 07:43] by Moderator

Re: How to apply background to specific column headers [message #1726553 is a reply to message #1726550] Mon, 14 March 2016 11:23 Go to previous messageGo to next message
Eclipse UserFriend
The mechanism is the same as for body cells. What do you mean by "using another ConfigRegistry"? Did you create a new instance for that? That would be wrong. There is only one ConfigRegistry per NatTable.
Re: How to apply background to specific column headers [message #1726556 is a reply to message #1726553] Mon, 14 March 2016 11:53 Go to previous messageGo to next message
Eclipse UserFriend
No I didn't create another configRegistry. I only created another ColumnOverrideLabelAccumulator. I tried to replace it by a simple IConfigLabelAccumulator and applied the same label to all elements of my ColumnGroupHeaderLayer but the result is the same.

Here is my source code:

			IColumnPropertyAccessor<MVirtualResource> columnPropertyAccessor = new VirtualRowColumnPropertyAccessor(columns);
			
			final CustomBodyLayerStack<MVirtualResource> bodyLayerStack = 
					new CustomBodyLayerStack<MVirtualResource>(
							viewData, 
							columnPropertyAccessor, new MVirtualResourceTreeFormat());
			
			// build the column header layer
			ILayer columnHeaderLayer = new ColumnHeaderLayer(new DataLayer(
					createColumnHeaderDataProvider(columns)), bodyLayerStack, bodyLayerStack.getSelectionLayer());
			
			// Add a group header for each column to display their origin.
			ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer, bodyLayerStack.getSelectionLayer(), new ColumnGroupModel());
			setGroups(columnGroupHeaderLayer, columns);
			
			// register labels to identify column headers for styling configuration
			columnGroupHeaderLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {
				@Override
				public void accumulateConfigLabels(LabelStack labelStack, int columnPosition, int rowPosition) {
					labelStack.addLabel("CUSTOM_HEADERS");
				}
			});
						
			// build the composite layer which regroup header and body layers
			CompositeLayer compositeLayer = new CompositeLayer(1, 2);
			compositeLayer.setChildLayer(GridRegion.COLUMN_HEADER, columnGroupHeaderLayer, 0, 0);
			compositeLayer.setChildLayer(GridRegion.BODY, bodyLayerStack, 0, 1);
			
			final NatTable natTable = new NatTable(parent, compositeLayer, false);
			natTable.setBackground(GUIHelper.COLOR_WHITE);
			GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
			natTable.setLayerPainter(new NatGridLayerPainter(natTable, DataLayer.DEFAULT_ROW_HEIGHT));
			
			natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
			
			final CustomRegistryConfiguration customConfiguration = new CustomRegistryConfiguration(columns);
			natTable.addConfiguration(customConfiguration);
			natTable.configure();


In my CustomRegistryConfiguration class which extends AbstractRegistryConfiguration, i just add the background for cells with the "CUSTOM_HEADERS" label:

		Style style = new Style();
		Color color = Display.getDefault().getSystemColor(SWT.COLOR_CYAN);
		style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, color);
		configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, style, DisplayMode.NORMAL, "CUSTOM_HEADERS");


This code should apply the CYAN color to every header but it doesn't. They still have the default gray color.
Re: How to apply background to specific column headers [message #1726558 is a reply to message #1726556] Mon, 14 March 2016 12:09 Go to previous messageGo to next message
Eclipse UserFriend
Looking into the sources it looks like the ColumnGroupHeaderLayer does not support (or better - misses) the usage of IConfigLabelAccumulator.

Please file a ticket so we can fix this.
Re: How to apply background to specific column headers [message #1726560 is a reply to message #1726558] Mon, 14 March 2016 12:24 Go to previous messageGo to next message
Eclipse UserFriend
Ok, thanks for your help Smile

I created the ticket on bugzilla. I just past the url so that users can follow it : https://bugs.eclipse.org/bugs/show_bug.cgi?id=489564

Best Regards Smile
Re: How to apply background to specific column headers [message #1726588 is a reply to message #1726560] Mon, 14 March 2016 16:54 Go to previous messageGo to next message
Eclipse UserFriend
I think I fixed it. If you want to specify different colors for different column groups, your accumulator should look similar to the following snippet

public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
    int idx = columnGroupHeaderLayer.getColumnIndexByPosition(columnPosition);
    // column group that starts at index 4 gets a custom label
    if (columnGroupModel.getColumnGroupByIndex(4).getMembers().contains(idx)) {
        configLabels.addLabel("custom_label");
    }
}
Re: How to apply background to specific column headers [message #1726670 is a reply to message #1726588] Tue, 15 March 2016 10:58 Go to previous messageGo to next message
Eclipse UserFriend
Thank you for the fix and for the sample code Smile It works for the header groups. However the color doesn't apply to the headers which are inside the header groups.

Maybe that's the expected behaviour? In this case, how can I apply the background color of a header group to its header children? I looked at my columHeaderLayer object and it doesn't have a setConfigLabelAccumulator() method.

Regards.
Re: How to apply background to specific column headers [message #1726677 is a reply to message #1726670] Tue, 15 March 2016 11:35 Go to previous messageGo to next message
Eclipse UserFriend
Quote:
the color doesn't apply to the headers which are inside the header groups


The config labels for the column header are retrieved from the column header, not the column group header. And of course ColumnHeaderLayer has a setConfigLabelAccumulator() via inheritance. But it also doesn't respect the config label accumulator. I reopen the ticket and fix it later.
As a workaround you could set the config label accumulator to the DataLayer of the column header, because that one is used.
Re: How to apply background to specific column headers [message #1726772 is a reply to message #1726677] Wed, 16 March 2016 07:42 Go to previous message
Eclipse UserFriend
Hi Smile

I tried with your last fix and everything works fine. Thank you for your help! ^^
Previous Topic:scrambled scroll on mac
Next Topic:How to capture Ctrl + A command in NatTable
Goto Forum:
  


Current Time: Mon Jun 23 18:59:59 EDT 2025

Powered by FUDForum. Page generated in 0.04451 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top