]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/SpreadsheetLine.java
SCL API for direct access to SpreadsheetBooks
[simantics/platform.git] / bundles / org.simantics.spreadsheet / src / org / simantics / spreadsheet / solver / SpreadsheetLine.java
1 package org.simantics.spreadsheet.solver;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Optional;
10 import java.util.function.Consumer;
11
12 import org.simantics.scl.runtime.function.Function1;
13 import org.simantics.scl.runtime.tuple.Tuple;
14 import org.simantics.spreadsheet.Range;
15 import org.simantics.spreadsheet.SpreadsheetVisitor;
16 import org.simantics.spreadsheet.Spreadsheets;
17
18 import it.unimi.dsi.fastutil.objects.ObjectArrayList;
19
20 @SuppressWarnings("rawtypes")
21 public class SpreadsheetLine implements SpreadsheetElement<SpreadsheetCell, SpreadsheetLines>, SheetNode {
22
23     private static final long serialVersionUID = -304574098117404663L;
24
25     final private SpreadsheetLines parent;
26     final public int row;
27     int id;
28
29     public ObjectArrayList<SpreadsheetCell> cells = new ObjectArrayList<>();
30
31     public SpreadsheetLine(SpreadsheetLines parent, int row) {
32         this.parent = parent;
33         this.row = row;
34         this.id = getEngine().getBook().getNewId(this);
35     }
36
37     public int getId() {
38         return id;
39     }
40
41     public SpreadsheetEngine getEngine() {
42         return ((SpreadsheetLines)parent).getEngine();
43     }
44     
45     public int getRow() {
46         return row;
47     }
48
49     public SpreadsheetLine possibleOffset(int offset) {
50         return getEngine().getLine(row+offset);
51     }
52     
53     public SpreadsheetCell cellAt(int column) {
54         int index = Collections.binarySearch(cells, new BinarySearch(column), new Comparator<BinarySearch>() {
55             @Override
56             public int compare(BinarySearch bs1, BinarySearch bs2) {
57                 return Integer.compare(bs1.column, bs2.column);
58             }
59         });
60         if(index >= 0)
61             return cells.get(index);
62         else
63             return null;
64     }
65
66     public String getLinesPath() {
67         return "/" + ((SpreadsheetLines)parent).getLinesPath() + "/" + getName();
68     }
69
70     public void forCells(Consumer<SpreadsheetCell> consumer, int min, int max) {
71         for(int i=min;i<cells.size() && i<max;i++) {
72             SpreadsheetCell cell = cells.get(i);
73             if(SpreadsheetCell.EMPTY == cell) continue;
74             consumer.accept(cell);
75         }
76     }
77
78     public List<SpreadsheetCell> getCells(int min, int max) {
79         ArrayList<SpreadsheetCell> result = new ArrayList<>();
80         forCells(cell -> result.add(cell), min, max);
81         return result;
82     }
83
84     @Override
85     public String getName() {
86         return "Row"+row;
87     }
88
89     @Override
90     public Map getChildren() {
91         String rowName = ""+row;
92         Map<String,SpreadsheetCell> result = new HashMap<>();
93         for(int i=0;i<cells.size();i++) {
94             SpreadsheetCell cell = cells.get(i);
95             if(SpreadsheetCell.EMPTY == cell) continue;
96             String name = "Row"+Spreadsheets.columnName(i) + rowName;
97             result.put(name, cell);
98         }
99         return result;
100     }
101
102     @Override
103     public Map getProperties() {
104         return Collections.singletonMap("typeURI", new SpreadsheetTypeNode(Spreadsheets.LINE_TYPE_URI));
105     }
106
107     Object resolve(String[] parts, int index) {
108
109         if(index == parts.length) return this;
110
111         Range r = Spreadsheets.decodeCellAbsolute(parts[index]);
112         return cells.get(r.startColumn);
113
114     }
115
116     public void accept(SpreadsheetVisitor v) {
117         v.visit(this);
118     }
119
120     public String getPath() {
121         return ((SpreadsheetLines)parent).getPath() + "/" + getName();
122     }
123
124     @Override
125     public Optional<SpreadsheetLines> getParent() {
126         return Optional.of(parent);
127     }
128
129     @Override
130     public List<SpreadsheetCell> getSpreadsheetChildren() {
131         return cells;
132     }
133
134     @Override
135     public void remove(SpreadsheetCell child) {
136
137     }
138
139     @Override
140     public int hashCode() {
141         final int prime = 31;
142         int result = 1;
143         result = prime * result + ((parent == null) ? 0 : parent.hashCode());
144         result = prime * result + row;
145         return result;
146     }
147
148     @Override
149     public boolean equals(Object obj) {
150         if (this == obj)
151             return true;
152         if (obj == null)
153             return false;
154         if (getClass() != obj.getClass())
155             return false;
156         SpreadsheetLine other = (SpreadsheetLine) obj;
157         if (parent == null) {
158             if (other.parent != null)
159                 return false;
160         } else if (!parent.equals(other.parent))
161             return false;
162         if (row != other.row)
163             return false;
164         return true;
165     }
166
167 }