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.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Group;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31 import org.simantics.Simantics;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.NamedResource;
36 import org.simantics.db.common.request.UniqueRead;
37 import org.simantics.db.common.request.WriteRequest;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.db.layer0.util.RemoverUtil;
40 import org.simantics.db.procedure.Listener;
41 import org.simantics.district.network.DistrictNetworkUtil;
42 import org.simantics.district.network.ontology.DistrictNetworkResource;
43 import org.simantics.district.network.profile.ActiveDynamicVisualisationsRequest;
44 import org.simantics.district.network.profile.DynamicVisualisationsRequest;
45 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions;
46 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicColoringObject;
47 import org.simantics.district.network.visualisations.DynamicVisualisationsContributions.DynamicSizingObject;
48 import org.simantics.district.network.visualisations.model.ColorBarOptions;
49 import org.simantics.district.network.visualisations.model.ColorBarOptions.ColorBarsLocation;
50 import org.simantics.district.network.visualisations.model.ColorBarOptions.ColorBarsSize;
51 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
52 import org.simantics.district.network.visualisations.model.DynamicColorMap;
53 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
54 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
55 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
56 import org.simantics.district.network.visualisations.model.SizeBarOptions;
57 import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsLocation;
58 import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsSize;
59 import org.simantics.utils.datastructures.Pair;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class DynamicVisualisationsUI extends Composite {
65 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsUI.class);
67 private Resource diagramResource;
68 private VisualisationListener listener;
69 private DynamicVisualisation visualisation;
71 private Button showSizeButton;
72 private Button sizeTicksButton;
73 private Combo sizeLocationCombo;
74 private Combo sizeSizeCombo;
75 private Button showColorButton;
76 private Button colorTicksButton;
77 private Combo colorLocationCombo;
78 private Combo colorSizeCombo;
80 private Combo templateSelectionCombo;
82 private List<Supplier<Pair<String, DynamicColorContribution>>> colorSuppliers;
84 private Button removeVisualisationTemplateButton;
86 private Button applyButton;
88 public DynamicVisualisationsUI(Composite parent, int style) {
90 // ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.V_SCROLL);
91 // scrolledComposite.setLayout(new GridLayout(1, false));
92 // scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
94 // Composite firstContent = new Composite(scrolledComposite, SWT.NONE);
95 // firstContent.setLayout(new GridLayout(1, false));
96 // firstContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
98 defaultInitializeUI(this);
100 // scrolledComposite.setContent(firstContent);
101 // scrolledComposite.setExpandHorizontal(true);
102 // scrolledComposite.setExpandVertical(true);
103 // scrolledComposite.setMinSize(firstContent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
107 private void defaultInitializeUI(Composite parent) {
109 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
110 GridLayoutFactory.fillDefaults().numColumns(1).margins(5, 5).applyTo(parent);
112 Composite selectionComposite = new Composite(parent, SWT.NONE);
113 GridDataFactory.fillDefaults().grab(true, false).applyTo(selectionComposite);
114 GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(selectionComposite);
116 Label templateNameLabel = new Label(selectionComposite, SWT.NONE);
117 templateNameLabel.setText("Visualisation template");
118 templateSelectionCombo = new Combo(selectionComposite, SWT.READ_ONLY);
119 GridDataFactory.fillDefaults().grab(true, false).applyTo(templateSelectionCombo);
120 templateSelectionCombo.addSelectionListener(new SelectionAdapter() {
123 public void widgetSelected(SelectionEvent e) {
124 String item = templateSelectionCombo.getItem(templateSelectionCombo.getSelectionIndex());
125 for (NamedResource template : visualisations) {
126 if (item.equals(template.getName())) {
127 Simantics.getSession().asyncRequest(new WriteRequest() {
130 public void perform(WriteGraph graph) throws DatabaseException {
131 DistrictNetworkUtil.setActiveVisualisation(graph, diagramResource, template.getResource());
140 Composite coloringObjectsComposite = new Composite(parent, SWT.NONE);
141 GridDataFactory.fillDefaults().grab(true, false).applyTo(coloringObjectsComposite);
142 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(coloringObjectsComposite);
143 initializeColoringObjects(coloringObjectsComposite);
145 Composite colorBarsComposite = new Composite(parent, SWT.NONE);
146 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorBarsComposite);
147 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(colorBarsComposite);
148 initializeColorBars(colorBarsComposite);
150 Composite objectSizesComposite = new Composite(parent, SWT.NONE);
151 GridDataFactory.fillDefaults().grab(true, false).applyTo(objectSizesComposite);
152 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(objectSizesComposite);
153 initializeObjectSizes(objectSizesComposite);
155 Composite sizeBarsComposite = new Composite(parent, SWT.NONE);
156 GridDataFactory.fillDefaults().grab(true, false).applyTo(sizeBarsComposite);
157 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(sizeBarsComposite);
158 initializeSizeBars(sizeBarsComposite);
160 Composite buttonBarsComposite = new Composite(parent, SWT.NONE);
161 GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonBarsComposite);
162 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonBarsComposite);
164 Button saveVisualisationTemplateAsButton = new Button(buttonBarsComposite, SWT.NONE);
165 saveVisualisationTemplateAsButton.setText("Save as visualisation template");
166 saveVisualisationTemplateAsButton.addSelectionListener(new SelectionAdapter() {
169 public void widgetSelected(SelectionEvent e) {
170 showSaveVisualisationTemplateDialog(e.widget.getDisplay().getActiveShell());
174 applyButton = new Button(buttonBarsComposite, SWT.NONE);
175 applyButton.setText("Apply");
176 applyButton.setEnabled(visualisation != null);
177 applyButton.addSelectionListener(new SelectionAdapter() {
180 public void widgetSelected(SelectionEvent e) {
181 persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
185 removeVisualisationTemplateButton = new Button(buttonBarsComposite, SWT.NONE);
186 removeVisualisationTemplateButton.setText("Remove");
187 removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null);
188 removeVisualisationTemplateButton.addSelectionListener(new SelectionAdapter() {
191 public void widgetSelected(SelectionEvent e) {
192 removeVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
198 protected void removeVisualisationTemplate(String name, Optional<Resource> of) {
199 if (of.isPresent()) {
200 Resource visualisation = of.get();
201 Simantics.getSession().asyncRequest(new WriteRequest() {
204 public void perform(WriteGraph graph) throws DatabaseException {
205 RemoverUtil.remove(graph, visualisation);
211 private void showSaveVisualisationTemplateDialog(Shell shell) {
213 InputDialog dialog = new InputDialog(shell, "Save visualisation template", "Give template a name", "", new IInputValidator() {
216 public String isValid(String newText) {
217 if (newText == null || newText.isEmpty())
218 return "Name cannot be empty";
223 if (dialog.open() == Dialog.OK) {
224 String name = dialog.getValue();
225 persistVisualisationTemplate(name, Optional.empty());
229 private void persistVisualisationTemplate(String templateName, Optional<Resource> existing) {
231 List<Pair<String, DynamicColorContribution>> colorCollect = colorSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
233 String colorLocation = colorLocationCombo.getItem(colorLocationCombo.getSelectionIndex());
234 String colorSize = colorSizeCombo.getItem(colorSizeCombo.getSelectionIndex());
236 ColorBarOptions colorBarOptions = new ColorBarOptions()
237 .showColorBars(showColorButton.getSelection())
238 .showColorBarsTicks(colorTicksButton.getSelection())
239 .withLocation(ColorBarsLocation.valueOf(colorLocation))
240 .withSize(ColorBarsSize.valueOf(colorSize));
242 List<Pair<String, DynamicSizeContribution>> sizeCollect = sizeSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
244 String sizeLocation = sizeLocationCombo.getItem(sizeLocationCombo.getSelectionIndex());
245 String sizeSize = sizeSizeCombo.getItem(sizeSizeCombo.getSelectionIndex());
247 SizeBarOptions sizeBarOptions = new SizeBarOptions()
248 .showSizeBars(showSizeButton.getSelection())
249 .showSizeBarsTicks(sizeTicksButton.getSelection())
250 .withLocation(SizeBarsLocation.valueOf(sizeLocation))
251 .withSize(SizeBarsSize.valueOf(sizeSize));
253 Simantics.getSession().asyncRequest(new WriteRequest() {
256 public void perform(WriteGraph graph) throws DatabaseException {
257 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
259 if (existing.isPresent()) {
260 exist = existing.get();
262 exist = DistrictNetworkUtil.createVisualisation(graph, diagramResource, templateName);
265 DistrictNetworkUtil.setColorContributions(graph, exist, colorCollect);
267 DistrictNetworkUtil.setColorBarOptions(graph, exist, colorBarOptions);
268 DistrictNetworkUtil.setSizeContributions(graph, exist, sizeCollect);
269 DistrictNetworkUtil.setSizeBarOptions(graph, exist, sizeBarOptions);
274 private void initializeColoringObjects(Composite parent) {
275 Group group = new Group(parent, SWT.NONE);
276 group.setText("Coloring Objects");
277 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
278 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
281 createColoringObjectHeaderRow(group);
283 colorSuppliers = new ArrayList<>();
286 Collection<DynamicColoringObject> result = Simantics.getSession().syncRequest(new UniqueRead<Collection<DynamicColoringObject>>() {
289 public Collection<DynamicColoringObject> perform(ReadGraph graph) throws DatabaseException {
290 return DynamicVisualisationsContributions.dynamicColoringObjects(graph);
294 for (DynamicColoringObject object : result) {
295 colorSuppliers.add(createColoringObjectRow(group, object));
298 } catch (DatabaseException e) {
299 LOGGER.error("Could not create coloring objecst", e);
304 private void createColoringObjectHeaderRow(Composite parent) {
306 Label label = new Label(parent, SWT.NONE);
307 label.setText("Label");
308 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
310 label = new Label(parent, SWT.NONE);
311 label.setText("Used");
312 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
314 label = new Label(parent, SWT.NONE);
315 label.setText("Variable");
316 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
318 label = new Label(parent, SWT.NONE);
319 label.setText("Min");
320 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
322 label = new Label(parent, SWT.NONE);
323 label.setText("Max");
324 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
326 label = new Label(parent, SWT.NONE);
327 label.setText("Unit");
328 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
330 label = new Label(parent, SWT.NONE);
331 label.setText("ColorMap");
332 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
334 label = new Label(parent, SWT.NONE);
335 label.setText("Default");
336 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
339 private Map<String, ColoringObjectRow> coloringRows = new HashMap<>();
340 private Map<String, SizingObjectRow> sizingRows = new HashMap<>();
342 private VisualisationsListener visualisationsListener;
344 private Collection<NamedResource> visualisations;
346 private List<Supplier<Pair<String, DynamicSizeContribution>>> sizeSuppliers;
348 private static class ColoringObjectRow {
350 private final Label label;
351 private final Button usedButton;
352 private final Combo variableCombo;
353 private final Text minText;
354 private final Text maxText;
355 private final Label unit;
356 private final Combo colorMapCombo;
357 private final Button defaultButton;
359 public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit,
360 Combo colorMapCombo, Button defaultButton) {
363 this.usedButton = usedButton;
364 this.variableCombo = variableCombo;
365 this.minText = minText;
366 this.maxText = maxText;
368 this.colorMapCombo = colorMapCombo;
369 this.defaultButton = defaultButton;
372 public void update(DynamicColorContribution colorContribution) {
373 String[] items = variableCombo.getItems();
374 for (int i = 0; i < items.length; i++) {
375 if (colorContribution.getLabel().equals(items[i])) {
376 variableCombo.select(i);
380 minText.setText(Double.toString(colorContribution.getDefaultMin()));
381 maxText.setText(Double.toString(colorContribution.getDefaultMax()));
382 unit.setText(colorContribution.getUnit());
384 // color map only supports single for now
385 colorMapCombo.setItems(colorContribution.getDefaultColorMap().getLabel());
386 colorMapCombo.select(0);
387 // String[] colorItems = colorMapCombo.getItems();
388 // for (int i = 0; i < colorItems.length; i++) {
390 // if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) {
391 // colorMapCombo.select(i);
395 usedButton.setSelection(colorContribution.isUsed());
396 defaultButton.setSelection(colorContribution.isUseDefault());
398 minText.setEnabled(!colorContribution.isUseDefault());
399 maxText.setEnabled(!colorContribution.isUseDefault());
400 colorMapCombo.setEnabled(!colorContribution.isUseDefault());
404 private static class SizingObjectRow {
406 private final Label label;
407 private final Button usedButton;
408 private final Combo variableCombo;
409 private final Text minText;
410 private final Text maxText;
411 private final Label unit;
412 private final Combo sizeMapCombo;
413 private final Button defaultButton;
415 public SizingObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit,
416 Combo sizeMapCombo, Button defaultButton) {
419 this.usedButton = usedButton;
420 this.variableCombo = variableCombo;
421 this.minText = minText;
422 this.maxText = maxText;
424 this.sizeMapCombo = sizeMapCombo;
425 this.defaultButton = defaultButton;
428 public void update(DynamicSizeContribution sizeContribution) {
429 String[] items = variableCombo.getItems();
430 for (int i = 0; i < items.length; i++) {
431 if (sizeContribution.getLabel().equals(items[i])) {
432 variableCombo.select(i);
436 minText.setText(Double.toString(sizeContribution.getDefaultMin()));
437 maxText.setText(Double.toString(sizeContribution.getDefaultMax()));
438 unit.setText(sizeContribution.getUnit());
440 // color map only supports single for now
441 sizeMapCombo.setItems(sizeContribution.getDefaultSizeMap().getLabel());
442 sizeMapCombo.select(0);
443 // String[] colorItems = colorMapCombo.getItems();
444 // for (int i = 0; i < colorItems.length; i++) {
446 // if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) {
447 // colorMapCombo.select(i);
451 usedButton.setSelection(sizeContribution.isUsed());
452 defaultButton.setSelection(sizeContribution.isUseDefault());
454 minText.setEnabled(!sizeContribution.isUseDefault());
455 maxText.setEnabled(!sizeContribution.isUseDefault());
456 sizeMapCombo.setEnabled(!sizeContribution.isUseDefault());
460 private Supplier<Pair<String, DynamicColorContribution>> createColoringObjectRow(Composite parent, DynamicColoringObject object) {
461 Label label = new Label(parent, SWT.NONE);
462 label.setText(object.getColoringObject().getName());
463 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
465 Map<String, DynamicColorContribution> colorContributions = object.getColorContributions();
467 Button usedButton = new Button(parent, SWT.CHECK);
468 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
470 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
471 variableCombo.setItems(colorContributions.keySet().toArray(new String[colorContributions.size()]));
473 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
475 Text minText = new Text(parent, SWT.BORDER);
476 GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(minText);
478 Text maxText = new Text(parent, SWT.BORDER);
479 GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(maxText);
481 Label unit = new Label(parent, SWT.NONE);
483 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit);
485 Combo colorMapCombo = new Combo(parent, SWT.READ_ONLY);
486 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorMapCombo);
488 Button defaultButton = new Button(parent, SWT.CHECK);
489 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
490 defaultButton.addSelectionListener(new SelectionAdapter() {
493 public void widgetSelected(SelectionEvent e) {
494 int index = variableCombo.getSelectionIndex();
496 String key = variableCombo.getItem(index);
497 DynamicColorContribution cont = colorContributions.get(key);
499 minText.setText(Double.toString(cont.getDefaultMin()));
500 minText.setEnabled(!defaultButton.getSelection());
501 maxText.setText(Double.toString(cont.getDefaultMax()));
502 maxText.setEnabled(!defaultButton.getSelection());
503 unit.setText(cont.getUnit());
505 colorMapCombo.setItems(cont.getDefaultColorMap().getLabel());
506 colorMapCombo.select(0);
507 colorMapCombo.setEnabled(!defaultButton.getSelection());
512 variableCombo.addSelectionListener(new SelectionAdapter() {
515 public void widgetSelected(SelectionEvent e) {
516 // handle update for others
517 int index = variableCombo.getSelectionIndex();
519 String key = variableCombo.getItem(index);
520 DynamicColorContribution cont = colorContributions.get(key);
522 if (minText.getText().isEmpty()) {
523 minText.setText(Double.toString(cont.getDefaultMin()));
525 if (maxText.getText().isEmpty()) {
526 maxText.setText(Double.toString(cont.getDefaultMax()));
528 unit.setText(cont.getUnit());
530 colorMapCombo.setItems(cont.getDefaultColorMap().getLabel());
531 colorMapCombo.select(0);
533 defaultButton.setSelection(true);
538 coloringRows.put(object.getColoringObject().getName(), new ColoringObjectRow(label, usedButton, variableCombo, minText, maxText, unit, colorMapCombo, defaultButton));
540 return new Supplier<Pair<String, DynamicColorContribution>>() {
543 public Pair<String, DynamicColorContribution> get() {
544 int selectionIndex = variableCombo.getSelectionIndex();
545 if (selectionIndex >= 0) {
546 String key = variableCombo.getItem(selectionIndex);
547 DynamicColorContribution cont = colorContributions.get(key);
549 String colorMap = colorMapCombo.getItem(colorMapCombo.getSelectionIndex());
551 Map<String, DynamicColorMap> colorMaps = Simantics.getSession().syncRequest(new UniqueRead<Map<String, DynamicColorMap>>() {
554 public Map<String, DynamicColorMap> perform(ReadGraph graph) throws DatabaseException {
555 return DynamicVisualisationsContributions.dynamicColorMaps(graph);
558 DynamicColorMap dColorMap = colorMaps.get(colorMap);
559 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
561 DynamicColorContribution dcc = new DynamicColorContribution(label, cont.getModuleName(), cont.getAttributeName(), unit.getText(), cont.getVariableGain(), cont.getVariableBias(), dColorMap, Double.parseDouble(minText.getText()), Double.parseDouble(maxText.getText()));
562 dcc.setUsed(usedButton.getSelection());
563 dcc.setUseDefault(defaultButton.getSelection());
565 return Pair.make(object.getColoringObject().getName(), dcc);
566 } catch (DatabaseException e) {
567 LOGGER.error("Could not get DynamicColorContribution", e);
576 private void createSizingObjectHeaderRow(Composite parent) {
578 Label label = new Label(parent, SWT.NONE);
579 label.setText("Label");
580 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
582 label = new Label(parent, SWT.NONE);
583 label.setText("Used");
584 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
586 label = new Label(parent, SWT.NONE);
587 label.setText("Variable");
588 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
590 label = new Label(parent, SWT.NONE);
591 label.setText("Min");
592 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
594 label = new Label(parent, SWT.NONE);
595 label.setText("Max");
596 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
598 label = new Label(parent, SWT.NONE);
599 label.setText("Unit");
600 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
602 label = new Label(parent, SWT.NONE);
603 label.setText("SizeMap");
604 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
606 label = new Label(parent, SWT.NONE);
607 label.setText("Default");
608 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
611 private Supplier<Pair<String, DynamicSizeContribution>> createSizingObjectRow(Composite parent, DynamicSizingObject object) {
612 Label label = new Label(parent, SWT.NONE);
613 label.setText(object.getSizingObject().getName());
614 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
616 Map<String, DynamicSizeContribution> sizeContributions = object.getSizeContributions();
618 Button usedButton = new Button(parent, SWT.CHECK);
619 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
621 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
622 variableCombo.setItems(sizeContributions.keySet().toArray(new String[sizeContributions.size()]));
624 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
626 Text minText = new Text(parent, SWT.BORDER);
627 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(minText);
629 Text maxText = new Text(parent, SWT.BORDER);
630 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(maxText);
632 Label unit = new Label(parent, SWT.NONE);
634 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit);
636 Combo sizeMapCombo = new Combo(parent, SWT.READ_ONLY);
637 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(sizeMapCombo);
639 Button defaultButton = new Button(parent, SWT.CHECK);
640 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
642 variableCombo.addSelectionListener(new SelectionAdapter() {
645 public void widgetSelected(SelectionEvent e) {
646 // handle update for others
647 String key = variableCombo.getItem(variableCombo.getSelectionIndex());
648 DynamicSizeContribution cont = sizeContributions.get(key);
650 if (minText.getText().isEmpty()) {
651 minText.setText(Double.toString(cont.getDefaultMin()));
653 if (maxText.getText().isEmpty()) {
654 maxText.setText(Double.toString(cont.getDefaultMax()));
656 unit.setText(cont.getUnit());
658 sizeMapCombo.setItems(cont.getDefaultSizeMap().getLabel());
659 sizeMapCombo.select(0);
661 defaultButton.setSelection(true);
665 sizingRows.put(object.getSizingObject().getName(), new SizingObjectRow(label, usedButton, variableCombo, minText, maxText, unit, sizeMapCombo, defaultButton));
667 return new Supplier<Pair<String, DynamicSizeContribution>>() {
670 public Pair<String, DynamicSizeContribution> get() {
671 int selectionIndex = variableCombo.getSelectionIndex();
672 if (selectionIndex >= 0) {
673 String key = variableCombo.getItem(selectionIndex);
674 DynamicSizeContribution cont = sizeContributions.get(key);
676 String sizeMap = sizeMapCombo.getItem(sizeMapCombo.getSelectionIndex());
678 Map<String, DynamicSizeMap> sizeMaps = Simantics.getSession().syncRequest(new UniqueRead<Map<String, DynamicSizeMap>>() {
681 public Map<String, DynamicSizeMap> perform(ReadGraph graph) throws DatabaseException {
682 return DynamicVisualisationsContributions.dynamicSizeMaps(graph);
685 DynamicSizeMap dColorMap = sizeMaps.get(sizeMap);
686 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
688 DynamicSizeContribution dsc = new DynamicSizeContribution(label, cont.getModuleName(), cont.getAttributeName(), unit.getText(), cont.getVariableGain(), cont.getVariableBias(), dColorMap, Double.parseDouble(minText.getText()), Double.parseDouble(maxText.getText()));
689 dsc.setUsed(usedButton.getSelection());
690 dsc.setUseDefault(defaultButton.getSelection());
692 return Pair.make(object.getSizingObject().getName(), dsc);
693 } catch (DatabaseException e) {
694 LOGGER.error("Could not get DynamicColorContribution", e);
703 private void initializeColorBars(Composite parent) {
704 Group group = new Group(parent, SWT.NONE);
705 group.setText("Color Bars");
706 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
707 GridLayoutFactory.fillDefaults().numColumns(6).margins(5, 5).applyTo(group);
709 createColorBars(group);
712 private void createColorBars(Composite parent) {
714 showColorButton = new Button(parent, SWT.CHECK);
715 showColorButton.setText("Show");
717 colorTicksButton = new Button(parent, SWT.CHECK);
718 colorTicksButton.setText("Ticks");
720 Label label = new Label(parent, SWT.NONE);
721 label.setText("Location");
722 colorLocationCombo = new Combo(parent, SWT.READ_ONLY);
723 colorLocationCombo.setItems(Stream.of(ColorBarsLocation.values()).map(size -> size.toString()).toArray(String[]::new));
725 label = new Label(parent, SWT.NONE);
726 label.setText("Size");
727 colorSizeCombo = new Combo(parent, SWT.READ_ONLY);
728 colorSizeCombo.setItems(Stream.of(ColorBarsSize.values()).map(size -> size.toString()).toArray(String[]::new));
731 private void initializeObjectSizes(Composite parent) {
732 Group group = new Group(parent, SWT.NONE);
733 group.setText("Object Sizes");
734 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
735 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
738 createSizingObjectHeaderRow(group);
739 createObjectSizes(group);
743 private void createObjectSizes(Composite parent) {
745 sizeSuppliers = new ArrayList<>();
747 Collection<DynamicSizingObject> resultSizing = Simantics.getSession().syncRequest(new UniqueRead<Collection<DynamicSizingObject>>() {
750 public Collection<DynamicSizingObject> perform(ReadGraph graph) throws DatabaseException {
751 return DynamicVisualisationsContributions.dynamicSizingObjects(graph);
755 for (DynamicSizingObject object : resultSizing) {
756 sizeSuppliers.add(createSizingObjectRow(parent, object));
758 } catch (DatabaseException e) {
759 LOGGER.error("Could not create object sizes", e);
763 private void initializeSizeBars(Composite parent) {
764 Group group = new Group(parent, SWT.NONE);
765 group.setText("Size Bars");
766 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
767 GridLayoutFactory.fillDefaults().numColumns(6).margins(5, 5).applyTo(group);
769 createSizeBars(group);
772 private void createSizeBars(Composite parent) {
773 showSizeButton = new Button(parent, SWT.CHECK);
774 showSizeButton.setText("Show");
776 sizeTicksButton = new Button(parent, SWT.CHECK);
777 sizeTicksButton.setText("Ticks");
779 Label label = new Label(parent, SWT.NONE);
780 label.setText("Location");
781 sizeLocationCombo = new Combo(parent, SWT.READ_ONLY);
782 sizeLocationCombo.setItems(Stream.of(SizeBarsLocation.values()).map(size -> size.toString()).toArray(String[]::new));
784 label = new Label(parent, SWT.NONE);
785 label.setText("Size");
786 sizeSizeCombo = new Combo(parent, SWT.READ_ONLY);
787 sizeSizeCombo.setItems(Stream.of(SizeBarsSize.values()).map(size -> size.toString()).toArray(String[]::new));
790 public void setDiagramResource(Resource diagramResource) {
791 if (this.diagramResource != diagramResource) {
792 this.diagramResource = diagramResource;
797 private void updateListening() {
798 if (visualisationsListener != null)
799 visualisationsListener.dispose();
800 visualisationsListener = new VisualisationsListener(this);
801 Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(diagramResource), visualisationsListener);
803 if (listener != null)
805 listener = new VisualisationListener(this);
806 Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(diagramResource), listener);
809 private static class VisualisationsListener implements Listener<Collection<NamedResource>> {
811 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationsListener.class);
813 private boolean disposed;
814 private DynamicVisualisationsUI ui;
816 public VisualisationsListener(DynamicVisualisationsUI ui) {
821 public void execute(Collection<NamedResource> result) {
822 ui.updateVisualisations(result);
826 public void exception(Throwable t) {
827 LOGGER.error("Could not listen visualisation", t);
831 public boolean isDisposed() {
832 return disposed || ui.isDisposed();
835 public void dispose() {
836 this.disposed = true;
840 private static class VisualisationListener implements Listener<DynamicVisualisation> {
842 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationListener.class);
844 private boolean disposed;
845 private DynamicVisualisationsUI ui;
847 public VisualisationListener(DynamicVisualisationsUI ui) {
852 public void execute(DynamicVisualisation result) {
853 ui.updateVisualisation(result);
857 public void exception(Throwable t) {
858 LOGGER.error("Could not listen visualisation", t);
862 public boolean isDisposed() {
863 return disposed || ui.isDisposed();
866 public void dispose() {
867 this.disposed = true;
871 public void updateVisualisation(DynamicVisualisation result) {
872 this.visualisation = result;
873 if (visualisation != null) {
874 Display.getDefault().asyncExec(() -> {
875 if (getParent().isDisposed())
878 applyButton.setEnabled(visualisation != null);
880 String[] items = templateSelectionCombo.getItems();
881 for (int i = 0; i < items.length; i++) {
882 if (visualisation.getName().equals(items[i])) {
883 templateSelectionCombo.select(i);
888 Map<String, DynamicColorContribution> colorContributions = visualisation.getColorContributions();
889 for (Entry<String, DynamicColorContribution> entry : colorContributions.entrySet()) {
891 ColoringObjectRow coloringObjectRow = coloringRows.get(entry.getKey());
892 if (coloringObjectRow != null) {
894 coloringObjectRow.update(entry.getValue());
897 LOGGER.info("No coloring object visualisation row for key {}", entry.getKey());
900 ColorBarOptions colorOptions = visualisation.getColorBarOptions();
901 showColorButton.setSelection(colorOptions.isShowColorBars());
902 colorTicksButton.setSelection(colorOptions.isShowColorBarsTicks());
903 for (int i = 0; i < colorLocationCombo.getItems().length; i++) {
904 String item = colorLocationCombo.getItem(i);
905 if (item.equals(colorOptions.getLocation().toString())) {
906 colorLocationCombo.select(i);
910 for (int i = 0; i < colorSizeCombo.getItems().length; i++) {
911 String item = colorSizeCombo.getItem(i);
912 if (item.equals(colorOptions.getSize().toString())) {
913 colorSizeCombo.select(i);
918 Map<String, DynamicSizeContribution> sizeContributions = visualisation.getSizeContributions();
919 for (Entry<String, DynamicSizeContribution> entry : sizeContributions.entrySet()) {
921 SizingObjectRow sizingObjectRow = sizingRows.get(entry.getKey());
922 if (sizingObjectRow != null) {
924 sizingObjectRow.update(entry.getValue());
927 LOGGER.info("No sizing object visualisation row for key {}", entry.getKey());
930 SizeBarOptions sizeOptions = visualisation.getSizeBarOptions();
931 showSizeButton.setSelection(sizeOptions.isShowSizeBars());
932 sizeTicksButton.setSelection(sizeOptions.isShowSizeBarsTicks());
933 for (int i = 0; i < sizeLocationCombo.getItems().length; i++) {
934 String item = sizeLocationCombo.getItem(i);
935 if (item.equals(sizeOptions.getLocation().toString())) {
936 sizeLocationCombo.select(i);
940 for (int i = 0; i < sizeSizeCombo.getItems().length; i++) {
941 String item = sizeSizeCombo.getItem(i);
942 if (item.equals(sizeOptions.getSize().toString())) {
943 sizeSizeCombo.select(i);
949 removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null);
954 public void updateVisualisations(Collection<NamedResource> result) {
955 this.visualisations = result;
957 Display.getDefault().asyncExec(() -> {
958 if (getParent().isDisposed())
960 templateSelectionCombo.setItems(visualisations.stream().map(NamedResource::getName).collect(Collectors.toList()).toArray(new String[visualisations.size()]));
962 if (visualisation != null) {
963 String[] items = templateSelectionCombo.getItems();
964 for (int i = 0; i < items.length; i++) {
965 if (visualisation.getName().equals(items[i])) {
966 templateSelectionCombo.select(i);