1 package org.simantics.spreadsheet.graph.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.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;
17 public class StyleUpdater extends ModuleUpdaterBase<SheetLineComponent> {
19 private static class ForegroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
21 public String getPropertyName() {
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;
33 private static class BackgroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
35 public String getPropertyName() {
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;
47 private static class FontUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
49 public String getPropertyName() {
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();
61 private static class AlignUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
63 public String getPropertyName() {
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;
75 private static class FormatStringUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
77 public String getPropertyName() {
78 return "formatString";
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;
89 private static class FormatIndexUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
91 public String getPropertyName() {
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;
103 private static class BorderUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
105 public String getPropertyName() {
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;
117 private static class LockedUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
119 public String getPropertyName() {
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;
131 private static class ColumnSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
133 public String getPropertyName() {
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;
145 private static class RowSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
147 public String getPropertyName() {
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;
160 public StyleUpdater(String moduleType) {
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());
176 public CommandBuilder createAddCommandBuilder(final String name) {
177 return new StyleCommandBuilder(name, false);
181 public CommandBuilder createUpdateCommandBuilder(String name) {
182 return new StyleCommandBuilder(name, true);
185 private static class StyleCommandBuilder implements CommandBuilder {
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;
198 private boolean update;
201 public StyleCommandBuilder(String name, boolean update) {
202 this.name = name.split("/")[1];
203 this.update = update;
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);
217 @SuppressWarnings("unchecked")
219 public <T> T getConcrete() {
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();