| 1 | package com.googlecode.lanterna.gui2.dialogs; |
| 2 | |
| 3 | import com.googlecode.lanterna.TerminalSize; |
| 4 | |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Arrays; |
| 7 | import java.util.List; |
| 8 | |
| 9 | /** |
| 10 | * Dialog builder for the {@code ListSelectDialog} class, use this to create instances of that class and to customize |
| 11 | * them |
| 12 | * @author Martin |
| 13 | */ |
| 14 | public class ListSelectDialogBuilder<T> extends AbstractDialogBuilder<ListSelectDialogBuilder<T>, ListSelectDialog<T>> { |
| 15 | private TerminalSize listBoxSize; |
| 16 | private boolean canCancel; |
| 17 | private List<T> content; |
| 18 | |
| 19 | /** |
| 20 | * Default constructor |
| 21 | */ |
| 22 | public ListSelectDialogBuilder() { |
| 23 | super("ListSelectDialog"); |
| 24 | this.listBoxSize = null; |
| 25 | this.canCancel = true; |
| 26 | this.content = new ArrayList<T>(); |
| 27 | } |
| 28 | |
| 29 | @Override |
| 30 | protected ListSelectDialogBuilder<T> self() { |
| 31 | return this; |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | protected ListSelectDialog<T> buildDialog() { |
| 36 | return new ListSelectDialog<T>( |
| 37 | title, |
| 38 | description, |
| 39 | listBoxSize, |
| 40 | canCancel, |
| 41 | content); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Sets the size of the list box in the dialog, scrollbars will be used if there is not enough space to draw all |
| 46 | * items. If set to {@code null}, the dialog will ask for enough space to be able to draw all items. |
| 47 | * @param listBoxSize Size of the list box in the dialog |
| 48 | * @return Itself |
| 49 | */ |
| 50 | public ListSelectDialogBuilder<T> setListBoxSize(TerminalSize listBoxSize) { |
| 51 | this.listBoxSize = listBoxSize; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Size of the list box in the dialog or {@code null} if the dialog will ask for enough space to draw all items |
| 57 | * @return Size of the list box in the dialog or {@code null} if the dialog will ask for enough space to draw all items |
| 58 | */ |
| 59 | public TerminalSize getListBoxSize() { |
| 60 | return listBoxSize; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sets if the dialog can be cancelled or not (default: {@code true}) |
| 65 | * @param canCancel If {@code true}, the user has the option to cancel the dialog, if {@code false} there is no such |
| 66 | * button in the dialog |
| 67 | * @return Itself |
| 68 | */ |
| 69 | public ListSelectDialogBuilder<T> setCanCancel(boolean canCancel) { |
| 70 | this.canCancel = canCancel; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns {@code true} if the dialog can be cancelled once it's opened |
| 76 | * @return {@code true} if the dialog can be cancelled once it's opened |
| 77 | */ |
| 78 | public boolean isCanCancel() { |
| 79 | return canCancel; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Adds an item to the list box at the end |
| 84 | * @param item Item to add to the list box |
| 85 | * @return Itself |
| 86 | */ |
| 87 | public ListSelectDialogBuilder<T> addListItem(T item) { |
| 88 | this.content.add(item); |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Adds a list of items to the list box at the end, in the order they are passed in |
| 94 | * @param items Items to add to the list box |
| 95 | * @return Itself |
| 96 | */ |
| 97 | public ListSelectDialogBuilder<T> addListItems(T... items) { |
| 98 | this.content.addAll(Arrays.asList(items)); |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns a copy of the list of items in the list box |
| 104 | * @return Copy of the list of items in the list box |
| 105 | */ |
| 106 | public List<T> getListItems() { |
| 107 | return new ArrayList<T>(content); |
| 108 | } |
| 109 | } |