diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractRangeValueIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractRangeValueIndicator.java index 01718d3ff..5a988b2b6 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractRangeValueIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractRangeValueIndicator.java @@ -25,14 +25,14 @@ public abstract class AbstractRangeValueIndicator extends AbstractValueIndicator private final DoubleProperty lowerBound = new SimpleDoubleProperty(this, "lowerBound") { @Override protected void invalidated() { - layoutChildren(); + runPostLayout(); } }; private final DoubleProperty upperBound = new SimpleDoubleProperty(this, "upperBound") { @Override protected void invalidated() { - layoutChildren(); + runPostLayout(); } }; @@ -43,7 +43,7 @@ protected void invalidated() { if (get() < 0 || get() > 1) { throw new IllegalArgumentException("labelHorizontalPosition must be in rage [0,1]"); } - layoutChildren(); + runPostLayout(); } }; @@ -53,7 +53,7 @@ protected void invalidated() { if (get() < 0 || get() > 1) { throw new IllegalArgumentException("labelVerticalPosition must be in rage [0,1]"); } - layoutChildren(); + runPostLayout(); } }; diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractSingleValueIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractSingleValueIndicator.java index e606d08a1..78f79e570 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractSingleValueIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractSingleValueIndicator.java @@ -56,7 +56,7 @@ protected void invalidated() { private final DoubleProperty value = new SimpleDoubleProperty(this, "value") { @Override protected void invalidated() { - fireInvalidated(ChartBits.ChartPluginState, ChartBits.ChartLayout); + runPostLayout(); } }; @@ -66,7 +66,7 @@ protected void invalidated() { if (get() < 0 || get() > 1) { throw new IllegalArgumentException("labelPosition must be in rage [0,1]"); } - fireInvalidated(ChartBits.ChartPluginState); + runPostLayout(); } }; @@ -98,6 +98,7 @@ protected AbstractSingleValueIndicator(Axis axis, final double value, final Stri // Need to add them so that at initialization of the stage the CCS is // applied and we can calculate label's width and height getChartChildren().addAll(line, label); + PropUtil.runOnChange(getBitState().onAction(ChartBits.ChartPluginState), this.value); } /** diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractValueIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractValueIndicator.java index 98aacec34..5a6f0cea0 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractValueIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/AbstractValueIndicator.java @@ -7,12 +7,12 @@ import io.fair_acc.dataset.events.EventSource; import io.fair_acc.dataset.events.BitState; import io.fair_acc.dataset.events.ChartBits; +import io.fair_acc.dataset.events.StateListener; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.StringProperty; -import javafx.beans.value.ChangeListener; import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener.Change; import javafx.geometry.Bounds; @@ -37,7 +37,7 @@ */ public abstract class AbstractValueIndicator extends ChartPlugin implements EventSource { private final Axis axis; - private final ChangeListener axisBoundsListener = (obs, oldVal, newVal) -> layoutChildren(); + private final StateListener axisBoundsListener = (source, bits) -> runPostLayout(); private final BitState state = BitState.initDirty(this); @@ -54,7 +54,7 @@ public abstract class AbstractValueIndicator extends ChartPlugin implements Even protected final BooleanProperty editableIndicator = new SimpleBooleanProperty(this, "editableIndicator", true) { @Override protected void invalidated() { - fireInvalidated(ChartBits.ChartPlugins); + runPostLayout(); } }; @@ -62,7 +62,7 @@ protected void invalidated() { "labelHorizontalAnchor", HPos.CENTER) { @Override protected void invalidated() { - fireInvalidated(ChartBits.ChartPlugins); + runPostLayout(); } }; @@ -70,7 +70,7 @@ protected void invalidated() { VPos.CENTER) { @Override protected void invalidated() { - fireInvalidated(ChartBits.ChartPlugins); + runPostLayout(); } }; @@ -137,21 +137,19 @@ protected AbstractValueIndicator(Axis axis, final String text) { if (oldChart != null) { removeAxisListener(); removePluginsListListener(oldChart); - this.state.removeChangeListener(oldChart.getBitState()); } if (newChart != null) { addAxisListener(); addPluginsListListener(newChart); - this.state.addChangeListener(newChart.getBitState()); } }); - textProperty().addListener(getBitState().onPropChange(ChartBits.ChartPluginState)::set); + textProperty().addListener((obs, oldText, newText) -> runPostLayout()); } private void addAxisListener() { final Axis valueAxis = getAxis(); - valueAxis.getBitState().addChangeListener(ChartBits.AxisRange, this.getBitState()); + valueAxis.getBitState().addChangeListener(ChartBits.AxisRange, axisBoundsListener); } protected void addChildNodeIfNotPresent(final Node node) { @@ -306,8 +304,7 @@ protected final void layoutLabel(final Bounds bounds, final double hPos, final d private void removeAxisListener() { final Axis valueAxis = getAxis(); - valueAxis.minProperty().removeListener(axisBoundsListener); - valueAxis.maxProperty().removeListener(axisBoundsListener); + valueAxis.getBitState().removeChangeListener(axisBoundsListener); } private void removePluginsListListener(final Chart chart) { diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/ChartPlugin.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/ChartPlugin.java index 47053c66e..e052341e3 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/ChartPlugin.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/ChartPlugin.java @@ -62,16 +62,10 @@ public abstract class ChartPlugin implements Measurable.EmptyDefault { protected ChartPlugin() { chartProperty().addListener((obs, oldChart, newChart) -> { if (oldChart != null) { - //if (this instanceof EventSource) { - // oldChart.removeListener((EventSource) this); - //} removeEventHandlers(oldChart.getPlotArea()); removeEventHandlers(oldChart.getPlotBackground()); } if (newChart != null) { - //if (this instanceof EventSource) { - // newChart.addListener((EventSource) this); - //} addEventHandlers(newChart.getPlotArea()); addEventHandlers(newChart.getPlotBackground()); } @@ -211,9 +205,7 @@ public void layoutChildren() { // #NOPMD * Optional method that allows plug-in render something after axes and charts have been rendered */ public void runPostLayout() { // #NOPMD - //if (this instanceof EventSource) { - // ((EventSource) this).getBitState().clear(); - //} + // empty by default } /** diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XRangeIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XRangeIndicator.java index ccbf52e3a..48f3ce72f 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XRangeIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XRangeIndicator.java @@ -1,5 +1,6 @@ package io.fair_acc.chartfx.plugins; +import io.fair_acc.chartfx.Chart; import javafx.geometry.BoundingBox; import javafx.geometry.Bounds; @@ -45,11 +46,12 @@ public XRangeIndicator(Axis axis, final double lowerBound, final double upperBou } @Override - public void layoutChildren() { - if (getChart() == null) { + public void runPostLayout() { + Chart chart = getChart(); + if (chart == null) { return; } - final Bounds plotAreaBounds = getChart().getCanvas().getBoundsInLocal(); + final Bounds plotAreaBounds = chart.getCanvas().getBoundsInLocal(); final double minX = plotAreaBounds.getMinX(); final double maxX = plotAreaBounds.getMaxX(); final double minY = plotAreaBounds.getMinY(); diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XValueIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XValueIndicator.java index cb28116a6..37ec3819c 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XValueIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/XValueIndicator.java @@ -7,7 +7,6 @@ import io.fair_acc.chartfx.axes.Axis; import io.fair_acc.chartfx.ui.geometry.Side; -import io.fair_acc.dataset.events.EventSource; /** * A vertical line drawn on the plot area, indicating specified X value, with an optional {@link #textProperty() text @@ -24,7 +23,7 @@ * * @author mhrabia */ -public class XValueIndicator extends AbstractSingleValueIndicator implements EventSource, ValueIndicator { +public class XValueIndicator extends AbstractSingleValueIndicator implements ValueIndicator { /** * Creates a new instance of the indicator. * @@ -62,11 +61,15 @@ protected void handleDragMouseEvent(final MouseEvent mouseEvent) { } mouseEvent.consume(); + runPostLayout(); } @Override - public void layoutChildren() { - if (getBitState().clear() == 0) { return; } + public void runPostLayout() { + if (getChart() == null) { + return; + } + final Bounds plotAreaBounds = getChart().getCanvas().getBoundsInLocal(); final double minX = plotAreaBounds.getMinX(); final double maxX = plotAreaBounds.getMaxX(); diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YRangeIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YRangeIndicator.java index b919aaa93..02f8e015d 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YRangeIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YRangeIndicator.java @@ -1,5 +1,7 @@ package io.fair_acc.chartfx.plugins; +import io.fair_acc.chartfx.Chart; +import io.fair_acc.chartfx.XYChart; import javafx.geometry.BoundingBox; import javafx.geometry.Bounds; @@ -45,8 +47,12 @@ public YRangeIndicator(final Axis axis, final double lowerBound, final double up } @Override - public void runPreLayout() { - final Bounds plotAreaBounds = getChart().getCanvas().getBoundsInLocal(); + public void runPostLayout() { + Chart chart = getChart(); + if (chart == null) { + return; + } + final Bounds plotAreaBounds = chart.getCanvas().getBoundsInLocal(); final double minX = plotAreaBounds.getMinX(); final double maxX = plotAreaBounds.getMaxX(); final double minY = plotAreaBounds.getMinY(); diff --git a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YValueIndicator.java b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YValueIndicator.java index cf97db339..1fb178c72 100644 --- a/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YValueIndicator.java +++ b/chartfx-chart/src/main/java/io/fair_acc/chartfx/plugins/YValueIndicator.java @@ -12,7 +12,6 @@ import io.fair_acc.chartfx.axes.Axis; import io.fair_acc.chartfx.ui.geometry.Side; -import io.fair_acc.dataset.events.EventSource; /** * A horizontal line drawn on the plot area, indicating specified Y value, with an optional {@link #textProperty() text @@ -29,7 +28,7 @@ * * @author mhrabia */ -public class YValueIndicator extends AbstractSingleValueIndicator implements EventSource, ValueIndicator { +public class YValueIndicator extends AbstractSingleValueIndicator implements ValueIndicator { /** * Creates a new instance indicating given Y value belonging to the specified {@code yAxis}. * @@ -70,11 +69,14 @@ protected void handleDragMouseEvent(final MouseEvent mouseEvent) { } mouseEvent.consume(); + runPostLayout(); } @Override - public void layoutChildren() { - if (getBitState().clear() == 0) { return; } + public void runPostLayout() { + if (getChart() == null) { + return; + } final Bounds plotAreaBounds = getChart().getCanvas().getBoundsInLocal(); final double minX = plotAreaBounds.getMinX(); final double maxX = plotAreaBounds.getMaxX();