]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/ComponentMappingPage.java
Enhancements to district functionalities and code
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / ComponentMappingPage.java
1 package org.simantics.district.imports.ui;
2
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.Map;
6
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;
27
28 public class ComponentMappingPage extends WizardPage {
29     
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;
36
37     public ComponentMappingPage(CSVImportModel model) {
38         super("Select component mappings");
39         this.model = model;
40         setMessage("Select component mappings");
41     }
42
43     @Override
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);
48         
49         setControl(composite);
50         
51         validatePageComplete();
52         
53         IWizardContainer container = getContainer();
54         if (container instanceof IPageChangeProvider) {
55             ((IPageChangeProvider) container).addPageChangedListener(new IPageChangedListener() {
56
57                 @Override
58                 public void pageChanged(PageChangedEvent event) {
59                     if (container.getCurrentPage().equals(ComponentMappingPage.this)) {
60                         ComponentMappingPage.this.updateComponentMappings();
61                     }
62                 }
63             });
64         }
65     }
66     
67     private void updateComponentMappings() {
68         try {
69             int mappingIndex = model.getComponentMappingIndex();
70             distinctMappingIndexColumnValues = DistrictImportUtils.readDistinctValuesOfColumn(model.getSource(), model.getDelimiter(), mappingIndex);
71             
72             try {
73                 Simantics.getSession().sync(new ReadRequest() {
74                     
75                     @Override
76                     public void run(ReadGraph graph) throws DatabaseException {
77                         if (model.isVertexImport())
78                             componentMappings = Functions.getVertexMappings(graph, model.getParentDiagram());
79                         else
80                             componentMappings = Functions.getEdgeMappings(graph, model.getParentDiagram());
81                     }
82                 });
83             } catch (DatabaseException e) {
84                 e.printStackTrace();
85             }
86             
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);
93             
94             for (String value : distinctMappingIndexColumnValues) {
95                 
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);
99                 
100                 Label t = new Label(newCompo, SWT.NONE);
101                 t.setText(value);
102                 
103                 Combo c = new Combo(newCompo, SWT.READ_ONLY);
104                 c.setItems(componentMappings.keySet().toArray(new String[componentMappings.size()]));
105                 c.addSelectionListener(new SelectionListener() {
106                     
107                     @Override
108                     public void widgetSelected(SelectionEvent e) {
109                         model.setComponentMappings(value, componentMappings.get(c.getItem(c.getSelectionIndex())));
110                         validatePageComplete();
111                     }
112                     
113                     @Override
114                     public void widgetDefaultSelected(SelectionEvent e) {
115                         widgetSelected(e);
116                     }
117                 });
118             }
119             composite.layout(true, true);
120         } catch (IOException e) {
121             e.printStackTrace();
122         }
123     }
124
125     private void validatePageComplete() {
126         Map<String, Resource> currentMappings = model.getComponentMappings();
127         if (currentMappings != null && distinctMappingIndexColumnValues != null && currentMappings.keySet().containsAll(distinctMappingIndexColumnValues))
128             setPageComplete(true);
129         else 
130             setPageComplete(false);
131     }
132
133 }