1 package com
.googlecode
.lanterna
.gui2
.dialogs
;
3 import com
.googlecode
.lanterna
.TerminalTextUtils
;
4 import com
.googlecode
.lanterna
.TerminalSize
;
5 import com
.googlecode
.lanterna
.gui2
.*;
10 * Dialog that allows the user to select an item from a list
12 * @param <T> Type of elements in the list
15 public class ListSelectDialog
<T
> extends DialogWindow
{
21 TerminalSize listBoxPreferredSize
,
27 if(content
.isEmpty()) {
28 throw new IllegalStateException("ListSelectDialog needs at least one item");
31 ActionListBox listBox
= new ActionListBox(listBoxPreferredSize
);
32 for(final T item
: content
) {
33 listBox
.addItem(item
.toString(), new Runnable() {
41 Panel mainPanel
= new Panel();
42 mainPanel
.setLayoutManager(
45 .setRightMarginSize(1));
46 if(description
!= null) {
47 mainPanel
.addComponent(new Label(description
));
48 mainPanel
.addComponent(new EmptySpace(TerminalSize
.ONE
));
50 listBox
.setLayoutData(
51 GridLayout
.createLayoutData(
52 GridLayout
.Alignment
.FILL
,
53 GridLayout
.Alignment
.CENTER
,
57 mainPanel
.addComponent(new EmptySpace(TerminalSize
.ONE
));
60 Panel buttonPanel
= new Panel();
61 buttonPanel
.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1));
62 buttonPanel
.addComponent(new Button(LocalizedString
.Cancel
.toString(), new Runnable() {
67 }).setLayoutData(GridLayout
.createLayoutData(GridLayout
.Alignment
.CENTER
, GridLayout
.Alignment
.CENTER
, true, false)));
68 buttonPanel
.setLayoutData(
69 GridLayout
.createLayoutData(
70 GridLayout
.Alignment
.END
,
71 GridLayout
.Alignment
.CENTER
,
76 setComponent(mainPanel
);
79 private void onSelect(T item
) {
84 private void onCancel() {
91 * @param textGUI Text GUI to add the dialog to
92 * @return The item in the list that was selected or {@code null} if the dialog was cancelled
95 public T
showDialog(WindowBasedTextGUI textGUI
) {
97 super.showDialog(textGUI
);
102 * Shortcut for quickly creating a new dialog
103 * @param textGUI Text GUI to add the dialog to
104 * @param title Title of the dialog
105 * @param description Description of the dialog
106 * @param items Items in the dialog
107 * @param <T> Type of items in the dialog
108 * @return The selected item or {@code null} if cancelled
110 public static <T
> T
showDialog(WindowBasedTextGUI textGUI
, String title
, String description
, T
... items
) {
111 return showDialog(textGUI
, title
, description
, null, items
);
115 * Shortcut for quickly creating a new dialog
116 * @param textGUI Text GUI to add the dialog to
117 * @param title Title of the dialog
118 * @param description Description of the dialog
119 * @param listBoxHeight Maximum height of the list box, scrollbars will be used if there are more items
120 * @param items Items in the dialog
121 * @param <T> Type of items in the dialog
122 * @return The selected item or {@code null} if cancelled
124 public static <T
> T
showDialog(WindowBasedTextGUI textGUI
, String title
, String description
, int listBoxHeight
, T
... items
) {
127 width
= Math
.max(width
, TerminalTextUtils
.getColumnWidth(item
.toString()));
130 return showDialog(textGUI
, title
, description
, new TerminalSize(width
, listBoxHeight
), items
);
134 * Shortcut for quickly creating a new dialog
135 * @param textGUI Text GUI to add the dialog to
136 * @param title Title of the dialog
137 * @param description Description of the dialog
138 * @param listBoxSize Maximum size of the list box, scrollbars will be used if the items cannot fit
139 * @param items Items in the dialog
140 * @param <T> Type of items in the dialog
141 * @return The selected item or {@code null} if cancelled
143 public static <T
> T
showDialog(WindowBasedTextGUI textGUI
, String title
, String description
, TerminalSize listBoxSize
, T
... items
) {
144 ListSelectDialog
<T
> listSelectDialog
= new ListSelectDialogBuilder
<T
>()
146 .setDescription(description
)
147 .setListBoxSize(listBoxSize
)
150 return listSelectDialog
.showDialog(textGUI
);