org.fxmisc.wellbehaved.skin.Skins Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.fxmisc.wellbehaved.skin;
import java.util.List;
import java.util.function.Function;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.control.SkinBase;
/**
* Provides factory methods to wire a {@link Visual} and a {@link Behavior} to
* form a {@link Skin}. The factory methods are meant to be used to implement
* the {@link Control#createDefaultSkin()} method.
*
* @deprecated Since 0.3. We have come to believe that skins, as designed in
* JavaFX, are not very useful and not worth the trouble. Package
* {@link org.fxmisc.wellbehaved.skin} will be removed in a future version.
*/
@Deprecated
public final class Skins {
/**
* Creates a skin whose visual consists of a single node. The returned skin
* attaches that node to the control and takes care of removing it from the
* control on disposal.
*
* The intended usage is
*
*
* {@code
* protected Skin> createDefaultSkin() {
* return Skins.createSimpleSkin(
* this,
* control -> new FooVisual<>(control),
* (control, visual) -> new FooBehavior(control, visual));
* }
* }
*
*
* or, more concisely
*
*
* {@code
* protected Skin> createDefaultSkin() {
* return Skins.createSimpleSkin(this, FooVisual::new, FooBehavior::new);
* }
* }
*
*
* @param control control for which the skin is going to be created.
* @param visualFactory function to create the Visual, given the control.
* @param behaviorFactory function to create the Behavior, given the Visual.
* @return a Skin that delegates the view aspect to the Visual and the
* controller aspect to the Behavior.
*/
public static > Skin createSimpleSkin(
C control,
Function super C, ? extends V> visualFactory,
Function super V, ? extends Behavior> behaviorFactory) {
return new SkinBase(control) {
private final V visual = visualFactory.apply(control);
private final Behavior behavior = behaviorFactory.apply(visual);
private final Node node = visual.getNode();
{
getChildren().add(node);
}
@Override
public void dispose() {
behavior.dispose();
visual.dispose();
getChildren().remove(node);
}
@Override
public List> getCssMetaData() {
return visual.getCssMetaData();
}
};
}
/**
* Creates a skin whose visual is in direct control of managing the
* control's child list.
*
* The intended usage is
*
*
* {@code
* protected Skin> createDefaultSkin() {
* return Skins.createComplexSkin(
* this,
* control -> new FooVisual<>(control),
* (control, visual) -> new FooBehavior(control, visual));
* }
* }
*
*
* or, more concisely
*
*
* {@code
* protected Skin> createDefaultSkin() {
* return Skins.createComplexSkin(this, FooVisual::new, FooBehavior::new);
* }
* }
*
*
* @param control control for which the skin is going to be created.
* @param visualFactory function to create the Visual, given the control.
* @param behaviorFactory function to create the Behavior, given the Visual.
* @return a Skin that delegates the view aspect to the Visual and the
* controller aspect to the Behavior.
*/
public static > Skin createComplexSkin(
C control,
Function super C, ? extends V> visualFactory,
Function super V, ? extends Behavior> behaviorFactory) {
return new SkinBase(control) {
private final V visual = visualFactory.apply(control);
private final Behavior behavior = behaviorFactory.apply(visual);
@Override
public void dispose() {
behavior.dispose();
visual.dispose();
}
@Override
public List> getCssMetaData() {
return visual.getCssMetaData();
}
};
}
}