1 package org.simantics.district.network.ui.techtype.table;
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;
10 import java.util.stream.Collectors;
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;
18 public class TechTypeTableDataProvider implements IDataProvider {
20 @SuppressWarnings("unused")
21 private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableDataProvider.class);
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;
29 public TechTypeTableDataProvider(String data) {
34 public String getVariableName(int columnIndex) {
35 return variables != null && columnIndex < variables.size() ? variables.get(columnIndex) : null;
38 public int getVariableIndex(String variableName) {
39 return variables != null ? variables.indexOf(variableName) : -1;
42 public String getHeaderValue(int columnIndex) {
43 if (headers == null) {
46 return headers.get(columnIndex);
50 public Object getDataValue(int columnIndex, int rowIndex) {
51 return filteredRecords.get(rowIndex).get(columnIndex);
55 public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
60 public int getColumnCount() {
61 if (records.isEmpty()) {
64 return records.get(0).size();
68 public int getRowCount() {
69 return filteredRecords.size() - 1;
72 public boolean isEditable(int columnIndex, int rowIndex) {
76 public void setFilter(String text) {
77 this.filter = text.toLowerCase();
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)) {
87 }).collect(Collectors.toList());
91 * Read a CSV file into table contents.
93 * Set path to null to create an empty table.
95 * @param path The path of the CSV file to be read.
97 public void setPath(String path) {
99 filteredRecords.clear();
101 Path techTypeCsv = Paths.get(path);
103 DistrictImportUtils.consumeCSV(techTypeCsv, ';', false, record -> {
107 } catch (IOException e) {
116 * Set table data contents to a given string of CSV data.
118 * Set 'data' to null to create an empty table.
120 * @param data The CSV data to be shown in the table.
122 public void setData(String data) {
124 filteredRecords.clear();
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);
131 DistrictImportUtils.consumeCSV(reader, delim, false, record -> {
135 } catch (IOException e) {
136 LOGGER.error("Error reading CSV file", e);
140 CSVRecord header = records.remove(0);
141 CSVRecord units = records.remove(0);
143 variables = new ArrayList<>();
144 headers = new ArrayList<>();
146 Iterator<String> it = header.iterator();
147 Iterator<String> uit = units.iterator();
149 while (it.hasNext()) {
150 String variable = it.next().trim();
151 String unit = uit.hasNext() ? uit.next().trim() : null;
153 variables.add(variable);
154 headers.add(variable + (unit != null && !unit.isEmpty() && !(unit.startsWith("(") && unit.endsWith(")")) ? " [" + unit + "]" : ""));