[maven-release-plugin] prepare for next development iteration
[fanfix.git] / src / jexer / TComboBox.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import java.util.List;
32
33 import jexer.bits.CellAttributes;
34 import jexer.bits.GraphicsChars;
35 import jexer.event.TKeypressEvent;
36 import jexer.event.TMouseEvent;
37 import static jexer.TKeypress.*;
38
39 /**
40 * TComboBox implements a combobox containing a drop-down list and edit
41 * field. Alt-Down can be used to show the drop-down.
42 */
43 public class TComboBox extends TWidget {
44
45 // ------------------------------------------------------------------------
46 // Variables --------------------------------------------------------------
47 // ------------------------------------------------------------------------
48
49 /**
50 * The list of items in the drop-down.
51 */
52 private TList list;
53
54 /**
55 * The edit field containing the value to return.
56 */
57 private TField field;
58
59 /**
60 * The action to perform when the user selects an item (clicks or enter).
61 */
62 private TAction updateAction = null;
63
64 /**
65 * If true, the field cannot be updated to a value not on the list.
66 */
67 private boolean limitToListValue = true;
68
69 // ------------------------------------------------------------------------
70 // Constructors -----------------------------------------------------------
71 // ------------------------------------------------------------------------
72
73 /**
74 * Public constructor.
75 *
76 * @param parent parent widget
77 * @param x column relative to parent
78 * @param y row relative to parent
79 * @param width visible combobox width, including the down-arrow
80 * @param values the possible values for the box, shown in the drop-down
81 * @param valuesIndex the initial index in values, or -1 for no default
82 * value
83 * @param valuesHeight the height of the values drop-down when it is
84 * visible
85 * @param updateAction action to call when a new value is selected from
86 * the list or enter is pressed in the edit field
87 */
88 public TComboBox(final TWidget parent, final int x, final int y,
89 final int width, final List<String> values, final int valuesIndex,
90 final int valuesHeight, final TAction updateAction) {
91
92 // Set parent and window
93 super(parent, x, y, width, 1);
94
95 assert (values != null);
96
97 this.updateAction = updateAction;
98
99 field = addField(0, 0, width - 3, false, "", updateAction, null);
100 if (valuesIndex >= 0) {
101 field.setText(values.get(valuesIndex));
102 }
103
104 list = addList(values, 0, 1, width, valuesHeight,
105 new TAction() {
106 public void DO() {
107 field.setText(list.getSelected());
108 list.setEnabled(false);
109 list.setVisible(false);
110 TComboBox.this.setHeight(1);
111 if (TComboBox.this.limitToListValue == false) {
112 TComboBox.this.activate(field);
113 }
114 if (updateAction != null) {
115 updateAction.DO();
116 }
117 }
118 }
119 );
120 if (valuesIndex >= 0) {
121 list.setSelectedIndex(valuesIndex);
122 }
123
124 list.setEnabled(false);
125 list.setVisible(false);
126 setHeight(1);
127 if (limitToListValue) {
128 field.setEnabled(false);
129 } else {
130 activate(field);
131 }
132 }
133
134 // ------------------------------------------------------------------------
135 // Event handlers ---------------------------------------------------------
136 // ------------------------------------------------------------------------
137
138 /**
139 * Returns true if the mouse is currently on the down arrow.
140 *
141 * @param mouse mouse event
142 * @return true if the mouse is currently on the down arrow
143 */
144 private boolean mouseOnArrow(final TMouseEvent mouse) {
145 if ((mouse.getY() == 0)
146 && (mouse.getX() >= getWidth() - 3)
147 && (mouse.getX() <= getWidth() - 1)
148 ) {
149 return true;
150 }
151 return false;
152 }
153
154 /**
155 * Handle mouse down clicks.
156 *
157 * @param mouse mouse button down event
158 */
159 @Override
160 public void onMouseDown(final TMouseEvent mouse) {
161 if ((mouseOnArrow(mouse)) && (mouse.isMouse1())) {
162 // Make the list visible or not.
163 if (list.isActive()) {
164 list.setEnabled(false);
165 list.setVisible(false);
166 setHeight(1);
167 if (limitToListValue == false) {
168 activate(field);
169 }
170 } else {
171 list.setEnabled(true);
172 list.setVisible(true);
173 setHeight(list.getHeight() + 1);
174 activate(list);
175 }
176 }
177
178 // Pass to parent for the things we don't care about.
179 super.onMouseDown(mouse);
180 }
181
182 /**
183 * Handle keystrokes.
184 *
185 * @param keypress keystroke event
186 */
187 @Override
188 public void onKeypress(final TKeypressEvent keypress) {
189 if (keypress.equals(kbEsc)) {
190 if (list.isActive()) {
191 list.setEnabled(false);
192 list.setVisible(false);
193 setHeight(1);
194 if (limitToListValue == false) {
195 activate(field);
196 }
197 return;
198 }
199 }
200
201 if (keypress.equals(kbAltDown)) {
202 list.setEnabled(true);
203 list.setVisible(true);
204 setHeight(list.getHeight() + 1);
205 activate(list);
206 return;
207 }
208
209 if (keypress.equals(kbTab)
210 || (keypress.equals(kbShiftTab))
211 || (keypress.equals(kbBackTab))
212 ) {
213 if (list.isActive()) {
214 list.setEnabled(false);
215 list.setVisible(false);
216 setHeight(1);
217 if (limitToListValue == false) {
218 activate(field);
219 }
220 return;
221 }
222 }
223
224 // Pass to parent for the things we don't care about.
225 super.onKeypress(keypress);
226 }
227
228 // ------------------------------------------------------------------------
229 // TWidget ----------------------------------------------------------------
230 // ------------------------------------------------------------------------
231
232 /**
233 * Draw the combobox down arrow.
234 */
235 @Override
236 public void draw() {
237 CellAttributes comboBoxColor;
238
239 if (!isAbsoluteActive()) {
240 // We lost focus, turn off the list.
241 if (list.isActive()) {
242 list.setEnabled(false);
243 list.setVisible(false);
244 setHeight(1);
245 if (limitToListValue == false) {
246 activate(field);
247 }
248 }
249 }
250
251 if (isAbsoluteActive()) {
252 comboBoxColor = getTheme().getColor("tcombobox.active");
253 } else {
254 comboBoxColor = getTheme().getColor("tcombobox.inactive");
255 }
256
257 putCharXY(getWidth() - 3, 0, GraphicsChars.DOWNARROWLEFT,
258 comboBoxColor);
259 putCharXY(getWidth() - 2, 0, GraphicsChars.DOWNARROW,
260 comboBoxColor);
261 putCharXY(getWidth() - 1, 0, GraphicsChars.DOWNARROWRIGHT,
262 comboBoxColor);
263 }
264
265 // ------------------------------------------------------------------------
266 // TComboBox --------------------------------------------------------------
267 // ------------------------------------------------------------------------
268
269 /**
270 * Get combobox text value.
271 *
272 * @return text in the edit field
273 */
274 public String getText() {
275 return field.getText();
276 }
277
278 /**
279 * Set combobox text value.
280 *
281 * @param text the new text in the edit field
282 */
283 public void setText(final String text) {
284 setText(text, true);
285 }
286
287 /**
288 * Set combobox text value.
289 *
290 * @param text the new text in the edit field
291 * @param caseSensitive if true, perform a case-sensitive search for the
292 * list item
293 */
294 public void setText(final String text, final boolean caseSensitive) {
295 field.setText(text);
296 for (int i = 0; i < list.getMaxSelectedIndex(); i++) {
297 if (caseSensitive == true) {
298 if (list.getListItem(i).equals(text)) {
299 list.setSelectedIndex(i);
300 return;
301 }
302 } else {
303 if (list.getListItem(i).toLowerCase().equals(text.toLowerCase())) {
304 list.setSelectedIndex(i);
305 return;
306 }
307 }
308 }
309 list.setSelectedIndex(-1);
310 }
311
312 /**
313 * Set combobox text to one of the list values.
314 *
315 * @param index the index in the list
316 */
317 public void setIndex(final int index) {
318 list.setSelectedIndex(index);
319 field.setText(list.getSelected());
320 }
321
322 /**
323 * Get a copy of the list of strings to display.
324 *
325 * @return the list of strings
326 */
327 public final List<String> getList() {
328 return list.getList();
329 }
330
331 /**
332 * Set the new list of strings to display.
333 *
334 * @param list new list of strings
335 */
336 public final void setList(final List<String> list) {
337 this.list.setList(list);
338 field.setText("");
339 }
340
341 }