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;
38 private Object content;
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 Object c = sclConstant.getContent();
130 if(c instanceof Variant) {
131 Variant v = (Variant)c;
133 } else if (c instanceof ExternalRef) {
134 ExternalRefData erd = env.getBook().getExternalRefValue(makeReferenceKey(), (ExternalRef)c);
137 throw new IllegalStateException("Unsupported content " + c);
140 this.inProgress = false;
145 public long makeReferenceKey() {
146 SpreadsheetBook book = getBook();
147 SpreadsheetEngine engine = getEngine();
148 long engineIndex = book.getEngineIndex(engine);
151 return (engineIndex << 40) + (row << 20) + col;
154 public void invalidate() {
155 getEngine().rangeCache = null;
156 if(content instanceof SpreadsheetFormula) {
157 SpreadsheetFormula f = (SpreadsheetFormula)content;
163 public void accept(SpreadsheetVisitor v) {
168 public Optional<SpreadsheetElement> getParent() {
169 return Optional.of(line);
173 public List<SpreadsheetElement> getSpreadsheetChildren() {
174 return Collections.emptyList();
178 public void remove(SpreadsheetElement child) {
179 // TODO Auto-generated method stub
184 public int hashCode() {
185 final int prime = 31;
187 result = prime * result + column;
188 result = prime * result + ((line == null) ? 0 : line.hashCode());
193 public boolean equals(Object obj) {
198 if (getClass() != obj.getClass())
200 SpreadsheetCell other = (SpreadsheetCell) obj;
201 if (column != other.column)
204 if (other.line != null)
206 } else if (!line.equals(other.line))
211 public void setStyle(int styleId) {
212 this.style = styleId;
215 public int getStyle() {
219 public Object getContent() {
223 public static SpreadsheetCell empty(SpreadsheetLine line, int column) {
224 SpreadsheetCell cell = new SpreadsheetCell(line, column);
226 cell.setStyle(SpreadsheetStyle.empty().getStyleId());