]> gerrit.simantics Code Review - simantics/district.git/blob
9ba7e2da0d277104471268eb0fd41d7cc5f1c120
[simantics/district.git] /
1 package org.simantics.district.imports.ui;
2
3 import java.nio.file.Files;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.eclipse.jface.layout.GridDataFactory;
11 import org.eclipse.jface.layout.GridLayoutFactory;
12 import org.eclipse.jface.wizard.WizardPage;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Combo;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Group;
20 import org.simantics.Simantics;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.district.network.ui.function.Functions;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.utils.ui.widgets.FileOrDirectorySelectionWidget;
28 import org.simantics.utils.ui.widgets.FileSelectionListener;
29 import org.simantics.utils.ui.widgets.FileSelectionWidget;
30
31 public class CSVImportWizardFirstPage extends WizardPage implements FileSelectionListener {
32
33     private final CSVImportModel model;
34     
35     private Map<String, Resource> diagrams = new HashMap<>();
36     
37     private Combo diagramCombo;
38     private FileSelectionWidget csvSelection;
39
40     public CSVImportWizardFirstPage(CSVImportModel model) {
41         super("Select source for CSV data");
42         this.model = model;
43         setMessage("Select target network diagram and source for CSV-data");
44     }
45     
46     @Override
47     public void createControl(Composite parent) {
48         Composite composite = new Composite(parent, SWT.NONE);
49         composite.setLayout(new GridLayout(1,false));
50         GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(composite);
51         
52         Group parentConfigurationGroup = new Group(composite, SWT.NONE);
53         parentConfigurationGroup.setText("Select target network diagram");
54         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(parentConfigurationGroup);
55         
56         diagramCombo = new Combo(parentConfigurationGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
57         diagramCombo.addSelectionListener(new SelectionListener() {
58             
59             @Override
60             public void widgetSelected(SelectionEvent e) {
61                 widgetDefaultSelected(e);
62             }
63             
64             @Override
65             public void widgetDefaultSelected(SelectionEvent e) {
66                 String selection = diagramCombo.getItem(diagramCombo.getSelectionIndex());
67                 model.setParentDiagram(diagrams.get(selection));
68                 validateComplete();
69             }
70         });
71         
72         GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(parentConfigurationGroup);
73         
74         csvSelection = new FileSelectionWidget(composite, "CSV file", SWT.OPEN);
75         csvSelection.addListener(this);
76         
77         updateAvailableParents();
78         
79         validateComplete();
80         
81         setControl(composite);
82     }
83
84     private void updateAvailableParents() {
85         try {
86             Simantics.getSession().syncRequest(new ReadRequest() {
87                 
88                 @Override
89                 public void run(ReadGraph graph) throws DatabaseException {
90                     Collection<Resource> diagrams = Functions.getDistrictDiagrams(graph);
91                     ModelingResources MOD = ModelingResources.getInstance(graph);
92                     Resource projectResource = Simantics.getProjectResource();
93                     String projectURI = graph.getURI(projectResource);
94                     for (Resource diagram : diagrams) {
95                         Resource composite = graph.getSingleObject(diagram, MOD.DiagramToComposite);
96                         String compositeURI = graph.getURI(composite);
97                         String path = compositeURI.replace(projectURI, "");
98                         CSVImportWizardFirstPage.this.diagrams.put(path, diagram);
99                     }
100                 }
101             });
102             diagramCombo.setItems(diagrams.keySet().toArray(new String[diagrams.size()]));
103             if (diagrams.size() > 0) {
104                 diagramCombo.select(0);
105                 model.setParentDiagram(diagrams.get(diagramCombo.getItem(0)));
106                 diagramCombo.getDisplay().asyncExec(() -> validateComplete());
107             }
108         } catch (DatabaseException e) {
109             e.printStackTrace();
110         }
111     }
112
113     @Override
114     public void fileSelected(FileOrDirectorySelectionWidget source, String[] filename) {
115         String[] selection = csvSelection.getFilename();
116         if (selection != null && selection.length > 0) {
117             Path csvFile = Paths.get(selection[0]);
118             if (!Files.exists(csvFile)) {
119                 setErrorMessage("File " + csvFile.toAbsolutePath() + " does not exist");
120             } else {
121                 model.setSource(csvFile);
122                 validateComplete();
123             }
124         }
125     }
126     
127     private void validateComplete() {
128         setPageComplete(model.getSource() != null && model.getParentDiagram() != null);
129     }
130 }