]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/DefaultMappingsDialog.java
Allow product-specific customization of new network diagram creation
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / function / DefaultMappingsDialog.java
1 package org.simantics.district.network.ui.function;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.stream.Collectors;
7
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Combo;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Group;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.ui.dialogs.SelectionStatusDialog;
19 import org.simantics.Simantics;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.IndexRoot;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.exception.ServiceException;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.layer0.Layer0;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class DefaultMappingsDialog extends SelectionStatusDialog {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultMappingsDialog.class);
34
35     private Combo vertexMappingCombo;
36     private Combo rightClickMappingCombo;
37     private Combo leftClickMappingCombo;
38     private Combo edgeMappingCombo;
39     //private Combo crsCombo;
40     private Composite composite;
41
42     private Resource configuration;
43     private Map<String, Resource> vertexMappings = new HashMap<>();
44     private Map<String, Resource> edgeMappings = new HashMap<>();
45     private Map<String, Resource> composites = new HashMap<>();
46     private Map<String, Resource> crss = new HashMap<>();
47
48     private Resource defaultVertexMapping;
49     private Resource defaultEdgeMapping;
50     private Resource rightClickVertexMapping;
51     private Resource leftClickVertexMapping;
52     //private Resource defaultCRS;
53
54     //private Combo compositeMappingCombo;
55     //private Combo componentMappingCombo;
56
57     protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
58         super(parentShell);
59         this.configuration = configuration;
60         setTitle("Select mappings for new DN diagram");
61     }
62
63     public Resource getDefaultVertexMapping() {
64         return defaultVertexMapping;
65     }
66
67     public Resource getRightClickVertexMapping() {
68         return rightClickVertexMapping;
69     }
70
71     public Resource getLeftClickVertexMapping() {
72         return leftClickVertexMapping;
73     }
74
75     public Resource getDefaultEdgeMapping() {
76         return defaultEdgeMapping;
77     }
78
79     @Override
80     protected Control createDialogArea(Composite parent) {
81         composite = (Composite) super.createDialogArea(parent);
82
83         createMappingsGroup(composite);
84         //createExistingCompositeGroup(composite);
85         //createCRSSettingsGroup(composite);
86
87         // compute default values
88         Simantics.getSession().asyncRequest(new ReadRequest() {
89
90             @Override
91             public void run(ReadGraph graph) throws DatabaseException {
92                 Resource indexRoot = graph.sync(new IndexRoot(configuration));
93                 vertexMappings = Functions.getVertexMappings(graph, indexRoot);
94                 edgeMappings = Functions.getEdgeMappings(graph, indexRoot);
95
96                 composites = getComposites(graph, configuration);
97
98                 crss = Functions.getCRSs(graph, configuration);
99
100                 composite.getDisplay().asyncExec(() -> {
101
102                     vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
103                     rightClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
104                     leftClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
105                     edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()]));
106
107                     //crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
108
109                     //compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()]));
110                     vertexMappingCombo.select(0);
111                     rightClickMappingCombo.select(0);
112                     leftClickMappingCombo.select(0);
113                     edgeMappingCombo.select(0);
114
115                     //crsCombo.select(0);
116
117                     //if (!composites.isEmpty())
118                     //    compositeMappingCombo.select(0);
119                 }); 
120
121             }
122         });
123         return composite;
124     }
125
126     protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
127         List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
128             try {
129                 return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite);
130             } catch (ServiceException e1) {
131                 LOGGER.error("Could not check if composite " + comp + " is instanceOf DistrictNetwork.composite");
132                 return false;
133             }
134         }).collect(Collectors.toList());
135         Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
136         Layer0 L0 = Layer0.getInstance(graph);
137         nonDistrictComposites.forEach(mapping -> {
138             try {
139                 String name = graph.getRelatedValue2(mapping, L0.HasName);
140                 result.put(name, mapping);
141             } catch (DatabaseException e) {
142                 LOGGER.error("Could not read name of " + mapping, e);
143             }
144         });
145         return result;
146     }
147
148     private void createMappingsGroup(Composite parent) {
149         Group group= new Group(parent, SWT.NONE);
150         group.setFont(parent.getFont());
151         group.setText("Default mappings");
152         GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
153         group.setLayout(new GridLayout(1, false));
154
155         Composite cmposite = new Composite(group, SWT.NONE);
156         cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
157         cmposite.setLayout(new GridLayout(2, false));
158
159         Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
160         vertexMappingLabel.setText("Default vertex mapping");
161
162         vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
163         GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
164
165         Label rightClickMappingLabel = new Label(cmposite, SWT.NONE);
166         rightClickMappingLabel.setText("Default right click mapping");
167
168         rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
169         GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
170
171         Label leftClickMappingLabel = new Label(cmposite, SWT.NONE);
172         leftClickMappingLabel.setText("Default left click mapping");
173
174         leftClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
175         GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
176
177         Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
178         edgeMappingLabel.setText("Default edge mapping");
179
180         edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
181         GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
182     }
183
184     private void createExistingCompositeGroup(Composite parent) {
185         Group group= new Group(parent, SWT.NONE);
186         group.setFont(parent.getFont());
187         group.setText("Mapped composite");
188         GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
189         group.setLayout(new GridLayout(1, false));
190
191         Composite cmposite = new Composite(group, SWT.NONE);
192         cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
193         cmposite.setLayout(new GridLayout(2, false));
194
195         Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
196         compositeMappingLabel.setText("Select composite");
197
198 //            compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
199 //            GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
200 //            compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
201 //                
202 //                @Override
203 //                public void widgetSelected(SelectionEvent e) {
204 //                    super.widgetSelected(e);
205 //                    recalculateMappapleComponents();
206 //                }
207 //            });
208
209 //            Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
210 //            compojnentMappingLabel.setText("Select component");
211 //            
212 //            componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
213 //            GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
214     }
215
216     protected void recalculateMappapleComponents() {
217         Simantics.getSession().asyncRequest(new ReadRequest() {
218
219             @Override
220             public void run(ReadGraph graph) throws DatabaseException {
221
222
223                 composite.getDisplay().asyncExec(() -> {
224
225                 }); 
226             }
227         });
228     }
229
230     private void createCRSSettingsGroup(Composite parent) {
231         Group group= new Group(parent, SWT.NONE);
232         group.setFont(parent.getFont());
233         group.setText("CRS settings");
234         GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
235         group.setLayout(new GridLayout(1, false));
236
237         Composite cmposite = new Composite(group, SWT.NONE);
238         cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
239         cmposite.setLayout(new GridLayout(2, false));
240
241         Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
242         vertexMappingLabel.setText("Default CRS");
243
244         //crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
245         //GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
246         //crsCombo.setLayoutData(textData);
247     }
248
249
250     @Override
251     protected void computeResult() {
252         defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
253         defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex()));
254         rightClickVertexMapping = vertexMappings.get(rightClickMappingCombo.getItem(rightClickMappingCombo.getSelectionIndex()));
255         leftClickVertexMapping = vertexMappings.get(leftClickMappingCombo.getItem(leftClickMappingCombo.getSelectionIndex()));
256         //defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex()));
257     }
258
259     public Resource getCRS() {
260         return crss.get("EPSG_4326"); // this is only supported
261     }
262
263 }