]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java
Enhancements to district functionalities and code
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / CSVImportModel.java
1 package org.simantics.district.imports.ui;
2
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.commons.csv.CSVRecord;
11 import org.simantics.db.Resource;
12 import org.simantics.district.imports.DistrictImportUtils;
13 import org.simantics.district.imports.DistrictImportUtils.CSVHeader;
14
15 public class CSVImportModel {
16
17     // First page fills these
18     private Resource targetDiagram;
19     private Path source;
20     
21     // Second page fills these
22     private char delimiter;
23     private boolean read;
24     private Map<String, Character> delimiters;
25     private Path wktFile;
26     
27     // Vertex import
28     private int xCoordIndex = -1;
29     private int yCoordIndex = -1;
30     private int zCoordIndex = -1;
31     
32     // Edge import
33     private int startXCoordIndex = -1;
34     private int startYCoordIndex = -1;
35     private int startZCoordIndex = -1;
36     private int endXCoordIndex = -1;
37     private int endYCoordIndex = -1;
38     private int endZCoordIndex = -1;
39     
40     // common
41     private int componentMappingIndex = -1;
42     private Map<String, Resource> componentMappings = new HashMap<>();
43     private boolean isVertexImport;
44     
45     // Third page
46
47     public CSVImportModel() {
48         delimiters = DistrictImportUtils.getSupportedCSVDelimiterFormats();
49     }
50     
51     public void setSource(Path source) {
52         this.source = source;
53     }
54
55     public void setDelimiter(char delimiter) {
56         this.delimiter = delimiter;
57     }
58
59     public void setReadFirstAsHeader(boolean read) {
60         this.read = read;
61     }
62
63     public Path getSource() {
64         return source;
65     }
66
67     public List<CSVRecord> getRows(int amount) throws IOException {
68         if (source != null)
69             return DistrictImportUtils.readRows(source, delimiter, amount);
70         else
71             return Collections.emptyList();
72     }
73     
74     public Map<CSVHeader, List<String>> getHeaderAndRows(int amount) throws IOException {
75         if (source != null)
76             return DistrictImportUtils.readCSVHeaderAndRows(source, delimiter, read, amount);
77         else
78             return Collections.emptyMap();
79     }
80     
81     public Map<String, Integer> getHeader() throws IOException {
82         Map<String, Integer> header = null;
83         if (source != null)
84             header = DistrictImportUtils.readCSVHeader(source, delimiter, read);
85         if (header == null)
86             header = Collections.emptyMap();
87         return header;
88     }
89
90     public boolean getReadFirstAsHeader() {
91         return read;
92     }
93
94     public String[] getDelimiterFormats() {
95         return delimiters.keySet().toArray(new String[delimiters.size()]);
96     }
97
98     public void setDelimiterByLabel(String item) {
99         setDelimiter(delimiters.get(item));
100     }
101
102     public List<Map<String, String>> readRows(int amount) throws IOException {
103         if (source != null)
104             return DistrictImportUtils.readRows(source, delimiter, read, amount);
105         else
106             return Collections.emptyList();
107     }
108
109     public char getDelimiter() {
110         return delimiter;
111     }
112
113     public Path getWKTFile() {
114         return wktFile;
115     }
116
117     public int getXCoordIndex() {
118         return xCoordIndex;
119     }
120
121     public void setXCoordIndex(int xCoordIndex) {
122         this.xCoordIndex = xCoordIndex;
123     }
124
125     public int getYCoordIndex() {
126         return yCoordIndex;
127     }
128
129     public void setYCoordIndex(int yCoordIndex) {
130         this.yCoordIndex = yCoordIndex;
131     }
132
133     public int getZCoordIndex() {
134         return zCoordIndex;
135     }
136
137     public void setZCoordIndex(int zCoordIndex) {
138         this.zCoordIndex = zCoordIndex;
139     }
140
141     public int getComponentMappingIndex() {
142         return componentMappingIndex;
143     }
144     
145     public void setComponentMappingIndex(int componentMappingIndex) {
146         this.componentMappingIndex = componentMappingIndex;
147     }
148
149     public void setParentDiagram(Resource diagram) {
150         this.targetDiagram = diagram;
151     }
152
153     public Resource getParentDiagram() {
154         return targetDiagram;
155     }
156
157     public void setWKTFile(Path wktFile) {
158         this.wktFile = wktFile;
159     }
160
161     public void setComponentMappings(String value, Resource resource) {
162         componentMappings.put(value, resource);
163     }
164
165     public Map<String, Resource> getComponentMappings() {
166         return componentMappings;
167     }
168
169     public boolean isVertexImport() {
170         return isVertexImport;
171     }
172     
173     public void setVertexImport(boolean isVertexImport) {
174         this.isVertexImport = isVertexImport;
175     }
176
177     public void setStartXCoordIndex(int parseInt) {
178         this.startXCoordIndex = parseInt;
179     }
180     
181     public int getStartXCoordIndex() {
182         return startXCoordIndex;
183     }
184
185     public void setStartYCoordIndex(int parseInt) {
186         this.startYCoordIndex = parseInt;
187     }
188     
189     public int getStartYCoordIndex() {
190         return startYCoordIndex;
191     }
192
193     public void setStartZCoordIndex(int parseInt) {
194         this.startZCoordIndex = parseInt;
195     }
196     
197     public int getStartZCoordIndex() {
198         return startZCoordIndex;
199     }
200
201     public void setEndXCoordIndex(int parseInt) {
202         this.endXCoordIndex = parseInt;
203     }
204     
205     public int getEndXCoordIndex() {
206         return endXCoordIndex;
207     }
208
209     public void setEndYCoordIndex(int parseInt) {
210         this.endYCoordIndex = parseInt;
211     }
212     
213     public int getEndYCoordIndex() {
214         return endYCoordIndex;
215     }
216
217     public void setEndZCoordIndex(int parseInt) {
218         this.endZCoordIndex = parseInt;
219     }
220     
221     public int getEndZCoordIndex() {
222         return endZCoordIndex;
223     }
224
225 }