Skip to content

Commit

Permalink
ValueIndicators: move from bitstate to directly calling postListener
Browse files Browse the repository at this point in the history
  • Loading branch information
wirew0rm committed Sep 25, 2023
1 parent 24768c2 commit 8939d07
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand All @@ -43,7 +43,7 @@ protected void invalidated() {
if (get() < 0 || get() > 1) {
throw new IllegalArgumentException("labelHorizontalPosition must be in rage [0,1]");
}
layoutChildren();
runPostLayout();
}
};

Expand All @@ -53,7 +53,7 @@ protected void invalidated() {
if (get() < 0 || get() > 1) {
throw new IllegalArgumentException("labelVerticalPosition must be in rage [0,1]");
}
layoutChildren();
runPostLayout();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand All @@ -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();
}
};

Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +37,7 @@
*/
public abstract class AbstractValueIndicator extends ChartPlugin implements EventSource {
private final Axis axis;
private final ChangeListener<? super Number> axisBoundsListener = (obs, oldVal, newVal) -> layoutChildren();
private final StateListener axisBoundsListener = (source, bits) -> runPostLayout();

private final BitState state = BitState.initDirty(this);

Expand All @@ -54,23 +54,23 @@ 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();
}
};

private final ObjectProperty<HPos> labelHorizontalAnchor = new SimpleObjectProperty<>(this,
"labelHorizontalAnchor", HPos.CENTER) {
@Override
protected void invalidated() {
fireInvalidated(ChartBits.ChartPlugins);
runPostLayout();
}
};

private final ObjectProperty<VPos> labelVerticalAnchor = new SimpleObjectProperty<>(this, "labelVerticalAnchor",
VPos.CENTER) {
@Override
protected void invalidated() {
fireInvalidated(ChartBits.ChartPlugins);
runPostLayout();
}
};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.fair_acc.chartfx.plugins;

import io.fair_acc.chartfx.Chart;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}.
*
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8939d07

Please sign in to comment.