UIForToolBar.java

  1. package swingtree;

  2. import sprouts.Val;

  3. import javax.swing.*;
  4. import java.util.Objects;

  5. public final class UIForToolBar<T extends JToolBar> extends UIForAnySwing<UIForToolBar<T>, T>
  6. {
  7.     private final BuilderState<T> _state;

  8.     /**
  9.      * {@link UIForAnySwing} (sub)types always wrap
  10.      * a single component for which they are responsible.
  11.      *
  12.      * @param state The {@link BuilderState} modelling how the component is built.
  13.      */
  14.     UIForToolBar( BuilderState<T> state ) {
  15.         Objects.requireNonNull(state);
  16.         _state = state;
  17.     }

  18.     @Override
  19.     protected BuilderState<T> _state() {
  20.         return _state;
  21.     }
  22.    
  23.     @Override
  24.     protected UIForToolBar<T> _newBuilderWithState(BuilderState<T> newState ) {
  25.         return new UIForToolBar<>(newState);
  26.     }

  27.     /**
  28.      *  Use this to set the orientation of the {@link JToolBar},
  29.      *  which is a layout mode that is either horizontal or vertical.
  30.      *  It translates to a call to {@link JToolBar#setOrientation(int)}.
  31.      *
  32.      * @param alignment The {@link UI.Align} value mapping to the {@link JToolBar}'s orientation.
  33.      *                  See {@link JToolBar#setOrientation(int)}.
  34.      * @return This builder node.
  35.      */
  36.     public final UIForToolBar<T> withOrientation( UI.Align alignment ) {
  37.         NullUtil.nullArgCheck(alignment, "alignment", UI.Align.class);
  38.         return _with( thisComponent -> {
  39.                     thisComponent.setOrientation(alignment.forToolBar());
  40.                 })
  41.                 ._this();
  42.     }

  43.     /**
  44.      *  Dynamically determines the orientation of the {@link JToolBar} based on the value of the given {@link Val},
  45.      *  which means that whenever the value of the {@link Val} changes, the orientation of the {@link JToolBar} will change.
  46.      *  This translates to a call to {@link JToolBar#setOrientation(int)}.
  47.      *  The orientation must have either the value HORIZONTAL or VERTICAL.
  48.      *
  49.      * @param alignment The {@link UI.Align} property mapping to the {@link JToolBar}'s orientation.
  50.      *                  See {@link JToolBar#setOrientation(int)}.
  51.      * @return This builder node.
  52.      */
  53.     public final UIForToolBar<T> withOrientation( Val<UI.Align> alignment ) {
  54.         NullUtil.nullArgCheck(alignment, "alignment", Val.class);
  55.         NullUtil.nullPropertyCheck(alignment, "alignment", "Null is not a valid alignment.");
  56.         return _withOnShow( alignment, (c,v) -> {
  57.                     c.setOrientation(v.forToolBar());
  58.                 })
  59.                 ._with( thisComponent -> {
  60.                     thisComponent.setOrientation(alignment.orElseThrowUnchecked().forToolBar());
  61.                 })
  62.                 ._this();
  63.     }

  64. }