--- /dev/null
+package org.simantics.district.imports.ui;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.district.network.ui.function.Functions;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.utils.ui.widgets.FileOrDirectorySelectionWidget;
+import org.simantics.utils.ui.widgets.FileSelectionListener;
+import org.simantics.utils.ui.widgets.FileSelectionWidget;
+
+public class CSVImportWizardFirstPage extends WizardPage implements FileSelectionListener {
+
+ private final CSVImportModel model;
+
+ private Map<String, Resource> diagrams = new HashMap<>();
+
+ private Combo diagramCombo;
+ private FileSelectionWidget csvSelection;
+
+ public CSVImportWizardFirstPage(CSVImportModel model) {
+ super("Select source for CSV data");
+ this.model = model;
+ setMessage("Select target network diagram and source for CSV-data");
+ }
+
+ @Override
+ public void createControl(Composite parent) {
+ Composite composite = new Composite(parent, SWT.NONE);
+ composite.setLayout(new GridLayout(1,false));
+ GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(composite);
+
+ Group parentConfigurationGroup = new Group(composite, SWT.NONE);
+ parentConfigurationGroup.setText("Select target network diagram");
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(parentConfigurationGroup);
+
+ diagramCombo = new Combo(parentConfigurationGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
+ diagramCombo.addSelectionListener(new SelectionListener() {
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ widgetDefaultSelected(e);
+ }
+
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ String selection = diagramCombo.getItem(diagramCombo.getSelectionIndex());
+ model.setParentDiagram(diagrams.get(selection));
+ validateComplete();
+ }
+ });
+
+ GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(parentConfigurationGroup);
+
+ csvSelection = new FileSelectionWidget(composite, "CSV file", SWT.OPEN);
+ csvSelection.addListener(this);
+
+ updateAvailableParents();
+
+ validateComplete();
+
+ setControl(composite);
+ }
+
+ private void updateAvailableParents() {
+ try {
+ Simantics.getSession().syncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ Collection<Resource> diagrams = Functions.getDistrictDiagrams(graph);
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+ Resource projectResource = Simantics.getProjectResource();
+ String projectURI = graph.getURI(projectResource);
+ for (Resource diagram : diagrams) {
+ Resource composite = graph.getSingleObject(diagram, MOD.DiagramToComposite);
+ String compositeURI = graph.getURI(composite);
+ String path = compositeURI.replace(projectURI, "");
+ CSVImportWizardFirstPage.this.diagrams.put(path, diagram);
+ }
+ }
+ });
+ diagramCombo.setItems(diagrams.keySet().toArray(new String[diagrams.size()]));
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void fileSelected(FileOrDirectorySelectionWidget source, String[] filename) {
+ String[] selection = csvSelection.getFilename();
+ if (selection != null && selection.length > 0) {
+ Path csvFile = Paths.get(selection[0]);
+ if (!Files.exists(csvFile)) {
+ setErrorMessage("File " + csvFile.toAbsolutePath() + " does not exist");
+ } else {
+ model.setSource(csvFile);
+ validateComplete();
+ }
+ }
+ }
+
+ private void validateComplete() {
+ setPageComplete(model.getSource() != null && model.getParentDiagram() != null);
+ }
+}