Fix UTF8 bug, create first executable JAR file
[jvcard.git] / src / com / googlecode / lanterna / gui2 / dialogs / ActionListDialog.java
CommitLineData
a3b510ab
NR
1package com.googlecode.lanterna.gui2.dialogs;
2
3import com.googlecode.lanterna.TerminalSize;
4import com.googlecode.lanterna.gui2.*;
5
6import java.util.List;
7
8/**
9 * Dialog containing a multiple item action list box
10 * @author Martin
11 */
12public class ActionListDialog extends DialogWindow {
13
14 ActionListDialog(
15 String title,
16 String description,
17 TerminalSize actionListPreferredSize,
18 boolean canCancel,
19 List<Runnable> actions) {
20
21 super(title);
22 if(actions.isEmpty()) {
23 throw new IllegalStateException("ActionListDialog needs at least one item");
24 }
25
26 ActionListBox listBox = new ActionListBox(actionListPreferredSize);
27 for(final Runnable action: actions) {
28 listBox.addItem(action.toString(), new Runnable() {
29 @Override
30 public void run() {
31 action.run();
32 close();
33 }
34 });
35 }
36
37 Panel mainPanel = new Panel();
38 mainPanel.setLayoutManager(
39 new GridLayout(1)
40 .setLeftMarginSize(1)
41 .setRightMarginSize(1));
42 if(description != null) {
43 mainPanel.addComponent(new Label(description));
44 mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));
45 }
46 listBox.setLayoutData(
47 GridLayout.createLayoutData(
48 GridLayout.Alignment.FILL,
49 GridLayout.Alignment.CENTER,
50 true,
51 false))
52 .addTo(mainPanel);
53 mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));
54
55 if(canCancel) {
56 Panel buttonPanel = new Panel();
57 buttonPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(1));
58 buttonPanel.addComponent(new Button(LocalizedString.Cancel.toString(), new Runnable() {
59 @Override
60 public void run() {
61 onCancel();
62 }
63 }).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER, true, false)));
64 buttonPanel.setLayoutData(
65 GridLayout.createLayoutData(
66 GridLayout.Alignment.END,
67 GridLayout.Alignment.CENTER,
68 false,
69 false))
70 .addTo(mainPanel);
71 }
72 setComponent(mainPanel);
73 }
74
75 private void onCancel() {
76 close();
77 }
78
79 /**
80 * Helper method for immediately displaying a {@code ActionListDialog}, the method will return when the dialog is
81 * closed
82 * @param textGUI Text GUI the dialog should be added to
83 * @param title Title of the dialog
84 * @param description Description of the dialog
85 * @param items Items in the {@code ActionListBox}, the label will be taken from each {@code Runnable} by calling
86 * {@code toString()} on each one
87 */
88 public static void showDialog(WindowBasedTextGUI textGUI, String title, String description, Runnable... items) {
89 ActionListDialog actionListDialog = new ActionListDialogBuilder()
90 .setTitle(title)
91 .setDescription(description)
92 .addActions(items)
93 .build();
94 actionListDialog.showDialog(textGUI);
95 }
96}