From: jsimomaa Date: Fri, 6 Sep 2019 12:11:26 +0000 (+0300) Subject: Support showing ticks in DynamicVisualisations X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F25%2F3225%2F1;p=simantics%2Fdistrict.git Support showing ticks in DynamicVisualisations gitlab #59 Change-Id: Ib89d1b0b237b68cb9169cdd5390a5d049cf0b29d --- diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java index f0305fad..ffa48d36 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictDiagramViewer.java @@ -182,30 +182,38 @@ public class DistrictDiagramViewer extends DiagramViewer { private void queueColoringObjectsChangeEvent(Map result) { - if (result != null) { + if (result != null && !canvasContext.isDisposed()) { canvasContext.getDefaultHintContext().setHint(KEY_MAP_COLORING_OBJECTS, result); canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), MAP_COLORING_OBJECTS_CHANGE)); + } else { + LOGGER.info("Result is either null or canvasContext is disposed", String.valueOf(result)); } } private void queueColorBarOptionsChangeEvent(ColorBarOptions result) { - if (result != null) { + if (result != null && !canvasContext.isDisposed()) { canvasContext.getDefaultHintContext().setHint(KEY_MAP_COLOR_BAR_OPTIONS, result); canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), MAP_COLOR_BAR_OPTIONS_CHANGE)); + } else { + LOGGER.info("Result is either null or canvasContext is disposed", String.valueOf(result)); } } private void queueSizingObjectsChangeEvent(Map result) { - if (result != null) { + if (result != null && !canvasContext.isDisposed()) { canvasContext.getDefaultHintContext().setHint(KEY_MAP_SIZING_OBJECTS, result); canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), MAP_SIZING_OBJECTS_CHANGE)); + } else { + LOGGER.info("Result is either null or canvasContext is disposed", String.valueOf(result)); } } private void queueSizeBarOptionsChangeEvent(SizeBarOptions result) { - if (result != null) { + if (result != null && !canvasContext.isDisposed()) { canvasContext.getDefaultHintContext().setHint(KEY_MAP_SIZE_BAR_OPTIONS, result); canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), MAP_SIZE_BAR_OPTIONS_CHANGE)); + } else { + LOGGER.info("Result is either null or canvasContext is disposed", String.valueOf(result)); } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java index 53d7862c..7705a201 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DynamicVisualisationContributionsNode.java @@ -119,25 +119,33 @@ public class DynamicVisualisationContributionsNode extends G2DNode { Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9)); g2d.setFont(rulerFont); - g2d.setColor(Color.BLACK); - - String str = Double.toString(max); - g2d.drawString(str, (int)(colorBarBoxLeft + colorBarBoxWidth - 30), (int)(colorBarBoxTop + colorBarBoxHeight)); - - str = Double.toString(min); - g2d.drawString(str, (int)colorBarBoxLeft + 1, (int)(colorBarBoxTop + colorBarBoxHeight)); + double interval = (max - min) / intensities.size(); - for (RGBIntensity intensity : intensities) { + for (int j = 0; j < intensities.size(); j++) { + + RGBIntensity intensity = intensities.get(j); g2d.setColor(new Color((float)intensity.getRed(), (float)intensity.getGreen(), (float)intensity.getBlue(), 1f)); Rectangle2D colorVertical = new Rectangle2D.Double(colorVerticalLeft, colorVerticalTop, colorVerticalWidth, colorVerticalHeigth); g2d.fill(colorVertical); + + double value = min + j * interval; + String str = Double.toString(value); + if (str.length() > 4) { + str = str.substring(0, 3); + } + g2d.setColor(Color.BLACK); + g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight)); colorVerticalLeft = colorVerticalLeft + colorVerticalWidth; } g2d.setColor(Color.BLACK); + + String str = Double.toString(max); + g2d.drawString(str, (float)(colorVerticalLeft - 8), (float)(colorBarBoxTop + colorBarBoxHeight)); + str = object.getKey() + " - " + label + " [" + unit + "]"; - g2d.drawString(str, (int)colorBarBoxLeft + 5, (int)colorBarBoxTop + 10); + g2d.drawString(str, (float)colorBarBoxLeft + 5, (float)colorBarBoxTop + 10); } } } @@ -223,12 +231,7 @@ public class DynamicVisualisationContributionsNode extends G2DNode { double sizeBarBoxTop = (sizeBarBoxTopInitial + (colorBarBoxHeight * i)); i++; -// double backgroundBoxPaddingRight = 20; -// double backgroundBoxHeight = 50; -// double backgroundBoxWidth = 300; -// double backgroundBoxRight = bounds.getMaxX() - backgroundBoxPaddingRight; -// double backgroundBoxLeft = backgroundBoxRight - backgroundBoxWidth; -// double backgroundBoxTop = bounds.getMaxY();// + (initialPosition + (position * i)); + g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.95f)); @@ -243,24 +246,33 @@ public class DynamicVisualisationContributionsNode extends G2DNode { Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9)); g2d.setFont(rulerFont); - g2d.setColor(Color.BLACK); - - String str = Double.toString(max); - g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight)); - - str = Double.toString(min); - g2d.drawString(str, (int)sizeBarBoxLeft + 1, (int)(sizeBarBoxTop + sizeBarBoxHeight)); - + + double interval = (max - min) / sizes.size(); - g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f)); - for (Double size: sizes) { + for (int j = 0; j < sizes.size(); j++) { + + Double size = sizes.get(j); + + g2d.setColor(new Color((float)0, (float)0, (float)0.8, 0.8f)); + double sizedWidth = (size / 5) * sizeVerticalHeigth; + Rectangle2D rect = new Rectangle2D.Double(sizeVerticalLeft, (sizeVerticalTop), sizedWidth, sizeVerticalHeigth); + g2d.fill(rect); - Ellipse2D ellipse = new Ellipse2D.Double(sizeVerticalLeft + sizeVerticalWidth / 2, (sizeVerticalTop), size * sizeVerticalHeigth, size * sizeVerticalHeigth); - g2d.fill(ellipse); + double value = min + j * interval; + String str = Double.toString(value); + if (str.length() > 4) { + str = str.substring(0, 3); + } + g2d.setColor(Color.BLACK); + g2d.drawString(str, (float)(sizeVerticalLeft - 8), (float)(sizeBarBoxTop + sizeBarBoxHeight)); sizeVerticalLeft = sizeVerticalLeft + sizeVerticalWidth; } g2d.setColor(Color.BLACK); + + String str = Double.toString(max); + g2d.drawString(str, (int)(sizeBarBoxLeft + sizeBarBoxWidth - 30), (int)(sizeBarBoxTop + sizeBarBoxHeight)); + str = object.getKey() + " - " + label + " [" + unit + "]"; g2d.drawString(str, (int)sizeBarBoxLeft + 5, (int)sizeBarBoxTop + 10); } 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 031e5029..8ed98fad 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 @@ -28,7 +28,6 @@ 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.ui.IEditorPart; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -37,6 +36,7 @@ import org.simantics.db.common.NamedResource; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteRequest; 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; @@ -56,9 +56,7 @@ import org.simantics.district.network.visualisations.model.DynamicVisualisation; import org.simantics.district.network.visualisations.model.SizeBarOptions; import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsLocation; import org.simantics.district.network.visualisations.model.SizeBarOptions.SizeBarsSize; -import org.simantics.ui.workbench.IResourceEditorPart; import org.simantics.utils.datastructures.Pair; -import org.simantics.utils.ui.workbench.WorkbenchUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,17 +81,35 @@ public class DynamicVisualisationsUI extends Composite { private List>> colorSuppliers; + private Button removeVisualisationTemplateButton; + + private Button applyButton; + public DynamicVisualisationsUI(Composite parent, int style) { super(parent, style); - - defaultInitializeUI(); +// ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.V_SCROLL); +// scrolledComposite.setLayout(new GridLayout(1, false)); +// scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); +// +// Composite firstContent = new Composite(scrolledComposite, SWT.NONE); +// firstContent.setLayout(new GridLayout(1, false)); +// firstContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + + defaultInitializeUI(this); + +// scrolledComposite.setContent(firstContent); +// scrolledComposite.setExpandHorizontal(true); +// scrolledComposite.setExpandVertical(true); +// scrolledComposite.setMinSize(firstContent.computeSize(SWT.DEFAULT, SWT.DEFAULT)); + } - private void defaultInitializeUI() { - GridDataFactory.fillDefaults().grab(true, true).applyTo(this); - GridLayoutFactory.fillDefaults().numColumns(1).margins(5, 5).applyTo(this); + private void defaultInitializeUI(Composite parent) { - Composite selectionComposite = new Composite(this, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); + GridLayoutFactory.fillDefaults().numColumns(1).margins(5, 5).applyTo(parent); + + Composite selectionComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().grab(true, false).applyTo(selectionComposite); GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(selectionComposite); @@ -121,49 +137,77 @@ public class DynamicVisualisationsUI extends Composite { } }); - Composite coloringObjectsComposite = new Composite(this, SWT.NONE); + Composite coloringObjectsComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().grab(true, false).applyTo(coloringObjectsComposite); GridLayoutFactory.fillDefaults().numColumns(1).applyTo(coloringObjectsComposite); initializeColoringObjects(coloringObjectsComposite); - Composite colorBarsComposite = new Composite(this, SWT.NONE); + Composite colorBarsComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().grab(true, false).applyTo(colorBarsComposite); GridLayoutFactory.fillDefaults().numColumns(1).applyTo(colorBarsComposite); initializeColorBars(colorBarsComposite); - Composite objectSizesComposite = new Composite(this, SWT.NONE); + Composite objectSizesComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().grab(true, false).applyTo(objectSizesComposite); GridLayoutFactory.fillDefaults().numColumns(1).applyTo(objectSizesComposite); initializeObjectSizes(objectSizesComposite); - Composite sizeBarsComposite = new Composite(this, SWT.NONE); + Composite sizeBarsComposite = new Composite(parent, SWT.NONE); GridDataFactory.fillDefaults().grab(true, false).applyTo(sizeBarsComposite); GridLayoutFactory.fillDefaults().numColumns(1).applyTo(sizeBarsComposite); initializeSizeBars(sizeBarsComposite); - Button saveVisualisationTemplateButton = new Button(this, SWT.NONE); - saveVisualisationTemplateButton.setText("Save"); - saveVisualisationTemplateButton.setEnabled(visualisation == null || visualisation.getVisualisationResource() != null); - saveVisualisationTemplateButton.addSelectionListener(new SelectionAdapter() { + Composite buttonBarsComposite = new Composite(parent, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonBarsComposite); + GridLayoutFactory.fillDefaults().numColumns(3).applyTo(buttonBarsComposite); + + Button saveVisualisationTemplateAsButton = new Button(buttonBarsComposite, SWT.NONE); + saveVisualisationTemplateAsButton.setText("Save as visualisation template"); + saveVisualisationTemplateAsButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource())); + showSaveVisualisationTemplateDialog(e.widget.getDisplay().getActiveShell()); } }); - Button saveVisualisationTemplateAsButton = new Button(this, SWT.NONE); - saveVisualisationTemplateAsButton.setText("Save as visualisation template"); - saveVisualisationTemplateAsButton.addSelectionListener(new SelectionAdapter() { + applyButton = new Button(buttonBarsComposite, SWT.NONE); + applyButton.setText("Apply"); + applyButton.setEnabled(visualisation != null); + applyButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - showSaveVisualisationTemplateDialog(e.widget.getDisplay().getActiveShell()); + persistVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource())); + } + }); + + removeVisualisationTemplateButton = new Button(buttonBarsComposite, SWT.NONE); + removeVisualisationTemplateButton.setText("Remove"); + removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null); + removeVisualisationTemplateButton.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + removeVisualisationTemplate(visualisation.getName(), Optional.of(visualisation.getVisualisationResource())); } }); } + protected void removeVisualisationTemplate(String name, Optional of) { + if (of.isPresent()) { + Resource visualisation = of.get(); + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + RemoverUtil.remove(graph, visualisation); + } + }); + } + } + private void showSaveVisualisationTemplateDialog(Shell shell) { InputDialog dialog = new InputDialog(shell, "Save visualisation template", "Give template a name", "", new IInputValidator() { @@ -255,24 +299,6 @@ public class DynamicVisualisationsUI extends Composite { LOGGER.error("Could not create coloring objecst", e); } } - { - Button applyButton = new Button(group, SWT.NONE); - applyButton.setText("Apply"); - applyButton.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - List> collect = colorSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList()); - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkUtil.setColorContributions(graph, visualisation.getVisualisationResource(), collect); - } - }); - } - }); - } } private void createColoringObjectHeaderRow(Composite parent) { @@ -422,6 +448,12 @@ public class DynamicVisualisationsUI extends Composite { // break; // } // } + usedButton.setSelection(sizeContribution.isUsed()); + defaultButton.setSelection(sizeContribution.isUseDefault()); + + minText.setEnabled(!sizeContribution.isUseDefault()); + maxText.setEnabled(!sizeContribution.isUseDefault()); + sizeMapCombo.setEnabled(!sizeContribution.isUseDefault()); } } @@ -672,7 +704,7 @@ public class DynamicVisualisationsUI extends Composite { Group group = new Group(parent, SWT.NONE); group.setText("Color Bars"); GridDataFactory.fillDefaults().grab(true, false).applyTo(group); - GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(group); + GridLayoutFactory.fillDefaults().numColumns(6).margins(5, 5).applyTo(group); createColorBars(group); } @@ -694,37 +726,6 @@ public class DynamicVisualisationsUI extends Composite { label.setText("Size"); colorSizeCombo = new Combo(parent, SWT.READ_ONLY); colorSizeCombo.setItems(Stream.of(ColorBarsSize.values()).map(size -> size.toString()).toArray(String[]::new)); - - Button applyButton = new Button(parent, SWT.NONE); - applyButton.setText("Apply"); - - applyButton.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - // persist changes - IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); - if (activeEditor instanceof IResourceEditorPart) { - - String colorLocation = colorLocationCombo.getItem(colorLocationCombo.getSelectionIndex()); - String colorSize = colorSizeCombo.getItem(colorSizeCombo.getSelectionIndex()); - - ColorBarOptions options = new ColorBarOptions() - .showColorBars(showColorButton.getSelection()) - .showColorBarsTicks(colorTicksButton.getSelection()) - .withLocation(ColorBarsLocation.valueOf(colorLocation)) - .withSize(ColorBarsSize.valueOf(colorSize)); - - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkUtil.setColorBarOptions(graph, visualisation.getVisualisationResource(), options); - } - }); - } - } - }); } private void initializeObjectSizes(Composite parent) { @@ -757,32 +758,13 @@ public class DynamicVisualisationsUI extends Composite { } catch (DatabaseException e) { LOGGER.error("Could not create object sizes", e); } - - { - Button applyButton = new Button(parent, SWT.NONE); - applyButton.setText("Apply"); - applyButton.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - List> collect = sizeSuppliers.stream().map(s -> s.get()).filter(Objects::nonNull).collect(Collectors.toList()); - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkUtil.setSizeContributions(graph, visualisation.getVisualisationResource(), collect); - } - }); - } - }); - } } private void initializeSizeBars(Composite parent) { Group group = new Group(parent, SWT.NONE); group.setText("Size Bars"); GridDataFactory.fillDefaults().grab(true, false).applyTo(group); - GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(group); + GridLayoutFactory.fillDefaults().numColumns(6).margins(5, 5).applyTo(group); createSizeBars(group); } @@ -803,37 +785,6 @@ public class DynamicVisualisationsUI extends Composite { label.setText("Size"); sizeSizeCombo = new Combo(parent, SWT.READ_ONLY); sizeSizeCombo.setItems(Stream.of(SizeBarsSize.values()).map(size -> size.toString()).toArray(String[]::new)); - - Button applyButton = new Button(parent, SWT.READ_ONLY); - applyButton.setText("Apply"); - - applyButton.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - // persist changes - IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); - if (activeEditor instanceof IResourceEditorPart) { - - String sizeLocation = sizeLocationCombo.getItem(sizeLocationCombo.getSelectionIndex()); - String sizeSize = sizeSizeCombo.getItem(sizeSizeCombo.getSelectionIndex()); - - SizeBarOptions options = new SizeBarOptions() - .showSizeBars(showSizeButton.getSelection()) - .showSizeBarsTicks(sizeTicksButton.getSelection()) - .withLocation(SizeBarsLocation.valueOf(sizeLocation)) - .withSize(SizeBarsSize.valueOf(sizeSize)); - - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkUtil.setSizeBarOptions(graph, visualisation.getVisualisationResource(), options); - } - }); - } - } - }); } public void setDiagramResource(Resource diagramResource) { @@ -924,6 +875,7 @@ public class DynamicVisualisationsUI extends Composite { if (getParent().isDisposed()) return; + applyButton.setEnabled(visualisation != null); String[] items = templateSelectionCombo.getItems(); for (int i = 0; i < items.length; i++) { @@ -992,6 +944,9 @@ public class DynamicVisualisationsUI extends Composite { break; } } + + + removeVisualisationTemplateButton.setEnabled(visualisation != null && visualisation.getVisualisationResource() != null); }); } } @@ -1003,6 +958,17 @@ public class DynamicVisualisationsUI extends Composite { if (getParent().isDisposed()) return; templateSelectionCombo.setItems(visualisations.stream().map(NamedResource::getName).collect(Collectors.toList()).toArray(new String[visualisations.size()])); + + if (visualisation != null) { + String[] items = templateSelectionCombo.getItems(); + for (int i = 0; i < items.length; i++) { + if (visualisation.getName().equals(items[i])) { + templateSelectionCombo.select(i); + break; + } + } + } + }); } }