1 package org.simantics.district.imports.ui;
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;
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;
32 public class CSVImportWizardFirstPage extends WizardPage implements FileSelectionListener {
34 private final CSVImportModel model;
36 private Map<String, Resource> diagrams = new HashMap<>();
38 private Combo diagramCombo;
39 private FileSelectionWidget csvSelection;
41 public CSVImportWizardFirstPage(CSVImportModel model) {
42 super("Select source for CSV data");
44 setMessage("Select target network diagram and source for CSV-data");
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);
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);
57 diagramCombo = new Combo(parentConfigurationGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
58 diagramCombo.addSelectionListener(new SelectionListener() {
61 public void widgetSelected(SelectionEvent e) {
62 widgetDefaultSelected(e);
66 public void widgetDefaultSelected(SelectionEvent e) {
67 String selection = diagramCombo.getItem(diagramCombo.getSelectionIndex());
68 model.setParentDiagram(diagrams.get(selection));
73 GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(parentConfigurationGroup);
75 csvSelection = new FileSelectionWidget(composite, "CSV file", SWT.OPEN);
76 csvSelection.addListener(this);
78 updateAvailableParents();
82 setControl(composite);
85 private void updateAvailableParents() {
87 Simantics.getSession().syncRequest(new ReadRequest() {
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);
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());
109 } catch (DatabaseException e) {
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");
122 model.setSource(csvFile);
128 private void validateComplete() {
129 setPageComplete(model.getSource() != null && model.getParentDiagram() != null);