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