Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard.tui; |
2 | ||
5ad0e17e | 3 | import java.io.IOException; |
bcb54330 | 4 | import java.util.ArrayList; |
a3b510ab NR |
5 | import java.util.Arrays; |
6 | import java.util.LinkedList; | |
7 | import java.util.List; | |
8 | ||
7da41ecd NR |
9 | import be.nikiroo.jvcard.launcher.Main; |
10 | import be.nikiroo.jvcard.resources.StringUtils; | |
e119a1c1 NR |
11 | import be.nikiroo.jvcard.resources.enums.ColorOption; |
12 | import be.nikiroo.jvcard.resources.enums.StringId; | |
a3b510ab | 13 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
f04d8b1c | 14 | import be.nikiroo.jvcard.tui.panes.ContactDetails; |
296a0b75 | 15 | import be.nikiroo.jvcard.tui.panes.ContactDetailsRaw; |
fae07ea7 NR |
16 | import be.nikiroo.jvcard.tui.panes.ContactList; |
17 | import be.nikiroo.jvcard.tui.panes.MainContent; | |
a3b510ab NR |
18 | |
19 | import com.googlecode.lanterna.TerminalSize; | |
20 | import com.googlecode.lanterna.gui2.BasicWindow; | |
21 | import com.googlecode.lanterna.gui2.BorderLayout; | |
22 | import com.googlecode.lanterna.gui2.Direction; | |
23 | import com.googlecode.lanterna.gui2.Interactable; | |
24 | import com.googlecode.lanterna.gui2.Label; | |
25 | import com.googlecode.lanterna.gui2.LinearLayout; | |
26 | import com.googlecode.lanterna.gui2.Panel; | |
27 | import com.googlecode.lanterna.gui2.TextBox; | |
a3b510ab NR |
28 | import com.googlecode.lanterna.gui2.Window; |
29 | import com.googlecode.lanterna.input.KeyStroke; | |
30 | import com.googlecode.lanterna.input.KeyType; | |
31 | ||
32 | /** | |
0b0b2b0f NR |
33 | * This is the main "window" of the program. It can show up to one |
34 | * {@link MainContent} at any one time, but keeps a stack of contents. | |
a3b510ab NR |
35 | * |
36 | * @author niki | |
37 | * | |
38 | */ | |
f720df72 | 39 | |
a3b510ab NR |
40 | public class MainWindow extends BasicWindow { |
41 | private List<KeyAction> defaultActions = new LinkedList<KeyAction>(); | |
42 | private List<KeyAction> actions = new LinkedList<KeyAction>(); | |
0b0b2b0f | 43 | private List<MainContent> contentStack = new LinkedList<MainContent>(); |
f720df72 | 44 | private UserQuestion userQuestion; |
bcb54330 | 45 | private String titleCache; |
a3b510ab NR |
46 | private Panel titlePanel; |
47 | private Panel mainPanel; | |
48 | private Panel contentPanel; | |
49 | private Panel actionPanel; | |
50 | private Panel messagePanel; | |
51 | private TextBox text; | |
52 | ||
f720df72 NR |
53 | /** |
54 | * Information about a question to ask the user and its answer. | |
55 | * | |
56 | * @author niki | |
57 | * | |
58 | */ | |
59 | private class UserQuestion { | |
60 | private boolean oneKeyAnswer; | |
61 | private KeyAction action; | |
62 | private String answer; | |
63 | ||
64 | /** | |
65 | * Create a new {@link UserQuestion}. | |
66 | * | |
67 | * @param action | |
68 | * the action that triggered the question | |
69 | * @param oneKeyAnswer | |
70 | * TRUE if we expect a one-key answer | |
71 | */ | |
72 | public UserQuestion(KeyAction action, boolean oneKeyAnswer) { | |
73 | this.action = action; | |
74 | this.oneKeyAnswer = oneKeyAnswer; | |
75 | } | |
76 | ||
77 | /** | |
78 | * Return the {@link KeyAction} that triggered the question. | |
79 | * | |
80 | * @return the {@link KeyAction} | |
81 | */ | |
82 | public KeyAction getAction() { | |
83 | return action; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Check if a one-key answer is expected. | |
88 | * | |
89 | * @return TRUE if a one-key answer is expected | |
90 | */ | |
91 | public boolean isOneKeyAnswer() { | |
92 | return oneKeyAnswer; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Return the user answer. | |
97 | * | |
98 | * @return the user answer | |
99 | */ | |
100 | public String getAnswer() { | |
101 | return answer; | |
102 | } | |
103 | ||
104 | /** | |
105 | * Set the user answer. | |
106 | * | |
107 | * @param answer | |
108 | * the new answer | |
109 | */ | |
110 | public void setAnswer(String answer) { | |
111 | this.answer = answer; | |
112 | } | |
113 | } | |
114 | ||
0b0b2b0f NR |
115 | /** |
116 | * Create a new, empty window. | |
117 | */ | |
a3b510ab NR |
118 | public MainWindow() { |
119 | this(null); | |
120 | } | |
121 | ||
0b0b2b0f NR |
122 | /** |
123 | * Create a new window hosting the given content. | |
124 | * | |
125 | * @param content | |
126 | * the content to host | |
127 | */ | |
a3b510ab NR |
128 | public MainWindow(MainContent content) { |
129 | super(content == null ? "" : content.getTitle()); | |
130 | ||
131 | setHints(Arrays.asList(Window.Hint.FULL_SCREEN, | |
132 | Window.Hint.NO_DECORATIONS, Window.Hint.FIT_TERMINAL_WINDOW)); | |
133 | ||
134 | defaultActions.add(new KeyAction(Mode.BACK, 'q', | |
135 | StringId.KEY_ACTION_BACK)); | |
136 | defaultActions.add(new KeyAction(Mode.BACK, KeyType.Escape, | |
137 | StringId.NULL)); | |
138 | defaultActions.add(new KeyAction(Mode.HELP, 'h', | |
139 | StringId.KEY_ACTION_HELP)); | |
140 | defaultActions.add(new KeyAction(Mode.HELP, KeyType.F1, StringId.NULL)); | |
141 | ||
142 | actionPanel = new Panel(); | |
143 | contentPanel = new Panel(); | |
144 | mainPanel = new Panel(); | |
145 | messagePanel = new Panel(); | |
146 | titlePanel = new Panel(); | |
147 | ||
148 | Panel actionMessagePanel = new Panel(); | |
149 | ||
150 | LinearLayout llayout = new LinearLayout(Direction.HORIZONTAL); | |
151 | llayout.setSpacing(0); | |
152 | actionPanel.setLayoutManager(llayout); | |
153 | ||
0b0b2b0f NR |
154 | BorderLayout blayout = new BorderLayout(); |
155 | titlePanel.setLayoutManager(blayout); | |
a3b510ab NR |
156 | |
157 | llayout = new LinearLayout(Direction.VERTICAL); | |
158 | llayout.setSpacing(0); | |
159 | messagePanel.setLayoutManager(llayout); | |
160 | ||
0b0b2b0f | 161 | blayout = new BorderLayout(); |
a3b510ab NR |
162 | mainPanel.setLayoutManager(blayout); |
163 | ||
164 | blayout = new BorderLayout(); | |
165 | contentPanel.setLayoutManager(blayout); | |
166 | ||
167 | blayout = new BorderLayout(); | |
168 | actionMessagePanel.setLayoutManager(blayout); | |
169 | ||
170 | actionMessagePanel | |
171 | .addComponent(messagePanel, BorderLayout.Location.TOP); | |
172 | actionMessagePanel.addComponent(actionPanel, | |
173 | BorderLayout.Location.CENTER); | |
174 | ||
175 | mainPanel.addComponent(titlePanel, BorderLayout.Location.TOP); | |
176 | mainPanel.addComponent(contentPanel, BorderLayout.Location.CENTER); | |
177 | mainPanel | |
178 | .addComponent(actionMessagePanel, BorderLayout.Location.BOTTOM); | |
179 | ||
180 | pushContent(content); | |
181 | ||
182 | setComponent(mainPanel); | |
183 | } | |
184 | ||
0b0b2b0f NR |
185 | /** |
186 | * "push" some content to the window stack. | |
187 | * | |
188 | * @param content | |
189 | * the new top-of-the-stack content | |
190 | */ | |
a3b510ab NR |
191 | public void pushContent(MainContent content) { |
192 | List<KeyAction> actions = null; | |
a3b510ab NR |
193 | |
194 | contentPanel.removeAllComponents(); | |
195 | if (content != null) { | |
a3b510ab NR |
196 | actions = content.getKeyBindings(); |
197 | contentPanel.addComponent(content, BorderLayout.Location.CENTER); | |
0b0b2b0f | 198 | this.contentStack.add(content); |
9c8baf0c NR |
199 | |
200 | Interactable focus = content.nextFocus(null); | |
201 | if (focus != null) | |
202 | focus.takeFocus(); | |
a3b510ab NR |
203 | } |
204 | ||
bcb54330 | 205 | setTitle(); |
0b0b2b0f | 206 | setActions(actions, true); |
a3b510ab NR |
207 | } |
208 | ||
0b0b2b0f NR |
209 | /** |
210 | * "pop" the latest, top-of-the-stack content from the window stack. | |
211 | * | |
212 | * @return the removed content if any | |
213 | */ | |
9c8baf0c NR |
214 | public MainContent popContent() { |
215 | MainContent removed = null; | |
216 | MainContent prev = null; | |
9c8baf0c | 217 | |
0b0b2b0f NR |
218 | MainContent content = getContent(); |
219 | if (content != null) | |
220 | removed = contentStack.remove(contentStack.size() - 1); | |
9c8baf0c | 221 | |
0b0b2b0f NR |
222 | if (contentStack.size() > 0) |
223 | prev = contentStack.remove(contentStack.size() - 1); | |
a3b510ab | 224 | |
5ad0e17e NR |
225 | if (prev != null) { |
226 | try { | |
227 | String mess = prev.wakeup(); | |
228 | if (mess != null) | |
229 | setMessage(mess, false); | |
230 | } catch (IOException e) { | |
231 | setMessage(e.getMessage(), true); | |
232 | } | |
233 | } | |
234 | ||
0b0b2b0f | 235 | pushContent(prev); |
a3b510ab | 236 | |
0b0b2b0f | 237 | return removed; |
a3b510ab NR |
238 | } |
239 | ||
9c8baf0c NR |
240 | /** |
241 | * Show the given message on screen. It will disappear at the next action. | |
242 | * | |
243 | * @param mess | |
244 | * the message to display | |
245 | * @param error | |
246 | * TRUE for an error message, FALSE for an information message | |
bcb54330 NR |
247 | * |
248 | * @return TRUE if changes were performed | |
9c8baf0c | 249 | */ |
bcb54330 NR |
250 | public boolean setMessage(String mess, boolean error) { |
251 | if (mess != null || messagePanel.getChildCount() > 0) { | |
252 | messagePanel.removeAllComponents(); | |
253 | if (mess != null) { | |
e119a1c1 NR |
254 | ColorOption element = (error ? ColorOption.LINE_MESSAGE_ERR |
255 | : ColorOption.LINE_MESSAGE); | |
256 | Label lbl = UiColors.createLabel(element, " " + mess + " "); | |
bcb54330 NR |
257 | messagePanel.addComponent(lbl, LinearLayout |
258 | .createLayoutData(LinearLayout.Alignment.Center)); | |
259 | } | |
260 | return true; | |
261 | } | |
262 | ||
263 | return false; | |
264 | } | |
265 | ||
266 | /** | |
267 | * Show a question to the user and switch to "ask for answer" mode see | |
268 | * {@link MainWindow#handleQuestion}. The user will be asked to enter some | |
269 | * answer and confirm with ENTER. | |
270 | * | |
296a0b75 NR |
271 | * @param action |
272 | * the related action | |
bcb54330 NR |
273 | * @param question |
274 | * the question to ask | |
275 | * @param initial | |
276 | * the initial answer if any (to be edited by the user) | |
277 | */ | |
296a0b75 NR |
278 | public void setQuestion(KeyAction action, String question, String initial) { |
279 | setQuestion(action, question, initial, false); | |
bcb54330 NR |
280 | } |
281 | ||
282 | /** | |
283 | * Show a question to the user and switch to "ask for answer" mode see | |
284 | * {@link MainWindow#handleQuestion}. The user will be asked to hit one key | |
285 | * as an answer. | |
286 | * | |
296a0b75 NR |
287 | * @param action |
288 | * the related action | |
bcb54330 NR |
289 | * @param question |
290 | * the question to ask | |
291 | */ | |
296a0b75 NR |
292 | public void setQuestion(KeyAction action, String question) { |
293 | setQuestion(action, question, null, true); | |
9c8baf0c NR |
294 | } |
295 | ||
0b0b2b0f NR |
296 | /** |
297 | * Show a question to the user and switch to "ask for answer" mode see | |
298 | * {@link MainWindow#handleQuestion}. | |
299 | * | |
296a0b75 NR |
300 | * @param action |
301 | * the related action | |
bcb54330 NR |
302 | * @param question |
303 | * the question to ask | |
304 | * @param initial | |
305 | * the initial answer if any (to be edited by the user) | |
0b0b2b0f NR |
306 | * @param oneKey |
307 | * TRUE for a one-key answer, FALSE for a text answer validated | |
308 | * by ENTER | |
309 | */ | |
296a0b75 | 310 | private void setQuestion(KeyAction action, String question, String initial, |
bcb54330 | 311 | boolean oneKey) { |
f720df72 | 312 | userQuestion = new UserQuestion(action, oneKey); |
0b0b2b0f | 313 | |
9c8baf0c | 314 | messagePanel.removeAllComponents(); |
9c8baf0c | 315 | |
0b0b2b0f NR |
316 | Panel hpanel = new Panel(); |
317 | LinearLayout llayout = new LinearLayout(Direction.HORIZONTAL); | |
318 | llayout.setSpacing(0); | |
319 | hpanel.setLayoutManager(llayout); | |
9c8baf0c | 320 | |
e119a1c1 | 321 | Label lbl = UiColors.createLabel(ColorOption.LINE_MESSAGE_QUESTION, " " |
bcb54330 | 322 | + question + " "); |
296a0b75 NR |
323 | text = new TextBox(new TerminalSize(getSize().getColumns() |
324 | - lbl.getSize().getColumns(), 1)); | |
f720df72 NR |
325 | |
326 | if (initial != null) { | |
327 | // add all chars one by one so the caret is at the end | |
328 | for (int index = 0; index < initial.length(); index++) { | |
329 | text.handleInput(new KeyStroke(initial.charAt(index), false, | |
330 | false)); | |
331 | } | |
332 | } | |
9c8baf0c | 333 | |
bcb54330 NR |
334 | hpanel.addComponent(lbl, |
335 | LinearLayout.createLayoutData(LinearLayout.Alignment.Beginning)); | |
336 | hpanel.addComponent(text, | |
337 | LinearLayout.createLayoutData(LinearLayout.Alignment.Fill)); | |
9c8baf0c | 338 | |
bcb54330 NR |
339 | messagePanel |
340 | .addComponent(hpanel, LinearLayout | |
341 | .createLayoutData(LinearLayout.Alignment.Beginning)); | |
9c8baf0c | 342 | |
0b0b2b0f | 343 | text.takeFocus(); |
9c8baf0c NR |
344 | } |
345 | ||
296a0b75 NR |
346 | /** |
347 | * Refresh the window and the empty-space handling. You should call this | |
348 | * method when the window size changed. | |
349 | * | |
350 | * @param size | |
351 | * the new size of the window | |
352 | */ | |
353 | public void refresh(TerminalSize size) { | |
354 | if (size == null) | |
355 | return; | |
356 | ||
357 | if (getSize() == null || !getSize().equals(size)) | |
358 | setSize(size); | |
359 | ||
360 | setTitle(); | |
361 | ||
362 | if (actions != null) | |
363 | setActions(new ArrayList<KeyAction>(actions), false); | |
364 | ||
365 | invalidate(); | |
366 | } | |
367 | ||
368 | @Override | |
369 | public void invalidate() { | |
370 | super.invalidate(); | |
371 | for (MainContent content : contentStack) { | |
372 | content.invalidate(); | |
373 | } | |
374 | } | |
375 | ||
376 | @Override | |
377 | public boolean handleInput(KeyStroke key) { | |
378 | boolean handled = false; | |
379 | ||
f720df72 NR |
380 | if (userQuestion != null) { |
381 | handled = handleQuestion(userQuestion, key); | |
382 | if (handled) { | |
383 | if (userQuestion.getAnswer() != null) { | |
384 | handleAction(userQuestion.getAction(), | |
385 | userQuestion.getAnswer()); | |
296a0b75 | 386 | |
f720df72 NR |
387 | userQuestion = null; |
388 | } | |
296a0b75 NR |
389 | } |
390 | } else { | |
391 | handled = handleKey(key); | |
392 | } | |
393 | ||
f720df72 | 394 | if (!handled) { |
296a0b75 | 395 | handled = super.handleInput(key); |
f720df72 | 396 | } |
296a0b75 NR |
397 | |
398 | return handled; | |
399 | } | |
400 | ||
bcb54330 NR |
401 | /** |
402 | * Actually set the title <b>inside</b> the window. Will also call | |
296a0b75 | 403 | * {@link BasicWindow#setTitle} with the computed parameters. |
bcb54330 NR |
404 | */ |
405 | private void setTitle() { | |
0b0b2b0f NR |
406 | String prefix = " " + Main.APPLICATION_TITLE + " (version " |
407 | + Main.APPLICATION_VERSION + ")"; | |
408 | ||
bcb54330 | 409 | String title = null; |
0b0b2b0f | 410 | int count = -1; |
bcb54330 | 411 | |
0b0b2b0f | 412 | MainContent content = getContent(); |
bcb54330 NR |
413 | if (content != null) { |
414 | title = content.getTitle(); | |
0b0b2b0f | 415 | count = content.getCount(); |
bcb54330 NR |
416 | } |
417 | ||
418 | if (title == null) | |
419 | title = ""; | |
0b0b2b0f | 420 | |
bcb54330 | 421 | if (title.length() > 0) { |
0b0b2b0f | 422 | prefix = prefix + ": "; |
7da41ecd | 423 | title = StringUtils.sanitize(title, Main.isUnicode()); |
0b0b2b0f NR |
424 | } |
425 | ||
bcb54330 NR |
426 | String countStr = ""; |
427 | if (count > -1) { | |
428 | countStr = "[" + count + "]"; | |
429 | } | |
430 | ||
296a0b75 NR |
431 | int width = -1; |
432 | if (getSize() != null) { | |
433 | width = getSize().getColumns(); | |
434 | } | |
435 | ||
bcb54330 NR |
436 | if (width > 0) { |
437 | int padding = width - prefix.length() - title.length() | |
438 | - countStr.length(); | |
439 | if (padding > 0) { | |
440 | if (title.length() > 0) | |
441 | title = StringUtils.padString(title, title.length() | |
442 | + padding); | |
443 | else | |
444 | prefix = StringUtils.padString(prefix, prefix.length() | |
445 | + padding); | |
446 | } | |
0b0b2b0f | 447 | } |
0b0b2b0f | 448 | |
bcb54330 NR |
449 | String titleCache = prefix + title + count; |
450 | if (!titleCache.equals(this.titleCache)) { | |
451 | super.setTitle(prefix); | |
0b0b2b0f NR |
452 | |
453 | Label lblPrefix = new Label(prefix); | |
e119a1c1 | 454 | UiColors.themeLabel(ColorOption.TITLE_MAIN, lblPrefix); |
0b0b2b0f NR |
455 | |
456 | Label lblTitle = null; | |
bcb54330 | 457 | if (title.length() > 0) { |
0b0b2b0f | 458 | lblTitle = new Label(title); |
e119a1c1 | 459 | UiColors.themeLabel(ColorOption.TITLE_VARIABLE, lblTitle); |
0b0b2b0f NR |
460 | } |
461 | ||
462 | Label lblCount = null; | |
bcb54330 NR |
463 | if (countStr != null) { |
464 | lblCount = new Label(countStr); | |
e119a1c1 | 465 | UiColors.themeLabel(ColorOption.TITLE_COUNT, lblCount); |
0b0b2b0f NR |
466 | } |
467 | ||
468 | titlePanel.removeAllComponents(); | |
a3b510ab | 469 | |
0b0b2b0f NR |
470 | titlePanel.addComponent(lblPrefix, BorderLayout.Location.LEFT); |
471 | if (lblTitle != null) | |
472 | titlePanel.addComponent(lblTitle, BorderLayout.Location.CENTER); | |
473 | if (lblCount != null) | |
474 | titlePanel.addComponent(lblCount, BorderLayout.Location.RIGHT); | |
0b0b2b0f NR |
475 | } |
476 | } | |
477 | ||
478 | /** | |
479 | * Return the current {@link MainContent} from the stack if any. | |
480 | * | |
481 | * @return the current {@link MainContent} | |
482 | */ | |
483 | private MainContent getContent() { | |
484 | if (contentStack.size() > 0) { | |
485 | return contentStack.get(contentStack.size() - 1); | |
486 | } | |
487 | ||
488 | return null; | |
489 | } | |
490 | ||
491 | /** | |
492 | * Update the list of actions and refresh the action panel. | |
493 | * | |
494 | * @param actions | |
495 | * the list of actions to support | |
496 | * @param enableDefaultactions | |
497 | * TRUE to enable the default actions | |
498 | */ | |
499 | private void setActions(List<KeyAction> actions, | |
500 | boolean enableDefaultactions) { | |
a3b510ab | 501 | this.actions.clear(); |
9c8baf0c | 502 | |
a3b510ab NR |
503 | if (enableDefaultactions) |
504 | this.actions.addAll(defaultActions); | |
505 | ||
506 | if (actions != null) | |
507 | this.actions.addAll(actions); | |
508 | ||
509 | actionPanel.removeAllComponents(); | |
510 | for (KeyAction action : this.actions) { | |
668268fc | 511 | String trans = " " + Main.trans(action.getStringId()) + " "; |
a3b510ab NR |
512 | |
513 | if (" ".equals(trans)) | |
514 | continue; | |
515 | ||
7da41ecd | 516 | String keyTrans = KeyAction.trans(action.getKey()); |
a3b510ab NR |
517 | |
518 | Panel kPane = new Panel(); | |
519 | LinearLayout layout = new LinearLayout(Direction.HORIZONTAL); | |
520 | layout.setSpacing(0); | |
521 | kPane.setLayoutManager(layout); | |
522 | ||
e119a1c1 NR |
523 | kPane.addComponent(UiColors.createLabel(ColorOption.ACTION_KEY, |
524 | keyTrans)); | |
59597d59 NR |
525 | kPane.addComponent(UiColors.createLabel(ColorOption.ACTION_DESC, |
526 | trans)); | |
a3b510ab NR |
527 | |
528 | actionPanel.addComponent(kPane); | |
529 | } | |
bcb54330 NR |
530 | |
531 | // fill with "desc" colour | |
296a0b75 NR |
532 | int width = -1; |
533 | if (getSize() != null) { | |
534 | width = getSize().getColumns(); | |
535 | } | |
536 | ||
bcb54330 | 537 | if (width > 0) { |
59597d59 NR |
538 | actionPanel.addComponent(UiColors.createLabel( |
539 | ColorOption.ACTION_DESC, StringUtils.padString("", width))); | |
bcb54330 | 540 | } |
a3b510ab NR |
541 | } |
542 | ||
0b0b2b0f NR |
543 | /** |
544 | * Handle user input when in "ask for question" mode (see | |
f720df72 | 545 | * {@link MainWindow#userQuestion}). |
0b0b2b0f | 546 | * |
f720df72 NR |
547 | * @param userQuestion |
548 | * the question data | |
0b0b2b0f NR |
549 | * @param key |
550 | * the key that has been pressed by the user | |
551 | * | |
f720df72 | 552 | * @return TRUE if the {@link KeyStroke} was handled |
0b0b2b0f | 553 | */ |
f720df72 NR |
554 | private boolean handleQuestion(UserQuestion userQuestion, KeyStroke key) { |
555 | userQuestion.setAnswer(null); | |
ae22c247 | 556 | |
f720df72 NR |
557 | if (userQuestion.isOneKeyAnswer()) { |
558 | userQuestion.setAnswer("" + key.getCharacter()); | |
a3b510ab | 559 | } else { |
f720df72 NR |
560 | // ^h == Backspace |
561 | if (key.isCtrlDown() && key.getCharacter() == 'h') { | |
562 | key = new KeyStroke(KeyType.Backspace); | |
563 | } | |
564 | ||
565 | switch (key.getKeyType()) { | |
566 | case Enter: | |
a3b510ab | 567 | if (text != null) |
f720df72 | 568 | userQuestion.setAnswer(text.getText()); |
a3b510ab | 569 | else |
f720df72 NR |
570 | userQuestion.setAnswer(""); |
571 | break; | |
572 | case Backspace: | |
573 | int pos = text.getCaretPosition().getColumn(); | |
574 | if (pos > 0) { | |
575 | String current = text.getText(); | |
576 | // force caret one space before: | |
577 | text.setText(current.substring(0, pos - 1)); | |
578 | // re-add full text: | |
579 | text.setText(current.substring(0, pos - 1) | |
580 | + current.substring(pos)); | |
581 | } | |
582 | return true; | |
583 | default: | |
584 | // Do nothing (continue entering text) | |
585 | break; | |
a3b510ab NR |
586 | } |
587 | } | |
588 | ||
f720df72 | 589 | if (userQuestion.getAnswer() != null) { |
a3b510ab | 590 | Interactable focus = null; |
0b0b2b0f NR |
591 | MainContent content = getContent(); |
592 | if (content != null) | |
593 | focus = content.nextFocus(null); | |
a3b510ab | 594 | |
fae07ea7 | 595 | focus.takeFocus(); |
f720df72 NR |
596 | |
597 | return true; | |
a3b510ab NR |
598 | } |
599 | ||
f720df72 | 600 | return false; |
a3b510ab NR |
601 | } |
602 | ||
0b0b2b0f NR |
603 | /** |
604 | * Handle the input in case of "normal" (not "ask for answer") mode. | |
605 | * | |
606 | * @param key | |
607 | * the key that was pressed | |
608 | * @param answer | |
609 | * the answer given for this key | |
610 | * | |
bcb54330 | 611 | * @return if the window handled the input |
0b0b2b0f | 612 | */ |
296a0b75 | 613 | private boolean handleKey(KeyStroke key) { |
a3b510ab NR |
614 | boolean handled = false; |
615 | ||
296a0b75 NR |
616 | if (setMessage(null, false)) |
617 | return true; | |
a3b510ab | 618 | |
0b0b2b0f NR |
619 | for (KeyAction action : actions) { |
620 | if (!action.match(key)) | |
621 | continue; | |
a3b510ab | 622 | |
0b0b2b0f NR |
623 | handled = true; |
624 | ||
5ad0e17e NR |
625 | action.getObject(); // see {@link KeyAction#getMessage()} |
626 | String mess = action.getMessage(); | |
627 | if (mess != null) { | |
628 | setMessage(mess, action.isError()); | |
629 | } | |
630 | ||
59597d59 | 631 | if (!action.isError() && action.onAction()) { |
296a0b75 NR |
632 | handleAction(action, null); |
633 | } | |
0b0b2b0f | 634 | |
296a0b75 NR |
635 | break; |
636 | } | |
0b0b2b0f | 637 | |
296a0b75 NR |
638 | return handled; |
639 | } | |
640 | ||
641 | /** | |
642 | * Handle the input in case of "normal" (not "ask for answer") mode. | |
643 | * | |
644 | * @param key | |
645 | * the key that was pressed | |
646 | * @param answer | |
647 | * the answer given for this key | |
648 | * | |
649 | * @return if the window handled the input | |
650 | */ | |
651 | private void handleAction(KeyAction action, String answer) { | |
652 | MainContent content = getContent(); | |
653 | ||
296a0b75 NR |
654 | switch (action.getMode()) { |
655 | case MOVE: | |
656 | int x = 0; | |
657 | int y = 0; | |
658 | ||
659 | if (action.getKey().getKeyType() == KeyType.ArrowUp) | |
660 | x = -1; | |
661 | if (action.getKey().getKeyType() == KeyType.ArrowDown) | |
662 | x = 1; | |
663 | if (action.getKey().getKeyType() == KeyType.ArrowLeft) | |
664 | y = -1; | |
665 | if (action.getKey().getKeyType() == KeyType.ArrowRight) | |
666 | y = 1; | |
667 | ||
668 | if (content != null) { | |
669 | String err = content.move(x, y); | |
670 | if (err != null) | |
671 | setMessage(err, true); | |
672 | } | |
673 | ||
674 | break; | |
675 | // mode with windows: | |
676 | case CONTACT_LIST: | |
ae22c247 NR |
677 | if (action.getCard() != null) { |
678 | pushContent(new ContactList(action.getCard())); | |
5ad0e17e NR |
679 | } else if (action.getObject() != null |
680 | && action.getObject() instanceof MainContent) { | |
681 | MainContent mergeContent = (MainContent) action.getObject(); | |
682 | pushContent(mergeContent); | |
296a0b75 NR |
683 | } |
684 | break; | |
685 | case CONTACT_DETAILS: | |
ae22c247 NR |
686 | if (action.getContact() != null) { |
687 | pushContent(new ContactDetails(action.getContact())); | |
f04d8b1c NR |
688 | } |
689 | break; | |
690 | case CONTACT_DETAILS_RAW: | |
ae22c247 NR |
691 | if (action.getContact() != null) { |
692 | pushContent(new ContactDetailsRaw(action.getContact())); | |
296a0b75 NR |
693 | } |
694 | break; | |
695 | // mode interpreted by MainWindow: | |
696 | case HELP: | |
697 | // TODO | |
f720df72 | 698 | setMessage("Help! I need somebody! Help!", false); |
296a0b75 NR |
699 | |
700 | break; | |
701 | case BACK: | |
702 | String warning = content.getExitWarning(); | |
703 | if (warning != null) { | |
704 | if (answer == null) { | |
705 | setQuestion(action, warning); | |
706 | } else { | |
707 | setMessage(null, false); | |
708 | if (answer.equalsIgnoreCase("y")) { | |
bcb54330 | 709 | popContent(); |
a3b510ab | 710 | } |
296a0b75 NR |
711 | } |
712 | } else { | |
713 | popContent(); | |
714 | } | |
0b0b2b0f | 715 | |
296a0b75 NR |
716 | if (contentStack.size() == 0) { |
717 | close(); | |
718 | } | |
bcb54330 | 719 | |
296a0b75 NR |
720 | break; |
721 | // action modes: | |
ae22c247 | 722 | case ASK_USER: |
296a0b75 | 723 | if (answer == null) { |
ae22c247 NR |
724 | setQuestion(action, action.getQuestion(), |
725 | action.getDefaultAnswer()); | |
296a0b75 | 726 | } else { |
ae22c247 NR |
727 | setMessage(action.callback(answer), true); |
728 | content.refreshData(); | |
729 | invalidate(); | |
730 | setTitle(); | |
296a0b75 NR |
731 | } |
732 | break; | |
ae22c247 | 733 | case ASK_USER_KEY: |
296a0b75 | 734 | if (answer == null) { |
ae22c247 | 735 | setQuestion(action, action.getQuestion()); |
296a0b75 | 736 | } else { |
ae22c247 NR |
737 | setMessage(action.callback(answer), true); |
738 | content.refreshData(); | |
739 | invalidate(); | |
740 | setTitle(); | |
0b0b2b0f | 741 | } |
296a0b75 NR |
742 | break; |
743 | default: | |
744 | case NONE: | |
0b0b2b0f NR |
745 | break; |
746 | } | |
a3b510ab NR |
747 | } |
748 | } |