Commit | Line | Data |
---|---|---|
a3b510ab NR |
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 ActionListDialog} class, use this to create instances of that class and to customize | |
11 | * them | |
12 | * @author Martin | |
13 | */ | |
14 | public class ActionListDialogBuilder extends AbstractDialogBuilder<ActionListDialogBuilder, ActionListDialog> { | |
15 | private TerminalSize listBoxSize; | |
16 | private boolean canCancel; | |
17 | private List<Runnable> actions; | |
18 | ||
19 | /** | |
20 | * Default constructor | |
21 | */ | |
22 | public ActionListDialogBuilder() { | |
23 | super("ActionListDialogBuilder"); | |
24 | this.listBoxSize = null; | |
25 | this.canCancel = true; | |
26 | this.actions = new ArrayList<Runnable>(); | |
27 | } | |
28 | ||
29 | @Override | |
30 | protected ActionListDialogBuilder self() { | |
31 | return this; | |
32 | } | |
33 | ||
34 | @Override | |
35 | protected ActionListDialog buildDialog() { | |
36 | return new ActionListDialog( | |
37 | title, | |
38 | description, | |
39 | listBoxSize, | |
40 | canCancel, | |
41 | actions); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Sets the size of the internal {@code ActionListBox} in columns and rows, forcing scrollbars to appear if the | |
46 | * space isn't big enough to contain all the items | |
47 | * @param listBoxSize Size of the {@code ActionListBox} | |
48 | * @return Itself | |
49 | */ | |
50 | public ActionListDialogBuilder setListBoxSize(TerminalSize listBoxSize) { | |
51 | this.listBoxSize = listBoxSize; | |
52 | return this; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Returns the specified size of the internal {@code ActionListBox} or {@code null} if there is no size and the list | |
57 | * box will attempt to take up enough size to draw all items | |
58 | * @return Specified size of the internal {@code ActionListBox} or {@code null} if there is no size | |
59 | */ | |
60 | public TerminalSize getListBoxSize() { | |
61 | return listBoxSize; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Sets if the dialog can be cancelled or not (default: {@code true}) | |
66 | * @param canCancel If {@code true}, the user has the option to cancel the dialog, if {@code false} there is no such | |
67 | * button in the dialog | |
68 | * @return Itself | |
69 | */ | |
70 | public ActionListDialogBuilder setCanCancel(boolean canCancel) { | |
71 | this.canCancel = canCancel; | |
72 | return this; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Returns {@code true} if the dialog can be cancelled once it's opened | |
77 | * @return {@code true} if the dialog can be cancelled once it's opened | |
78 | */ | |
79 | public boolean isCanCancel() { | |
80 | return canCancel; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Adds an additional action to the {@code ActionListBox} that is to be displayed when the dialog is opened | |
85 | * @param label Label of the new action | |
86 | * @param action Action to perform if the user selects this item | |
87 | * @return Itself | |
88 | */ | |
89 | public ActionListDialogBuilder addAction(final String label, final Runnable action) { | |
90 | return addAction(new Runnable() { | |
91 | @Override | |
92 | public String toString() { | |
93 | return label; | |
94 | } | |
95 | ||
96 | @Override | |
97 | public void run() { | |
98 | action.run(); | |
99 | } | |
100 | }); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Adds an additional action to the {@code ActionListBox} that is to be displayed when the dialog is opened. The | |
105 | * label of this item will be derived by calling {@code toString()} on the runnable | |
106 | * @param action Action to perform if the user selects this item | |
107 | * @return Itself | |
108 | */ | |
109 | public ActionListDialogBuilder addAction(Runnable action) { | |
110 | this.actions.add(action); | |
111 | return this; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Adds additional actions to the {@code ActionListBox} that is to be displayed when the dialog is opened. The | |
116 | * label of the items will be derived by calling {@code toString()} on each runnable | |
117 | * @param actions Items to add to the {@code ActionListBox} | |
118 | * @return Itself | |
119 | */ | |
120 | public ActionListDialogBuilder addActions(Runnable... actions) { | |
121 | this.actions.addAll(Arrays.asList(actions)); | |
122 | return this; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Returns a copy of the internal list of actions currently inside this builder that will be assigned to the | |
127 | * {@code ActionListBox} in the dialog when built | |
128 | * @return Copy of the internal list of actions currently inside this builder | |
129 | */ | |
130 | public List<Runnable> getActions() { | |
131 | return new ArrayList<Runnable>(actions); | |
132 | } | |
133 | } |