]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
NatTable based graph explorer omits background decoration. 12/4912/2
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 28 Apr 2022 10:18:40 +0000 (13:18 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 28 Apr 2022 10:35:00 +0000 (10:35 +0000)
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

bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEStyler.java
bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java

index c9c37ab50b5d98494894af1c6507ee669df254c5..2f2b5fa74b6fb17392acf68276982094519d6fa7 100644 (file)
@@ -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<ConfigAttribute<?>, Object> styleAttributeValueMap = new HashMap<ConfigAttribute<?>, Object>();
+
+           @Override
+           @SuppressWarnings("unchecked")
+           public <T> T getAttributeValue(ConfigAttribute<T> styleAttribute) {
+               return (T) this.styleAttributeValueMap.get(styleAttribute);
+           }
+
+           @Override
+           public <T> void setAttributeValue(ConfigAttribute<T> 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<ConfigAttribute<?>, 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<ConfigAttribute<?>, 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<ConfigAttribute<?>, Map<String, Map<String, ?>>> configRegistry = new HashMap<ConfigAttribute<?>, Map<String, Map<String, ?>>>();
index 529d02f4916c3745ae9e6a56de4b51c296d0f425..f82ce3a6f5182450bcd7b4b33b275d9459274d29 100644 (file)
@@ -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)));
 
        }