| 1 | package com.googlecode.lanterna.gui2.dialogs; |
| 2 | |
| 3 | import com.googlecode.lanterna.TerminalTextUtils; |
| 4 | import com.googlecode.lanterna.TerminalSize; |
| 5 | import com.googlecode.lanterna.gui2.*; |
| 6 | |
| 7 | import java.util.List; |
| 8 | |
| 9 | /** |
| 10 | * Dialog that allows the user to select an item from a list |
| 11 | * |
| 12 | * @param <T> Type of elements in the list |
| 13 | * @author Martin |
| 14 | */ |
| 15 | public class ListSelectDialog<T> extends DialogWindow { |
| 16 | private T result; |
| 17 | |
| 18 | ListSelectDialog( |
| 19 | String title, |
| 20 | String description, |
| 21 | TerminalSize listBoxPreferredSize, |
| 22 | boolean canCancel, |
| 23 | List<T> content) { |
| 24 | |
| 25 | super(title); |
| 26 | this.result = null; |
| 27 | if(content.isEmpty()) { |
| 28 | throw new IllegalStateException("ListSelectDialog needs at least one item"); |
| 29 | } |
| 30 | |
| 31 | ActionListBox listBox = new ActionListBox(listBoxPreferredSize); |
| 32 | for(final T item: content) { |
| 33 | listBox.addItem(item.toString(), new Runnable() { |
| 34 | @Override |
| 35 | public void run() { |
| 36 | onSelect(item); |
| 37 | } |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | Panel mainPanel = new Panel(); |
| 42 | mainPanel.setLayoutManager( |
| 43 | new GridLayout(1) |
| 44 | .setLeftMarginSize(1) |
| 45 | .setRightMarginSize(1)); |
| 46 | if(description != null) { |
| 47 | mainPanel.addComponent(new Label(description)); |
| 48 | mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); |
| 49 | } |
| 50 | listBox.setLayoutData( |
| 51 | GridLayout.createLayoutData( |
| 52 | GridLayout.Alignment.FILL, |
| 53 | GridLayout.Alignment.CENTER, |
| 54 | true, |
| 55 | false)) |
| 56 | .addTo(mainPanel); |
| 57 | mainPanel.addComponent(new EmptySpace(TerminalSize.ONE)); |
| 58 | |
| 59 | if(canCancel) { |
| 60 | Panel buttonPanel = new Panel(); |
| 61 | buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1)); |
| 62 | buttonPanel.addComponent(new Button(LocalizedString.Cancel.toString(), new Runnable() { |
| 63 | @Override |
| 64 | public void run() { |
| 65 | onCancel(); |
| 66 | } |
| 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, |
| 72 | false, |
| 73 | false)) |
| 74 | .addTo(mainPanel); |
| 75 | } |
| 76 | setComponent(mainPanel); |
| 77 | } |
| 78 | |
| 79 | private void onSelect(T item) { |
| 80 | result = item; |
| 81 | close(); |
| 82 | } |
| 83 | |
| 84 | private void onCancel() { |
| 85 | close(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | * |
| 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 |
| 93 | */ |
| 94 | @Override |
| 95 | public T showDialog(WindowBasedTextGUI textGUI) { |
| 96 | result = null; |
| 97 | super.showDialog(textGUI); |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | /** |
| 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 |
| 109 | */ |
| 110 | public static <T> T showDialog(WindowBasedTextGUI textGUI, String title, String description, T... items) { |
| 111 | return showDialog(textGUI, title, description, null, items); |
| 112 | } |
| 113 | |
| 114 | /** |
| 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 |
| 123 | */ |
| 124 | public static <T> T showDialog(WindowBasedTextGUI textGUI, String title, String description, int listBoxHeight, T... items) { |
| 125 | int width = 0; |
| 126 | for(T item: items) { |
| 127 | width = Math.max(width, TerminalTextUtils.getColumnWidth(item.toString())); |
| 128 | } |
| 129 | width += 2; |
| 130 | return showDialog(textGUI, title, description, new TerminalSize(width, listBoxHeight), items); |
| 131 | } |
| 132 | |
| 133 | /** |
| 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 |
| 142 | */ |
| 143 | public static <T> T showDialog(WindowBasedTextGUI textGUI, String title, String description, TerminalSize listBoxSize, T... items) { |
| 144 | ListSelectDialog<T> listSelectDialog = new ListSelectDialogBuilder<T>() |
| 145 | .setTitle(title) |
| 146 | .setDescription(description) |
| 147 | .setListBoxSize(listBoxSize) |
| 148 | .addListItems(items) |
| 149 | .build(); |
| 150 | return listSelectDialog.showDialog(textGUI); |
| 151 | } |
| 152 | } |