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;
66 public String getName() {
67 return Spreadsheets.cellName(line.row, column);
71 public Map<?, ?> getChildren() {
72 return Collections.emptyMap();
76 public Map<String, SheetNode> getProperties() {
80 public SpreadsheetBook getBook() {
81 return line.getEngine().getBook();
84 public SpreadsheetEngine getEngine() {
85 return line.getEngine();
88 public <T> T evaluate(SpreadsheetEvaluationEnvironment env) {
89 return evaluate(env, null);
92 @SuppressWarnings("unchecked")
93 public <T> T evaluate(SpreadsheetEvaluationEnvironment env, CellValueVisitor caller) {
95 caller.addReference(makeReferenceKey());
96 if(content instanceof SpreadsheetFormula) {
97 SpreadsheetFormula f = (SpreadsheetFormula)content;
98 if(f.result == null) {
99 CellValueVisitor visitor = new CellValueVisitor(env, this);
100 AstValue value = ((SpreadsheetFormula)content).value;
101 if(this.inProgress == true) this.iterations++;
103 if(!env.getBook().isIterationEnabled()){
104 if(this.inProgress == false){
105 this.inProgress = true;
106 f.result = value.accept(visitor);
108 else f.result = FormulaError2.CIRCULAR_REF.getString();
110 else if(this.iterations<env.iterationLimit){
111 this.inProgress = true;
112 f.result = value.accept(visitor);
118 env.getBook().registerReferences(makeReferenceKey(), visitor.getReferences());
120 this.inProgress = false;
123 } else if (content instanceof SpreadsheetSCLConstant) {
124 SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) content;
125 if(sclConstant.content instanceof Variant) {
126 Variant v = (Variant)sclConstant.content;
127 return (T) sclConstant.content;
128 } else if (sclConstant.content instanceof ExternalRef) {
129 return (T)env.getBook().getExternalRefValue(makeReferenceKey(), (ExternalRef)sclConstant.content);
131 throw new IllegalStateException();
134 this.inProgress = false;
139 public long makeReferenceKey() {
140 SpreadsheetBook book = getBook();
141 SpreadsheetEngine engine = getEngine();
142 long engineIndex = book.getEngineIndex(engine);
145 return (engineIndex << 40) + (row << 20) + col;
148 public void invalidate() {
149 getEngine().rangeCache = null;
150 if(content instanceof SpreadsheetFormula) {
151 SpreadsheetFormula f = (SpreadsheetFormula)content;
157 public void accept(SpreadsheetVisitor v) {
162 public Optional<SpreadsheetElement> getParent() {
163 return Optional.of(line);
167 public List<SpreadsheetElement> getSpreadsheetChildren() {
168 return Collections.emptyList();
172 public void remove(SpreadsheetElement child) {
173 // TODO Auto-generated method stub
178 public int hashCode() {
179 final int prime = 31;
181 result = prime * result + column;
182 result = prime * result + ((line == null) ? 0 : line.hashCode());
187 public boolean equals(Object obj) {
192 if (getClass() != obj.getClass())
194 SpreadsheetCell other = (SpreadsheetCell) obj;
195 if (column != other.column)
198 if (other.line != null)
200 } else if (!line.equals(other.line))
205 public void setStyle(int styleId) {
206 this.style = styleId;
209 public int getStyle() {
213 public Object getContent() {
217 public static SpreadsheetCell empty(SpreadsheetLine line, int column) {
218 SpreadsheetCell cell = new SpreadsheetCell(line, column);
220 cell.setStyle(SpreadsheetStyle.empty().getStyleId());