From: Marko Luukkainen Date: Thu, 28 Apr 2022 10:18:40 +0000 (+0300) Subject: NatTable based graph explorer omits background decoration. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c161686d5f46e5d4715be7b001a890f4e72ab66a;p=simantics%2Fplatform.git NatTable based graph explorer omits background decoration. With trees, it is not enough to override just BODY style. Also EVEN_STYLE and ODD_STYLE must be overridden. Re-use existing style values when possible, so that GE contributions do not erase default styles, like even/odd row coloring. refs #830 Change-Id: Ibafd6ed72a1b2db62edb540bbfdefd2df8fa3200 --- diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEStyler.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEStyler.java index c9c37ab50..2f2b5fa74 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEStyler.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEStyler.java @@ -3,6 +3,7 @@ package org.simantics.browsing.ui.nattable; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes; import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry; @@ -14,6 +15,7 @@ import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes; import org.eclipse.nebula.widgets.nattable.style.ConfigAttribute; import org.eclipse.nebula.widgets.nattable.style.DisplayMode; import org.eclipse.nebula.widgets.nattable.style.IDisplayModeOrdering; +import org.eclipse.nebula.widgets.nattable.style.IStyle; import org.eclipse.nebula.widgets.nattable.style.Style; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; @@ -35,19 +37,75 @@ public class GEStyler extends CellPainterWrapper{ wrapper.clear(); wrapper.wrappedRegistry = configRegistry; TreeNode node = treeData.getDataAtIndex(cell.getRowIndex()); - Style style = new Style(); + PubStyle style = new PubStyle(); node.getStyle(cell.getColumnIndex(), style); Image image = node.getImage(cell.getColumnIndex()); if (image != null) style.setAttributeValue(CellStyleAttributes.IMAGE, image); - wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "BODY", style); -// wrapper.setSpecificConfigAttribute(CellStyleAttributes.FOREGROUND_COLOR, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR)); -// wrapper.setSpecificConfigAttribute(CellStyleAttributes.BACKGROUND_COLOR, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR)); -// wrapper.setSpecificConfigAttribute(CellStyleAttributes.FONT, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.FONT)); + { + // Attempt to reuse existing style, so that we override only those parts that GE contributions define. + Style defStyle = getStyle("BODY"); + style.applyTo(defStyle); + wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "BODY", defStyle); + } + if (configRegistry.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "ODD_BODY") != null) { + Style oddStyle = getStyle("ODD_BODY"); + style.applyTo(oddStyle); + wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "ODD_BODY", oddStyle); + } + if (configRegistry.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "EVEN_BODY") != null) { + Style evenStyle = getStyle("EVEN_BODY"); + style.applyTo(evenStyle); + wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "EVEN_BODY", evenStyle); + } super.paintCell(cell, gc, rectangle, wrapper); } + private static class PubStyle implements IStyle { + Map, Object> styleAttributeValueMap = new HashMap, Object>(); + + @Override + @SuppressWarnings("unchecked") + public T getAttributeValue(ConfigAttribute styleAttribute) { + return (T) this.styleAttributeValueMap.get(styleAttribute); + } + + @Override + public void setAttributeValue(ConfigAttribute styleAttribute, T value) { + this.styleAttributeValueMap.put(styleAttribute, value); + } + + @Override + public String toString() { + StringBuilder resultBuilder = new StringBuilder(); + resultBuilder.append(this.getClass().getSimpleName() + ": "); //$NON-NLS-1$ + + for (Entry, Object> entry : this.styleAttributeValueMap.entrySet()) { + resultBuilder.append(entry.getKey() + + ": " + entry.getValue() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ + } + + return resultBuilder.toString(); + } + + public void applyTo(IStyle style) { + for (Entry, Object> e : styleAttributeValueMap.entrySet()) { + style.setAttributeValue((ConfigAttribute)e.getKey(), e.getValue()); + } + } + + } + + private Style getStyle(String configLabel) { + Style s = (Style)wrapper.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, configLabel); + if (s == null) + s = new Style(); + else + s = s.clone(); + return s; + } + private class ConfigRegistryWrapper extends ConfigRegistry { IConfigRegistry wrappedRegistry; Map, Map>> configRegistry = new HashMap, Map>>(); diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java index 529d02f49..f82ce3a6f 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java @@ -12,7 +12,7 @@ import org.eclipse.jface.resource.ColorDescriptor; import org.eclipse.jface.resource.FontDescriptor; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes; -import org.eclipse.nebula.widgets.nattable.style.Style; +import org.eclipse.nebula.widgets.nattable.style.IStyle; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Image; @@ -190,7 +190,7 @@ public class TreeNode implements IAdaptable { } } - public void getStyle(int column, Style style) { + public void getStyle(int column, IStyle style) { String key = explorerContext.getGe().getColumns()[column].getKey(); FontDescriptor font = explorerContext.getGe().originalFont; ColorDescriptor bg = explorerContext.getGe().originalBackground; @@ -224,12 +224,12 @@ public class TreeNode implements IAdaptable { } if (bg != explorerContext.getGe().originalBackground) style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR,(Color) explorerContext.getGe().localResourceManager.get(bg)); - else - style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR,(Color) (explorerContext.getGe().originalBackground != null ? explorerContext.getGe().localResourceManager.get(explorerContext.getGe().originalBackground) : null)); + else if (explorerContext.getGe().originalBackground != null) + style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR,(Color) (explorerContext.getGe().localResourceManager.get(explorerContext.getGe().originalBackground))); if (fg != explorerContext.getGe().originalForeground) style.setAttributeValue(CellStyleAttributes.FOREGROUND_COLOR,(Color) explorerContext.getGe().localResourceManager.get(fg)); - else - style.setAttributeValue(CellStyleAttributes.FOREGROUND_COLOR,(Color) (explorerContext.getGe().originalForeground != null ? explorerContext.getGe().localResourceManager.get(explorerContext.getGe().originalForeground) : null)); + else if (explorerContext.getGe().originalForeground != null) + style.setAttributeValue(CellStyleAttributes.FOREGROUND_COLOR,(Color) (explorerContext.getGe().localResourceManager.get(explorerContext.getGe().originalForeground))); }