1 package org.simantics.spreadsheet.graph;
3 import java.util.Collections;
4 import java.util.HashMap;
7 import java.util.Optional;
9 import org.simantics.spreadsheet.graph.formula.FormulaError2;
10 import org.simantics.spreadsheet.graph.formula.SpreadsheetEvaluationEnvironment;
11 import org.simantics.spreadsheet.graph.parser.ast.AstValue;
12 import org.simantics.spreadsheet.resource.SpreadsheetResource;
13 import org.simantics.spreadsheet.util.SpreadsheetUtils;
15 public class SpreadsheetCell implements SpreadsheetElement, SheetNode {
17 private static final long serialVersionUID = 6616793596542239339L;
19 public static SpreadsheetCell EMPTY;
22 EMPTY = new SpreadsheetCell(null, -1);
24 EMPTY.setStyle(SpreadsheetStyle.empty().getStyleId());
27 private boolean inProgress = false;
28 private int iterations = 0;
30 final private SpreadsheetLine line;
31 final private int column;
34 final private Map<String, SheetNode> properties;
36 public SpreadsheetCell(SpreadsheetLine line, int column) {
37 this.properties = createProperties();
42 //All SpreadsheetCells have these properties - create them when object is created
43 private Map<String, SheetNode> createProperties() {
44 Map<String, SheetNode> p = new HashMap<>();
45 p.put("typeURI", new SpreadsheetTypeNode(SpreadsheetResource.URIs.Cell));
46 p.put("content", new SpreadsheetCellContent(this));
47 p.put("style", new SpreadsheetCellStyle(this));
48 p.put("editable", new SpreadsheetCellEditable(this));
52 public boolean hasExpression() {
53 return content instanceof SpreadsheetFormula || content instanceof SpreadsheetSCLConstant;
56 public void setContent(Object newContent) {
57 // if(newContent != null) {
58 // if (!(newContent instanceof Serializable)) {
59 // throw new AssertionError("content not instanceof Serializable but it is " + newContent.getClass().getSimpleName());
63 System.out.println("SpreadsheetCell.setContent "+ this + " " + newContent);
64 this.content = newContent;
68 public String getName() {
69 return SpreadsheetUtils.cellName(line.row, column);
73 public Map<?, ?> getChildren() {
74 return Collections.emptyMap();
77 private static String[] keys = { "typeURI", "content" };
80 public Map<String, SheetNode> getProperties() {
82 System.out.println("SpreadsheetCell.getProperties: " + this + " " + content + " " + style);
83 return properties; //Return this SpreadsheetCells's properties, rather than a new HashMap
86 public SpreadsheetBook getBook() {
87 return line.getEngine().getBook();
90 public SpreadsheetEngine getEngine() {
91 return line.getEngine();
94 public <T> T evaluate(SpreadsheetEvaluationEnvironment env) {
95 return evaluate(env, null);
98 public <T> T evaluate(SpreadsheetEvaluationEnvironment env, CellValueVisitor caller) {
99 // System.err.println(getEngine().getName() + ":" + getName() + ": evaluate");
101 caller.addReference(makeReferenceKey());
102 if(content instanceof SpreadsheetFormula) {
103 SpreadsheetFormula f = (SpreadsheetFormula)content;
104 if(f.result == null) {
105 CellValueVisitor visitor = new CellValueVisitor(env, this);
106 AstValue value = ((SpreadsheetFormula)content).value;
107 if(this.inProgress == true) this.iterations++;
109 if(!env.getBook().isIterationEnabled()){
110 if(this.inProgress == false){
111 this.inProgress = true;
112 f.result = value.accept(visitor);
114 else f.result = FormulaError2.CIRCULAR_REF.getString();
116 else if(this.iterations<env.iterationLimit){
117 this.inProgress = true;
118 f.result = value.accept(visitor);
125 env.getBook().registerReferences(makeReferenceKey(), visitor.getReferences());
127 this.inProgress = false;
130 } else if (content instanceof SpreadsheetSCLConstant) {
131 SpreadsheetSCLConstant sclConstant = (SpreadsheetSCLConstant) content;
132 return (T) sclConstant.content;
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());