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.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;
31 public class CSVImportWizardFirstPage extends WizardPage implements FileSelectionListener {
33 private final CSVImportModel model;
35 private Map<String, Resource> diagrams = new HashMap<>();
37 private Combo diagramCombo;
38 private FileSelectionWidget csvSelection;
40 public CSVImportWizardFirstPage(CSVImportModel model) {
41 super("Select source for CSV data");
43 setMessage("Select target network diagram and source for CSV-data");
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);
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);
56 diagramCombo = new Combo(parentConfigurationGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
57 diagramCombo.addSelectionListener(new SelectionListener() {
60 public void widgetSelected(SelectionEvent e) {
61 widgetDefaultSelected(e);
65 public void widgetDefaultSelected(SelectionEvent e) {
66 String selection = diagramCombo.getItem(diagramCombo.getSelectionIndex());
67 model.setParentDiagram(diagrams.get(selection));
72 GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(parentConfigurationGroup);
74 csvSelection = new FileSelectionWidget(composite, "CSV file", SWT.OPEN);
75 csvSelection.addListener(this);
77 updateAvailableParents();
81 setControl(composite);
84 private void updateAvailableParents() {
86 Simantics.getSession().syncRequest(new ReadRequest() {
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);
102 diagramCombo.setItems(diagrams.keySet().toArray(new String[diagrams.size()]));
103 } catch (DatabaseException e) {
109 public void fileSelected(FileOrDirectorySelectionWidget source, String[] filename) {
110 String[] selection = csvSelection.getFilename();
111 if (selection != null && selection.length > 0) {
112 Path csvFile = Paths.get(selection[0]);
113 if (!Files.exists(csvFile)) {
114 setErrorMessage("File " + csvFile.toAbsolutePath() + " does not exist");
116 model.setSource(csvFile);
122 private void validateComplete() {
123 setPageComplete(model.getSource() != null && model.getParentDiagram() != null);