NatTable based Graph Explorer styling fixes
[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
7 import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
8 import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
9 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
10 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
11 import org.eclipse.nebula.widgets.nattable.painter.cell.CellPainterWrapper;
12 import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
13 import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
14 import org.eclipse.nebula.widgets.nattable.style.ConfigAttribute;
15 import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
16 import org.eclipse.nebula.widgets.nattable.style.IDisplayModeOrdering;
17 import org.eclipse.nebula.widgets.nattable.style.Style;
18 import org.eclipse.swt.graphics.GC;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.Rectangle;
21
22 public class GEStyler extends CellPainterWrapper{
23                 
24         private GETreeData treeData;
25         
26         public GEStyler(GETreeData treeData, ICellPainter painter) {
27                 super(painter);
28                 this.treeData = treeData;
29         }
30         
31         private ConfigRegistryWrapper wrapper = new ConfigRegistryWrapper();
32
33         @Override
34         public void paintCell(ILayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry) {
35                 wrapper.clear();
36                 wrapper.wrappedRegistry = configRegistry;
37                 TreeNode node = treeData.getDataAtIndex(cell.getRowIndex());
38                 Style style = new Style();
39                 node.getStyle(cell.getColumnIndex(), style);
40                 Image image = node.getImage(cell.getColumnIndex());
41                 if (image != null)
42                         style.setAttributeValue(CellStyleAttributes.IMAGE, image);
43                 
44                 wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, "BODY", style);
45                 wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.SELECT, "BODY", style);
46                 wrapper.setSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.SELECT_HOVER, "BODY", style);
47 //              wrapper.setSpecificConfigAttribute(CellStyleAttributes.FOREGROUND_COLOR, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR));
48 //              wrapper.setSpecificConfigAttribute(CellStyleAttributes.BACKGROUND_COLOR, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR));
49 //              wrapper.setSpecificConfigAttribute(CellStyleAttributes.FONT, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.FONT));
50                 super.paintCell(cell, gc, rectangle, wrapper);
51         }
52         
53         private class ConfigRegistryWrapper extends ConfigRegistry {
54                 IConfigRegistry wrappedRegistry;
55                  Map<ConfigAttribute<?>, Map<String, Map<String, ?>>> configRegistry = new HashMap<ConfigAttribute<?>, Map<String, Map<String, ?>>>();
56                 
57                 public void clear() {
58                         configRegistry.clear();
59                 }
60
61                 @Override
62                 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
63                                 String... configLabels) {
64                         return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
65                 }
66
67                 @Override
68                 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
69                                 List<String> configLabels) {
70                         return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
71                 }
72
73                 @Override
74                 public <T> T getSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
75                                 String configLabel) {
76                         T value = _getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
77                         if (value != null)
78                                 return value;
79                         return wrappedRegistry.getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
80                 }
81                 
82                  public <T> T _getSpecificConfigAttribute(ConfigAttribute<T> configAttribute,
83                             String displayMode, String configLabel) {
84                         T attributeValue = null;
85
86                         Map<String, Map<String, ?>> displayModeConfigAttributeMap = this.configRegistry
87                                 .get(configAttribute);
88                         if (displayModeConfigAttributeMap != null) {
89                             Map<String, T> configAttributeMap = (Map<String, T>) displayModeConfigAttributeMap.get(displayMode);
90                             if (configAttributeMap != null) {
91                                 attributeValue = configAttributeMap.get(configLabel);
92                                 if (attributeValue != null) {
93                                     return attributeValue;
94                                 }
95                             }
96                         }
97
98                         return attributeValue;
99                     }
100                 
101                 public <T> void setSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
102                                 String configLabel, T attributeValue) {
103                         Map<String, Map<String, ?>> displayModeConfigAttributeMap = this.configRegistry
104                         .get(configAttribute);
105                 if (displayModeConfigAttributeMap == null) {
106                     displayModeConfigAttributeMap = new HashMap<String, Map<String, ?>>();
107                     this.configRegistry.put(configAttribute, displayModeConfigAttributeMap);
108                 }
109
110                 Map<String, T> configAttributeMap = (Map<String, T>) displayModeConfigAttributeMap.get(displayMode);
111                 if (configAttributeMap == null) {
112                     configAttributeMap = new HashMap<String, T>();
113                     displayModeConfigAttributeMap.put(displayMode, configAttributeMap);
114                 }
115
116                 configAttributeMap.put(configLabel, attributeValue);
117                 }
118
119                 @Override
120                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue) {
121                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue);
122                         
123                 }
124
125                 @Override
126                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
127                                 String targetDisplayMode) {
128                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode);
129                         
130                 }
131
132                 @Override
133                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
134                                 String targetDisplayMode, String configLabel) {
135                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode, configLabel);
136                         
137                 }
138
139                 @Override
140                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType) {
141                         wrappedRegistry.unregisterConfigAttribute(configAttributeType);
142                         
143                 }
144
145                 @Override
146                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode) {
147                         wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode);
148                         
149                 }
150
151                 @Override
152                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode,
153                                 String configLabel) {
154                         wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode, configLabel);
155                 }
156
157                 @Override
158                 public IDisplayModeOrdering getDisplayModeOrdering() {
159                         return wrappedRegistry.getDisplayModeOrdering();
160                 }
161                 
162         }
163 }