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

org.fxmisc.wellbehaved.skin.Skins Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
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 visualFactory, Function 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 visualFactory, Function 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(); } }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy