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

io.github.palexdev.virtualizedfx.cells.VFXSimpleTableCell Maven / Gradle / Ivy

There is a newer version: 21.6.4
Show newest version
/*
 * Copyright (C) 2024 Parisi Alessandro - [email protected]
 * This file is part of VirtualizedFX (https://github.com/palexdev/VirtualizedFX)
 *
 * VirtualizedFX 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.
 *
 * VirtualizedFX 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 VirtualizedFX. If not, see .
 */

package io.github.palexdev.virtualizedfx.cells;

import io.github.palexdev.mfxcore.controls.SkinBase;
import io.github.palexdev.mfxcore.utils.converters.FunctionalStringConverter;
import io.github.palexdev.virtualizedfx.cells.base.VFXMappingTableCell;
import io.github.palexdev.virtualizedfx.cells.base.VFXTableCell;
import io.github.palexdev.virtualizedfx.table.VFXTable;
import io.github.palexdev.virtualizedfx.table.VFXTableColumn;
import io.github.palexdev.virtualizedfx.table.VFXTableRow;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.util.StringConverter;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * Extension of {@link VFXCellBase} which also implements {@link VFXMappingTableCell}. Uses an inline extension of {@link VFXLabeledCellSkin}
 * as the default skin (see below why). This is intended to be used with models that do not use JavaFX's properties.
 * Expands the default style classes to be: ".cell-base" and ".table-cell".
 * 

* There are four new properties: *

1) The {@link #columnProperty()} holds the column that created the cell. *

2) The {@link #rowProperty()} holds the row containing the cell. *

3) The {@link #getExtractor()} is the function used to extract a piece of data {@link E} from an item of type {@link T}. * Note that the extractor function should take into account {@code null} inputs! *

4) The {@link #getConverter()} is a helper class which converts data of type {@link E} to a {@code String}. * The method {@link #buildSkin()} will build an inline extension of {@link VFXLabeledCellSkin} to override its * {@link VFXLabeledCellSkin#update()} method and make use of the converter. */ public class VFXSimpleTableCell extends VFXCellBase implements VFXMappingTableCell { //================================================================================ // Properties //================================================================================ private final ReadOnlyObjectWrapper>> column = new ReadOnlyObjectWrapper<>(); private final ReadOnlyObjectWrapper> row = new ReadOnlyObjectWrapper<>(); private Function extractor; private StringConverter converter; //================================================================================ // Constructors //================================================================================ public VFXSimpleTableCell(T item, Function extractor) { this(item, extractor, FunctionalStringConverter.to(t -> (t == null) ? "" : t.toString())); } public VFXSimpleTableCell(T item, Function extractor, StringConverter converter) { super(item); this.extractor = extractor; this.converter = converter; } //================================================================================ // Delegate Methods //================================================================================ /** * @return the {@link VFXTable} instance by using the {@link #rowProperty()}. {@code Null} if the row instance has * not been set yet. */ public VFXTable getTable() { return Optional.ofNullable(getRow()) .map(VFXTableRow::getTable) .orElse(null); } /** * @return the index of the row containing this cell by using the {@link #rowProperty()}. -1 if the row instance has * not been set yet. */ public int getRowIndex() { return Optional.ofNullable(getRow()) .map(VFXTableRow::getIndex) .orElse(-1); } //================================================================================ // Overridden Methods //================================================================================ @Override protected SkinBase buildSkin() { return new VFXLabeledCellSkin<>(this) { @Override protected void update() { T item = getItem(); E e = extractor.apply(item); String s = converter.toString(e); label.setText(s); } }; } @Override public List defaultStyleClasses() { return List.of("cell-base", "table-cell"); } @Override public void updateColumn(VFXTableColumn> column) { setColumn(column); } @Override public void updateRow(VFXTableRow row) { setRow(row); } //================================================================================ // Getters/Setters //================================================================================ public VFXTableColumn> getColumn() { return column.get(); } /** * Specifies the instance of the column that created this cell. */ public ReadOnlyObjectProperty>> columnProperty() { return column.getReadOnlyProperty(); } protected void setColumn(VFXTableColumn> column) { this.column.set(column); } public VFXTableRow getRow() { return row.get(); } /** * Specifies the instance of the row that contains this cell. */ public ReadOnlyObjectProperty> rowProperty() { return row.getReadOnlyProperty(); } protected void setRow(VFXTableRow row) { this.row.set(row); } @Override public Function getExtractor() { return extractor; } @Override public VFXSimpleTableCell setExtractor(Function extractor) { this.extractor = extractor; return this; } @Override public StringConverter getConverter() { return converter; } @Override public VFXSimpleTableCell setConverter(StringConverter converter) { this.converter = converter; return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy