1 package org.simantics.district.network.ui.function;
3 import java.util.HashMap;
6 import java.util.stream.Collectors;
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;
31 public class DefaultMappingsDialog extends SelectionStatusDialog {
33 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultMappingsDialog.class);
35 private Combo vertexMappingCombo;
36 private Combo rightClickMappingCombo;
37 private Combo leftClickMappingCombo;
38 private Combo edgeMappingCombo;
39 //private Combo crsCombo;
40 private Composite composite;
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<>();
48 private Resource defaultVertexMapping;
49 private Resource defaultEdgeMapping;
50 private Resource rightClickVertexMapping;
51 private Resource leftClickVertexMapping;
52 //private Resource defaultCRS;
54 //private Combo compositeMappingCombo;
55 //private Combo componentMappingCombo;
57 protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
59 this.configuration = configuration;
60 setTitle("Select mappings for new DN diagram");
63 public Resource getDefaultVertexMapping() {
64 return defaultVertexMapping;
67 public Resource getRightClickVertexMapping() {
68 return rightClickVertexMapping;
71 public Resource getLeftClickVertexMapping() {
72 return leftClickVertexMapping;
75 public Resource getDefaultEdgeMapping() {
76 return defaultEdgeMapping;
80 protected Control createDialogArea(Composite parent) {
81 composite = (Composite) super.createDialogArea(parent);
83 createMappingsGroup(composite);
84 //createExistingCompositeGroup(composite);
85 //createCRSSettingsGroup(composite);
87 // compute default values
88 Simantics.getSession().asyncRequest(new ReadRequest() {
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);
96 composites = getComposites(graph, configuration);
98 crss = Functions.getCRSs(graph, configuration);
100 composite.getDisplay().asyncExec(() -> {
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()]));
107 //crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
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);
115 //crsCombo.select(0);
117 //if (!composites.isEmpty())
118 // compositeMappingCombo.select(0);
126 protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
127 List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
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");
134 }).collect(Collectors.toList());
135 Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
136 Layer0 L0 = Layer0.getInstance(graph);
137 nonDistrictComposites.forEach(mapping -> {
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);
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));
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));
159 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
160 vertexMappingLabel.setText("Default vertex mapping");
162 vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
163 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
165 Label rightClickMappingLabel = new Label(cmposite, SWT.NONE);
166 rightClickMappingLabel.setText("Default right click mapping");
168 rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
169 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
171 Label leftClickMappingLabel = new Label(cmposite, SWT.NONE);
172 leftClickMappingLabel.setText("Default left click mapping");
174 leftClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
175 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
177 Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
178 edgeMappingLabel.setText("Default edge mapping");
180 edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
181 GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
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));
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));
195 Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
196 compositeMappingLabel.setText("Select composite");
198 // compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
199 // GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
200 // compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
203 // public void widgetSelected(SelectionEvent e) {
204 // super.widgetSelected(e);
205 // recalculateMappapleComponents();
209 // Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
210 // compojnentMappingLabel.setText("Select component");
212 // componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
213 // GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
216 protected void recalculateMappapleComponents() {
217 Simantics.getSession().asyncRequest(new ReadRequest() {
220 public void run(ReadGraph graph) throws DatabaseException {
223 composite.getDisplay().asyncExec(() -> {
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));
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));
241 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
242 vertexMappingLabel.setText("Default CRS");
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);
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()));
259 public Resource getCRS() {
260 return crss.get("EPSG_4326"); // this is only supported