1 package org.simantics.district.imports.ui;
3 import java.io.IOException;
4 import java.util.Collection;
7 import org.eclipse.jface.dialogs.IPageChangeProvider;
8 import org.eclipse.jface.dialogs.IPageChangedListener;
9 import org.eclipse.jface.dialogs.PageChangedEvent;
10 import org.eclipse.jface.layout.GridDataFactory;
11 import org.eclipse.jface.wizard.IWizardContainer;
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.Label;
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.DistrictImportUtils;
26 import org.simantics.district.network.ui.function.Functions;
28 public class ComponentMappingPage extends WizardPage {
30 private CSVImportModel model;
31 private Composite composite;
32 private Composite childComposite;
33 protected Map<String, Resource> componentMappings;
34 private Collection<String> distinctMappingIvalues;
35 private Collection<String> distinctMappingIndexColumnValues;
37 public ComponentMappingPage(CSVImportModel model) {
38 super("Select component mappings");
40 setMessage("Select component mappings");
44 public void createControl(Composite parent) {
45 composite = new Composite(parent, SWT.NONE);
46 composite.setLayout(new GridLayout(1,false));
47 GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(composite);
49 setControl(composite);
51 validatePageComplete();
53 IWizardContainer container = getContainer();
54 if (container instanceof IPageChangeProvider) {
55 ((IPageChangeProvider) container).addPageChangedListener(new IPageChangedListener() {
58 public void pageChanged(PageChangedEvent event) {
59 if (container.getCurrentPage().equals(ComponentMappingPage.this)) {
60 ComponentMappingPage.this.updateComponentMappings();
67 private void updateComponentMappings() {
69 int mappingIndex = model.getComponentMappingIndex();
70 distinctMappingIndexColumnValues = DistrictImportUtils.readDistinctValuesOfColumn(model.getSource(), model.getDelimiter(), mappingIndex);
73 Simantics.getSession().sync(new ReadRequest() {
76 public void run(ReadGraph graph) throws DatabaseException {
77 if (model.isVertexImport())
78 componentMappings = Functions.getVertexMappings(graph, model.getParentDiagram());
80 componentMappings = Functions.getEdgeMappings(graph, model.getParentDiagram());
83 } catch (DatabaseException e) {
87 System.out.println(distinctMappingIndexColumnValues);
88 if (childComposite != null)
89 childComposite.dispose();
90 childComposite = new Composite(composite, SWT.NONE);
91 childComposite.setLayout(new GridLayout(1,false));
92 GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(childComposite);
94 for (String value : distinctMappingIndexColumnValues) {
96 Composite newCompo = new Composite(childComposite, SWT.NONE);
97 newCompo.setLayout(new GridLayout(2,false));
98 GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(newCompo);
100 Label t = new Label(newCompo, SWT.NONE);
103 Combo c = new Combo(newCompo, SWT.READ_ONLY);
104 c.setItems(componentMappings.keySet().toArray(new String[componentMappings.size()]));
105 c.addSelectionListener(new SelectionListener() {
108 public void widgetSelected(SelectionEvent e) {
109 model.setComponentMappings(value, componentMappings.get(c.getItem(c.getSelectionIndex())));
110 validatePageComplete();
114 public void widgetDefaultSelected(SelectionEvent e) {
119 composite.layout(true, true);
120 } catch (IOException e) {
125 private void validatePageComplete() {
126 Map<String, Resource> currentMappings = model.getComponentMappings();
127 if (currentMappings != null && distinctMappingIndexColumnValues != null && currentMappings.keySet().containsAll(distinctMappingIndexColumnValues))
128 setPageComplete(true);
130 setPageComplete(false);