]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/ImportTechTypeCSVHandler.java
First testing version of TechTypeTable
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / table / ImportTechTypeCSVHandler.java
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/ImportTechTypeCSVHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/table/ImportTechTypeCSVHandler.java
new file mode 100644 (file)
index 0000000..7ee841b
--- /dev/null
@@ -0,0 +1,53 @@
+package org.simantics.district.network.ui.table;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Map;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.district.imports.DistrictImportUtils;
+import org.simantics.district.network.ui.techtype.table.TechTypeTableView;
+import org.simantics.utils.ui.ExceptionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ImportTechTypeCSVHandler {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ImportTechTypeCSVHandler.class);
+
+    @Inject
+    EPartService partService;
+
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell s) {
+        // here we can import based on the current CSV table data
+
+        FileDialog dialog = new FileDialog(s);
+        String path = dialog.open();
+        try {
+            if (path != null) {
+                
+                Path p = Paths.get(path);
+                if (Files.exists(p)) {
+                    Map<String, Integer> readCSVHeader = DistrictImportUtils.readCSVHeader(p, ';', true);
+                    TechTypeTableView.table.setTechTypePath(path);
+                } else {
+                    LOGGER.warn("Path does not exist even though path != null: {}", p);
+                }
+            } else {
+                LOGGER.error("Invalid file path given {}", path);
+            }
+        } catch (Exception e) {
+            LOGGER.error("Could not read file {}", path, e);
+            ExceptionUtils.logAndShowError("Could not read file " + path + " : " + e.getMessage(), e);
+        }
+    }
+}