]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 package org.simantics.district.network.ui.table;
2
3 import java.nio.file.Files;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6 import java.util.Map;
7
8 import javax.inject.Inject;
9 import javax.inject.Named;
10
11 import org.eclipse.e4.core.di.annotations.Execute;
12 import org.eclipse.e4.ui.services.IServiceConstants;
13 import org.eclipse.e4.ui.workbench.modeling.EPartService;
14 import org.eclipse.swt.widgets.FileDialog;
15 import org.eclipse.swt.widgets.Shell;
16 import org.simantics.district.imports.DistrictImportUtils;
17 import org.simantics.district.network.ui.techtype.table.TechTypeTableView;
18 import org.simantics.utils.ui.ExceptionUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class ImportTechTypeCSVHandler {
23
24     private static final Logger LOGGER = LoggerFactory.getLogger(ImportTechTypeCSVHandler.class);
25
26     @Inject
27     EPartService partService;
28
29     @Execute
30     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell s) {
31         // here we can import based on the current CSV table data
32
33         FileDialog dialog = new FileDialog(s);
34         String path = dialog.open();
35         try {
36             if (path != null) {
37                 
38                 Path p = Paths.get(path);
39                 if (Files.exists(p)) {
40                     Map<String, Integer> readCSVHeader = DistrictImportUtils.readCSVHeader(p, ';', true);
41                     TechTypeTableView.table.setTechTypePath(path);
42                 } else {
43                     LOGGER.warn("Path does not exist even though path != null: {}", p);
44                 }
45             } else {
46                 LOGGER.error("Invalid file path given {}", path);
47             }
48         } catch (Exception e) {
49             LOGGER.error("Could not read file {}", path, e);
50             ExceptionUtils.logAndShowError("Could not read file " + path + " : " + e.getMessage(), e);
51         }
52     }
53 }