UIForAnyMenuItem.java

  1. package swingtree;

  2. import sprouts.Val;

  3. import javax.swing.*;

  4. /**
  5.  * A builder for {@link JMenuItem}s.
  6.  *
  7.  * @param <I> The type of the builder itself. This is used to allow for method chaining.
  8.  * @param <M> The type of the {@link JMenuItem} being built.
  9.  */
  10. public abstract class UIForAnyMenuItem<I, M extends JMenuItem> extends UIForAnyButton<I, M>
  11. {
  12.     /**
  13.      * Sets the key combination which invokes the wrapped {@link JMenuItem}'s
  14.      * action listeners without navigating the menu hierarchy. It is the
  15.      * UI's responsibility to install the correct action.  Note that
  16.      * when the keyboard accelerator is typed, it will work whether or
  17.      * whether not the menu is currently displayed.
  18.      *
  19.      * @param keyStroke the <code>KeyStroke</code> which will
  20.      *          serve as an accelerator
  21.      * @return This very builder to allow for method chaining.
  22.      */
  23.     public I withKeyStroke( KeyStroke keyStroke ) {
  24.         return _with( thisComponent ->
  25.                     thisComponent.setAccelerator(keyStroke)
  26.                )
  27.                ._this();
  28.     }

  29.     /**
  30.      * Sets the key combination property which invokes the wrapped {@link JMenuItem}'s
  31.      * action listeners without navigating the menu hierarchy. It is the
  32.      * UI's responsibility to install the correct action.  Note that
  33.      * when the keyboard accelerator is typed, it will work whether or
  34.      * whether not the menu is currently displayed.
  35.      *
  36.      * @param keyStroke the dynamically set <code>KeyStroke</code> property which will
  37.      *          serve as an accelerator
  38.      * @return This very builder to allow for method chaining.
  39.      */
  40.     public I withKeyStroke( Val<KeyStroke> keyStroke ) {
  41.         return _withOnShow( keyStroke, (thisComponent,value) -> {
  42.                     thisComponent.setAccelerator(value);
  43.                 })
  44.                 ._with( thisComponent -> {
  45.                     thisComponent.setAccelerator(keyStroke.orElseNull());
  46.                 })
  47.                 ._this();
  48.     }
  49. }