]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.browsing.ui.nattable;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Map.Entry;
7
8 import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
9 import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
10 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
11 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
12 import org.eclipse.nebula.widgets.nattable.painter.cell.CellPainterWrapper;
13 import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
14 import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
15 import org.eclipse.nebula.widgets.nattable.style.ConfigAttribute;
16 import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
17 import org.eclipse.nebula.widgets.nattable.style.IDisplayModeOrdering;
18 import org.eclipse.nebula.widgets.nattable.style.IStyle;
19 import org.eclipse.nebula.widgets.nattable.style.Style;
20 import org.eclipse.swt.graphics.GC;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Rectangle;
23
24 public class GEStyler extends CellPainterWrapper{
25                 
26         private GETreeData treeData;
27         
28         public GEStyler(GETreeData treeData, ICellPainter painter) {
29                 super(painter);
30                 this.treeData = treeData;
31         }
32         
33         private ConfigRegistryWrapper wrapper = new ConfigRegistryWrapper();
34
35         @Override
36         public void paintCell(ILayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry) {
37                 wrapper.clear();
38                 wrapper.wrappedRegistry = configRegistry;
39                 TreeNode node = treeData.getDataAtIndex(cell.getRowIndex());
40                 PubStyle style = new PubStyle();
41                 node.getStyle(cell.getColumnIndex(), style);
42                 Image image = node.getImage(cell.getColumnIndex());
43                 if (image != null)
44                         style.setAttributeValue(CellStyleAttributes.IMAGE, image);
45                 
46                 {
47                         // Attempt to reuse existing style, so that we override only those parts that GE contributions define.
48                         Style defStyle = getStyle("BODY");
49                         style.applyTo(defStyle);
50                         wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "BODY", defStyle);
51                 }
52                 if (configRegistry.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "ODD_BODY") != null) {
53                         Style oddStyle = getStyle("ODD_BODY");
54                         style.applyTo(oddStyle);
55                         wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "ODD_BODY", oddStyle);
56                 }
57                 if (configRegistry.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "EVEN_BODY") != null) {
58                         Style evenStyle = getStyle("EVEN_BODY");
59                         style.applyTo(evenStyle);
60                         wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "EVEN_BODY", evenStyle);
61                 }
62                 super.paintCell(cell, gc, rectangle, wrapper);
63         }
64         
65         private static class PubStyle implements IStyle {
66                 Map<ConfigAttribute<?>, Object> styleAttributeValueMap = new HashMap<ConfigAttribute<?>, Object>();
67
68             @Override
69             @SuppressWarnings("unchecked")
70             public <T> T getAttributeValue(ConfigAttribute<T> styleAttribute) {
71                 return (T) this.styleAttributeValueMap.get(styleAttribute);
72             }
73
74             @Override
75             public <T> void setAttributeValue(ConfigAttribute<T> styleAttribute, T value) {
76                 this.styleAttributeValueMap.put(styleAttribute, value);
77             }
78
79             @Override
80             public String toString() {
81                 StringBuilder resultBuilder = new StringBuilder();
82                 resultBuilder.append(this.getClass().getSimpleName() + ": "); //$NON-NLS-1$
83
84                 for (Entry<ConfigAttribute<?>, Object> entry : this.styleAttributeValueMap.entrySet()) {
85                     resultBuilder.append(entry.getKey()
86                             + ": " + entry.getValue() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
87                 }
88
89                 return resultBuilder.toString();
90             }
91             
92             public void applyTo(IStyle style) {
93                 for (Entry<ConfigAttribute<?>, Object> e : styleAttributeValueMap.entrySet()) {
94                         style.setAttributeValue((ConfigAttribute)e.getKey(), e.getValue());
95                 }
96             }
97                 
98         }
99         
100         private Style getStyle(String configLabel)  {
101                 Style s  = (Style)wrapper.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, configLabel);
102                 if (s == null)
103                         s = new Style();
104                 else 
105                         s = s.clone();
106                 return s;
107         }
108         
109         private class ConfigRegistryWrapper extends ConfigRegistry {
110                 IConfigRegistry wrappedRegistry;
111                  Map<ConfigAttribute<?>, Map<String, Map<String, ?>>> configRegistry = new HashMap<ConfigAttribute<?>, Map<String, Map<String, ?>>>();
112                 
113                 public void clear() {
114                         configRegistry.clear();
115                 }
116
117                 @Override
118                 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
119                                 String... configLabels) {
120                         return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
121                 }
122
123                 @Override
124                 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
125                                 List<String> configLabels) {
126                         return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
127                 }
128
129                 @Override
130                 public <T> T getSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
131                                 String configLabel) {
132                         T value = _getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
133                         if (value != null)
134                                 return value;
135                         return wrappedRegistry.getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
136                 }
137                 
138                  public <T> T _getSpecificConfigAttribute(ConfigAttribute<T> configAttribute,
139                             String displayMode, String configLabel) {
140                         T attributeValue = null;
141
142                         Map<String, Map<String, ?>> displayModeConfigAttributeMap = this.configRegistry
143                                 .get(configAttribute);
144                         if (displayModeConfigAttributeMap != null) {
145                             Map<String, T> configAttributeMap = (Map<String, T>) displayModeConfigAttributeMap.get(displayMode);
146                             if (configAttributeMap != null) {
147                                 attributeValue = configAttributeMap.get(configLabel);
148                                 if (attributeValue != null) {
149                                     return attributeValue;
150                                 }
151                             }
152                         }
153
154                         return attributeValue;
155                     }
156                 
157                 public <T> void setSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
158                                 String configLabel, T attributeValue) {
159                         Map<String, Map<String, ?>> displayModeConfigAttributeMap = this.configRegistry
160                         .get(configAttribute);
161                 if (displayModeConfigAttributeMap == null) {
162                     displayModeConfigAttributeMap = new HashMap<String, Map<String, ?>>();
163                     this.configRegistry.put(configAttribute, displayModeConfigAttributeMap);
164                 }
165
166                 Map<String, T> configAttributeMap = (Map<String, T>) displayModeConfigAttributeMap.get(displayMode);
167                 if (configAttributeMap == null) {
168                     configAttributeMap = new HashMap<String, T>();
169                     displayModeConfigAttributeMap.put(displayMode, configAttributeMap);
170                 }
171
172                 configAttributeMap.put(configLabel, attributeValue);
173                 }
174
175                 @Override
176                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue) {
177                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue);
178                         
179                 }
180
181                 @Override
182                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
183                                 String targetDisplayMode) {
184                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode);
185                         
186                 }
187
188                 @Override
189                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
190                                 String targetDisplayMode, String configLabel) {
191                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode, configLabel);
192                         
193                 }
194
195                 @Override
196                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType) {
197                         wrappedRegistry.unregisterConfigAttribute(configAttributeType);
198                         
199                 }
200
201                 @Override
202                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode) {
203                         wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode);
204                         
205                 }
206
207                 @Override
208                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode,
209                                 String configLabel) {
210                         wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode, configLabel);
211                 }
212
213                 @Override
214                 public IDisplayModeOrdering getDisplayModeOrdering() {
215                         return wrappedRegistry.getDisplayModeOrdering();
216                 }
217                 
218         }
219 }