]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEStyler.java
NatTable based graph explorer omits background decoration.
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / GEStyler.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, ?>>>();