c9c37ab50b5d98494894af1c6507ee669df254c5
[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(CellStyleAttributes.FOREGROUND_COLOR, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR));
46 //              wrapper.setSpecificConfigAttribute(CellStyleAttributes.BACKGROUND_COLOR, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.BACKGROUND_COLOR));
47 //              wrapper.setSpecificConfigAttribute(CellStyleAttributes.FONT, DisplayMode.NORMAL, "BODY", style.getAttributeValue(CellStyleAttributes.FONT));
48                 super.paintCell(cell, gc, rectangle, wrapper);
49         }
50         
51         private class ConfigRegistryWrapper extends ConfigRegistry {
52                 IConfigRegistry wrappedRegistry;
53                  Map<ConfigAttribute<?>, Map<String, Map<String, ?>>> configRegistry = new HashMap<ConfigAttribute<?>, Map<String, Map<String, ?>>>();
54                 
55                 public void clear() {
56                         configRegistry.clear();
57                 }
58
59                 @Override
60                 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
61                                 String... configLabels) {
62                         return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
63                 }
64
65                 @Override
66                 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
67                                 List<String> configLabels) {
68                         return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
69                 }
70
71                 @Override
72                 public <T> T getSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
73                                 String configLabel) {
74                         T value = _getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
75                         if (value != null)
76                                 return value;
77                         return wrappedRegistry.getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
78                 }
79                 
80                  public <T> T _getSpecificConfigAttribute(ConfigAttribute<T> configAttribute,
81                             String displayMode, String configLabel) {
82                         T attributeValue = null;
83
84                         Map<String, Map<String, ?>> displayModeConfigAttributeMap = this.configRegistry
85                                 .get(configAttribute);
86                         if (displayModeConfigAttributeMap != null) {
87                             Map<String, T> configAttributeMap = (Map<String, T>) displayModeConfigAttributeMap.get(displayMode);
88                             if (configAttributeMap != null) {
89                                 attributeValue = configAttributeMap.get(configLabel);
90                                 if (attributeValue != null) {
91                                     return attributeValue;
92                                 }
93                             }
94                         }
95
96                         return attributeValue;
97                     }
98                 
99                 public <T> void setSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
100                                 String configLabel, T attributeValue) {
101                         Map<String, Map<String, ?>> displayModeConfigAttributeMap = this.configRegistry
102                         .get(configAttribute);
103                 if (displayModeConfigAttributeMap == null) {
104                     displayModeConfigAttributeMap = new HashMap<String, Map<String, ?>>();
105                     this.configRegistry.put(configAttribute, displayModeConfigAttributeMap);
106                 }
107
108                 Map<String, T> configAttributeMap = (Map<String, T>) displayModeConfigAttributeMap.get(displayMode);
109                 if (configAttributeMap == null) {
110                     configAttributeMap = new HashMap<String, T>();
111                     displayModeConfigAttributeMap.put(displayMode, configAttributeMap);
112                 }
113
114                 configAttributeMap.put(configLabel, attributeValue);
115                 }
116
117                 @Override
118                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue) {
119                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue);
120                         
121                 }
122
123                 @Override
124                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
125                                 String targetDisplayMode) {
126                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode);
127                         
128                 }
129
130                 @Override
131                 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
132                                 String targetDisplayMode, String configLabel) {
133                         wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode, configLabel);
134                         
135                 }
136
137                 @Override
138                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType) {
139                         wrappedRegistry.unregisterConfigAttribute(configAttributeType);
140                         
141                 }
142
143                 @Override
144                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode) {
145                         wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode);
146                         
147                 }
148
149                 @Override
150                 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode,
151                                 String configLabel) {
152                         wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode, configLabel);
153                 }
154
155                 @Override
156                 public IDisplayModeOrdering getDisplayModeOrdering() {
157                         return wrappedRegistry.getDisplayModeOrdering();
158                 }
159                 
160         }
161 }