1 package org.simantics.district.network.ui.visualisations;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
8 import java.util.Map.Entry;
9 import java.util.Objects;
10 import java.util.Optional;
11 import java.util.function.Supplier;
12 import java.util.stream.Collectors;
13 import java.util.stream.Stream;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IInputValidator;
17 import org.eclipse.jface.dialogs.InputDialog;
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.ScrolledComposite;
22 import org.eclipse.swt.events.FocusAdapter;
23 import org.eclipse.swt.events.FocusEvent;
24 import org.eclipse.swt.events.KeyAdapter;
25 import org.eclipse.swt.events.KeyEvent;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Combo;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Group;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Text;
38 import org.eclipse.swt.widgets.Widget;
39 import org.simantics.Simantics;
40 import org.simantics.db.ReadGraph;
41 import org.simantics.db.Resource;
42 import org.simantics.db.WriteGraph;
43 import org.simantics.db.common.NamedResource;
44 import org.simantics.db.common.request.UniqueRead;
45 import org.simantics.db.common.request.WriteRequest;
46 import org.simantics.db.exception.DatabaseException;
47 import org.simantics.db.layer0.util.RemoverUtil;
48 import org.simantics.db.procedure.Listener;
49 import org.simantics.district.network.profile.ActiveDynamicVisualisationsRequest;
50 import org.simantics.district.network.profile.DynamicVisualisationsRequest;
51 import org.simantics.district.network.visualisations.DynamicVisualisations;
52 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions;
53 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicArrowObject;
54 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicColoringObject;
55 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicSizingObject;
56 import org.simantics.district.network.visualisations.model.ColorBarOptions;
57 import org.simantics.district.network.visualisations.model.ColorBarOptions.ColorBarsLocation;
58 import org.simantics.district.network.visualisations.model.ColorBarOptions.ColorBarsSize;
59 import org.simantics.district.network.visualisations.model.DynamicArrowContribution;
60 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
61 import org.simantics.district.network.visualisations.model.DynamicColorMap;
62 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
63 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
64 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
65 import org.simantics.district.network.visualisations.model.SizeBarOptions;
66 import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsLocation;
67 import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsSize;
68 import org.simantics.utils.datastructures.Pair;
69 import org.simantics.utils.ui.dialogs.ShowError;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 public class DynamicVisualisationsUI {
75 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsUI.class);
77 private Resource parentResource;
78 private VisualisationListener listener;
79 private DynamicVisualisation visualisation;
81 private Button showSizeButton;
82 private Button sizeTicksButton;
83 private Button sizeGradientButton;
84 private Combo sizeLocationCombo;
85 private Combo sizeSizeCombo;
86 private Button showColorButton;
87 private Button colorTicksButton;
88 private Button colorGradientButton;
89 private Combo colorLocationCombo;
90 private Combo colorSizeCombo;
92 private Combo templateSelectionCombo;
94 private List<Supplier<Pair<String, DynamicColorContribution>>> colorSuppliers;
96 private Button removeVisualisationTemplateButton;
98 private Composite parent;
100 private Button disableUpdatesButton;
102 private List<Supplier<Pair<String, DynamicArrowContribution>>> edgeArrowSuppliers;
104 private Button saveVisualisationTemplateAsButton;
106 private Button hideEdgesButton;
107 private Button hidePointsButton;
108 private Button hideConsumersButton;
109 private Button hideProducersButton;
110 private Button hideValvesButton;
111 private Button hidePumpingStationsButton;
113 private Button networkBranchesStaticPropertiesButton;
114 private Button pointsStaticPropertiesButton;
115 private Button consumersStaticPropertiesButton;
117 private Button dynamicSymbolsEdgesButton;
118 private Button dynamicSymbolsProducersButton;
119 private Button dynamicSymbolsValvesButton;
120 private Button dynamicSymbolsPumpingStationsButton;
122 public DynamicVisualisationsUI(Composite parent) {
123 this.parent = parent;
124 ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
125 scrolledComposite.setLayout(new GridLayout(1, false));
126 scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128 Composite firstContent = new Composite(scrolledComposite, SWT.NONE);
129 firstContent.setLayout(new GridLayout(1, false));
130 firstContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
132 defaultInitializeUI(firstContent);
134 scrolledComposite.setContent(firstContent);
135 scrolledComposite.setExpandHorizontal(true);
136 scrolledComposite.setExpandVertical(true);
137 scrolledComposite.setMinSize(firstContent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
140 private void defaultInitializeUI(Composite parent) {
142 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
143 GridLayoutFactory.fillDefaults().numColumns(1).margins(5, 5).applyTo(parent);
145 Composite selectionComposite = new Composite(parent, SWT.NONE);
146 GridDataFactory.fillDefaults().grab(true, false).applyTo(selectionComposite);
147 GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(selectionComposite);
149 Label templateNameLabel = new Label(selectionComposite, SWT.NONE);
150 templateNameLabel.setText("Visualisation template");
151 templateSelectionCombo = new Combo(selectionComposite, SWT.READ_ONLY);
152 GridDataFactory.fillDefaults().grab(true, false).applyTo(templateSelectionCombo);
153 templateSelectionCombo.addSelectionListener(new SelectionAdapter() {
156 public void widgetSelected(SelectionEvent e) {
157 String item = templateSelectionCombo.getItem(templateSelectionCombo.getSelectionIndex());
158 for (NamedResource template : visualisations) {
159 if (item.equals(template.getName())) {
160 Simantics.getSession().asyncRequest(new WriteRequest() {
163 public void perform(WriteGraph graph) throws DatabaseException {
164 Resource vf = DynamicVisualisations.getVisualisationFolder(graph, parentResource);
165 DynamicVisualisations.setActiveVisualisation(graph, vf, template.getResource());
174 Composite hideElementsComposite = new Composite(parent, SWT.NONE);
175 GridDataFactory.fillDefaults().grab(true, false).applyTo(hideElementsComposite);
176 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(hideElementsComposite);
177 initializeHideElements(hideElementsComposite);
179 Composite staticPropertiesComposite = new Composite(parent, SWT.NONE);
180 GridDataFactory.fillDefaults().grab(true, false).applyTo(staticPropertiesComposite);
181 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(staticPropertiesComposite);
182 initializeStaticProperties(staticPropertiesComposite);
184 Composite dynamicSymbolsComposite = new Composite(parent, SWT.NONE);
185 GridDataFactory.fillDefaults().grab(true, false).applyTo(dynamicSymbolsComposite);
186 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(dynamicSymbolsComposite);
187 initializeDynamicSymbols(dynamicSymbolsComposite);
189 Composite coloringObjectsComposite = new Composite(parent, SWT.NONE);
190 GridDataFactory.fillDefaults().grab(true, false).applyTo(coloringObjectsComposite);
191 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(coloringObjectsComposite);
192 initializeColoringObjects(coloringObjectsComposite);
194 Composite colorBarsComposite = new Composite(parent, SWT.NONE);
195 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorBarsComposite);
196 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(colorBarsComposite);
197 initializeColorBars(colorBarsComposite);
199 Composite objectSizesComposite = new Composite(parent, SWT.NONE);
200 GridDataFactory.fillDefaults().grab(true, false).applyTo(objectSizesComposite);
201 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(objectSizesComposite);
202 initializeObjectSizes(objectSizesComposite);
204 Composite sizeBarsComposite = new Composite(parent, SWT.NONE);
205 GridDataFactory.fillDefaults().grab(true, false).applyTo(sizeBarsComposite);
206 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(sizeBarsComposite);
207 initializeSizeBars(sizeBarsComposite);
209 Composite edgeArrowsComposite = new Composite(parent, SWT.NONE);
210 GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeArrowsComposite);
211 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(edgeArrowsComposite);
212 initializeEdgeArrows(edgeArrowsComposite);
214 Composite buttonBarsComposite = new Composite(parent, SWT.NONE);
215 GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonBarsComposite);
216 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonBarsComposite);
218 saveVisualisationTemplateAsButton = new Button(buttonBarsComposite, SWT.NONE);
219 saveVisualisationTemplateAsButton.setText("Save as visualisation template");
220 saveVisualisationTemplateAsButton.addSelectionListener(new SelectionAdapter() {
223 public void widgetSelected(SelectionEvent e) {
224 showSaveVisualisationTemplateDialog(e.widget.getDisplay().getActiveShell());
228 removeVisualisationTemplateButton = new Button(buttonBarsComposite, SWT.NONE);
229 removeVisualisationTemplateButton.setText("Remove");
230 removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null);
231 removeVisualisationTemplateButton.addSelectionListener(new SelectionAdapter() {
234 public void widgetSelected(SelectionEvent e) {
235 removeVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
239 disableUpdatesButton = new Button(buttonBarsComposite, SWT.CHECK);
240 disableUpdatesButton.setText("Disable updates");
241 disableUpdatesButton.addSelectionListener(new SelectionAdapter() {
244 public void widgetSelected(SelectionEvent e) {
250 private void initializeHideElements(Composite parent) {
251 Group group = new Group(parent, SWT.NONE);
252 group.setText("Hide Elements");
253 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
254 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
256 createHideElements(group);
259 private void createHideElements(Composite parent) {
261 hideEdgesButton = new Button(parent, SWT.CHECK);
262 hideEdgesButton.setText("Edges");
263 addSelectionListener(hideEdgesButton);
265 hidePointsButton = new Button(parent, SWT.CHECK);
266 hidePointsButton.setText("Points");
267 addSelectionListener(hidePointsButton);
269 hideConsumersButton = new Button(parent, SWT.CHECK);
270 hideConsumersButton.setText("Consumers");
271 addSelectionListener(hideConsumersButton);
273 hideProducersButton = new Button(parent, SWT.CHECK);
274 hideProducersButton.setText("Producers");
275 addSelectionListener(hideProducersButton);
277 hideValvesButton = new Button(parent, SWT.CHECK);
278 hideValvesButton.setText("Valves");
279 addSelectionListener(hideValvesButton);
281 hidePumpingStationsButton = new Button(parent, SWT.CHECK);
282 hidePumpingStationsButton.setText("Pumping Stations");
283 addSelectionListener(hidePumpingStationsButton);
286 private void initializeStaticProperties(Composite parent) {
287 Group group = new Group(parent, SWT.NONE);
288 group.setText("Static Properties");
289 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
290 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
292 createStaticProperties(group);
295 private void createStaticProperties(Composite parent) {
297 networkBranchesStaticPropertiesButton = new Button(parent, SWT.CHECK);
298 networkBranchesStaticPropertiesButton.setText("Network Branches");
299 addSelectionListener(networkBranchesStaticPropertiesButton);
301 pointsStaticPropertiesButton = new Button(parent, SWT.CHECK);
302 pointsStaticPropertiesButton.setText("Points");
303 addSelectionListener(pointsStaticPropertiesButton);
305 consumersStaticPropertiesButton = new Button(parent, SWT.CHECK);
306 consumersStaticPropertiesButton.setText("Consumers");
307 addSelectionListener(consumersStaticPropertiesButton);
311 private void initializeDynamicSymbols(Composite parent) {
312 Group group = new Group(parent, SWT.NONE);
313 group.setText("Dynamic Symbols");
314 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
315 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
317 createDynamicSymbols(group);
320 private void createDynamicSymbols(Composite parent) {
322 dynamicSymbolsEdgesButton = new Button(parent, SWT.CHECK);
323 dynamicSymbolsEdgesButton.setText("Shutoff Valves in Pipes");
324 addSelectionListener(dynamicSymbolsEdgesButton);
326 dynamicSymbolsProducersButton = new Button(parent, SWT.CHECK);
327 dynamicSymbolsProducersButton.setText("Producers");
328 addSelectionListener(dynamicSymbolsProducersButton);
330 dynamicSymbolsValvesButton = new Button(parent, SWT.CHECK);
331 dynamicSymbolsValvesButton.setText("Valves");
332 addSelectionListener(dynamicSymbolsValvesButton);
334 dynamicSymbolsPumpingStationsButton = new Button(parent, SWT.CHECK);
335 dynamicSymbolsPumpingStationsButton.setText("Pumping Stations");
336 addSelectionListener(dynamicSymbolsPumpingStationsButton);
339 private void initializeEdgeArrows(Composite parent) {
340 Group group = new Group(parent, SWT.NONE);
341 group.setText("Edge Arrows");
342 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
343 GridLayoutFactory.fillDefaults().numColumns(6).margins(5, 5).applyTo(group);
345 createEdgeArrowsHeaderRow(group);
347 edgeArrowSuppliers = new ArrayList<>();
350 Collection<DynamicArrowObject> result = Simantics.getSession().syncRequest(new UniqueRead<Collection<DynamicArrowObject>>() {
353 public Collection<DynamicArrowObject> perform(ReadGraph graph) throws DatabaseException {
354 return DynamicVisualisationsContributions.dynamicEdgeArrowObjects(graph);
358 for (DynamicArrowObject object : result) {
359 edgeArrowSuppliers.add(createEdgeArrowRow(group, object));
361 } catch (DatabaseException e) {
362 LOGGER.error("Could not create coloring objecst", e);
368 private void createEdgeArrowsHeaderRow(Composite parent) {
370 Label label = new Label(parent, SWT.NONE);
371 label.setText("Label");
372 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
374 label = new Label(parent, SWT.NONE);
375 label.setText("Used");
376 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
378 label = new Label(parent, SWT.NONE);
379 label.setText("Variable");
380 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
382 label = new Label(parent, SWT.NONE);
383 label.setText("Gain");
384 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
386 label = new Label(parent, SWT.NONE);
387 label.setText("Bias");
388 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
390 label = new Label(parent, SWT.NONE);
391 label.setText("Default");
392 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
395 private Supplier<Pair<String, DynamicArrowContribution>> createEdgeArrowRow(Composite parent, DynamicArrowObject object) {
397 Map<String, DynamicArrowContribution> arrowContributions = object.getArrowContributions();
398 if (arrowContributions.isEmpty()) {
399 // ok, no point in showing empty combo boxes
403 Label label = new Label(parent, SWT.NONE);
404 label.setText(object.getArrowObject().getName());
405 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
407 Button usedButton = new Button(parent, SWT.CHECK);
408 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
409 addSelectionListener(usedButton);
411 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
412 variableCombo.setItems(arrowContributions.keySet().toArray(new String[arrowContributions.size()]));
413 addSelectionListener(variableCombo);
415 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
417 Text gainText = new Text(parent, SWT.BORDER);
418 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(gainText);
419 addSelectionListener(gainText);
421 Text biasText = new Text(parent, SWT.BORDER);
422 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(biasText);
423 addSelectionListener(biasText);
425 Button defaultButton = new Button(parent, SWT.CHECK);
426 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
427 addSelectionListener(defaultButton);
428 defaultButton.addSelectionListener(new SelectionAdapter() {
431 public void widgetSelected(SelectionEvent e) {
432 int index = variableCombo.getSelectionIndex();
434 String key = variableCombo.getItem(index);
435 DynamicArrowContribution cont = arrowContributions.get(key);
437 gainText.setText(Double.toString(cont.getDefaultGain()));
438 gainText.setEnabled(!defaultButton.getSelection());
439 biasText.setText(Double.toString(cont.getDefaultBias()));
440 biasText.setEnabled(!defaultButton.getSelection());
446 variableCombo.addSelectionListener(new SelectionAdapter() {
449 public void widgetSelected(SelectionEvent e) {
450 // handle update for others
451 String key = variableCombo.getItem(variableCombo.getSelectionIndex());
452 DynamicArrowContribution cont = arrowContributions.get(key);
454 if (gainText.getText().isEmpty()) {
455 gainText.setText(Double.toString(cont.getDefaultGain()));
457 if (biasText.getText().isEmpty()) {
458 biasText.setText(Double.toString(cont.getDefaultBias()));
461 defaultButton.setSelection(true);
465 arrowRows.put(object.getArrowObject().getName(), new ArrowObjectRow(label, usedButton, variableCombo, gainText, biasText, defaultButton));
467 return new Supplier<Pair<String, DynamicArrowContribution>>() {
470 public Pair<String, DynamicArrowContribution> get() {
471 int selectionIndex = variableCombo.getSelectionIndex();
472 if (selectionIndex >= 0) {
473 String key = variableCombo.getItem(selectionIndex);
474 DynamicArrowContribution cont = arrowContributions.get(key);
476 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
478 double gain = cont.getDefaultGain();
479 String gainS = gainText.getText();
480 if (gainS != null && !gainS.isEmpty()) {
481 gain = Double.parseDouble(gainS);
483 double bias = cont.getDefaultBias();
484 String biasS = biasText.getText();
485 if (biasS != null && !biasS.isEmpty()) {
486 bias = Double.parseDouble(biasText.getText());
489 DynamicArrowContribution dsc = new DynamicArrowContribution(label, cont.getModuleName(), cont.getAttributeName(), gain, bias);
490 dsc.setUsed(usedButton.getSelection());
491 dsc.setUseDefault(defaultButton.getSelection());
493 return Pair.make(object.getArrowObject().getName(), dsc);
501 protected void disableUpdates() {
505 protected void removeVisualisationTemplate(String name, Optional<Resource> of) {
506 if (of.isPresent()) {
507 Resource visualisation = of.get();
508 Simantics.getSession().asyncRequest(new WriteRequest() {
511 public void perform(WriteGraph graph) throws DatabaseException {
512 RemoverUtil.remove(graph, visualisation);
518 private void showSaveVisualisationTemplateDialog(Shell shell) {
520 InputDialog dialog = new InputDialog(shell, "Save visualisation template", "Give template a name", "", new IInputValidator() {
523 public String isValid(String newText) {
524 if (newText == null || newText.isEmpty())
525 return "Name cannot be empty";
530 if (dialog.open() == Dialog.OK) {
531 String name = dialog.getValue();
533 persistVisualisationTemplate(name, Optional.empty());
534 } catch (Exception e) {
535 LOGGER.error("Could not persist visualisation template", e);
536 ShowError.showError("Could not persist visualisation template", e.getMessage(), e);
541 private void persistCurrentVisualisationTemplateIfAvailable() {
542 if (visualisation != null) {
544 persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
545 } catch (Exception e1) {
546 LOGGER.error("Could not persist visualisation template", e1);
547 ShowError.showError("Could not persist visualisation template", e1.getMessage(), e1);
550 LOGGER.info("No current visualisation template selected for saving");
554 private void persistVisualisationTemplate(String templateName, Optional<Resource> existing) throws Exception {
556 List<Pair<String, DynamicColorContribution>> colorCollect = colorSuppliers.stream().filter(Objects::nonNull).map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
558 String colorLocation = colorLocationCombo.getItem(colorLocationCombo.getSelectionIndex());
559 String colorSize = colorSizeCombo.getItem(colorSizeCombo.getSelectionIndex());
561 ColorBarOptions colorBarOptions = new ColorBarOptions()
562 .showColorBars(showColorButton.getSelection())
563 .showColorBarsTicks(colorTicksButton.getSelection())
564 .useGradients(colorGradientButton.getSelection())
565 .withLocation(ColorBarsLocation.valueOf(colorLocation))
566 .withSize(ColorBarsSize.valueOf(colorSize));
568 List<Pair<String, DynamicSizeContribution>> sizeCollect = sizeSuppliers.stream().filter(Objects::nonNull).map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
570 String sizeLocation = sizeLocationCombo.getItem(sizeLocationCombo.getSelectionIndex());
571 String sizeSize = sizeSizeCombo.getItem(sizeSizeCombo.getSelectionIndex());
573 final SizeBarOptions sizeBarOptions = new SizeBarOptions()
574 .showSizeBars(showSizeButton.getSelection())
575 .showSizeBarsTicks(sizeTicksButton.getSelection())
576 .useGradients(sizeGradientButton.getSelection())
577 .withLocation(SizeBarsLocation.valueOf(sizeLocation))
578 .withSize(SizeBarsSize.valueOf(sizeSize));
580 List<Pair<String, DynamicArrowContribution>> edgeArrowCollect = edgeArrowSuppliers.stream().filter(Objects::nonNull).map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
582 boolean hideEdges = hideEdgesButton.getSelection();
583 boolean hidePoints = hidePointsButton.getSelection();
584 boolean hideConsumers = hideConsumersButton.getSelection();
585 boolean hideProducers = hideProducersButton.getSelection();
586 boolean hideValves = hideValvesButton.getSelection();
587 boolean hidePumpingStations = hidePumpingStationsButton.getSelection();
589 boolean networkBranchesStaticProperties = networkBranchesStaticPropertiesButton.getSelection();
590 boolean pointsStaticProperties = pointsStaticPropertiesButton.getSelection();
591 boolean consumersStaticProperties = consumersStaticPropertiesButton.getSelection();
593 boolean dynamicSymbolsEdges = dynamicSymbolsEdgesButton.getSelection();
594 boolean dynamicSymbolsProducers = dynamicSymbolsProducersButton.getSelection();
595 boolean dynamicSymbolsValves = dynamicSymbolsValvesButton.getSelection();
596 boolean dynamicSymbolsPumpingStations = dynamicSymbolsPumpingStationsButton.getSelection();
598 Simantics.getSession().asyncRequest(new WriteRequest() {
601 public void perform(WriteGraph graph) throws DatabaseException {
603 if (existing.isPresent()) {
604 exist = existing.get();
606 exist = DynamicVisualisations.createVisualisation(graph, parentResource, templateName);
608 DynamicVisualisations.setColorContributions(graph, exist, colorCollect);
609 DynamicVisualisations.setColorBarOptions(graph, exist, colorBarOptions);
610 DynamicVisualisations.setSizeContributions(graph, exist, sizeCollect);
611 DynamicVisualisations.setSizeBarOptions(graph, exist, sizeBarOptions);
612 DynamicVisualisations.setEdgeArrowContributions(graph, exist, edgeArrowCollect);
613 DynamicVisualisations.setHideElements(graph, exist,
622 DynamicVisualisations.setStaticProperties(graph, exist,
623 networkBranchesStaticProperties,
624 pointsStaticProperties,
625 consumersStaticProperties
628 DynamicVisualisations.setDynamicSymbols(graph, exist,
630 dynamicSymbolsProducers,
631 dynamicSymbolsValves,
632 dynamicSymbolsPumpingStations
638 private void initializeColoringObjects(Composite parent) {
639 Group group = new Group(parent, SWT.NONE);
640 group.setText("Coloring Objects");
641 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
642 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
645 createColoringObjectHeaderRow(group);
647 colorSuppliers = new ArrayList<>();
650 Pair<Collection<DynamicColoringObject>, Map<String, DynamicColorMap>> result = Simantics.getSession().syncRequest(new UniqueRead<Pair<Collection<DynamicColoringObject>, Map<String, DynamicColorMap>>>() {
653 public Pair<Collection<DynamicColoringObject>, Map<String, DynamicColorMap>> perform(ReadGraph graph) throws DatabaseException {
654 Map<String, DynamicColorMap> dynamicColorMaps = DynamicVisualisationsContributions.dynamicColorMaps(graph);
655 return Pair.make(DynamicVisualisationsContributions.dynamicColoringObjects(graph), dynamicColorMaps);
659 for (DynamicColoringObject object : result.first) {
660 colorSuppliers.add(createColoringObjectRow(group, object, result.second));
663 } catch (DatabaseException e) {
664 LOGGER.error("Could not create coloring objecst", e);
669 private void createColoringObjectHeaderRow(Composite parent) {
671 Label label = new Label(parent, SWT.NONE);
672 label.setText("Label");
673 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
675 label = new Label(parent, SWT.NONE);
676 label.setText("Used");
677 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
679 label = new Label(parent, SWT.NONE);
680 label.setText("Variable");
681 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
683 label = new Label(parent, SWT.NONE);
684 label.setText("Min");
685 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
687 label = new Label(parent, SWT.NONE);
688 label.setText("Max");
689 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
691 label = new Label(parent, SWT.NONE);
692 label.setText("Unit");
693 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
695 label = new Label(parent, SWT.NONE);
696 label.setText("ColorMap");
697 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
699 label = new Label(parent, SWT.NONE);
700 label.setText("Default");
701 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
704 private Map<String, ColoringObjectRow> coloringRows = new HashMap<>();
705 private Map<String, SizingObjectRow> sizingRows = new HashMap<>();
706 private Map<String, ArrowObjectRow> arrowRows = new HashMap<>();
708 private VisualisationsListener visualisationsListener;
710 private Collection<NamedResource> visualisations;
712 private List<Supplier<Pair<String, DynamicSizeContribution>>> sizeSuppliers;
714 private static class ColoringObjectRow {
716 private final Label label;
717 private final Button usedButton;
718 private final Combo variableCombo;
719 private final Text minText;
720 private final Text maxText;
721 private final Text unit;
722 private final Combo colorMapCombo;
723 private final Button defaultButton;
725 public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Text unit,
726 Combo colorMapCombo, Button defaultButton) {
729 this.usedButton = usedButton;
730 this.variableCombo = variableCombo;
731 this.minText = minText;
732 this.maxText = maxText;
734 this.colorMapCombo = colorMapCombo;
735 this.defaultButton = defaultButton;
738 public void update(DynamicColorContribution colorContribution) {
739 String[] items = variableCombo.getItems();
740 for (int i = 0; i < items.length; i++) {
741 if (colorContribution.getLabel().equals(items[i])) {
742 variableCombo.select(i);
746 minText.setText(Double.toString(colorContribution.getDefaultMin()));
747 maxText.setText(Double.toString(colorContribution.getDefaultMax()));
748 unit.setText(colorContribution.getUnit());
750 String[] colorItems = colorMapCombo.getItems();
751 for (int i = 0; i < colorItems.length; i++) {
753 if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) {
754 colorMapCombo.select(i);
758 usedButton.setSelection(colorContribution.isUsed());
759 defaultButton.setSelection(colorContribution.isUseDefault());
761 minText.setEnabled(!colorContribution.isUseDefault());
762 maxText.setEnabled(!colorContribution.isUseDefault());
763 colorMapCombo.setEnabled(!colorContribution.isUseDefault());
767 private static class SizingObjectRow {
769 private final Label label;
770 private final Button usedButton;
771 private final Combo variableCombo;
772 private final Text minText;
773 private final Text maxText;
774 private final Label unit;
775 private final Combo sizeMapCombo;
776 private final Button defaultButton;
778 public SizingObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit,
779 Combo sizeMapCombo, Button defaultButton) {
782 this.usedButton = usedButton;
783 this.variableCombo = variableCombo;
784 this.minText = minText;
785 this.maxText = maxText;
787 this.sizeMapCombo = sizeMapCombo;
788 this.defaultButton = defaultButton;
791 public void update(DynamicSizeContribution sizeContribution) {
792 String[] items = variableCombo.getItems();
793 for (int i = 0; i < items.length; i++) {
794 if (sizeContribution.getLabel().equals(items[i])) {
795 variableCombo.select(i);
799 minText.setText(Double.toString(sizeContribution.getDefaultMin()));
800 maxText.setText(Double.toString(sizeContribution.getDefaultMax()));
801 unit.setText(sizeContribution.getUnit());
803 String[] sizeItems = sizeMapCombo.getItems();
804 for (int i = 0; i < sizeItems.length; i++) {
805 if (sizeContribution.getDefaultSizeMap().getLabel().equals(sizeItems[i])) {
806 sizeMapCombo.select(i);
810 usedButton.setSelection(sizeContribution.isUsed());
811 defaultButton.setSelection(sizeContribution.isUseDefault());
813 minText.setEnabled(!sizeContribution.isUseDefault());
814 maxText.setEnabled(!sizeContribution.isUseDefault());
815 sizeMapCombo.setEnabled(!sizeContribution.isUseDefault());
819 private static class ArrowObjectRow {
821 private final Label label;
822 private final Button usedButton;
823 private final Combo variableCombo;
824 private final Text gainText;
825 private final Text biasText;
826 private final Button defaultButton;
828 public ArrowObjectRow(Label label, Button usedButton, Combo variableCombo, Text gainText, Text biasText, Button defaultButton) {
830 this.usedButton = usedButton;
831 this.variableCombo = variableCombo;
832 this.gainText = gainText;
833 this.biasText = biasText;
834 this.defaultButton = defaultButton;
837 public void update(DynamicColorContribution colorContribution) {
838 String[] items = variableCombo.getItems();
839 for (int i = 0; i < items.length; i++) {
840 if (colorContribution.getLabel().equals(items[i])) {
841 variableCombo.select(i);
845 gainText.setText(Double.toString(colorContribution.getDefaultMin()));
846 biasText.setText(Double.toString(colorContribution.getDefaultMax()));
848 usedButton.setSelection(colorContribution.isUsed());
849 defaultButton.setSelection(colorContribution.isUseDefault());
851 gainText.setEnabled(!colorContribution.isUseDefault());
852 biasText.setEnabled(!colorContribution.isUseDefault());
856 private Supplier<Pair<String, DynamicColorContribution>> createColoringObjectRow(Composite parent, DynamicColoringObject object, Map<String, DynamicColorMap> colorMaps) {
857 Label label = new Label(parent, SWT.NONE);
858 label.setText(object.getColoringObject().getName());
859 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
861 Map<String, DynamicColorContribution> colorContributions = object.getColorContributions();
863 Button usedButton = new Button(parent, SWT.CHECK);
864 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
865 addSelectionListener(usedButton);
867 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
868 variableCombo.setItems(colorContributions.keySet().toArray(new String[colorContributions.size()]));
869 addSelectionListener(variableCombo);
871 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
873 Text minText = new Text(parent, SWT.BORDER);
874 GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(minText);
875 addSelectionListener(minText);
877 Text maxText = new Text(parent, SWT.BORDER);
878 GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(maxText);
879 addSelectionListener(maxText);
881 Text unit = new Text(parent, SWT.READ_ONLY);
882 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(unit);
884 Combo colorMapCombo = new Combo(parent, SWT.READ_ONLY);
885 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorMapCombo);
886 colorMapCombo.setItems(colorMaps.keySet().toArray(new String[colorMaps.keySet().size()]));
887 addSelectionListener(colorMapCombo);
889 Button defaultButton = new Button(parent, SWT.CHECK);
890 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
891 addSelectionListener(defaultButton);
892 defaultButton.addSelectionListener(new SelectionAdapter() {
895 public void widgetSelected(SelectionEvent e) {
896 int index = variableCombo.getSelectionIndex();
898 String key = variableCombo.getItem(index);
899 DynamicColorContribution cont = colorContributions.get(key);
901 minText.setText(Double.toString(cont.getDefaultMin()));
902 minText.setEnabled(!defaultButton.getSelection());
903 maxText.setText(Double.toString(cont.getDefaultMax()));
904 maxText.setEnabled(!defaultButton.getSelection());
905 unit.setText(cont.getUnit());
907 String[] items = colorMapCombo.getItems();
908 for (int i = 0; i < items.length; i++) {
909 String item = items[i];
910 if (item.equals(cont.getDefaultColorMap().getLabel())) {
911 colorMapCombo.select(i);
915 colorMapCombo.setEnabled(!defaultButton.getSelection());
920 variableCombo.addSelectionListener(new SelectionAdapter() {
923 public void widgetSelected(SelectionEvent e) {
924 // handle update for others
925 int index = variableCombo.getSelectionIndex();
927 String key = variableCombo.getItem(index);
928 DynamicColorContribution cont = colorContributions.get(key);
930 if (minText.getText().isEmpty()) {
931 minText.setText(Double.toString(cont.getDefaultMin()));
933 if (maxText.getText().isEmpty()) {
934 maxText.setText(Double.toString(cont.getDefaultMax()));
936 unit.setText(cont.getUnit());
938 String[] items = colorMapCombo.getItems();
939 for (int i = 0; i < items.length; i++) {
940 String item = items[i];
941 if (item.equals(cont.getDefaultColorMap().getLabel())) {
942 colorMapCombo.select(i);
947 defaultButton.setSelection(true);
952 coloringRows.put(object.getColoringObject().getName(), new ColoringObjectRow(label, usedButton, variableCombo, minText, maxText, unit, colorMapCombo, defaultButton));
954 return new Supplier<Pair<String, DynamicColorContribution>>() {
957 public Pair<String, DynamicColorContribution> get() {
958 int selectionIndex = variableCombo.getSelectionIndex();
959 if (selectionIndex >= 0) {
960 String key = variableCombo.getItem(selectionIndex);
961 DynamicColorContribution cont = colorContributions.get(key);
963 String colorMap = colorMapCombo.getItem(colorMapCombo.getSelectionIndex());
965 Map<String, DynamicColorMap> colorMaps = Simantics.getSession().syncRequest(new UniqueRead<Map<String, DynamicColorMap>>() {
968 public Map<String, DynamicColorMap> perform(ReadGraph graph) throws DatabaseException {
969 return DynamicVisualisationsContributions.dynamicColorMaps(graph);
972 DynamicColorMap dColorMap = colorMaps.get(colorMap);
973 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
975 DynamicColorContribution dcc = new DynamicColorContribution(label, cont.getModuleName(), cont.getAttributeName(), unit.getText(), cont.getVariableGain(), cont.getVariableBias(), dColorMap, Double.parseDouble(minText.getText()), Double.parseDouble(maxText.getText()));
976 dcc.setUsed(usedButton.getSelection());
977 dcc.setUseDefault(defaultButton.getSelection());
979 return Pair.make(object.getColoringObject().getName(), dcc);
980 } catch (DatabaseException e) {
981 LOGGER.error("Could not get DynamicColorContribution", e);
990 private void createSizingObjectHeaderRow(Composite parent) {
992 Label label = new Label(parent, SWT.NONE);
993 label.setText("Label");
994 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
996 label = new Label(parent, SWT.NONE);
997 label.setText("Used");
998 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1000 label = new Label(parent, SWT.NONE);
1001 label.setText("Variable");
1002 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1004 label = new Label(parent, SWT.NONE);
1005 label.setText("Min");
1006 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1008 label = new Label(parent, SWT.NONE);
1009 label.setText("Max");
1010 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1012 label = new Label(parent, SWT.NONE);
1013 label.setText("Unit");
1014 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1016 label = new Label(parent, SWT.NONE);
1017 label.setText("SizeMap");
1018 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1020 label = new Label(parent, SWT.NONE);
1021 label.setText("Default");
1022 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
1025 private Supplier<Pair<String, DynamicSizeContribution>> createSizingObjectRow(Composite parent, DynamicSizingObject object, Map<String, DynamicSizeMap> sizeMaps) {
1026 Label label = new Label(parent, SWT.NONE);
1027 label.setText(object.getSizingObject().getName());
1028 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
1030 Map<String, DynamicSizeContribution> sizeContributions = object.getSizeContributions();
1032 Button usedButton = new Button(parent, SWT.CHECK);
1033 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
1034 addSelectionListener(usedButton);
1036 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
1037 variableCombo.setItems(sizeContributions.keySet().toArray(new String[sizeContributions.size()]));
1038 addSelectionListener(variableCombo);
1040 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
1042 Text minText = new Text(parent, SWT.BORDER);
1043 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(minText);
1044 addSelectionListener(minText);
1046 Text maxText = new Text(parent, SWT.BORDER);
1047 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(maxText);
1048 addSelectionListener(maxText);
1050 Label unit = new Label(parent, SWT.NONE);
1051 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(unit);
1053 Combo sizeMapCombo = new Combo(parent, SWT.READ_ONLY);
1054 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(sizeMapCombo);
1055 sizeMapCombo.setItems(sizeMaps.keySet().toArray(new String[sizeMaps.keySet().size()]));
1056 addSelectionListener(sizeMapCombo);
1058 Button defaultButton = new Button(parent, SWT.CHECK);
1059 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
1060 addSelectionListener(defaultButton);
1061 defaultButton.addSelectionListener(new SelectionAdapter() {
1064 public void widgetSelected(SelectionEvent e) {
1065 int index = variableCombo.getSelectionIndex();
1067 String key = variableCombo.getItem(index);
1068 DynamicSizeContribution cont = sizeContributions.get(key);
1070 minText.setText(Double.toString(cont.getDefaultMin()));
1071 minText.setEnabled(!defaultButton.getSelection());
1072 maxText.setText(Double.toString(cont.getDefaultMax()));
1073 maxText.setEnabled(!defaultButton.getSelection());
1074 unit.setText(cont.getUnit());
1076 String[] items = sizeMapCombo.getItems();
1077 for (int i = 0; i < items.length; i++) {
1078 String item = items[i];
1079 if (item.equals(cont.getDefaultSizeMap().getLabel())) {
1080 sizeMapCombo.select(i);
1084 sizeMapCombo.setEnabled(!defaultButton.getSelection());
1089 variableCombo.addSelectionListener(new SelectionAdapter() {
1092 public void widgetSelected(SelectionEvent e) {
1093 // handle update for others
1094 String key = variableCombo.getItem(variableCombo.getSelectionIndex());
1095 DynamicSizeContribution cont = sizeContributions.get(key);
1097 if (minText.getText().isEmpty()) {
1098 minText.setText(Double.toString(cont.getDefaultMin()));
1100 if (maxText.getText().isEmpty()) {
1101 maxText.setText(Double.toString(cont.getDefaultMax()));
1103 unit.setText(cont.getUnit());
1105 String[] items = sizeMapCombo.getItems();
1106 for (int i = 0; i < items.length; i++) {
1107 String item = items[i];
1108 if (item.equals(cont.getDefaultSizeMap().getLabel())) {
1109 sizeMapCombo.select(i);
1114 defaultButton.setSelection(true);
1118 sizingRows.put(object.getSizingObject().getName(), new SizingObjectRow(label, usedButton, variableCombo, minText, maxText, unit, sizeMapCombo, defaultButton));
1120 return new Supplier<Pair<String, DynamicSizeContribution>>() {
1123 public Pair<String, DynamicSizeContribution> get() {
1124 int selectionIndex = variableCombo.getSelectionIndex();
1125 if (selectionIndex >= 0) {
1126 String key = variableCombo.getItem(selectionIndex);
1127 DynamicSizeContribution cont = sizeContributions.get(key);
1129 String sizeMap = sizeMapCombo.getItem(sizeMapCombo.getSelectionIndex());
1131 Map<String, DynamicSizeMap> sizeMaps = Simantics.getSession().syncRequest(new UniqueRead<Map<String, DynamicSizeMap>>() {
1134 public Map<String, DynamicSizeMap> perform(ReadGraph graph) throws DatabaseException {
1135 return DynamicVisualisationsContributions.dynamicSizeMaps(graph);
1138 DynamicSizeMap dColorMap = sizeMaps.get(sizeMap);
1139 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
1141 DynamicSizeContribution dsc = new DynamicSizeContribution(label, cont.getModuleName(), cont.getAttributeName(), unit.getText(), cont.getVariableGain(), cont.getVariableBias(), dColorMap, Double.parseDouble(minText.getText()), Double.parseDouble(maxText.getText()));
1142 dsc.setUsed(usedButton.getSelection());
1143 dsc.setUseDefault(defaultButton.getSelection());
1145 return Pair.make(object.getSizingObject().getName(), dsc);
1146 } catch (DatabaseException e) {
1147 LOGGER.error("Could not get DynamicColorContribution", e);
1156 private void initializeColorBars(Composite parent) {
1157 Group group = new Group(parent, SWT.NONE);
1158 group.setText("Color Bars");
1159 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
1160 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
1162 createColorBars(group);
1165 private void createColorBars(Composite parent) {
1167 showColorButton = new Button(parent, SWT.CHECK);
1168 showColorButton.setText("Show");
1169 addSelectionListener(showColorButton);
1171 colorTicksButton = new Button(parent, SWT.CHECK);
1172 colorTicksButton.setText("Ticks");
1173 addSelectionListener(colorTicksButton);
1175 colorGradientButton = new Button(parent, SWT.CHECK);
1176 colorGradientButton.setText("Gradients");
1177 addSelectionListener(colorGradientButton);
1179 Label label = new Label(parent, SWT.NONE);
1180 label.setText("Location");
1181 colorLocationCombo = new Combo(parent, SWT.READ_ONLY);
1182 String[] colorLocationItems = Stream.of(ColorBarsLocation.values()).map(size -> size.toString()).toArray(String[]::new);
1183 colorLocationCombo.setItems(colorLocationItems);
1184 if (colorLocationItems.length > 0) {
1185 colorLocationCombo.select(0);
1187 addSelectionListener(colorLocationCombo);
1189 label = new Label(parent, SWT.NONE);
1190 label.setText("Size");
1191 colorSizeCombo = new Combo(parent, SWT.READ_ONLY);
1192 String[] colorSizeItems = Stream.of(ColorBarsSize.values()).map(size -> size.toString()).toArray(String[]::new);
1193 colorSizeCombo.setItems(colorSizeItems);
1194 if (colorSizeItems.length > 0) {
1195 colorSizeCombo.select(0);
1197 addSelectionListener(colorSizeCombo);
1200 private void initializeObjectSizes(Composite parent) {
1201 Group group = new Group(parent, SWT.NONE);
1202 group.setText("Object Sizes");
1203 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
1204 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
1207 createSizingObjectHeaderRow(group);
1208 createObjectSizes(group);
1212 private void createObjectSizes(Composite parent) {
1214 sizeSuppliers = new ArrayList<>();
1216 Pair<Collection<DynamicSizingObject>, Map<String, DynamicSizeMap>> resultSizing = Simantics.getSession().syncRequest(new UniqueRead<Pair<Collection<DynamicSizingObject>, Map<String, DynamicSizeMap>>>() {
1219 public Pair<Collection<DynamicSizingObject>, Map<String, DynamicSizeMap>> perform(ReadGraph graph) throws DatabaseException {
1220 Map<String, DynamicSizeMap> dynamicSizeMaps = DynamicVisualisationsContributions.dynamicSizeMaps(graph);
1221 return Pair.make(DynamicVisualisationsContributions.dynamicSizingObjects(graph), dynamicSizeMaps);
1225 for (DynamicSizingObject object : resultSizing.first) {
1226 sizeSuppliers.add(createSizingObjectRow(parent, object, resultSizing.second));
1228 } catch (DatabaseException e) {
1229 LOGGER.error("Could not create object sizes", e);
1233 private void initializeSizeBars(Composite parent) {
1234 Group group = new Group(parent, SWT.NONE);
1235 group.setText("Size Bars");
1236 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
1237 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
1239 createSizeBars(group);
1242 private void createSizeBars(Composite parent) {
1243 showSizeButton = new Button(parent, SWT.CHECK);
1244 showSizeButton.setText("Show");
1245 addSelectionListener(showSizeButton);
1247 sizeTicksButton = new Button(parent, SWT.CHECK);
1248 sizeTicksButton.setText("Ticks");
1249 addSelectionListener(sizeTicksButton);
1251 sizeGradientButton = new Button(parent, SWT.CHECK);
1252 sizeGradientButton.setText("Gradient");
1253 addSelectionListener(sizeGradientButton);
1255 Label label = new Label(parent, SWT.NONE);
1256 label.setText("Location");
1257 sizeLocationCombo = new Combo(parent, SWT.READ_ONLY);
1258 String[] sizeLocationItems = Stream.of(SizeBarsLocation.values()).map(size -> size.toString()).toArray(String[]::new);
1259 sizeLocationCombo.setItems(sizeLocationItems);
1260 if (sizeLocationItems.length > 0) {
1261 sizeLocationCombo.select(0);
1263 addSelectionListener(sizeLocationCombo);
1265 label = new Label(parent, SWT.NONE);
1266 label.setText("Size");
1267 sizeSizeCombo = new Combo(parent, SWT.READ_ONLY);
1268 String[] sizeSizeItems = Stream.of(SizeBarsSize.values()).map(size -> size.toString()).toArray(String[]::new);
1269 sizeSizeCombo.setItems(sizeSizeItems);
1270 if (sizeSizeItems.length > 0) {
1271 sizeSizeCombo.select(0);
1273 addSelectionListener(sizeSizeCombo);
1276 private void addSelectionListener(Widget widget) {
1277 if (widget instanceof Button) {
1278 ((Button) widget).addSelectionListener(new SelectionAdapter() {
1281 public void widgetSelected(SelectionEvent e) {
1282 persistCurrentVisualisationTemplateIfAvailable();
1285 } else if (widget instanceof Combo) {
1286 ((Combo) widget).addSelectionListener(new SelectionAdapter() {
1289 public void widgetSelected(SelectionEvent e) {
1290 persistCurrentVisualisationTemplateIfAvailable();
1293 } else if (widget instanceof Text) {
1294 ((Text) widget).addFocusListener(new FocusAdapter() {
1297 public void focusLost(FocusEvent e) {
1298 persistCurrentVisualisationTemplateIfAvailable();
1301 ((Text) widget).addKeyListener(new KeyAdapter() {
1304 public void keyReleased(KeyEvent e) {
1305 if(e.keyCode == SWT.CR || e.keyCode == SWT.LF) {
1306 persistCurrentVisualisationTemplateIfAvailable();
1313 public void setParentResource(Resource parentResource) {
1314 if (this.parentResource != parentResource) {
1315 this.parentResource = parentResource;
1318 saveVisualisationTemplateAsButton.setEnabled(parentResource != null);
1321 private void updateListening() {
1322 if (visualisationsListener != null)
1323 visualisationsListener.dispose();
1324 visualisationsListener = new VisualisationsListener(this);
1325 Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(parentResource), visualisationsListener);
1327 if (listener != null)
1329 listener = new VisualisationListener(this);
1330 Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(parentResource), listener);
1333 private static class VisualisationsListener implements Listener<Collection<NamedResource>> {
1335 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationsListener.class);
1337 private boolean disposed;
1338 private DynamicVisualisationsUI ui;
1340 public VisualisationsListener(DynamicVisualisationsUI ui) {
1345 public void execute(Collection<NamedResource> result) {
1346 ui.updateVisualisations(result);
1350 public void exception(Throwable t) {
1351 LOGGER.error("Could not listen visualisation", t);
1355 public boolean isDisposed() {
1356 return disposed || ui.getParent().isDisposed();
1359 public void dispose() {
1360 this.disposed = true;
1364 private static class VisualisationListener implements Listener<DynamicVisualisation> {
1366 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationListener.class);
1368 private boolean disposed;
1369 private DynamicVisualisationsUI ui;
1371 public VisualisationListener(DynamicVisualisationsUI ui) {
1376 public void execute(DynamicVisualisation result) {
1377 ui.updateVisualisation(result);
1381 public void exception(Throwable t) {
1382 LOGGER.error("Could not listen visualisation", t);
1386 public boolean isDisposed() {
1387 return disposed ||ui.getParent().isDisposed();
1390 public void dispose() {
1391 this.disposed = true;
1395 public void updateVisualisation(DynamicVisualisation result) {
1396 this.visualisation = result;
1397 Display.getDefault().asyncExec(() -> {
1398 if (getParent().isDisposed())
1401 removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null);
1403 if (visualisation != null) {
1404 String[] items = templateSelectionCombo.getItems();
1405 for (int i = 0; i < items.length; i++) {
1406 if (visualisation.getName().equals(items[i])) {
1407 templateSelectionCombo.select(i);
1412 Map<String, DynamicColorContribution> colorContributions = visualisation.getColorContributions();
1413 for (Entry<String, DynamicColorContribution> entry : colorContributions.entrySet()) {
1415 ColoringObjectRow coloringObjectRow = coloringRows.get(entry.getKey());
1416 if (coloringObjectRow != null) {
1418 coloringObjectRow.update(entry.getValue());
1421 LOGGER.info("No coloring object visualisation row for key {}", entry.getKey());
1424 ColorBarOptions colorOptions = visualisation.getColorBarOptions();
1425 showColorButton.setSelection(colorOptions.isShowColorBars());
1426 colorTicksButton.setSelection(colorOptions.isShowColorBarsTicks());
1427 colorGradientButton.setSelection(colorOptions.isUseGradients());
1428 for (int i = 0; i < colorLocationCombo.getItems().length; i++) {
1429 String item = colorLocationCombo.getItem(i);
1430 if (item.equals(colorOptions.getLocation().toString())) {
1431 colorLocationCombo.select(i);
1435 for (int i = 0; i < colorSizeCombo.getItems().length; i++) {
1436 String item = colorSizeCombo.getItem(i);
1437 if (item.equals(colorOptions.getSize().toString())) {
1438 colorSizeCombo.select(i);
1443 Map<String, DynamicSizeContribution> sizeContributions = visualisation.getSizeContributions();
1444 for (Entry<String, DynamicSizeContribution> entry : sizeContributions.entrySet()) {
1446 SizingObjectRow sizingObjectRow = sizingRows.get(entry.getKey());
1447 if (sizingObjectRow != null) {
1449 sizingObjectRow.update(entry.getValue());
1452 LOGGER.info("No sizing object visualisation row for key {}", entry.getKey());
1455 SizeBarOptions sizeOptions = visualisation.getSizeBarOptions();
1456 showSizeButton.setSelection(sizeOptions.isShowSizeBars());
1457 sizeTicksButton.setSelection(sizeOptions.isShowSizeBarsTicks());
1458 sizeGradientButton.setSelection(sizeOptions.isUseGradients());
1459 for (int i = 0; i < sizeLocationCombo.getItems().length; i++) {
1460 String item = sizeLocationCombo.getItem(i);
1461 if (item.equals(sizeOptions.getLocation().toString())) {
1462 sizeLocationCombo.select(i);
1466 for (int i = 0; i < sizeSizeCombo.getItems().length; i++) {
1467 String item = sizeSizeCombo.getItem(i);
1468 if (item.equals(sizeOptions.getSize().toString())) {
1469 sizeSizeCombo.select(i);
1477 public void updateVisualisations(Collection<NamedResource> result) {
1478 this.visualisations = result;
1480 Display.getDefault().asyncExec(() -> {
1481 if (getParent().isDisposed())
1483 templateSelectionCombo.setItems(visualisations.stream().map(NamedResource::getName).collect(Collectors.toList()).toArray(new String[visualisations.size()]));
1485 if (visualisation != null) {
1486 String[] items = templateSelectionCombo.getItems();
1487 for (int i = 0; i < items.length; i++) {
1488 if (visualisation.getName().equals(items[i])) {
1489 templateSelectionCombo.select(i);
1498 public Composite getParent() {