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

com.dlsc.pickerfx.Segment Maven / Gradle / Ivy

package com.dlsc.pickerfx;

import com.dlsc.pickerfx.skins.SegmentSkin;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.layout.Region;
import javafx.util.Callback;

import java.util.List;
import java.util.Objects;

/**
 * 

* A segment represents one single selection block that contains elements which the user can pick from. Basically a * segment is composed by a predefined list of elements ({@link #getItems() items}), and one * selected {@link #valueProperty() value}. *

* *

* Usually a segment is one piece of a bigger control called {@link Picker}, so in order to use segments a picker * must be created. In that sense, every time a Segment instance is required, it is also necessary to have a Picker * instance. The segment's value is independent of the picker value, so they do not have any direct binding. * However the segment's value can be used to compose the picker's value. *

* * @param The picker's value type. * @param The segment's value type. * * @see Picker */ public class Segment extends Control { private final Picker picker; /** * Creates a new instance of a segment with the given list of items and {@code null} value. * * @param picker The picker parent of this segment control. */ public Segment(Picker picker) { this.picker = Objects.requireNonNull(picker); getStyleClass().add("segment"); setMinHeight(Region.USE_PREF_SIZE); setMinWidth(Region.USE_PREF_SIZE); setCellFactory(p -> new SegmentCell<>()); readOnlyProperty().bind(picker.readOnlyProperty()); } @Override protected Skin createDefaultSkin() { return new SegmentSkin<>(this); } /** * @return The picker parent of this segment instance. */ public final Picker getPicker() { return picker; } @Override protected List> getControlCssMetaData() { return getClassCssMetaData(); } /** * @return The list of items available for the segment. */ public final ObservableList getItems() { return items; } private final ObservableList items = FXCollections.observableArrayList(); /** * @return The object property that holds the selected value. */ public final ObjectProperty valueProperty() { return value; } private final ObjectProperty value = new SimpleObjectProperty<>(this, "value"); public final S getValue() { return valueProperty().get(); } public final void setValue(S value) { this.value.set(value); } public final BooleanProperty wrapItemsProperty() { return wrapItems; } private final BooleanProperty wrapItems = new SimpleBooleanProperty(this, "wrapItems", true); public final boolean isWrapItems() { return wrapItems.get(); } public final void setWrapItems(boolean wrapItems) { this.wrapItems.set(wrapItems); } private final ObjectProperty, SegmentCell>> cellFactory = new SimpleObjectProperty<>(this, "cellFactory"); public final ObjectProperty, SegmentCell>> cellFactoryProperty() { return cellFactory; } public final Callback, SegmentCell> getCellFactory() { return cellFactoryProperty().get(); } public final void setCellFactory(Callback, SegmentCell> cellFactory) { cellFactoryProperty().set(cellFactory); } public final BooleanProperty readOnlyProperty() { return readOnly; } private final BooleanProperty readOnly = new SimpleBooleanProperty(this, "readOnly"); public final boolean isReadOnly() { return readOnlyProperty().get(); } public final void setReadOnly(boolean readOnly) { readOnlyProperty().set(readOnly); } }