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.eclipse.ui.IEditorPart;
32 import org.simantics.Simantics;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.WriteGraph;
36 import org.simantics.db.common.NamedResource;
37 import org.simantics.db.common.request.UniqueRead;
38 import org.simantics.db.common.request.WriteRequest;
39 import org.simantics.db.exception.DatabaseException;
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.ui.workbench.IResourceEditorPart;
60 import org.simantics.utils.datastructures.Pair;
61 import org.simantics.utils.ui.workbench.WorkbenchUtils;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 public class DynamicVisualisationsUI extends Composite {
67 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsUI.class);
69 private Resource diagramResource;
70 private VisualisationListener listener;
71 private DynamicVisualisation visualisation;
73 private Button showSizeButton;
74 private Button sizeTicksButton;
75 private Combo sizeLocationCombo;
76 private Combo sizeSizeCombo;
77 private Button showColorButton;
78 private Button colorTicksButton;
79 private Combo colorLocationCombo;
80 private Combo colorSizeCombo;
82 private Combo templateSelectionCombo;
84 private List<Supplier<Pair<String, DynamicColorContribution>>> colorSuppliers;
86 public DynamicVisualisationsUI(Composite parent, int style) {
89 defaultInitializeUI();
92 private void defaultInitializeUI() {
93 GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
94 GridLayoutFactory.fillDefaults().numColumns(1).margins(5, 5).applyTo(this);
96 Composite selectionComposite = new Composite(this, SWT.NONE);
97 GridDataFactory.fillDefaults().grab(true, false).applyTo(selectionComposite);
98 GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(selectionComposite);
100 Label templateNameLabel = new Label(selectionComposite, SWT.NONE);
101 templateNameLabel.setText("Visualisation template");
102 templateSelectionCombo = new Combo(selectionComposite, SWT.READ_ONLY);
103 GridDataFactory.fillDefaults().grab(true, false).applyTo(templateSelectionCombo);
104 templateSelectionCombo.addSelectionListener(new SelectionAdapter() {
107 public void widgetSelected(SelectionEvent e) {
108 String item = templateSelectionCombo.getItem(templateSelectionCombo.getSelectionIndex());
109 for (NamedResource template : visualisations) {
110 if (item.equals(template.getName())) {
111 Simantics.getSession().asyncRequest(new WriteRequest() {
114 public void perform(WriteGraph graph) throws DatabaseException {
115 DistrictNetworkUtil.setActiveVisualisation(graph, diagramResource, template.getResource());
124 Composite coloringObjectsComposite = new Composite(this, SWT.NONE);
125 GridDataFactory.fillDefaults().grab(true, false).applyTo(coloringObjectsComposite);
126 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(coloringObjectsComposite);
127 initializeColoringObjects(coloringObjectsComposite);
129 Composite colorBarsComposite = new Composite(this, SWT.NONE);
130 GridDataFactory.fillDefaults().grab(true, false).applyTo(colorBarsComposite);
131 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(colorBarsComposite);
132 initializeColorBars(colorBarsComposite);
134 Composite objectSizesComposite = new Composite(this, SWT.NONE);
135 GridDataFactory.fillDefaults().grab(true, false).applyTo(objectSizesComposite);
136 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(objectSizesComposite);
137 initializeObjectSizes(objectSizesComposite);
139 Composite sizeBarsComposite = new Composite(this, SWT.NONE);
140 GridDataFactory.fillDefaults().grab(true, false).applyTo(sizeBarsComposite);
141 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(sizeBarsComposite);
142 initializeSizeBars(sizeBarsComposite);
144 Button saveVisualisationTemplateButton = new Button(this, SWT.NONE);
145 saveVisualisationTemplateButton.setText("Save");
146 saveVisualisationTemplateButton.setEnabled(visualisation == null || visualisation.getVisualisationResource() != null);
147 saveVisualisationTemplateButton.addSelectionListener(new SelectionAdapter() {
150 public void widgetSelected(SelectionEvent e) {
151 persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource()));
155 Button saveVisualisationTemplateAsButton = new Button(this, SWT.NONE);
156 saveVisualisationTemplateAsButton.setText("Save as visualisation template");
157 saveVisualisationTemplateAsButton.addSelectionListener(new SelectionAdapter() {
160 public void widgetSelected(SelectionEvent e) {
161 showSaveVisualisationTemplateDialog(e.widget.getDisplay().getActiveShell());
167 private void showSaveVisualisationTemplateDialog(Shell shell) {
169 InputDialog dialog = new InputDialog(shell, "Save visualisation template", "Give template a name", "", new IInputValidator() {
172 public String isValid(String newText) {
173 if (newText == null || newText.isEmpty())
174 return "Name cannot be empty";
179 if (dialog.open() == Dialog.OK) {
180 String name = dialog.getValue();
181 persistVisualisationTemplate(name, Optional.empty());
185 private void persistVisualisationTemplate(String templateName, Optional<Resource> existing) {
187 List<Pair<String, DynamicColorContribution>> colorCollect = colorSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
189 String colorLocation = colorLocationCombo.getItem(colorLocationCombo.getSelectionIndex());
190 String colorSize = colorSizeCombo.getItem(colorSizeCombo.getSelectionIndex());
192 ColorBarOptions colorBarOptions = new ColorBarOptions()
193 .showColorBars(showColorButton.getSelection())
194 .showColorBarsTicks(colorTicksButton.getSelection())
195 .withLocation(ColorBarsLocation.valueOf(colorLocation))
196 .withSize(ColorBarsSize.valueOf(colorSize));
198 List<Pair<String, DynamicSizeContribution>> sizeCollect = sizeSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
200 String sizeLocation = sizeLocationCombo.getItem(sizeLocationCombo.getSelectionIndex());
201 String sizeSize = sizeSizeCombo.getItem(sizeSizeCombo.getSelectionIndex());
203 SizeBarOptions sizeBarOptions = new SizeBarOptions()
204 .showSizeBars(showSizeButton.getSelection())
205 .showSizeBarsTicks(sizeTicksButton.getSelection())
206 .withLocation(SizeBarsLocation.valueOf(sizeLocation))
207 .withSize(SizeBarsSize.valueOf(sizeSize));
209 Simantics.getSession().asyncRequest(new WriteRequest() {
212 public void perform(WriteGraph graph) throws DatabaseException {
213 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
215 if (existing.isPresent()) {
216 exist = existing.get();
218 exist = DistrictNetworkUtil.createVisualisation(graph, diagramResource, templateName);
221 DistrictNetworkUtil.setColorContributions(graph, exist, colorCollect);
223 DistrictNetworkUtil.setColorBarOptions(graph, exist, colorBarOptions);
224 DistrictNetworkUtil.setSizeContributions(graph, exist, sizeCollect);
225 DistrictNetworkUtil.setSizeBarOptions(graph, exist, sizeBarOptions);
230 private void initializeColoringObjects(Composite parent) {
231 Group group = new Group(parent, SWT.NONE);
232 group.setText("Coloring Objects");
233 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
234 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
237 createColoringObjectHeaderRow(group);
239 colorSuppliers = new ArrayList<>();
242 Collection<DynamicColoringObject> result = Simantics.getSession().syncRequest(new UniqueRead<Collection<DynamicColoringObject>>() {
245 public Collection<DynamicColoringObject> perform(ReadGraph graph) throws DatabaseException {
246 return DynamicVisualisationsContributions.dynamicColoringObjects(graph);
250 for (DynamicColoringObject object : result) {
251 colorSuppliers.add(createColoringObjectRow(group, object));
254 } catch (DatabaseException e) {
255 LOGGER.error("Could not create coloring objecst", e);
259 Button applyButton = new Button(group, SWT.NONE);
260 applyButton.setText("Apply");
261 applyButton.addSelectionListener(new SelectionAdapter() {
264 public void widgetSelected(SelectionEvent e) {
265 List<Pair<String, DynamicColorContribution>> collect = colorSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
266 Simantics.getSession().asyncRequest(new WriteRequest() {
269 public void perform(WriteGraph graph) throws DatabaseException {
270 DistrictNetworkUtil.setColorContributions(graph, visualisation.getVisualisationResource(), collect);
278 private void createColoringObjectHeaderRow(Composite parent) {
280 Label label = new Label(parent, SWT.NONE);
281 label.setText("Label");
282 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
284 label = new Label(parent, SWT.NONE);
285 label.setText("Used");
286 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
288 label = new Label(parent, SWT.NONE);
289 label.setText("Variable");
290 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
292 label = new Label(parent, SWT.NONE);
293 label.setText("Min");
294 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
296 label = new Label(parent, SWT.NONE);
297 label.setText("Max");
298 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
300 label = new Label(parent, SWT.NONE);
301 label.setText("Unit");
302 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
304 label = new Label(parent, SWT.NONE);
305 label.setText("ColorMap");
306 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
308 label = new Label(parent, SWT.NONE);
309 label.setText("Default");
310 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
313 private Map<String, ColoringObjectRow> coloringRows = new HashMap<>();
314 private Map<String, SizingObjectRow> sizingRows = new HashMap<>();
316 private VisualisationsListener visualisationsListener;
318 private Collection<NamedResource> visualisations;
320 private List<Supplier<Pair<String, DynamicSizeContribution>>> sizeSuppliers;
322 private static class ColoringObjectRow {
324 private final Label label;
325 private final Button usedButton;
326 private final Combo variableCombo;
327 private final Text minText;
328 private final Text maxText;
329 private final Label unit;
330 private final Combo colorMapCombo;
331 private final Button defaultButton;
333 public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit,
334 Combo colorMapCombo, Button defaultButton) {
337 this.usedButton = usedButton;
338 this.variableCombo = variableCombo;
339 this.minText = minText;
340 this.maxText = maxText;
342 this.colorMapCombo = colorMapCombo;
343 this.defaultButton = defaultButton;
346 public void update(DynamicColorContribution colorContribution) {
347 String[] items = variableCombo.getItems();
348 for (int i = 0; i < items.length; i++) {
349 if (colorContribution.getLabel().equals(items[i])) {
350 variableCombo.select(i);
354 minText.setText(Double.toString(colorContribution.getDefaultMin()));
355 maxText.setText(Double.toString(colorContribution.getDefaultMax()));
356 unit.setText(colorContribution.getUnit());
358 // color map only supports single for now
359 colorMapCombo.setItems(colorContribution.getDefaultColorMap().getLabel());
360 colorMapCombo.select(0);
361 // String[] colorItems = colorMapCombo.getItems();
362 // for (int i = 0; i < colorItems.length; i++) {
364 // if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) {
365 // colorMapCombo.select(i);
369 usedButton.setSelection(colorContribution.isUsed());
370 defaultButton.setSelection(colorContribution.isUseDefault());
372 minText.setEnabled(!colorContribution.isUseDefault());
373 maxText.setEnabled(!colorContribution.isUseDefault());
374 colorMapCombo.setEnabled(!colorContribution.isUseDefault());
378 private static class SizingObjectRow {
380 private final Label label;
381 private final Button usedButton;
382 private final Combo variableCombo;
383 private final Text minText;
384 private final Text maxText;
385 private final Label unit;
386 private final Combo sizeMapCombo;
387 private final Button defaultButton;
389 public SizingObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit,
390 Combo sizeMapCombo, Button defaultButton) {
393 this.usedButton = usedButton;
394 this.variableCombo = variableCombo;
395 this.minText = minText;
396 this.maxText = maxText;
398 this.sizeMapCombo = sizeMapCombo;
399 this.defaultButton = defaultButton;
402 public void update(DynamicSizeContribution sizeContribution) {
403 String[] items = variableCombo.getItems();
404 for (int i = 0; i < items.length; i++) {
405 if (sizeContribution.getLabel().equals(items[i])) {
406 variableCombo.select(i);
410 minText.setText(Double.toString(sizeContribution.getDefaultMin()));
411 maxText.setText(Double.toString(sizeContribution.getDefaultMax()));
412 unit.setText(sizeContribution.getUnit());
414 // color map only supports single for now
415 sizeMapCombo.setItems(sizeContribution.getDefaultSizeMap().getLabel());
416 sizeMapCombo.select(0);
417 // String[] colorItems = colorMapCombo.getItems();
418 // for (int i = 0; i < colorItems.length; i++) {
420 // if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) {
421 // colorMapCombo.select(i);
428 private Supplier<Pair<String, DynamicColorContribution>> createColoringObjectRow(Composite parent, DynamicColoringObject object) {
429 Label label = new Label(parent, SWT.NONE);
430 label.setText(object.getColoringObject().getName());
431 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
433 Map<String, DynamicColorContribution> colorContributions = object.getColorContributions();
435 Button usedButton = new Button(parent, SWT.CHECK);
436 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
438 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
439 variableCombo.setItems(colorContributions.keySet().toArray(new String[colorContributions.size()]));
441 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
443 Text minText = new Text(parent, SWT.BORDER);
444 GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(minText);
446 Text maxText = new Text(parent, SWT.BORDER);
447 GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(maxText);
449 Label unit = new Label(parent, SWT.NONE);
451 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit);
453 Combo colorMapCombo = new Combo(parent, SWT.READ_ONLY);
454 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorMapCombo);
456 Button defaultButton = new Button(parent, SWT.CHECK);
457 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
458 defaultButton.addSelectionListener(new SelectionAdapter() {
461 public void widgetSelected(SelectionEvent e) {
462 int index = variableCombo.getSelectionIndex();
464 String key = variableCombo.getItem(index);
465 DynamicColorContribution cont = colorContributions.get(key);
467 minText.setText(Double.toString(cont.getDefaultMin()));
468 minText.setEnabled(!defaultButton.getSelection());
469 maxText.setText(Double.toString(cont.getDefaultMax()));
470 maxText.setEnabled(!defaultButton.getSelection());
471 unit.setText(cont.getUnit());
473 colorMapCombo.setItems(cont.getDefaultColorMap().getLabel());
474 colorMapCombo.select(0);
475 colorMapCombo.setEnabled(!defaultButton.getSelection());
480 variableCombo.addSelectionListener(new SelectionAdapter() {
483 public void widgetSelected(SelectionEvent e) {
484 // handle update for others
485 int index = variableCombo.getSelectionIndex();
487 String key = variableCombo.getItem(index);
488 DynamicColorContribution cont = colorContributions.get(key);
490 if (minText.getText().isEmpty()) {
491 minText.setText(Double.toString(cont.getDefaultMin()));
493 if (maxText.getText().isEmpty()) {
494 maxText.setText(Double.toString(cont.getDefaultMax()));
496 unit.setText(cont.getUnit());
498 colorMapCombo.setItems(cont.getDefaultColorMap().getLabel());
499 colorMapCombo.select(0);
501 defaultButton.setSelection(true);
506 coloringRows.put(object.getColoringObject().getName(), new ColoringObjectRow(label, usedButton, variableCombo, minText, maxText, unit, colorMapCombo, defaultButton));
508 return new Supplier<Pair<String, DynamicColorContribution>>() {
511 public Pair<String, DynamicColorContribution> get() {
512 int selectionIndex = variableCombo.getSelectionIndex();
513 if (selectionIndex >= 0) {
514 String key = variableCombo.getItem(selectionIndex);
515 DynamicColorContribution cont = colorContributions.get(key);
517 String colorMap = colorMapCombo.getItem(colorMapCombo.getSelectionIndex());
519 Map<String, DynamicColorMap> colorMaps = Simantics.getSession().syncRequest(new UniqueRead<Map<String, DynamicColorMap>>() {
522 public Map<String, DynamicColorMap> perform(ReadGraph graph) throws DatabaseException {
523 return DynamicVisualisationsContributions.dynamicColorMaps(graph);
526 DynamicColorMap dColorMap = colorMaps.get(colorMap);
527 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
529 DynamicColorContribution dcc = new DynamicColorContribution(label, cont.getModuleName(), cont.getAttributeName(), unit.getText(), cont.getVariableGain(), cont.getVariableBias(), dColorMap, Double.parseDouble(minText.getText()), Double.parseDouble(maxText.getText()));
530 dcc.setUsed(usedButton.getSelection());
531 dcc.setUseDefault(defaultButton.getSelection());
533 return Pair.make(object.getColoringObject().getName(), dcc);
534 } catch (DatabaseException e) {
535 LOGGER.error("Could not get DynamicColorContribution", e);
544 private void createSizingObjectHeaderRow(Composite parent) {
546 Label label = new Label(parent, SWT.NONE);
547 label.setText("Label");
548 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
550 label = new Label(parent, SWT.NONE);
551 label.setText("Used");
552 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
554 label = new Label(parent, SWT.NONE);
555 label.setText("Variable");
556 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
558 label = new Label(parent, SWT.NONE);
559 label.setText("Min");
560 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
562 label = new Label(parent, SWT.NONE);
563 label.setText("Max");
564 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
566 label = new Label(parent, SWT.NONE);
567 label.setText("Unit");
568 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
570 label = new Label(parent, SWT.NONE);
571 label.setText("SizeMap");
572 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
574 label = new Label(parent, SWT.NONE);
575 label.setText("Default");
576 GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label);
579 private Supplier<Pair<String, DynamicSizeContribution>> createSizingObjectRow(Composite parent, DynamicSizingObject object) {
580 Label label = new Label(parent, SWT.NONE);
581 label.setText(object.getSizingObject().getName());
582 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label);
584 Map<String, DynamicSizeContribution> sizeContributions = object.getSizeContributions();
586 Button usedButton = new Button(parent, SWT.CHECK);
587 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton);
589 Combo variableCombo = new Combo(parent, SWT.READ_ONLY);
590 variableCombo.setItems(sizeContributions.keySet().toArray(new String[sizeContributions.size()]));
592 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo);
594 Text minText = new Text(parent, SWT.BORDER);
595 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(minText);
597 Text maxText = new Text(parent, SWT.BORDER);
598 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(maxText);
600 Label unit = new Label(parent, SWT.NONE);
602 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit);
604 Combo sizeMapCombo = new Combo(parent, SWT.READ_ONLY);
605 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(sizeMapCombo);
607 Button defaultButton = new Button(parent, SWT.CHECK);
608 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton);
610 variableCombo.addSelectionListener(new SelectionAdapter() {
613 public void widgetSelected(SelectionEvent e) {
614 // handle update for others
615 String key = variableCombo.getItem(variableCombo.getSelectionIndex());
616 DynamicSizeContribution cont = sizeContributions.get(key);
618 if (minText.getText().isEmpty()) {
619 minText.setText(Double.toString(cont.getDefaultMin()));
621 if (maxText.getText().isEmpty()) {
622 maxText.setText(Double.toString(cont.getDefaultMax()));
624 unit.setText(cont.getUnit());
626 sizeMapCombo.setItems(cont.getDefaultSizeMap().getLabel());
627 sizeMapCombo.select(0);
629 defaultButton.setSelection(true);
633 sizingRows.put(object.getSizingObject().getName(), new SizingObjectRow(label, usedButton, variableCombo, minText, maxText, unit, sizeMapCombo, defaultButton));
635 return new Supplier<Pair<String, DynamicSizeContribution>>() {
638 public Pair<String, DynamicSizeContribution> get() {
639 int selectionIndex = variableCombo.getSelectionIndex();
640 if (selectionIndex >= 0) {
641 String key = variableCombo.getItem(selectionIndex);
642 DynamicSizeContribution cont = sizeContributions.get(key);
644 String sizeMap = sizeMapCombo.getItem(sizeMapCombo.getSelectionIndex());
646 Map<String, DynamicSizeMap> sizeMaps = Simantics.getSession().syncRequest(new UniqueRead<Map<String, DynamicSizeMap>>() {
649 public Map<String, DynamicSizeMap> perform(ReadGraph graph) throws DatabaseException {
650 return DynamicVisualisationsContributions.dynamicSizeMaps(graph);
653 DynamicSizeMap dColorMap = sizeMaps.get(sizeMap);
654 String label = variableCombo.getItem(variableCombo.getSelectionIndex());
656 DynamicSizeContribution dsc = new DynamicSizeContribution(label, cont.getModuleName(), cont.getAttributeName(), unit.getText(), cont.getVariableGain(), cont.getVariableBias(), dColorMap, Double.parseDouble(minText.getText()), Double.parseDouble(maxText.getText()));
657 dsc.setUsed(usedButton.getSelection());
658 dsc.setUseDefault(defaultButton.getSelection());
660 return Pair.make(object.getSizingObject().getName(), dsc);
661 } catch (DatabaseException e) {
662 LOGGER.error("Could not get DynamicColorContribution", e);
671 private void initializeColorBars(Composite parent) {
672 Group group = new Group(parent, SWT.NONE);
673 group.setText("Color Bars");
674 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
675 GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(group);
677 createColorBars(group);
680 private void createColorBars(Composite parent) {
682 showColorButton = new Button(parent, SWT.CHECK);
683 showColorButton.setText("Show");
685 colorTicksButton = new Button(parent, SWT.CHECK);
686 colorTicksButton.setText("Ticks");
688 Label label = new Label(parent, SWT.NONE);
689 label.setText("Location");
690 colorLocationCombo = new Combo(parent, SWT.READ_ONLY);
691 colorLocationCombo.setItems(Stream.of(ColorBarsLocation.values()).map(size -> size.toString()).toArray(String[]::new));
693 label = new Label(parent, SWT.NONE);
694 label.setText("Size");
695 colorSizeCombo = new Combo(parent, SWT.READ_ONLY);
696 colorSizeCombo.setItems(Stream.of(ColorBarsSize.values()).map(size -> size.toString()).toArray(String[]::new));
698 Button applyButton = new Button(parent, SWT.NONE);
699 applyButton.setText("Apply");
701 applyButton.addSelectionListener(new SelectionAdapter() {
704 public void widgetSelected(SelectionEvent e) {
706 IEditorPart activeEditor = WorkbenchUtils.getActiveEditor();
707 if (activeEditor instanceof IResourceEditorPart) {
709 String colorLocation = colorLocationCombo.getItem(colorLocationCombo.getSelectionIndex());
710 String colorSize = colorSizeCombo.getItem(colorSizeCombo.getSelectionIndex());
712 ColorBarOptions options = new ColorBarOptions()
713 .showColorBars(showColorButton.getSelection())
714 .showColorBarsTicks(colorTicksButton.getSelection())
715 .withLocation(ColorBarsLocation.valueOf(colorLocation))
716 .withSize(ColorBarsSize.valueOf(colorSize));
718 Simantics.getSession().asyncRequest(new WriteRequest() {
721 public void perform(WriteGraph graph) throws DatabaseException {
722 DistrictNetworkUtil.setColorBarOptions(graph, visualisation.getVisualisationResource(), options);
730 private void initializeObjectSizes(Composite parent) {
731 Group group = new Group(parent, SWT.NONE);
732 group.setText("Object Sizes");
733 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
734 GridLayoutFactory.fillDefaults().numColumns(8).margins(5, 5).applyTo(group);
737 createSizingObjectHeaderRow(group);
738 createObjectSizes(group);
742 private void createObjectSizes(Composite parent) {
744 sizeSuppliers = new ArrayList<>();
746 Collection<DynamicSizingObject> resultSizing = Simantics.getSession().syncRequest(new UniqueRead<Collection<DynamicSizingObject>>() {
749 public Collection<DynamicSizingObject> perform(ReadGraph graph) throws DatabaseException {
750 return DynamicVisualisationsContributions.dynamicSizingObjects(graph);
754 for (DynamicSizingObject object : resultSizing) {
755 sizeSuppliers.add(createSizingObjectRow(parent, object));
757 } catch (DatabaseException e) {
758 LOGGER.error("Could not create object sizes", e);
762 Button applyButton = new Button(parent, SWT.NONE);
763 applyButton.setText("Apply");
764 applyButton.addSelectionListener(new SelectionAdapter() {
767 public void widgetSelected(SelectionEvent e) {
768 List<Pair<String, DynamicSizeContribution>> collect = sizeSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList());
769 Simantics.getSession().asyncRequest(new WriteRequest() {
772 public void perform(WriteGraph graph) throws DatabaseException {
773 DistrictNetworkUtil.setSizeContributions(graph, visualisation.getVisualisationResource(), collect);
781 private void initializeSizeBars(Composite parent) {
782 Group group = new Group(parent, SWT.NONE);
783 group.setText("Size Bars");
784 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
785 GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(group);
787 createSizeBars(group);
790 private void createSizeBars(Composite parent) {
791 showSizeButton = new Button(parent, SWT.CHECK);
792 showSizeButton.setText("Show");
794 sizeTicksButton = new Button(parent, SWT.CHECK);
795 sizeTicksButton.setText("Ticks");
797 Label label = new Label(parent, SWT.NONE);
798 label.setText("Location");
799 sizeLocationCombo = new Combo(parent, SWT.READ_ONLY);
800 sizeLocationCombo.setItems(Stream.of(SizeBarsLocation.values()).map(size -> size.toString()).toArray(String[]::new));
802 label = new Label(parent, SWT.NONE);
803 label.setText("Size");
804 sizeSizeCombo = new Combo(parent, SWT.READ_ONLY);
805 sizeSizeCombo.setItems(Stream.of(SizeBarsSize.values()).map(size -> size.toString()).toArray(String[]::new));
807 Button applyButton = new Button(parent, SWT.READ_ONLY);
808 applyButton.setText("Apply");
810 applyButton.addSelectionListener(new SelectionAdapter() {
813 public void widgetSelected(SelectionEvent e) {
815 IEditorPart activeEditor = WorkbenchUtils.getActiveEditor();
816 if (activeEditor instanceof IResourceEditorPart) {
818 String sizeLocation = sizeLocationCombo.getItem(sizeLocationCombo.getSelectionIndex());
819 String sizeSize = sizeSizeCombo.getItem(sizeSizeCombo.getSelectionIndex());
821 SizeBarOptions options = new SizeBarOptions()
822 .showSizeBars(showSizeButton.getSelection())
823 .showSizeBarsTicks(sizeTicksButton.getSelection())
824 .withLocation(SizeBarsLocation.valueOf(sizeLocation))
825 .withSize(SizeBarsSize.valueOf(sizeSize));
827 Simantics.getSession().asyncRequest(new WriteRequest() {
830 public void perform(WriteGraph graph) throws DatabaseException {
831 DistrictNetworkUtil.setSizeBarOptions(graph, visualisation.getVisualisationResource(), options);
839 public void setDiagramResource(Resource diagramResource) {
840 if (this.diagramResource != diagramResource) {
841 this.diagramResource = diagramResource;
846 private void updateListening() {
847 if (visualisationsListener != null)
848 visualisationsListener.dispose();
849 visualisationsListener = new VisualisationsListener(this);
850 Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(diagramResource), visualisationsListener);
852 if (listener != null)
854 listener = new VisualisationListener(this);
855 Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(diagramResource), listener);
858 private static class VisualisationsListener implements Listener<Collection<NamedResource>> {
860 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationsListener.class);
862 private boolean disposed;
863 private DynamicVisualisationsUI ui;
865 public VisualisationsListener(DynamicVisualisationsUI ui) {
870 public void execute(Collection<NamedResource> result) {
871 ui.updateVisualisations(result);
875 public void exception(Throwable t) {
876 LOGGER.error("Could not listen visualisation", t);
880 public boolean isDisposed() {
881 return disposed || ui.isDisposed();
884 public void dispose() {
885 this.disposed = true;
889 private static class VisualisationListener implements Listener<DynamicVisualisation> {
891 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationListener.class);
893 private boolean disposed;
894 private DynamicVisualisationsUI ui;
896 public VisualisationListener(DynamicVisualisationsUI ui) {
901 public void execute(DynamicVisualisation result) {
902 ui.updateVisualisation(result);
906 public void exception(Throwable t) {
907 LOGGER.error("Could not listen visualisation", t);
911 public boolean isDisposed() {
912 return disposed || ui.isDisposed();
915 public void dispose() {
916 this.disposed = true;
920 public void updateVisualisation(DynamicVisualisation result) {
921 this.visualisation = result;
922 if (visualisation != null) {
923 Display.getDefault().asyncExec(() -> {
924 if (getParent().isDisposed())
928 String[] items = templateSelectionCombo.getItems();
929 for (int i = 0; i < items.length; i++) {
930 if (visualisation.getName().equals(items[i])) {
931 templateSelectionCombo.select(i);
936 Map<String, DynamicColorContribution> colorContributions = visualisation.getColorContributions();
937 for (Entry<String, DynamicColorContribution> entry : colorContributions.entrySet()) {
939 ColoringObjectRow coloringObjectRow = coloringRows.get(entry.getKey());
940 if (coloringObjectRow != null) {
942 coloringObjectRow.update(entry.getValue());
945 LOGGER.info("No coloring object visualisation row for key {}", entry.getKey());
948 ColorBarOptions colorOptions = visualisation.getColorBarOptions();
949 showColorButton.setSelection(colorOptions.isShowColorBars());
950 colorTicksButton.setSelection(colorOptions.isShowColorBarsTicks());
951 for (int i = 0; i < colorLocationCombo.getItems().length; i++) {
952 String item = colorLocationCombo.getItem(i);
953 if (item.equals(colorOptions.getLocation().toString())) {
954 colorLocationCombo.select(i);
958 for (int i = 0; i < colorSizeCombo.getItems().length; i++) {
959 String item = colorSizeCombo.getItem(i);
960 if (item.equals(colorOptions.getSize().toString())) {
961 colorSizeCombo.select(i);
966 Map<String, DynamicSizeContribution> sizeContributions = visualisation.getSizeContributions();
967 for (Entry<String, DynamicSizeContribution> entry : sizeContributions.entrySet()) {
969 SizingObjectRow sizingObjectRow = sizingRows.get(entry.getKey());
970 if (sizingObjectRow != null) {
972 sizingObjectRow.update(entry.getValue());
975 LOGGER.info("No sizing object visualisation row for key {}", entry.getKey());
978 SizeBarOptions sizeOptions = visualisation.getSizeBarOptions();
979 showSizeButton.setSelection(sizeOptions.isShowSizeBars());
980 sizeTicksButton.setSelection(sizeOptions.isShowSizeBarsTicks());
981 for (int i = 0; i < sizeLocationCombo.getItems().length; i++) {
982 String item = sizeLocationCombo.getItem(i);
983 if (item.equals(sizeOptions.getLocation().toString())) {
984 sizeLocationCombo.select(i);
988 for (int i = 0; i < sizeSizeCombo.getItems().length; i++) {
989 String item = sizeSizeCombo.getItem(i);
990 if (item.equals(sizeOptions.getSize().toString())) {
991 sizeSizeCombo.select(i);
999 public void updateVisualisations(Collection<NamedResource> result) {
1000 this.visualisations = result;
1002 Display.getDefault().asyncExec(() -> {
1003 if (getParent().isDisposed())
1005 templateSelectionCombo.setItems(visualisations.stream().map(NamedResource::getName).collect(Collectors.toList()).toArray(new String[visualisations.size()]));