1 package org.simantics.browsing.ui.nattable;
3 import java.util.HashMap;
6 import java.util.Map.Entry;
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;
24 public class GEStyler extends CellPainterWrapper{
26 private GETreeData treeData;
28 public GEStyler(GETreeData treeData, ICellPainter painter) {
30 this.treeData = treeData;
33 private ConfigRegistryWrapper wrapper = new ConfigRegistryWrapper();
36 public void paintCell(ILayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry) {
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());
44 style.setAttributeValue(CellStyleAttributes.IMAGE, image);
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);
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);
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);
62 super.paintCell(cell, gc, rectangle, wrapper);
65 private static class PubStyle implements IStyle {
66 Map<ConfigAttribute<?>, Object> styleAttributeValueMap = new HashMap<ConfigAttribute<?>, Object>();
69 @SuppressWarnings("unchecked")
70 public <T> T getAttributeValue(ConfigAttribute<T> styleAttribute) {
71 return (T) this.styleAttributeValueMap.get(styleAttribute);
75 public <T> void setAttributeValue(ConfigAttribute<T> styleAttribute, T value) {
76 this.styleAttributeValueMap.put(styleAttribute, value);
80 public String toString() {
81 StringBuilder resultBuilder = new StringBuilder();
82 resultBuilder.append(this.getClass().getSimpleName() + ": "); //$NON-NLS-1$
84 for (Entry<ConfigAttribute<?>, Object> entry : this.styleAttributeValueMap.entrySet()) {
85 resultBuilder.append(entry.getKey()
86 + ": " + entry.getValue() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
89 return resultBuilder.toString();
92 public void applyTo(IStyle style) {
93 for (Entry<ConfigAttribute<?>, Object> e : styleAttributeValueMap.entrySet()) {
94 style.setAttributeValue((ConfigAttribute)e.getKey(), e.getValue());
100 private Style getStyle(String configLabel) {
101 Style s = (Style)wrapper.getSpecificConfigAttribute(CellConfigAttributes.CELL_STYLE, DisplayMode.NORMAL, configLabel);
109 private class ConfigRegistryWrapper extends ConfigRegistry {
110 IConfigRegistry wrappedRegistry;
111 Map<ConfigAttribute<?>, Map<String, Map<String, ?>>> configRegistry = new HashMap<ConfigAttribute<?>, Map<String, Map<String, ?>>>();
113 public void clear() {
114 configRegistry.clear();
118 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
119 String... configLabels) {
120 return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
124 public <T> T getConfigAttribute(ConfigAttribute<T> configAttribute, String targetDisplayMode,
125 List<String> configLabels) {
126 return wrappedRegistry.getConfigAttribute(configAttribute, targetDisplayMode, configLabels);
130 public <T> T getSpecificConfigAttribute(ConfigAttribute<T> configAttribute, String displayMode,
131 String configLabel) {
132 T value = _getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
135 return wrappedRegistry.getSpecificConfigAttribute(configAttribute, displayMode, configLabel);
138 public <T> T _getSpecificConfigAttribute(ConfigAttribute<T> configAttribute,
139 String displayMode, String configLabel) {
140 T attributeValue = null;
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;
154 return attributeValue;
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);
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);
172 configAttributeMap.put(configLabel, attributeValue);
176 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue) {
177 wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue);
182 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
183 String targetDisplayMode) {
184 wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode);
189 public <T> void registerConfigAttribute(ConfigAttribute<T> configAttribute, T attributeValue,
190 String targetDisplayMode, String configLabel) {
191 wrappedRegistry.registerConfigAttribute(configAttribute, attributeValue, targetDisplayMode, configLabel);
196 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType) {
197 wrappedRegistry.unregisterConfigAttribute(configAttributeType);
202 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode) {
203 wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode);
208 public <T> void unregisterConfigAttribute(ConfigAttribute<T> configAttributeType, String displayMode,
209 String configLabel) {
210 wrappedRegistry.unregisterConfigAttribute(configAttributeType, displayMode, configLabel);
214 public IDisplayModeOrdering getDisplayModeOrdering() {
215 return wrappedRegistry.getDisplayModeOrdering();