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

io.github.palexdev.materialfx.controls.base.AbstractMFXListView Maven / Gradle / Ivy

There is a newer version: 11.17.0
Show newest version
/*
 * Copyright (C) 2022 Parisi Alessandro
 * This file is part of MaterialFX (https://github.com/palexdev/MaterialFX).
 *
 * MaterialFX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MaterialFX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with MaterialFX.  If not, see .
 */

package io.github.palexdev.materialfx.controls.base;

import io.github.palexdev.materialfx.effects.DepthLevel;
import io.github.palexdev.materialfx.selection.MultipleSelectionModel;
import io.github.palexdev.materialfx.selection.base.IMultipleSelectionModel;
import io.github.palexdev.materialfx.utils.ColorUtils;
import io.github.palexdev.materialfx.utils.StyleablePropertiesUtils;
import io.github.palexdev.virtualizedfx.cell.Cell;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.css.*;
import javafx.scene.control.Control;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.util.Duration;
import javafx.util.StringConverter;

import java.util.List;

/**
 * Base class for all list views based on VirtualizedFX, defines common properties and behavior.
 *
 * @param  the type of data within the ListView
 */
public abstract class AbstractMFXListView> extends Control implements IListView {
	//================================================================================
	// Properties
	//================================================================================
	protected final ObjectProperty> items = new SimpleObjectProperty<>(FXCollections.observableArrayList());
	protected final ObjectProperty> converter = new SimpleObjectProperty<>();
	protected final IMultipleSelectionModel selectionModel = new MultipleSelectionModel<>(items);

	//================================================================================
	// Constructors
	//================================================================================
	public AbstractMFXListView() {}

	public AbstractMFXListView(ObservableList items) {
		setItems(items);
	}

	//================================================================================
	// Abstract Methods
	//================================================================================

	/**
	 * Abstract method called automatically to set a default factory for the cells.
	 */
	protected abstract void setDefaultCellFactory();

	//================================================================================
	// Methods
	//================================================================================
	protected void initialize() {
		setDefaultCellFactory();
		addBarsListeners();
	}

	protected void addBarsListeners() {
		this.trackColor.addListener((observable, oldValue, newValue) -> {
			if (!newValue.equals(oldValue)) {
				setColors();
			}
		});

		this.thumbColor.addListener((observable, oldValue, newValue) -> {
			if (!newValue.equals(oldValue)) {
				setColors();
			}
		});

		this.thumbHoverColor.addListener((observable, oldValue, newValue) -> {
			if (!newValue.equals(oldValue)) {
				setColors();
			}
		});
	}

	/**
	 * Sets the CSS looked-up colors
	 */
	protected void setColors() {
		StringBuilder sb = new StringBuilder();
		sb.append("-mfx-track-color: ").append(ColorUtils.toCss(trackColor.get()))
				.append(";\n-mfx-thumb-color: ").append(ColorUtils.toCss(thumbColor.get()))
				.append(";\n-mfx-thumb-hover-color: ").append(ColorUtils.toCss(thumbHoverColor.get()))
				.append(";");
		setStyle(sb.toString());
	}

	//================================================================================
	// ScrollBars Properties
	//================================================================================
	private final ObjectProperty trackColor = new SimpleObjectProperty<>(Color.rgb(230, 230, 230));
	private final ObjectProperty thumbColor = new SimpleObjectProperty<>(Color.rgb(137, 137, 137));
	private final ObjectProperty thumbHoverColor = new SimpleObjectProperty<>(Color.rgb(89, 88, 91));
	private final ObjectProperty hideAfter = new SimpleObjectProperty<>(Duration.seconds(1));

	public Paint getTrackColor() {
		return trackColor.get();
	}

	/**
	 * Specifies the color of the scrollbars' track.
	 */
	public ObjectProperty trackColorProperty() {
		return trackColor;
	}

	public void setTrackColor(Paint trackColor) {
		this.trackColor.set(trackColor);
	}

	public Paint getThumbColor() {
		return thumbColor.get();
	}

	/**
	 * Specifies the color of the scrollbars' thumb.
	 */
	public ObjectProperty thumbColorProperty() {
		return thumbColor;
	}

	public void setThumbColor(Paint thumbColor) {
		this.thumbColor.set(thumbColor);
	}

	public Paint getThumbHoverColor() {
		return thumbHoverColor.get();
	}

	/**
	 * Specifies the color of the scrollbars' thumb when mouse hover.
	 */
	public ObjectProperty thumbHoverColorProperty() {
		return thumbHoverColor;
	}

	public void setThumbHoverColor(Paint thumbHoverColor) {
		this.thumbHoverColor.set(thumbHoverColor);
	}

	public Duration getHideAfter() {
		return hideAfter.get();
	}

	/**
	 * Specifies the time after which the scrollbars are hidden.
	 */
	public ObjectProperty hideAfterProperty() {
		return hideAfter;
	}

	public void setHideAfter(Duration hideAfter) {
		this.hideAfter.set(hideAfter);
	}


	//================================================================================
	// Getters/Setters
	//================================================================================
	@Override
	public ObservableList getItems() {
		return items.get();
	}

	@Override
	public ObjectProperty> itemsProperty() {
		return items;
	}

	@Override
	public void setItems(ObservableList items) {
		this.items.set(items);
	}

	@Override
	public StringConverter getConverter() {
		return converter.get();
	}

	@Override
	public ObjectProperty> converterProperty() {
		return converter;
	}

	@Override
	public void setConverter(StringConverter converter) {
		this.converter.set(converter);
	}

	@Override
	public IMultipleSelectionModel getSelectionModel() {
		return selectionModel;
	}

	//================================================================================
	// Styleable Properties
	//================================================================================
	private final StyleableBooleanProperty hideScrollBars = new SimpleStyleableBooleanProperty(
			StyleableProperties.HIDE_SCROLLBARS,
			this,
			"hideScrollBars",
			false
	);

	private final StyleableObjectProperty depthLevel = new SimpleStyleableObjectProperty<>(
			StyleableProperties.DEPTH_LEVEL,
			this,
			"depthLevel",
			DepthLevel.LEVEL2
	);

	public boolean isHideScrollBars() {
		return hideScrollBars.get();
	}

	/**
	 * Specifies if the scrollbars should be hidden when the mouse is not on the list.
	 */
	public StyleableBooleanProperty hideScrollBarsProperty() {
		return hideScrollBars;
	}

	public void setHideScrollBars(boolean hideScrollBars) {
		this.hideScrollBars.set(hideScrollBars);
	}

	public DepthLevel getDepthLevel() {
		return depthLevel.get();
	}

	/**
	 * Specifies the shadow strength around the control.
	 */
	public StyleableObjectProperty depthLevelProperty() {
		return depthLevel;
	}

	public void setDepthLevel(DepthLevel depthLevel) {
		this.depthLevel.set(depthLevel);
	}

	private static class StyleableProperties {
		private static final StyleablePropertyFactory> FACTORY = new StyleablePropertyFactory<>(Control.getClassCssMetaData());
		private static final List> cssMetaDataList;

		private static final CssMetaData, Boolean> HIDE_SCROLLBARS =
				FACTORY.createBooleanCssMetaData(
						"-mfx-hide-scrollbars",
						AbstractMFXListView::hideScrollBarsProperty,
						false
				);

		private static final CssMetaData, DepthLevel> DEPTH_LEVEL =
				FACTORY.createEnumCssMetaData(
						DepthLevel.class,
						"-mfx-depth-level",
						AbstractMFXListView::depthLevelProperty,
						DepthLevel.LEVEL2
				);


		static {
			cssMetaDataList = StyleablePropertiesUtils.cssMetaDataList(
					Control.getClassCssMetaData(),
					HIDE_SCROLLBARS, DEPTH_LEVEL
			);
		}
	}

	public static List> getClassCssMetaData() {
		return StyleableProperties.cssMetaDataList;
	}

	@Override
	protected List> getControlCssMetaData() {
		return AbstractMFXListView.getClassCssMetaData();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy