X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ftechtype%2Ftable%2FTechTypeTableDataProvider.java;h=850cc39f3c3d86190ac7f91d7b814cb871231417;hb=refs%2Fchanges%2F33%2F4433%2F1;hp=534be1dafe5a57d2dfef57884de4b6e676203017;hpb=7b3111c1cbb19842294bffc6a34289a2fecabdf9;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java index 534be1da..850cc39f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/techtype/table/TechTypeTableDataProvider.java @@ -1,38 +1,54 @@ package org.simantics.district.network.ui.techtype.table; import java.io.IOException; +import java.io.StringReader; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.csv.CSVRecord; import org.eclipse.nebula.widgets.nattable.data.IDataProvider; import org.simantics.district.imports.DistrictImportUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TechTypeTableDataProvider implements IDataProvider { + @SuppressWarnings("unused") + private final static Logger LOGGER = LoggerFactory.getLogger(TechTypeTableDataProvider.class); + private List records = new ArrayList<>(); private List filteredRecords = new ArrayList<>(); private String filter = ""; + private List variables = null; + private List headers = null; - public TechTypeTableDataProvider() { + public TechTypeTableDataProvider(String data) { // load csv + setData(data); + } - setPath("C:\\projektit\\apros\\Semantum_VTT_Fortum portaali 2018-17-12\\järvenpää\\qgis\\TechTypeData.csv"); + public String getVariableName(int columnIndex) { + return variables != null && columnIndex < variables.size() ? variables.get(columnIndex) : null; } - public Object getHeaderValue(int columnIndex) { - if (records.isEmpty()) { + public int getVariableIndex(String variableName) { + return variables != null ? variables.indexOf(variableName) : -1; + } + + public String getHeaderValue(int columnIndex) { + if (headers == null) { return ""; } - return records.get(0).get(columnIndex); + return headers.get(columnIndex); } @Override public Object getDataValue(int columnIndex, int rowIndex) { - return filteredRecords.get(rowIndex + 1).get(columnIndex); + return filteredRecords.get(rowIndex).get(columnIndex); } @Override @@ -71,20 +87,75 @@ public class TechTypeTableDataProvider implements IDataProvider { }).collect(Collectors.toList()); } + /** + * Read a CSV file into table contents. + * + * Set path to null to create an empty table. + * + * @param path The path of the CSV file to be read. + */ public void setPath(String path) { records.clear(); filteredRecords.clear(); - Path techTypeCsv = Paths.get(path); - try { - DistrictImportUtils.consumeCSV(techTypeCsv, ';', false, record -> { - records.add(record); - return true; - }); - } catch (IOException e) { - e.printStackTrace(); + if (path != null) { + Path techTypeCsv = Paths.get(path); + try { + DistrictImportUtils.consumeCSV(techTypeCsv, ';', false, record -> { + records.add(record); + return true; + }); + } catch (IOException e) { + e.printStackTrace(); + } + } + + setFilter(""); + } + + /** + * Set table data contents to a given string of CSV data. + * + * Set 'data' to null to create an empty table. + * + * @param data The CSV data to be shown in the table. + */ + public void setData(String data) { + records.clear(); + filteredRecords.clear(); + if (data != null) { + long ncommas = data.chars().filter(c -> c == ',').count(); + long nsemis = data.chars().filter(c -> c == ';').count(); + char delim = nsemis > ncommas ? ';' : ','; + StringReader reader = new StringReader(data); + try { + DistrictImportUtils.consumeCSV(reader, delim, false, record -> { + records.add(record); + return true; + }); + } catch (IOException e) { + LOGGER.error("Error reading CSV file", e); + return; + } + + CSVRecord header = records.remove(0); + CSVRecord units = records.remove(0); + + variables = new ArrayList<>(); + headers = new ArrayList<>(); + + Iterator it = header.iterator(); + Iterator uit = units.iterator(); + + while (it.hasNext()) { + String variable = it.next().trim(); + String unit = uit.hasNext() ? uit.next().trim() : null; + + variables.add(variable); + headers.add(variable + (unit != null && !unit.isEmpty() && !(unit.startsWith("(") && unit.endsWith(")")) ? " [" + unit + "]" : "")); + } } setFilter(""); } -} +} \ No newline at end of file