org.eclipse.fx.ui.controls.tree.SimpleTreeCell Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.eclipse.fx.ui.controls Show documentation
Show all versions of org.eclipse.fx.ui.controls Show documentation
Custom JavaFX controls like a styled text component, ...
The newest version!
package org.eclipse.fx.ui.controls.tree;
import java.util.List;
import java.util.function.Function;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import org.eclipse.fx.ui.controls.styledtext.StyledLabel;
import org.eclipse.fx.ui.controls.styledtext.StyledString;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* A cell who knows how to deal {@link StyledString}
*
* @param
* the domain type
* @since 1.2
*/
public class SimpleTreeCell extends TreeCell {
@NonNull
private final Function<@Nullable T, @Nullable CharSequence> labelExtractor;
@NonNull
private final Function<@Nullable T, @Nullable Node> graphicExtractor;
@NonNull
private final Function<@Nullable T, @NonNull List<@NonNull String>> cssClassProvider;
/**
* Create a new instance
*
* @param labelExtractor
* extract the text from the domain object
* @param cssClassProvider
* get the CSS classes for the given domain object
*/
public SimpleTreeCell(
@NonNull Function<@Nullable T, @Nullable CharSequence> labelExtractor,
@NonNull Function<@Nullable T, @NonNull List<@NonNull String>> cssClassProvider) {
this(labelExtractor, i -> null, cssClassProvider);
}
/**
* Create a new instance
*
* @param labelExtractor
* extract the text from the domain object
* @param graphicExtractor
* extract graphics to displayed by the label
* @param cssClassProvider
* get the CSS classes for the given domain object
*/
public SimpleTreeCell(
@NonNull Function<@Nullable T, @Nullable CharSequence> labelExtractor,
@NonNull Function<@Nullable T, @Nullable Node> graphicExtractor,
@NonNull Function<@Nullable T, @NonNull List<@NonNull String>> cssClassProvider) {
this.labelExtractor = labelExtractor;
this.graphicExtractor = graphicExtractor;
this.cssClassProvider = cssClassProvider;
}
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
CharSequence t = this.labelExtractor.apply(item);
if (t == null) {
setGraphic(null);
} else if (t instanceof StyledString) {
StyledLabel l = new StyledLabel((StyledString) t);
l.getStyleClass().addAll(this.cssClassProvider.apply(item));
Node g = this.graphicExtractor.apply(item);
if( g != null ) {
l.setGraphic(g);
}
setGraphic(l);
} else {
Label l = new Label(t.toString());
l.getStyleClass().addAll(this.cssClassProvider.apply(item));
Node g = this.graphicExtractor.apply(item);
if( g != null ) {
l.setGraphic(g);
}
setGraphic(l);
}
} else {
setGraphic(null);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy