]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/synchronization/StyleUpdater.java
de5b47cc829b0edd68a35e3a63958d7f6a19d311
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / synchronization / StyleUpdater.java
1 package org.simantics.spreadsheet.graph.synchronization;
2
3 import java.util.Collection;
4 import java.util.Map;
5
6 import org.simantics.databoard.binding.mutable.Variant;
7 import org.simantics.datatypes.literal.Font;
8 import org.simantics.datatypes.literal.RGB;
9 import org.simantics.spreadsheet.graph.SpreadsheetBook;
10 import org.simantics.spreadsheet.graph.SpreadsheetStyle;
11 import org.simantics.structural.synchronization.base.CommandBuilder;
12 import org.simantics.structural.synchronization.base.ModuleUpdateContext;
13 import org.simantics.structural.synchronization.base.ModuleUpdaterBase;
14 import org.simantics.structural.synchronization.base.PropertyUpdateRule;
15 import org.simantics.structural.synchronization.base.Solver;
16
17 public class StyleUpdater extends ModuleUpdaterBase<SheetLineComponent> {
18
19     private static class ForegroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
20         @Override
21         public String getPropertyName() {
22             return "foreground";
23         }
24         
25         @Override
26         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
27             StyleCommandBuilder builder = context.getConcreteCommand();
28             RGB.Integer color = (RGB.Integer) value.getValue();
29             builder.foreground = color;
30         }
31     }
32     
33     private static class BackgroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
34         @Override
35         public String getPropertyName() {
36             return "background";
37         }
38
39         @Override
40         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
41             StyleCommandBuilder builder = context.getConcreteCommand();
42             RGB.Integer color = (RGB.Integer) value.getValue();
43             builder.background = color;
44         }
45     }
46     
47     private static class FontUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
48         @Override
49         public String getPropertyName() {
50             return "font";
51         }
52
53         @Override
54         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
55             StyleCommandBuilder builder = context.getConcreteCommand();
56             Font font = (Font) value.getValue();
57             builder.font = font;
58         }
59     }
60
61     private static class AlignUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
62         @Override
63         public String getPropertyName() {
64             return "align";
65         }
66
67         @Override
68         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
69             StyleCommandBuilder builder = context.getConcreteCommand();
70             int align = (int) value.getValue();
71             builder.align = align;
72         }
73     }
74     
75     private static class FormatStringUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
76         @Override
77         public String getPropertyName() {
78             return "formatString";
79         }
80
81         @Override
82         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
83             StyleCommandBuilder builder = context.getConcreteCommand();
84             String formatString = (String) value.getValue();
85             builder.formatString = formatString;
86         }
87     }
88
89     private static class FormatIndexUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
90         @Override
91         public String getPropertyName() {
92             return "formatIndex";
93         }
94
95         @Override
96         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
97             StyleCommandBuilder builder = context.getConcreteCommand();
98             int index = (int) value.getValue();
99             builder.formatIndex = index;
100         }
101     }
102
103     private static class BorderUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
104         @Override
105         public String getPropertyName() {
106             return "border";
107         }
108
109         @Override
110         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
111             StyleCommandBuilder builder = context.getConcreteCommand();
112             int border = (int) value.getValue();
113             builder.border = border;
114         }
115     }
116     
117     private static class LockedUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
118         @Override
119         public String getPropertyName() {
120             return "locked";
121         }
122
123         @Override
124         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
125             StyleCommandBuilder builder = context.getConcreteCommand();
126             boolean locked = (boolean) value.getValue();
127             builder.locked = locked;
128         }
129     }
130     
131     private static class ColumnSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
132         @Override
133         public String getPropertyName() {
134             return "columnSpan";
135         }
136
137         @Override
138         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
139             StyleCommandBuilder builder = context.getConcreteCommand();
140             int columnSpan = (int) value.getValue();
141             builder.columnSpan = columnSpan;
142         }
143     }
144     
145     private static class RowSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
146         @Override
147         public String getPropertyName() {
148             return "rowSpan";
149         }
150
151         @Override
152         public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
153             StyleCommandBuilder builder = context.getConcreteCommand();
154             int rowSpan = (int) value.getValue();
155             builder.rowSpan = rowSpan;
156         }
157     }
158
159     
160     public StyleUpdater(String moduleType) {
161         super(moduleType);
162         
163         addPropertyUpdateRule(new ForegroundUpdateRule());
164         addPropertyUpdateRule(new BackgroundUpdateRule());
165         addPropertyUpdateRule(new FontUpdateRule());
166         addPropertyUpdateRule(new AlignUpdateRule());
167         addPropertyUpdateRule(new FormatStringUpdateRule());
168         addPropertyUpdateRule(new FormatIndexUpdateRule());
169         addPropertyUpdateRule(new BorderUpdateRule());
170         addPropertyUpdateRule(new LockedUpdateRule());
171         addPropertyUpdateRule(new ColumnSpanUpdateRule());
172         addPropertyUpdateRule(new RowSpanUpdateRule());
173     }
174
175     @Override
176     public CommandBuilder createAddCommandBuilder(final String name) {
177         return new StyleCommandBuilder(name, false);
178     }
179
180     @Override
181     public CommandBuilder createUpdateCommandBuilder(String name) {
182         return new StyleCommandBuilder(name, true);
183     }
184     
185     private static class StyleCommandBuilder implements CommandBuilder {
186
187         protected int rowSpan;
188         protected int columnSpan;
189         protected int border;
190         protected boolean locked;
191         protected int formatIndex;
192         protected String formatString;
193         protected org.simantics.datatypes.literal.RGB.Integer background;
194         protected org.simantics.datatypes.literal.RGB.Integer foreground;
195         protected Font font;
196         protected int align;
197         
198         private boolean update;
199         private String name;
200         
201         public StyleCommandBuilder(String name, boolean update) {
202             this.name = name.split("/")[1];
203             this.update = update;
204         }
205         
206         @Override
207         public void apply(Solver solver) {
208             SpreadsheetBook book = solver.getConcreteSolver();
209             SpreadsheetStyle style = SpreadsheetStyle.newInstace().name(name).align(align).font(font)
210                     .background(background).foreground(foreground).border(border).locked(locked).rowSpan(rowSpan)
211                     .columnSpan(columnSpan).formatIndex((short) formatIndex).formatString(formatString).build();
212             if (book.getStyle(style.getStyleId()) == null)
213                 book.addStyle(style);
214             
215         }
216
217         @SuppressWarnings("unchecked")
218         @Override
219         public <T> T getConcrete() {
220             return (T) this;
221         }
222         
223         @Override
224         public String toString() {
225             StringBuilder sb = new StringBuilder();
226             sb.append("StyleCommandBuilder for ").append(name).append(" [background=").append(background).append(", foregroung=").append(foreground).append(", font=").append(font).append(", align=").append(align).append("]");
227             return sb.toString();
228         }
229         
230     }
231     
232 }