]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetEngine.java
285f6ce5e26edde167f59329e48713ca5a4cb01f
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / SpreadsheetEngine.java
1 package org.simantics.spreadsheet.graph;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Optional;
8
9 import org.simantics.spreadsheet.Range;
10 import org.simantics.spreadsheet.graph.parser.ast.AstRange;
11
12 public class SpreadsheetEngine implements SpreadsheetElement, SheetNode {
13
14         private static final long serialVersionUID = -5246063647558595642L;
15         
16         private static final String LINES = "Lines";
17         
18         private final SpreadsheetBook book;
19         private final String name;
20         private final int id;
21
22         public SpreadsheetLines lines;
23         
24         transient public Map<String,Object> rangeCache;
25
26         public Map<String,Object> getRangeCache() {
27                 if(rangeCache == null) rangeCache = new HashMap<String,Object>();
28                 return rangeCache;
29         }
30         
31         public Object getCachedRange(AstRange range) {
32                 if(range.sheetName != null) return null;
33                 return getRangeCache().get(range.first + ":" + range.second); 
34         }
35         
36         public void cacheRange(AstRange range, Object value) {
37                 if(range.sheetName != null) return;
38                 getRangeCache().put(range.first + ":" + range.second, value);
39         }
40
41         public SpreadsheetEngine(SpreadsheetBook book, String name) {
42                 this.book = book;
43                 this.name = name;
44                 this.id = book.getNewId(this);
45                 this.lines = new SpreadsheetLines(this, LINES);
46         }
47         
48         public SpreadsheetBook getBook() {
49                 return book;
50         }
51         
52         public int getId() {
53                 return id;
54         }
55         
56         Object resolve(String[] parts, int index) {
57                 
58                 String part = parts[index];
59                 if(!part.equals(LINES)) return null;
60                 
61                 if(index == parts.length-1) return lines;
62                 
63                 return lines.resolve(parts, index+1);
64
65         }
66         
67         @Override
68         public String getName() {
69                 return name;
70         }
71         
72         @Override
73         public Map<String, SheetNode> getChildren() {
74                 return Collections.singletonMap(LINES, lines);
75         }
76         
77         @Override
78         public Map<String, SheetNode> getProperties() {
79                 return Collections.emptyMap();
80         } 
81         
82         public Object ensureSubprocess(String[] path, int index) {
83                 
84                 String name = path[index];
85                 if(!LINES.equals(name)) throw new IllegalStateException();
86                 if(index == path.length - 1) return lines;
87                 return lines.ensureSubprocess(path, index+1);
88                 
89         }
90         
91         public SpreadsheetLine getLine(int row) {
92                 assert(lines.nodes.size() == 1);
93                 SpreadsheetLines root = lines.nodes.values().iterator().next();
94                 return root.getLine(row);
95         }
96
97         @Override
98         public void accept(SpreadsheetVisitor v) {
99                 v.visit(this);
100         }
101         
102         public Range actualRange(Range r) {
103                 if(r.isFullRows()) {
104                         SpreadsheetLines root = lines.nodes.values().iterator().next();
105                         Range result = new Range(r);
106                         result.startRow = 0;
107                         result.endRow = root.getMaxRow(); 
108                         return result;
109                 } else {
110                         return r;
111                 }
112         }
113
114     @Override
115     public Optional<SpreadsheetElement> getParent() {
116         return Optional.of(book);
117     }
118
119     @Override
120     public List<SpreadsheetElement> getSpreadsheetChildren() {
121         return Collections.singletonList(lines);
122     }
123
124     @Override
125     public void remove(SpreadsheetElement child) {
126         
127     }
128
129     @Override
130     public int hashCode() {
131         final int prime = 31;
132         int result = 1;
133         result = prime * result + ((book == null) ? 0 : book.hashCode());
134         result = prime * result + ((name == null) ? 0 : name.hashCode());
135         return result;
136     }
137
138     @Override
139     public boolean equals(Object obj) {
140         if (this == obj)
141             return true;
142         if (obj == null)
143             return false;
144         if (getClass() != obj.getClass())
145             return false;
146         SpreadsheetEngine other = (SpreadsheetEngine) obj;
147         if (book == null) {
148             if (other.book != null)
149                 return false;
150         } else if (!book.equals(other.book))
151             return false;
152         if (name == null) {
153             if (other.name != null)
154                 return false;
155         } else if (!name.equals(other.name))
156             return false;
157         return true;
158     }
159         
160         
161 }