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

tornadofx.control.ListMenu Maven / Gradle / Ivy

package tornadofx.control;

import javafx.beans.DefaultProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
import javafx.css.*;
import javafx.geometry.Orientation;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.layout.Region;
import tornadofx.control.skin.ListMenuSkin;

import java.util.List;

@DefaultProperty("children")
public class ListMenu extends Control {
	private static final PseudoClass ACTIVE_PSEUDOCLASS_STATE =
		PseudoClass.getPseudoClass("active");

	private static final StyleablePropertyFactory FACTORY = new StyleablePropertyFactory<>(Region.getClassCssMetaData());

	public final StyleableProperty graphicFixedSizeProperty() {
		return graphicFixedSize;
	}

	private StyleableProperty graphicFixedSize = FACTORY.createStyleableNumberProperty(this, "graphicFixedSize", "-fx-graphic-fixed-size", ListMenu::graphicFixedSizeProperty);

	private Property active = new SimpleObjectProperty(this, "active") {
		public void set(ListItem item) {
			ListItem previouslyActive = get();
			if (item == previouslyActive)
				return;

			if (previouslyActive != null)
				previouslyActive.pseudoClassStateChanged(ACTIVE_PSEUDOCLASS_STATE, false);

			item.pseudoClassStateChanged(ACTIVE_PSEUDOCLASS_STATE, true);
			super.set(item);
		}
	};

	public void setActive(ListItem item) {
		active.setValue(item);
	}

	public ListItem getActive() {
		return active.getValue();
	}

	public Property activeProperty() {
		return active;
	}

	public Orientation getOrientation() {
		return orientation.get();
	}

	public ObjectProperty orientationProperty() {
		return orientation;
	}

	public void setOrientation(Orientation orientation) {
		this.orientation.set(orientation);
	}

	private final ObjectProperty orientation = new SimpleObjectProperty(Orientation.VERTICAL) {
		protected void invalidated() {
			setNeedsLayout(true);
			requestLayout();
		}
	};

	private final ObjectProperty iconPosition = new SimpleObjectProperty(Side.LEFT) {
		protected void invalidated() {
			getChildren().forEach(child -> {
				if (child instanceof ListItem) {
					((ListItem) child).needsLayout();
				}
			});
		}
	};

	public Side getIconPosition() {
		return iconPosition.get();
	}

	public ObjectProperty iconPositionProperty() {
		return iconPosition;
	}

	public void setIconPosition(Side iconPosition) {
		this.iconPosition.set(iconPosition);
	}

	public ListMenu() {
		getStyleClass().add("list-menu");
		setFocusTraversable(true);
	}

	public ListMenu(ListItem... items) {
		this();
		if (items != null)
			getChildren().addAll(items);
	}

	protected List> getControlCssMetaData() {
		return FACTORY.getCssMetaData();
	}

	protected Skin createDefaultSkin() {
		return new ListMenuSkin(this);
	}

	public ObservableList getChildren() {
		return super.getChildren();
	}

	public String getUserAgentStylesheet() {
		return ListMenu.class.getResource("listmenu.css").toExternalForm();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy