All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.controlsfx.control.table.FilterValue Maven / Gradle / Ivy

Go to download

High quality UI controls and other tools to complement the core JavaFX distribution

There is a newer version: 11.2.1
Show newest version
package org.controlsfx.control.table;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.WeakInvalidationListener;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;

import java.util.Optional;

public final class FilterValue extends HBox implements Comparable> {

    private final R value;
    private final BooleanProperty isSelected = new SimpleBooleanProperty(true);
    private final BooleanProperty inScope = new SimpleBooleanProperty(true);
    private final ColumnFilter columnFilter;
    private final InvalidationListener scopeListener;


    FilterValue(R value, ColumnFilter columnFilter) {
        this.value = value;
        this.columnFilter = columnFilter;

        final CheckBox checkBox = new CheckBox();
        final Label label = new Label();
        label.setText(Optional.ofNullable(value).map(Object::toString).orElse(null));
        scopeListener = (Observable v) -> label.textFillProperty().set(getInScopeProperty().get() ? Color.BLACK : Color.LIGHTGRAY);
        inScope.addListener(new WeakInvalidationListener(scopeListener));
        checkBox.selectedProperty().bindBidirectional(selectedProperty());
        getChildren().addAll(checkBox,label);
    }

    /**
     * Returns the R value for this given FilterValue
     */
    public R getValue() {
        return value;
    }

    /**
     * Property indicating whether this value is selected or not.
     */
    public BooleanProperty selectedProperty() {
        return isSelected;
    }

    /**
     * Property indicating whether this value is in scope.
     */
    public BooleanProperty getInScopeProperty() {
        return inScope;
    }

    void refreshScope() {
        inScope.setValue(columnFilter.wasLastFiltered() || columnFilter.valueIsVisible(value));
    }

    @Override
    public String toString() {
        return Optional.ofNullable(value).map(Object::toString).orElse("");
    }


    @Override
    public int compareTo(FilterValue other) {
        if (value != null && other.value != null) {
            if (value instanceof Comparable && other.value instanceof Comparable) {
                return ((Comparable) value).compareTo(((Comparable) other.value));
            }
        }
        return Optional.ofNullable(value).map(Object::toString).orElse("")
                .compareTo(Optional.ofNullable(other).map(Object::toString).orElse(""));
    }
}