Class SelectionBuilder<E>


  • public class SelectionBuilder<E>
    extends Object
    Tabular selection with simple navigation. It displays a list of items in a table that can be paginated and navigated between using the arrow keys, numbers, pg-up, pg-down etc.
    
     {message}: [c=command, o=overview]
      1 {line 1}
      2 {line 2}
      3 {line 3}
     Your choice (1..N or c,o):
     

    When doing actions, it is possible to add extra output to the user, this will be printed below the `Your choice` line with a LinePrinter connected back to the SelectionBuilder instance.

    By default, there are no actions on the items, even exit is not provided. Actions are specified using a list of "commands", that each have an action, where each action has a Reaction. Note that any runtime exception thrown while handling an action exits the selection.

    • Constructor Detail

      • SelectionBuilder

        public SelectionBuilder​(EntrySource<E> source)
        Create a selection builder instance.
        Parameters:
        source - Entry source for getting entries, and specifying entry type.
    • Method Detail

      • initial

        public SelectionBuilder<E> initial​(E initial)
        Set the initially selected item.
        Parameters:
        initial - Initial entry. If entry is not found in the entry source, this method will throw exception.
        Returns:
        The builder.
      • initial

        public SelectionBuilder<E> initial​(int index)
        Set the initially selected item index.
        Parameters:
        index - The index to set.
        Returns:
        The builder.
      • on

        public SelectionBuilder<E> on​(char c,
                                      String name,
                                      SelectionReaction reaction)
        Add a reaction to key-press.
        Parameters:
        c - The character to react to.
        name - The command name.
        reaction - The reaction.
        Returns:
        The builder.
      • on

        public SelectionBuilder<E> on​(char c,
                                      String name,
                                      EntryAction<E> action)
        Add an action to key-press.
        Parameters:
        c - The character to react to.
        name - The command name.
        action - The action.
        Returns:
        The builder.
      • on

        public SelectionBuilder<E> on​(net.morimekta.strings.chr.Char c,
                                      String name,
                                      SelectionReaction reaction)
        Add a reaction to key-press.
        Parameters:
        c - The character to react to.
        name - The command name.
        reaction - The reaction.
        Returns:
        The builder.
      • on

        public SelectionBuilder<E> on​(net.morimekta.strings.chr.Char c,
                                      String name,
                                      Function<E,​SelectionReaction> action)
        Add an action to key-press.
        Parameters:
        c - The character to react to.
        name - The command name.
        action - The action.
        Returns:
        The builder.
      • on

        public SelectionBuilder<E> on​(net.morimekta.strings.chr.Char c,
                                      String name,
                                      EntryAction<E> action)
        Add an action to key-press.
        Parameters:
        c - The character to react to.
        name - The command name.
        action - The action.
        Returns:
        The builder.
      • hiddenOn

        public SelectionBuilder<E> hiddenOn​(char c,
                                            SelectionReaction reaction)
        Add a hidden reaction to key-press. This will not be shown in the prompt overview of actions.
        Parameters:
        c - The character to react to.
        reaction - The reaction.
        Returns:
        The builder.
      • hiddenOn

        public SelectionBuilder<E> hiddenOn​(char c,
                                            Function<E,​SelectionReaction> action)
        Add a hidden reaction to key-press. This will not be shown in the prompt overview of actions.
        Parameters:
        c - The character to react to.
        action - The action.
        Returns:
        The builder.
      • hiddenOn

        public SelectionBuilder<E> hiddenOn​(char c,
                                            EntryAction<E> action)
        Add a hidden reaction to key-press. This will not be shown in the prompt overview of actions.
        Parameters:
        c - The character to react to.
        action - The action.
        Returns:
        The builder.
      • hiddenOn

        public SelectionBuilder<E> hiddenOn​(net.morimekta.strings.chr.Char c,
                                            SelectionReaction reaction)
        Add a hidden reaction to key-press. This will not be shown in the prompt overview of actions.
        Parameters:
        c - The character to react to.
        reaction - The reaction.
        Returns:
        The builder.
      • hiddenOn

        public SelectionBuilder<E> hiddenOn​(net.morimekta.strings.chr.Char c,
                                            Function<E,​SelectionReaction> action)
        Add a hidden reaction to key-press. This will not be shown in the prompt overview of actions.
        Parameters:
        c - The character to react to.
        action - The action.
        Returns:
        The builder.
      • hiddenOn

        public SelectionBuilder<E> hiddenOn​(net.morimekta.strings.chr.Char c,
                                            EntryAction<E> action)
        Add a hidden reaction to key-press. This will not be shown in the prompt overview of actions.
        Parameters:
        c - The character to react to.
        action - The action.
        Returns:
        The builder.
      • prompt

        public SelectionBuilder<E> prompt​(String prompt)
        Parameters:
        prompt - Prompt to show before the selection.
        Returns:
        The builder.
      • printer

        public SelectionBuilder<E> printer​(EntryPrinter<E> printer)
        Parameters:
        printer - Printer for making selection line from items.
        Returns:
        The builder.
      • tty

        public SelectionBuilder<E> tty​(net.morimekta.io.tty.TTY tty)
        Parameters:
        tty - TTY to control terminal IO.
        Returns:
        The builder.
      • terminal

        public SelectionBuilder<E> terminal​(Terminal terminal)
        Parameters:
        terminal - Terminal for handling IO.
        Returns:
        The builder.
      • clock

        public SelectionBuilder<E> clock​(Clock clock)
        Parameters:
        clock - Clock for timing actions. Used to decide whether a digit adds digit to positional number or is a new position.
        Returns:
        The builder.
      • pageSize

        public SelectionBuilder<E> pageSize​(int pageSize)
        Parameters:
        pageSize - A fixed page size, or number of visible items.
        Returns:
        The builder.
      • paged

        public SelectionBuilder<E> paged()
        Force the selection to be paged. This is handy if it is possible to increase number of selection items while it is running.
        Returns:
        The builder.
      • pageMargin

        public SelectionBuilder<E> pageMargin​(int pageMargin)
        Parameters:
        pageMargin - Page margin, number of items above the page-size needed to trigger pagination on the selection itself. This is to avoid adding a single page #2 with only very few items in it.
        Returns:
        The builder.
      • lineWidth

        public SelectionBuilder<E> lineWidth​(int lineWidth)
        Parameters:
        lineWidth - Line width for formatting the selection lines itself. If not set will automatically detect line width from TTY.
        Returns:
        The builder.
      • build

        public Selection<E> build()
        Build and start the selection. The returned selection instance must be closed by the caller.
        Returns:
        The selection.