UIForBox.java

package swingtree;

import swingtree.components.JBox;

import java.awt.*;
import java.util.Objects;

/**
 *  A SwingTree builder node designed for configuring {@link swingtree.components.JBox} instances.
 *
 * @param <B> The type of {@link swingtree.components.JBox} that this {@link UIForBox} is configuring.
 */
public final class UIForBox<B extends JBox> extends UIForAnySwing<UIForBox<B>, B>
{
    private final BuilderState<B> _state;

    /**
     * {@link UIForAnySwing} (sub)types always wrap
     * a builder state which defines how a component is created.
     *
     * @param state The {@link BuilderState} containing the component which will be managed by this builder.
     */
    UIForBox(BuilderState<B> state) {
        Objects.requireNonNull(state, "state");
        _state = state;
    }

    @Override
    protected BuilderState<B> _state() {
        return _state;
    }
    
    @Override
    protected UIForBox<B> _newBuilderWithState(BuilderState<B> newState ) {
        return new UIForBox<>(newState);
    }

    @Override protected void _setEnabled( B thisComponent, boolean isEnabled ) {
        thisComponent.setEnabled( isEnabled );
        /*
            In the vast vast majority of cases regular JBoxs are simple wrappers for
            other components.
            They are mostly used to group things together, provide a border and
            position them using a fancy layout manager.
            Disabling only a JBox is therefore not very useful, because it will not
            disable the components it contains.
            So what we want to do here is traverse the tree of JBox instances
            and disable all the components that are contained in the tree
            except for the children of non JBoxs.
        */
        InternalUtil._traverseEnable( thisComponent, isEnabled );
        /*
            Note:
            If you really only want to disable the JBox itself, then you can
            simply peek into the tree and disable the JBox directly.
            Or, a better idea, is to simply change color and event handling
            of the JBox to make it look and behave like a disabled component,
            but still allow the user to interact with the components it contains.
         */
    }

}