]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java
d6b0c7d228bbf55554141b6cda434c426694718f
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / techtype / table / TechTypeTableDataProvider.java
1 package org.simantics.district.network.ui.techtype.table;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.stream.Collectors;
11
12 import org.apache.commons.csv.CSVRecord;
13 import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
14 import org.simantics.district.imports.DistrictImportUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class TechTypeTableDataProvider implements IDataProvider {
19
20     @SuppressWarnings("unused")
21     private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableDataProvider.class);
22
23     private List<CSVRecord> records = new ArrayList<>();
24     private List<CSVRecord> filteredRecords = new ArrayList<>();
25     private String filter = "";
26     private List<String> variables = null;
27     private List<String> headers = null;
28
29     public TechTypeTableDataProvider(String data) {
30         // load csv
31         setData(data);
32     }
33
34     public String getVariableName(int columnIndex) {
35         return variables != null && columnIndex < variables.size() ? variables.get(columnIndex) : null;
36     }
37
38     public int getVariableIndex(String variableName) {
39         return variables != null ? variables.indexOf(variableName) : -1;
40     }
41
42     public String getHeaderValue(int columnIndex) {
43         if (headers == null) {
44             return "<empty>";
45         }
46         return headers.get(columnIndex);
47     }
48
49     @Override
50     public Object getDataValue(int columnIndex, int rowIndex) {
51         return filteredRecords.get(rowIndex).get(columnIndex);
52     }
53
54     @Override
55     public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
56
57     }
58
59     @Override
60     public int getColumnCount() {
61         if (records.isEmpty()) {
62             return 0;
63         }
64         return records.get(0).size();
65     }
66
67     @Override
68     public int getRowCount() {
69         return filteredRecords.size();
70     }
71
72     public boolean isEditable(int columnIndex, int rowIndex) {
73         return false;
74     }
75
76     public void setFilter(String text) {
77         this.filter = text.toLowerCase();
78
79         filteredRecords = records.stream().filter(record -> {
80             for (int i = 0; i < record.size(); i++) {
81                 String columnContent = record.get(i);
82                 if (columnContent.toLowerCase().contains(filter)) {
83                     return true;
84                 }
85             }
86             return false;
87         }).collect(Collectors.toList());
88     }
89
90     /**
91      * Read a CSV file into table contents.
92      * 
93      * Set path to null to create an empty table.
94      * 
95      * @param path  The path of the CSV file to be read.
96      */
97     public void setPath(String path) {
98         records.clear();
99         filteredRecords.clear();
100         if (path != null) {
101             Path techTypeCsv = Paths.get(path);
102             try {
103                 DistrictImportUtils.consumeCSV(techTypeCsv, ';', false, record -> {
104                     records.add(record);
105                     return true;
106                 });
107             } catch (IOException e) {
108                 e.printStackTrace();
109             }
110         }
111
112         setFilter("");
113     }
114
115     /**
116      * Set table data contents to a given string of CSV data.
117      * 
118      * Set 'data' to null to create an empty table.
119      * 
120      * @param data  The CSV data to be shown in the table.
121      */
122     public void setData(String data) {
123         records.clear();
124         filteredRecords.clear();
125         if (data != null) {
126             long ncommas = data.chars().filter(c -> c == ',').count();
127             long nsemis = data.chars().filter(c -> c == ';').count();
128             char delim = nsemis > ncommas ? ';' : ',';
129             StringReader reader = new StringReader(data);
130             try {
131                 DistrictImportUtils.consumeCSV(reader, delim, false, record -> {
132                     records.add(record);
133                     return true;
134                 });
135             } catch (IOException e) {
136                 LOGGER.error("Error reading CSV file", e);
137                 return;
138             }
139
140             CSVRecord header = records.remove(0);
141             CSVRecord units = records.remove(0);
142
143             variables = new ArrayList<>();
144             headers = new ArrayList<>();
145
146             Iterator<String> it = header.iterator();
147             Iterator<String> uit = units.iterator();
148
149             while (it.hasNext()) {
150                 String variable = it.next().trim();
151                 String unit = uit.hasNext() ? uit.next().trim() : null;
152
153                 variables.add(variable);
154                 headers.add(variable + (unit != null && !unit.isEmpty() && !(unit.startsWith("(") && unit.endsWith(")")) ? " [" + unit + "]" : ""));
155             }
156         }
157
158         setFilter("");
159     }
160
161 }