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