Skip to content

Commit

Permalink
Remove all visibility listeners when datasets are removed from the chart
Browse files Browse the repository at this point in the history
  • Loading branch information
wirew0rm committed Feb 9, 2022
1 parent 0576d64 commit e2ffcbd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
13 changes: 10 additions & 3 deletions chartfx-chart/src/main/java/de/gsi/chart/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ public abstract class Chart extends HiddenSidesPane implements Observable {
protected BooleanBinding showingBinding;
protected final BooleanProperty showing = new SimpleBooleanProperty(this, "showing", false);
protected final ChangeListener<? super Boolean> showingListener = (ch2, o, n) -> showing.set(n);
/** When true any data changes will be animated. */
/**
* When true any data changes will be animated.
*/
private final BooleanProperty animated = new SimpleBooleanProperty(this, "animated", true);
// TODO: Check whether 'this' or chart contents need to be added
/** Animator for animating stuff on the chart */
/**
* Animator for animating stuff on the chart
*/
protected final ChartLayoutAnimator animator = new ChartLayoutAnimator(this);

/**
Expand Down Expand Up @@ -310,7 +314,7 @@ protected void invalidated() {

/**
* Creates a new default Chart instance.
*
*
* @param axes axes to be added to the chart
*/
public Chart(Axis... axes) {
Expand Down Expand Up @@ -985,6 +989,9 @@ protected void datasetsChanged(final ListChangeListener.Change<? extends DataSet
FXUtils.assertJavaFxThread();
while (change.next()) {
for (final DataSet set : change.getRemoved()) {
// remove Legend listeners from removed datasets
set.updateEventListener().removeIf(l -> l instanceof DefaultLegend.DatasetVisibilityListener);

set.removeListener(dataSetDataListener);
dataSetChanges = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.stream.Collectors;

import de.gsi.dataset.event.UpdatedMetaDataEvent;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
Expand All @@ -26,6 +25,9 @@
import de.gsi.chart.renderer.Renderer;
import de.gsi.chart.utils.StyleParser;
import de.gsi.dataset.DataSet;
import de.gsi.dataset.event.EventListener;
import de.gsi.dataset.event.UpdateEvent;
import de.gsi.dataset.event.UpdatedMetaDataEvent;

/**
* A chart legend that displays a list of items with symbols in a box
Expand Down Expand Up @@ -58,7 +60,9 @@ protected void invalidated() {
}
};

/** The legend items to display in this legend */
/**
* The legend items to display in this legend
*/
private final ObjectProperty<ObservableList<LegendItem>> items = new SimpleObjectProperty<>(
this, "items") {
private ObservableList<LegendItem> oldItems = null;
Expand Down Expand Up @@ -109,15 +113,27 @@ public final ObservableList<LegendItem> getItems() {
public LegendItem getNewLegendItem(final Renderer renderer, final DataSet series, final int seriesIndex) {
final Canvas symbol = renderer.drawLegendSymbol(series, seriesIndex, SYMBOL_WIDTH, SYMBOL_HEIGHT);
var item = new LegendItem(series.getName(), symbol);
item.setOnMouseClicked(event-> series.setVisible(!series.isVisible()));
item.setOnMouseClicked(event -> series.setVisible(!series.isVisible()));
item.pseudoClassStateChanged(disabledClass, !series.isVisible());
series.addListener(evt -> {
// TODO: are listeners strong or weak references? Do we need any cleanup?
series.addListener(new DatasetVisibilityListener(item, series));
return item;
}

public static class DatasetVisibilityListener implements EventListener {
private LegendItem item;
private DataSet series;

public DatasetVisibilityListener(final LegendItem item, final DataSet series) {
this.item = item;
this.series = series;
}

@Override
public void handle(final UpdateEvent evt) {
if (evt instanceof UpdatedMetaDataEvent) {
item.pseudoClassStateChanged(disabledClass, !series.isVisible());
}
});
return item;
}
}

private static final PseudoClass disabledClass = PseudoClass.getPseudoClass("disabled");
Expand All @@ -129,7 +145,7 @@ public Node getNode() {

/*
* (non-Javadoc)
*
*
* @see de.gsi.chart.legend.Legend#isVertical()
*/
@Override
Expand All @@ -147,7 +163,7 @@ public final void setItems(final ObservableList<LegendItem> value) {

/*
* (non-Javadoc)
*
*
* @see de.gsi.chart.legend.Legend#setVertical(boolean)
*/
@Override
Expand All @@ -157,7 +173,7 @@ public final void setVertical(final boolean value) {

/*
* (non-Javadoc)
*
*
* @see de.gsi.chart.legend.Legend#updateLegend(java.util.List, java.util.List)
*/
@Override
Expand Down Expand Up @@ -234,7 +250,9 @@ public final BooleanProperty verticalProperty() {
return vertical;
}

/** A item to be displayed on a Legend */
/**
* A item to be displayed on a Legend
*/
public static class LegendItem extends Label {
public LegendItem(final String text, final Node symbol) {
setText(text);
Expand Down

0 comments on commit e2ffcbd

Please sign in to comment.