From 4016abd33bb824de9ac42306393a377668dd9b3f Mon Sep 17 00:00:00 2001 From: Dirk Fauth Date: Wed, 7 Jan 2026 10:03:55 +0100 Subject: [PATCH] Impl #182 - Extend background painter to blend with selection style Signed-off-by: Dirk Fauth --- .../nattable/config/CellConfigAttributes.java | 10 +++ .../painter/cell/BackgroundPainter.java | 62 ++++++++++++++++++- .../_5016_DataChangeLayerExample.java | 10 ++- ...017_DataChangeLayerTempStorageExample.java | 10 ++- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/config/CellConfigAttributes.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/config/CellConfigAttributes.java index 73e4550f5..85aed67a7 100644 --- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/config/CellConfigAttributes.java +++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/config/CellConfigAttributes.java @@ -64,4 +64,14 @@ private CellConfigAttributes() { * @since 1.4 */ public static final ConfigAttribute GRID_LINE_WIDTH = new ConfigAttribute<>(); + + /** + * Attribute for configuring whether the selection background color should + * be blended with the normal background color. If not set or set to + * false, the selection background color will fully override + * the normal background color. + * + * @since 2.7 + */ + public static final ConfigAttribute BLEND_SELECTION_BACKGROUND = new ConfigAttribute<>(); } diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/BackgroundPainter.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/BackgroundPainter.java index 4f82bda31..42a5b541d 100644 --- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/BackgroundPainter.java +++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/BackgroundPainter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012, 2020 Original authors and others. + * Copyright (c) 2012, 2026 Original authors and others. * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -12,13 +12,18 @@ ******************************************************************************/ package org.eclipse.nebula.widgets.nattable.painter.cell; +import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes; import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry; import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry; import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell; import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes; +import org.eclipse.nebula.widgets.nattable.style.CellStyleProxy; import org.eclipse.nebula.widgets.nattable.style.CellStyleUtil; +import org.eclipse.nebula.widgets.nattable.style.DisplayMode; +import org.eclipse.nebula.widgets.nattable.util.GUIHelper; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.Rectangle; /** @@ -55,9 +60,64 @@ public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry super.paintCell(cell, gc, bounds, configRegistry); } + /** + * Get the background color for the given cell. + * + * @param cell + * The {@link ILayerCell} to get the background color for. + * @param configRegistry + * The {@link ConfigRegistry} needed to retrieve the color. + * @return The background color. + */ protected Color getBackgroundColour(ILayerCell cell, IConfigRegistry configRegistry) { + Boolean blend = configRegistry.getConfigAttribute( + CellConfigAttributes.BLEND_SELECTION_BACKGROUND, + DisplayMode.NORMAL, + cell.getConfigLabels()); + // if blending is enabled and the cell is selected, return blended color + if (blend != null && blend + && (cell.getDisplayMode() == DisplayMode.SELECT || cell.getDisplayMode() == DisplayMode.SELECT_HOVER)) { + return getBlendedBackgroundColour(cell, configRegistry); + } + return CellStyleUtil .getCellStyle(cell, configRegistry) .getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR); } + + /** + * Get the blended background color for the given cell by blending the + * normal and the select background colors. + * + * @param cell + * The {@link ILayerCell} to get the blended background color + * for. + * @param configRegistry + * The {@link ConfigRegistry} needed to retrieve the colors. + * @return The blended background color. + * + * @since 2.7 + */ + protected Color getBlendedBackgroundColour(ILayerCell cell, IConfigRegistry configRegistry) { + // get background color for DisplayMode.NORMAL + Color normalBackground = + new CellStyleProxy( + configRegistry, + DisplayMode.NORMAL, + cell.getConfigLabels()) + .getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR); + + // get background color for DisplayMode.SELECT or DisplayMode.SELECT_HOVER + Color selectBackground = + new CellStyleProxy( + configRegistry, + cell.getDisplayMode(), + cell.getConfigLabels()) + .getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR); + + // blend both colors + RGB background = normalBackground.getRGB(); + background = GUIHelper.blend(background, selectBackground.getRGB()); + return GUIHelper.getColor(background); + } } diff --git a/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5016_DataChangeLayerExample.java b/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5016_DataChangeLayerExample.java index 3deaa2b3b..ff4cc2f6c 100644 --- a/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5016_DataChangeLayerExample.java +++ b/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5016_DataChangeLayerExample.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2020 Dirk Fauth and others. + * Copyright (c) 2017, 2026 Dirk Fauth and others. * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -46,6 +46,7 @@ import org.eclipse.nebula.widgets.nattable.edit.editor.DateCellEditor; import org.eclipse.nebula.widgets.nattable.examples.AbstractNatExample; import org.eclipse.nebula.widgets.nattable.examples.runner.StandaloneNatExampleRunner; +import org.eclipse.nebula.widgets.nattable.grid.GridRegion; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider; @@ -245,6 +246,13 @@ class EditorConfiguration extends AbstractRegistryConfiguration { @Override public void configureRegistry(IConfigRegistry configRegistry) { + // configure to blend the background color on selection + configRegistry.registerConfigAttribute( + CellConfigAttributes.BLEND_SELECTION_BACKGROUND, + Boolean.TRUE, + DisplayMode.NORMAL, + GridRegion.BODY); + configRegistry.registerConfigAttribute( EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE); diff --git a/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5017_DataChangeLayerTempStorageExample.java b/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5017_DataChangeLayerTempStorageExample.java index f7a857a7b..1c867c941 100644 --- a/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5017_DataChangeLayerTempStorageExample.java +++ b/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_501_Data/_5017_DataChangeLayerTempStorageExample.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2020 Dirk Fauth and others. + * Copyright (c) 2017, 2026 Dirk Fauth and others. * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -44,6 +44,7 @@ import org.eclipse.nebula.widgets.nattable.edit.editor.DateCellEditor; import org.eclipse.nebula.widgets.nattable.examples.AbstractNatExample; import org.eclipse.nebula.widgets.nattable.examples.runner.StandaloneNatExampleRunner; +import org.eclipse.nebula.widgets.nattable.grid.GridRegion; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider; @@ -229,6 +230,13 @@ class EditorConfiguration extends AbstractRegistryConfiguration { @Override public void configureRegistry(IConfigRegistry configRegistry) { + // configure to blend the background color on selection + configRegistry.registerConfigAttribute( + CellConfigAttributes.BLEND_SELECTION_BACKGROUND, + Boolean.TRUE, + DisplayMode.NORMAL, + GridRegion.BODY); + configRegistry.registerConfigAttribute( EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);