Commit | Line | Data |
---|---|---|
c8398c23 NR |
1 | package be.nikiroo.jvcard.tui.windows; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.HashMap; | |
5 | import java.util.List; | |
6 | import java.util.Map; | |
7 | ||
8 | import jexer.TAction; | |
9 | import jexer.TApplication; | |
10 | import jexer.TKeypress; | |
11 | import jexer.TWindow; | |
12 | import jexer.event.TKeypressEvent; | |
13 | import be.nikiroo.jvcard.tui.TuiLauncherJexer; | |
14 | ||
15 | /** | |
16 | * A basic window for using with jVCard. | |
17 | * | |
18 | * @author niki | |
19 | */ | |
20 | public abstract class TuiBasicWindow extends TWindow { | |
21 | private TApplication app; | |
22 | private Map<TKeypress, TAction> keyBindings; | |
23 | private List<TAction> closeListeners; | |
24 | ||
25 | /** | |
26 | * Create a new window with the given title. | |
27 | * | |
28 | * @param app | |
29 | * the application that will manage this window | |
30 | * @param title | |
31 | * the window title | |
32 | */ | |
33 | public TuiBasicWindow(TApplication app, String title) { | |
34 | // Note: will not support screen with less than 10x10 | |
35 | super(app, title, // | |
36 | Math.min(36, app.getScreen().getWidth() - 9), // | |
37 | Math.min(16, app.getScreen().getHeight() - 9) // | |
38 | ); | |
39 | ||
40 | this.app = app; | |
41 | ||
42 | keyBindings = new HashMap<TKeypress, TAction>(); | |
43 | closeListeners = new ArrayList<TAction>(); | |
44 | ||
45 | if (TuiLauncherJexer.FULLSCREEN) { | |
46 | setFullscreen(true); | |
47 | } | |
48 | } | |
49 | ||
50 | /** | |
51 | * Add a key binding, that is, describe a key to press and its action on he | |
52 | * window. | |
53 | * | |
54 | * @param key | |
55 | * the key to press | |
56 | * @param action | |
57 | * the action | |
58 | */ | |
59 | public void addKeyBinding(TKeypress key, TAction action) { | |
60 | keyBindings.put(key, action); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Add a close listener on this window that will be called when the window | |
65 | * closes. | |
66 | * | |
67 | * @param listener | |
68 | * the listener | |
69 | */ | |
70 | public void addCloseListener(TAction listener) { | |
71 | closeListeners.add(listener); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Close the window. | |
76 | */ | |
77 | public void close() { | |
78 | app.closeWindow(this); | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void onClose() { | |
83 | super.onClose(); | |
84 | for (TAction listener : closeListeners) { | |
85 | listener.DO(); | |
86 | } | |
87 | } | |
88 | ||
89 | @Override | |
90 | public void onKeypress(TKeypressEvent keypress) { | |
91 | if (keyBindings.containsKey(keypress.getKey())) { | |
92 | keyBindings.get(keypress.getKey()).DO(); | |
93 | } else { | |
94 | super.onKeypress(keypress); | |
95 | } | |
96 | } | |
97 | } |