1 package org.simantics.spreadsheet.solver;
3 import java.util.Collections;
4 import java.util.HashMap;
7 import java.util.Optional;
9 import org.simantics.databoard.binding.mutable.Variant;
10 import org.simantics.spreadsheet.ExternalRef;
11 import org.simantics.spreadsheet.SpreadsheetCellStyle;
12 import org.simantics.spreadsheet.SpreadsheetVisitor;
13 import org.simantics.spreadsheet.Spreadsheets;
14 import org.simantics.spreadsheet.solver.formula.CellValueVisitor;
15 import org.simantics.spreadsheet.solver.formula.FormulaError2;
16 import org.simantics.spreadsheet.solver.formula.SpreadsheetEvaluationEnvironment;
17 import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue;
19 @SuppressWarnings("rawtypes")
20 public class SpreadsheetCell implements SpreadsheetElement, SheetNode {
22 private static final long serialVersionUID = 6616793596542239339L;
24 public static SpreadsheetCell EMPTY;
27 EMPTY = new SpreadsheetCell(null, -1);
29 EMPTY.setStyle(SpreadsheetStyle.empty().getStyleId());
32 private boolean inProgress = false;
33 private int iterations = 0;
35 final private SpreadsheetLine line;
36 final private int column;
39 final private Map<String, SheetNode> properties;
41 public SpreadsheetCell(SpreadsheetLine line, int column) {
42 this.properties = createProperties();
47 //All SpreadsheetCells have these properties - create them when object is created
48 private Map<String, SheetNode> createProperties() {
49 Map<String, SheetNode> p = new HashMap<>();
50 p.put("typeURI", new SpreadsheetTypeNode(Spreadsheets.CELL_TYPE_URI));
51 p.put("content", new SpreadsheetCellContent(this));
52 p.put("style", new SpreadsheetCellStyle(this));
53 p.put("editable", new SpreadsheetCellEditable(this));
57 public boolean hasExpression() {
58 return content instanceof SpreadsheetFormula || content instanceof SpreadsheetSCLConstant;
61 public void setContent(Object newContent) {
62 this.content = newContent;
65 public int getColumn() {
70 public String getName() {
71 return Spreadsheets.cellName(line.row, column);
75 public Map<?, ?> getChildren() {
76 return Collections.emptyMap();
80 public Map<String, SheetNode> getProperties() {
84 public SpreadsheetBook getBook() {
85 return line.getEngine().getBook();
88 public SpreadsheetEngine getEngine() {
89 return line.getEngine();
92 public <T> T evaluate(SpreadsheetEvaluationEnvironment env) {
93 return evaluate(env, null);
96 @SuppressWarnings("unchecked")
97 public <T> T evaluate(SpreadsheetEvaluationEnvironment env, CellValueVisitor caller) {
99 caller.addReference(makeReferenceKey());
100 if(content instanceof SpreadsheetFormula) {
101 SpreadsheetFormula f = (SpreadsheetFormula)content;
102 if(f.result == null) {
103 CellValueVisitor visitor = new CellValueVisitor(env, this);
104 AstValue value = ((SpreadsheetFormula)content).value;
105 if(this.inProgress == true) this.iterations++;
107 if(!env.getBook().isIterationEnabled()){
108 if(this.inProgress == false){
109 this.inProgress = true;
110 f.result = value.accept(visitor);
112 else f.result = FormulaError2.CIRCULAR_REF.getString();
114 else if(this.iterations<env.iterationLimit){
115 this.inProgress = true;
116 f.result = value.accept(visitor);
122 env.getBook().registerReferences(makeReferenceKey(), visitor.getReferences());
124 this.inProgress = false;
127 } else if (content instanceof SpreadsheetSCLConstant) {
128 SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) content;
129 if(sclConstant.content instanceof Variant) {
130 Variant v = (Variant)sclConstant.content;
131 return (T) sclConstant.content;
132 } else if (sclConstant.content instanceof ExternalRef) {
133 return (T)env.getBook().getExternalRefValue(makeReferenceKey(), (ExternalRef)sclConstant.content);
135 throw new IllegalStateException();
138 this.inProgress = false;
143 public long makeReferenceKey() {
144 SpreadsheetBook book = getBook();
145 SpreadsheetEngine engine = getEngine();
146 long engineIndex = book.getEngineIndex(engine);
149 return (engineIndex << 40) + (row << 20) + col;
152 public void invalidate() {
153 getEngine().rangeCache = null;
154 if(content instanceof SpreadsheetFormula) {
155 SpreadsheetFormula f = (SpreadsheetFormula)content;
161 public void accept(SpreadsheetVisitor v) {
166 public Optional<SpreadsheetElement> getParent() {
167 return Optional.of(line);
171 public List<SpreadsheetElement> getSpreadsheetChildren() {
172 return Collections.emptyList();
176 public void remove(SpreadsheetElement child) {
177 // TODO Auto-generated method stub
182 public int hashCode() {
183 final int prime = 31;
185 result = prime * result + column;
186 result = prime * result + ((line == null) ? 0 : line.hashCode());
191 public boolean equals(Object obj) {
196 if (getClass() != obj.getClass())
198 SpreadsheetCell other = (SpreadsheetCell) obj;
199 if (column != other.column)
202 if (other.line != null)
204 } else if (!line.equals(other.line))
209 public void setStyle(int styleId) {
210 this.style = styleId;
213 public int getStyle() {
217 public Object getContent() {
221 public static SpreadsheetCell empty(SpreadsheetLine line, int column) {
222 SpreadsheetCell cell = new SpreadsheetCell(line, column);
224 cell.setStyle(SpreadsheetStyle.empty().getStyleId());