1 package org.simantics.spreadsheet.graph.synchronization;
\r
3 import java.util.Collection;
\r
4 import java.util.Map;
\r
6 import org.simantics.databoard.binding.mutable.Variant;
\r
7 import org.simantics.datatypes.literal.Font;
\r
8 import org.simantics.datatypes.literal.RGB;
\r
9 import org.simantics.spreadsheet.graph.SpreadsheetBook;
\r
10 import org.simantics.spreadsheet.graph.SpreadsheetStyle;
\r
11 import org.simantics.structural.synchronization.base.CommandBuilder;
\r
12 import org.simantics.structural.synchronization.base.ModuleUpdateContext;
\r
13 import org.simantics.structural.synchronization.base.ModuleUpdaterBase;
\r
14 import org.simantics.structural.synchronization.base.PropertyUpdateRule;
\r
15 import org.simantics.structural.synchronization.base.Solver;
\r
17 public class StyleUpdater extends ModuleUpdaterBase<SheetLineComponent> {
\r
19 private static class ForegroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
21 public String getPropertyName() {
\r
22 return "foreground";
\r
26 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
27 StyleCommandBuilder builder = context.getConcreteCommand();
\r
28 RGB.Integer color = (RGB.Integer) value.getValue();
\r
29 builder.foreground = color;
\r
33 private static class BackgroundUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
35 public String getPropertyName() {
\r
36 return "background";
\r
40 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
41 StyleCommandBuilder builder = context.getConcreteCommand();
\r
42 RGB.Integer color = (RGB.Integer) value.getValue();
\r
43 builder.background = color;
\r
47 private static class FontUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
49 public String getPropertyName() {
\r
54 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
55 StyleCommandBuilder builder = context.getConcreteCommand();
\r
56 Font font = (Font) value.getValue();
\r
57 builder.font = font;
\r
61 private static class AlignUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
63 public String getPropertyName() {
\r
68 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
69 StyleCommandBuilder builder = context.getConcreteCommand();
\r
70 int align = (int) value.getValue();
\r
71 builder.align = align;
\r
75 private static class FormatStringUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
77 public String getPropertyName() {
\r
78 return "formatString";
\r
82 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
83 StyleCommandBuilder builder = context.getConcreteCommand();
\r
84 String formatString = (String) value.getValue();
\r
85 builder.formatString = formatString;
\r
89 private static class FormatIndexUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
91 public String getPropertyName() {
\r
92 return "formatIndex";
\r
96 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
97 StyleCommandBuilder builder = context.getConcreteCommand();
\r
98 int index = (int) value.getValue();
\r
99 builder.formatIndex = index;
\r
103 private static class BorderUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
105 public String getPropertyName() {
\r
110 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
111 StyleCommandBuilder builder = context.getConcreteCommand();
\r
112 int border = (int) value.getValue();
\r
113 builder.border = border;
\r
117 private static class LockedUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
119 public String getPropertyName() {
\r
124 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
125 StyleCommandBuilder builder = context.getConcreteCommand();
\r
126 boolean locked = (boolean) value.getValue();
\r
127 builder.locked = locked;
\r
131 private static class ColumnSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
133 public String getPropertyName() {
\r
134 return "columnSpan";
\r
138 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
139 StyleCommandBuilder builder = context.getConcreteCommand();
\r
140 int columnSpan = (int) value.getValue();
\r
141 builder.columnSpan = columnSpan;
\r
145 private static class RowSpanUpdateRule implements PropertyUpdateRule<SheetLineComponent> {
\r
147 public String getPropertyName() {
\r
152 public void apply(ModuleUpdateContext<SheetLineComponent> context, boolean isCreating, Map<String, Variant> propertyMap, Map<String, Collection<String>> connectionMap, Variant value) {
\r
153 StyleCommandBuilder builder = context.getConcreteCommand();
\r
154 int rowSpan = (int) value.getValue();
\r
155 builder.rowSpan = rowSpan;
\r
160 public StyleUpdater(String moduleType) {
\r
163 addPropertyUpdateRule(new ForegroundUpdateRule());
\r
164 addPropertyUpdateRule(new BackgroundUpdateRule());
\r
165 addPropertyUpdateRule(new FontUpdateRule());
\r
166 addPropertyUpdateRule(new AlignUpdateRule());
\r
167 addPropertyUpdateRule(new FormatStringUpdateRule());
\r
168 addPropertyUpdateRule(new FormatIndexUpdateRule());
\r
169 addPropertyUpdateRule(new BorderUpdateRule());
\r
170 addPropertyUpdateRule(new LockedUpdateRule());
\r
171 addPropertyUpdateRule(new ColumnSpanUpdateRule());
\r
172 addPropertyUpdateRule(new RowSpanUpdateRule());
\r
176 public CommandBuilder createAddCommandBuilder(final String name) {
\r
177 return new StyleCommandBuilder(name, false);
\r
181 public CommandBuilder createUpdateCommandBuilder(String name) {
\r
182 return new StyleCommandBuilder(name, true);
\r
185 private static class StyleCommandBuilder implements CommandBuilder {
\r
187 protected int rowSpan;
\r
188 protected int columnSpan;
\r
189 protected int border;
\r
190 protected boolean locked;
\r
191 protected int formatIndex;
\r
192 protected String formatString;
\r
193 protected org.simantics.datatypes.literal.RGB.Integer background;
\r
194 protected org.simantics.datatypes.literal.RGB.Integer foreground;
\r
195 protected Font font;
\r
196 protected int align;
\r
198 private boolean update;
\r
199 private String name;
\r
201 public StyleCommandBuilder(String name, boolean update) {
\r
202 this.name = name.split("/")[1];
\r
203 this.update = update;
\r
207 public void apply(Solver solver) {
\r
208 SpreadsheetBook book = solver.getConcreteSolver();
\r
209 SpreadsheetStyle style = SpreadsheetStyle.newInstace().name(name).align(align).font(font)
\r
210 .background(background).foreground(foreground).border(border).locked(locked).rowSpan(rowSpan)
\r
211 .columnSpan(columnSpan).formatIndex((short) formatIndex).formatString(formatString).build();
\r
212 if (book.getStyle(style.getStyleId()) == null)
\r
213 book.addStyle(style);
\r
217 @SuppressWarnings("unchecked")
\r
219 public <T> T getConcrete() {
\r
224 public String toString() {
\r
225 StringBuilder sb = new StringBuilder();
\r
226 sb.append("StyleCommandBuilder for ").append(name).append(" [background=").append(background).append(", foregroung=").append(foreground).append(", font=").append(font).append(", align=").append(align).append("]");
\r
227 return sb.toString();
\r