X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Fvisualisations%2FDynamicVisualisationsUI.java;h=bc0551527590a5aa7793cf0a5125f4de29340fec;hb=db03c6473f55b3ed34d3158376f9af267d37b22c;hp=5cb80a15d5fca59c50a72827a1bcbad7ac0dc94e;hpb=0249f20bb127e61c58db4432b8609ff0cb441480;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java index 5cb80a15..bc055152 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/visualisations/DynamicVisualisationsUI.java @@ -18,6 +18,12 @@ import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; @@ -28,6 +34,7 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Widget; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -39,7 +46,6 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.db.procedure.Listener; import org.simantics.district.network.DistrictNetworkUtil; -import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.profile.ActiveDynamicVisualisationsRequest; import org.simantics.district.network.profile.DynamicVisualisationsRequest; import org.simantics.district.network.visualisations.DynamicVisualisationsContributions; @@ -65,7 +71,7 @@ public class DynamicVisualisationsUI extends Composite { private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsUI.class); - private Resource diagramResource; + private Resource parentResource; private VisualisationListener listener; private DynamicVisualisation visualisation; @@ -86,8 +92,6 @@ public class DynamicVisualisationsUI extends Composite { private Button removeVisualisationTemplateButton; - private Button applyButton; - public DynamicVisualisationsUI(Composite parent, int style) { super(parent, style); // ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.V_SCROLL); @@ -131,7 +135,8 @@ public class DynamicVisualisationsUI extends Composite { @Override public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkUtil.setActiveVisualisation(graph, diagramResource, template.getResource()); + Resource vf = DistrictNetworkUtil.getVisualisationFolder(graph, parentResource); + DistrictNetworkUtil.setActiveVisualisation(graph, vf, template.getResource()); } }); break; @@ -174,22 +179,6 @@ public class DynamicVisualisationsUI extends Composite { } }); - applyButton = new Button(buttonBarsComposite, SWT.NONE); - applyButton.setText("Apply"); - applyButton.setEnabled(visualisation != null); - applyButton.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - try { - persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource())); - } catch (Exception e1) { - LOGGER.error("Could not persist visualisation template", e1); - ShowError.showError("Could not persist visualisation template", e1.getMessage(), e1); - } - } - }); - removeVisualisationTemplateButton = new Button(buttonBarsComposite, SWT.NONE); removeVisualisationTemplateButton.setText("Remove"); removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null); @@ -239,6 +228,19 @@ public class DynamicVisualisationsUI extends Composite { } } + private void persistCurrentVisualisationTemplateIfAvailable() { + if (visualisation != null) { + try { + persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource())); + } catch (Exception e1) { + LOGGER.error("Could not persist visualisation template", e1); + ShowError.showError("Could not persist visualisation template", e1.getMessage(), e1); + } + } else { + LOGGER.info("No current visualisation template selected for saving"); + } + } + private void persistVisualisationTemplate(String templateName, Optional existing) throws Exception { List> colorCollect = colorSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList()); @@ -269,12 +271,11 @@ public class DynamicVisualisationsUI extends Composite { @Override public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); Resource exist; if (existing.isPresent()) { exist = existing.get(); } else { - exist = DistrictNetworkUtil.createVisualisation(graph, diagramResource, templateName); + exist = DistrictNetworkUtil.createVisualisation(graph, parentResource, templateName); } DistrictNetworkUtil.setColorContributions(graph, exist, colorCollect); @@ -298,16 +299,17 @@ public class DynamicVisualisationsUI extends Composite { colorSuppliers = new ArrayList<>(); { try { - Collection result = Simantics.getSession().syncRequest(new UniqueRead>() { + Pair, Map> result = Simantics.getSession().syncRequest(new UniqueRead, Map>>() { @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - return DynamicVisualisationsContributions.dynamicColoringObjects(graph); + public Pair, Map> perform(ReadGraph graph) throws DatabaseException { + Map dynamicColorMaps = DynamicVisualisationsContributions.dynamicColorMaps(graph); + return Pair.make(DynamicVisualisationsContributions.dynamicColoringObjects(graph), dynamicColorMaps); } }); - for (DynamicColoringObject object : result) { - colorSuppliers.add(createColoringObjectRow(group, object)); + for (DynamicColoringObject object : result.first) { + colorSuppliers.add(createColoringObjectRow(group, object, result.second)); } } catch (DatabaseException e) { @@ -367,11 +369,11 @@ public class DynamicVisualisationsUI extends Composite { private final Combo variableCombo; private final Text minText; private final Text maxText; - private final Label unit; + private final Text unit; private final Combo colorMapCombo; private final Button defaultButton; - public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Label unit, + public ColoringObjectRow(Label label, Button usedButton, Combo variableCombo, Text minText, Text maxText, Text unit, Combo colorMapCombo, Button defaultButton) { super(); this.label = label; @@ -396,17 +398,14 @@ public class DynamicVisualisationsUI extends Composite { maxText.setText(Double.toString(colorContribution.getDefaultMax())); unit.setText(colorContribution.getUnit()); - // color map only supports single for now - colorMapCombo.setItems(colorContribution.getDefaultColorMap().getLabel()); - colorMapCombo.select(0); -// String[] colorItems = colorMapCombo.getItems(); -// for (int i = 0; i < colorItems.length; i++) { -// -// if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) { -// colorMapCombo.select(i); -// break; -// } -// } + String[] colorItems = colorMapCombo.getItems(); + for (int i = 0; i < colorItems.length; i++) { + + if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) { + colorMapCombo.select(i); + break; + } + } usedButton.setSelection(colorContribution.isUsed()); defaultButton.setSelection(colorContribution.isUseDefault()); @@ -452,17 +451,13 @@ public class DynamicVisualisationsUI extends Composite { maxText.setText(Double.toString(sizeContribution.getDefaultMax())); unit.setText(sizeContribution.getUnit()); - // color map only supports single for now - sizeMapCombo.setItems(sizeContribution.getDefaultSizeMap().getLabel()); - sizeMapCombo.select(0); -// String[] colorItems = colorMapCombo.getItems(); -// for (int i = 0; i < colorItems.length; i++) { -// -// if (colorContribution.getDefaultColorMap().getLabel().equals(colorItems[i])) { -// colorMapCombo.select(i); -// break; -// } -// } + String[] sizeItems = sizeMapCombo.getItems(); + for (int i = 0; i < sizeItems.length; i++) { + if (sizeContribution.getDefaultSizeMap().getLabel().equals(sizeItems[i])) { + sizeMapCombo.select(i); + break; + } + } usedButton.setSelection(sizeContribution.isUsed()); defaultButton.setSelection(sizeContribution.isUseDefault()); @@ -472,7 +467,7 @@ public class DynamicVisualisationsUI extends Composite { } } - private Supplier> createColoringObjectRow(Composite parent, DynamicColoringObject object) { + private Supplier> createColoringObjectRow(Composite parent, DynamicColoringObject object, Map colorMaps) { Label label = new Label(parent, SWT.NONE); label.setText(object.getColoringObject().getName()); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label); @@ -481,27 +476,33 @@ public class DynamicVisualisationsUI extends Composite { Button usedButton = new Button(parent, SWT.CHECK); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton); + addSelectionListener(usedButton); Combo variableCombo = new Combo(parent, SWT.READ_ONLY); variableCombo.setItems(colorContributions.keySet().toArray(new String[colorContributions.size()])); + addSelectionListener(variableCombo); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo); Text minText = new Text(parent, SWT.BORDER); GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(minText); + addSelectionListener(minText); Text maxText = new Text(parent, SWT.BORDER); GridDataFactory.fillDefaults().grab(true, false).hint(150, SWT.DEFAULT).align(SWT.CENTER, SWT.CENTER).applyTo(maxText); + addSelectionListener(maxText); - Label unit = new Label(parent, SWT.NONE); - unit.setText(""); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit); + Text unit = new Text(parent, SWT.READ_ONLY); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(unit); Combo colorMapCombo = new Combo(parent, SWT.READ_ONLY); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorMapCombo); + colorMapCombo.setItems(colorMaps.keySet().toArray(new String[colorMaps.keySet().size()])); + addSelectionListener(colorMapCombo); Button defaultButton = new Button(parent, SWT.CHECK); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton); + addSelectionListener(defaultButton); defaultButton.addSelectionListener(new SelectionAdapter() { @Override @@ -517,8 +518,14 @@ public class DynamicVisualisationsUI extends Composite { maxText.setEnabled(!defaultButton.getSelection()); unit.setText(cont.getUnit()); - colorMapCombo.setItems(cont.getDefaultColorMap().getLabel()); - colorMapCombo.select(0); + String[] items = colorMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultColorMap().getLabel())) { + colorMapCombo.select(i); + break; + } + } colorMapCombo.setEnabled(!defaultButton.getSelection()); } } @@ -542,8 +549,14 @@ public class DynamicVisualisationsUI extends Composite { } unit.setText(cont.getUnit()); - colorMapCombo.setItems(cont.getDefaultColorMap().getLabel()); - colorMapCombo.select(0); + String[] items = colorMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultColorMap().getLabel())) { + colorMapCombo.select(i); + break; + } + } defaultButton.setSelection(true); } @@ -623,7 +636,7 @@ public class DynamicVisualisationsUI extends Composite { GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(label); } - private Supplier> createSizingObjectRow(Composite parent, DynamicSizingObject object) { + private Supplier> createSizingObjectRow(Composite parent, DynamicSizingObject object, Map sizeMaps) { Label label = new Label(parent, SWT.NONE); label.setText(object.getSizingObject().getName()); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(label); @@ -632,27 +645,60 @@ public class DynamicVisualisationsUI extends Composite { Button usedButton = new Button(parent, SWT.CHECK); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(usedButton); + addSelectionListener(usedButton); Combo variableCombo = new Combo(parent, SWT.READ_ONLY); variableCombo.setItems(sizeContributions.keySet().toArray(new String[sizeContributions.size()])); + addSelectionListener(variableCombo); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(variableCombo); Text minText = new Text(parent, SWT.BORDER); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(minText); + addSelectionListener(minText); Text maxText = new Text(parent, SWT.BORDER); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(maxText); + addSelectionListener(maxText); Label unit = new Label(parent, SWT.NONE); - unit.setText(""); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(unit); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.CENTER, SWT.CENTER).applyTo(unit); Combo sizeMapCombo = new Combo(parent, SWT.READ_ONLY); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(sizeMapCombo); + sizeMapCombo.setItems(sizeMaps.keySet().toArray(new String[sizeMaps.keySet().size()])); + addSelectionListener(sizeMapCombo); Button defaultButton = new Button(parent, SWT.CHECK); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(defaultButton); + addSelectionListener(defaultButton); + defaultButton.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + int index = variableCombo.getSelectionIndex(); + if (index >= 0) { + String key = variableCombo.getItem(index); + DynamicSizeContribution cont = sizeContributions.get(key); + + minText.setText(Double.toString(cont.getDefaultMin())); + minText.setEnabled(!defaultButton.getSelection()); + maxText.setText(Double.toString(cont.getDefaultMax())); + maxText.setEnabled(!defaultButton.getSelection()); + unit.setText(cont.getUnit()); + + String[] items = sizeMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultSizeMap().getLabel())) { + sizeMapCombo.select(i); + break; + } + } + sizeMapCombo.setEnabled(!defaultButton.getSelection()); + } + } + }); variableCombo.addSelectionListener(new SelectionAdapter() { @@ -670,8 +716,14 @@ public class DynamicVisualisationsUI extends Composite { } unit.setText(cont.getUnit()); - sizeMapCombo.setItems(cont.getDefaultSizeMap().getLabel()); - sizeMapCombo.select(0); + String[] items = sizeMapCombo.getItems(); + for (int i = 0; i < items.length; i++) { + String item = items[i]; + if (item.equals(cont.getDefaultSizeMap().getLabel())) { + sizeMapCombo.select(i); + break; + } + } defaultButton.setSelection(true); } @@ -728,12 +780,15 @@ public class DynamicVisualisationsUI extends Composite { showColorButton = new Button(parent, SWT.CHECK); showColorButton.setText("Show"); + addSelectionListener(showColorButton); colorTicksButton = new Button(parent, SWT.CHECK); colorTicksButton.setText("Ticks"); + addSelectionListener(colorTicksButton); colorGradientButton = new Button(parent, SWT.CHECK); colorGradientButton.setText("Gradients"); + addSelectionListener(colorGradientButton); Label label = new Label(parent, SWT.NONE); label.setText("Location"); @@ -743,6 +798,7 @@ public class DynamicVisualisationsUI extends Composite { if (colorLocationItems.length > 0) { colorLocationCombo.select(0); } + addSelectionListener(colorLocationCombo); label = new Label(parent, SWT.NONE); label.setText("Size"); @@ -752,6 +808,7 @@ public class DynamicVisualisationsUI extends Composite { if (colorSizeItems.length > 0) { colorSizeCombo.select(0); } + addSelectionListener(colorSizeCombo); } private void initializeObjectSizes(Composite parent) { @@ -770,16 +827,17 @@ public class DynamicVisualisationsUI extends Composite { sizeSuppliers = new ArrayList<>(); try { - Collection resultSizing = Simantics.getSession().syncRequest(new UniqueRead>() { + Pair, Map> resultSizing = Simantics.getSession().syncRequest(new UniqueRead, Map>>() { @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - return DynamicVisualisationsContributions.dynamicSizingObjects(graph); + public Pair, Map> perform(ReadGraph graph) throws DatabaseException { + Map dynamicSizeMaps = DynamicVisualisationsContributions.dynamicSizeMaps(graph); + return Pair.make(DynamicVisualisationsContributions.dynamicSizingObjects(graph), dynamicSizeMaps); } }); - for (DynamicSizingObject object : resultSizing) { - sizeSuppliers.add(createSizingObjectRow(parent, object)); + for (DynamicSizingObject object : resultSizing.first) { + sizeSuppliers.add(createSizingObjectRow(parent, object, resultSizing.second)); } } catch (DatabaseException e) { LOGGER.error("Could not create object sizes", e); @@ -798,12 +856,15 @@ public class DynamicVisualisationsUI extends Composite { private void createSizeBars(Composite parent) { showSizeButton = new Button(parent, SWT.CHECK); showSizeButton.setText("Show"); + addSelectionListener(showSizeButton); sizeTicksButton = new Button(parent, SWT.CHECK); sizeTicksButton.setText("Ticks"); + addSelectionListener(sizeTicksButton); sizeGradientButton = new Button(parent, SWT.CHECK); sizeGradientButton.setText("Gradient"); + addSelectionListener(sizeGradientButton); Label label = new Label(parent, SWT.NONE); label.setText("Location"); @@ -813,6 +874,7 @@ public class DynamicVisualisationsUI extends Composite { if (sizeLocationItems.length > 0) { sizeLocationCombo.select(0); } + addSelectionListener(sizeLocationCombo); label = new Label(parent, SWT.NONE); label.setText("Size"); @@ -822,11 +884,49 @@ public class DynamicVisualisationsUI extends Composite { if (sizeSizeItems.length > 0) { sizeSizeCombo.select(0); } + addSelectionListener(sizeSizeCombo); + } + + private void addSelectionListener(Widget widget) { + if (widget instanceof Button) { + ((Button) widget).addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + persistCurrentVisualisationTemplateIfAvailable(); + } + }); + } else if (widget instanceof Combo) { + ((Combo) widget).addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + persistCurrentVisualisationTemplateIfAvailable(); + } + }); + } else if (widget instanceof Text) { + ((Text) widget).addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + persistCurrentVisualisationTemplateIfAvailable(); + } + }); + ((Text) widget).addKeyListener(new KeyAdapter() { + + @Override + public void keyReleased(KeyEvent e) { + if(e.keyCode == SWT.CR || e.keyCode == SWT.LF) { + persistCurrentVisualisationTemplateIfAvailable(); + } + } + }); + } } - public void setDiagramResource(Resource diagramResource) { - if (this.diagramResource != diagramResource) { - this.diagramResource = diagramResource; + public void setParentResource(Resource parentResource) { + if (this.parentResource != parentResource) { + this.parentResource = parentResource; updateListening(); } } @@ -835,12 +935,12 @@ public class DynamicVisualisationsUI extends Composite { if (visualisationsListener != null) visualisationsListener.dispose(); visualisationsListener = new VisualisationsListener(this); - Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(diagramResource), visualisationsListener); + Simantics.getSession().asyncRequest(new DynamicVisualisationsRequest(parentResource), visualisationsListener); if (listener != null) listener.dispose(); listener = new VisualisationListener(this); - Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(diagramResource), listener); + Simantics.getSession().asyncRequest(new ActiveDynamicVisualisationsRequest(parentResource), listener); } private static class VisualisationsListener implements Listener> { @@ -911,7 +1011,6 @@ public class DynamicVisualisationsUI extends Composite { if (getParent().isDisposed()) return; - applyButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null); removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null); if (visualisation != null) { @@ -938,6 +1037,7 @@ public class DynamicVisualisationsUI extends Composite { ColorBarOptions colorOptions = visualisation.getColorBarOptions(); showColorButton.setSelection(colorOptions.isShowColorBars()); colorTicksButton.setSelection(colorOptions.isShowColorBarsTicks()); + colorGradientButton.setSelection(colorOptions.isUseGradients()); for (int i = 0; i < colorLocationCombo.getItems().length; i++) { String item = colorLocationCombo.getItem(i); if (item.equals(colorOptions.getLocation().toString())) { @@ -968,6 +1068,7 @@ public class DynamicVisualisationsUI extends Composite { SizeBarOptions sizeOptions = visualisation.getSizeBarOptions(); showSizeButton.setSelection(sizeOptions.isShowSizeBars()); sizeTicksButton.setSelection(sizeOptions.isShowSizeBarsTicks()); + sizeGradientButton.setSelection(sizeOptions.isUseGradients()); for (int i = 0; i < sizeLocationCombo.getItems().length; i++) { String item = sizeLocationCombo.getItem(i); if (item.equals(sizeOptions.getLocation().toString())) {