PainterConf.java

  1. package swingtree.style;

  2. import com.google.errorprone.annotations.Immutable;
  3. import swingtree.UI;
  4. import swingtree.api.Painter;

  5. import java.util.Objects;

  6. /**
  7.  *  An immutable config API for specifying a painter
  8.  *  style for custom {@link Painter} implementations that are used to paint
  9.  *  the inner area of a component on a specific layer.
  10.  *  The following properties with their respective purpose are available:
  11.  *  <br>
  12.  *  <ol>
  13.  *      <li><h3>Painter</h3>
  14.  *          <p>
  15.  *              The painter is a function that takes a {@link java.awt.Graphics2D} instance
  16.  *              which is used to paint onto the inner area of a component.
  17.  *          </p>
  18.  *      </li>
  19.  *      <li><h3>Clip Area</h3>
  20.  *          <p>
  21.  *              The clip area specifies the area of the component that the painter
  22.  *              should be clipped to, which means that the things painted
  23.  *              will only be visible within the specified area.
  24.  *          </p>
  25.  *      </li>*
  26.  *  </ol>
  27.  *  <p>
  28.  *  Note that you can use the {@link #none()} method to specify that no painter should be used,
  29.  *  as the instance returned by that method is a painter with a {@link Painter#none()} painter,
  30.  *  effectively making it a representation of the absence of a painter.
  31.  *  <p>
  32.  */
  33. @Immutable
  34. @SuppressWarnings("Immutable")
  35. final class PainterConf
  36. {
  37.     private static final PainterConf _NONE = new PainterConf(Painter.none(), UI.ComponentArea.BODY);


  38.     static PainterConf none() { return _NONE; }

  39.     static PainterConf of( Painter painter, UI.ComponentArea area ) {
  40.         if ( painter == Painter.none() )
  41.             return none();
  42.         else
  43.             return new PainterConf(painter, area);
  44.     }


  45.     private final Painter _painter;
  46.     private final UI.ComponentArea _clipArea;


  47.     private PainterConf(Painter painter, UI.ComponentArea area )
  48.     {
  49.         _painter  = Objects.requireNonNull(painter);
  50.         _clipArea = Objects.requireNonNull(area);
  51.     }


  52.     public Painter painter() { return _painter; }

  53.     public UI.ComponentArea clipArea() {
  54.         return _clipArea;
  55.     }

  56.     @Override
  57.     public String toString() {
  58.         if ( _painter == Painter.none() )
  59.             return this.getClass().getSimpleName() + "[NONE]";
  60.         else
  61.             return this.getClass().getSimpleName() + "[" +
  62.                     "painter=" + StyleUtil.toString(_painter) + ", " +
  63.                     "clipArea=" + _clipArea +
  64.                 ']';
  65.     }

  66.     @Override
  67.     public boolean equals(Object o) {
  68.         if ( this == o )
  69.             return true;
  70.         if ( !(o instanceof PainterConf) )
  71.             return false;

  72.         PainterConf that = (PainterConf) o;

  73.         return _painter.equals(that._painter) &&
  74.                _clipArea == that._clipArea;
  75.     }

  76.     @Override
  77.     public int hashCode() {
  78.         return Objects.hash(_painter, _clipArea);
  79.     }
  80. }