1 package org.simantics.spreadsheet.synchronization;
3 import java.util.Collection;
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;
18 public class StyleUpdater extends ModuleUpdaterBase<SheetLineComponent> {
20 private static class ForegroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
22 public String getPropertyName() {
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;
34 private static class BackgroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
36 public String getPropertyName() {
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;
48 private static class FontUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
50 public String getPropertyName() {
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();
62 private static class AlignUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
64 public String getPropertyName() {
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;
76 private static class FormatStringUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
78 public String getPropertyName() {
79 return "formatString";
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;
90 private static class FormatIndexUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
92 public String getPropertyName() {
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;
104 private static class BorderUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
106 public String getPropertyName() {
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;
118 private static class LockedUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
120 public String getPropertyName() {
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;
132 private static class ColumnSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
134 public String getPropertyName() {
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;
146 private static class RowSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
148 public String getPropertyName() {
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;
161 public StyleUpdater(String moduleType) {
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());
177 public CommandBuilder createAddCommandBuilder(final String name) {
178 return new StyleCommandBuilder(name, false);
182 public CommandBuilder createUpdateCommandBuilder(String name) {
183 return new StyleCommandBuilder(name, true);
186 private static class StyleCommandBuilder implements CommandBuilder {
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;
201 public StyleCommandBuilder(String name, boolean update) {
202 this.name = name.split("/")[1];
206 public void apply(Solver solver) {
207 SpreadsheetBook book = solver.getConcreteSolver();
208 SpreadsheetStyle style = SpreadsheetStyle.newInstace().name(name).align(align).font(font)
209 .background(background).foreground(foreground).border(border).locked(locked).rowSpan(rowSpan)
210 .columnSpan(columnSpan).formatIndex((short) formatIndex).formatString(formatString).build();
211 if (book.getStyle(style.getStyleId()) == null)
212 book.addStyle(style);
216 @SuppressWarnings("unchecked")
218 public <T> T getConcrete() {
223 public String toString() {
224 StringBuilder sb = new StringBuilder();
225 sb.append("StyleCommandBuilder for ").append(name).append(" [background=").append(background).append(", foregroung=").append(foreground).append(", font=").append(font).append(", align=").append(align).append("]");
226 return sb.toString();