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