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; | |
5c5abfd2 | 11 | import jexer.TStatusBar; |
c8398c23 NR |
12 | import jexer.TWindow; |
13 | import jexer.event.TKeypressEvent; | |
14 | import be.nikiroo.jvcard.tui.TuiLauncherJexer; | |
15 | ||
16 | /** | |
17 | * A basic window for using with jVCard. | |
18 | * | |
19 | * @author niki | |
20 | */ | |
21 | public abstract class TuiBasicWindow extends TWindow { | |
22 | private TApplication app; | |
23 | private Map<TKeypress, TAction> keyBindings; | |
24 | private List<TAction> closeListeners; | |
25 | ||
5c5abfd2 NR |
26 | /** |
27 | * Create a new window with the given title. | |
28 | * | |
29 | * @param parent | |
30 | * the parent {@link TuiBasicWindow} | |
31 | * @param title | |
32 | * the window title | |
33 | */ | |
34 | public TuiBasicWindow(TuiBasicWindow parent, String title) { | |
35 | this(parent.app, title, parent.getWidth(), parent.getHeight()); | |
36 | } | |
37 | ||
c8398c23 NR |
38 | /** |
39 | * Create a new window with the given title. | |
40 | * | |
41 | * @param app | |
42 | * the application that will manage this window | |
43 | * @param title | |
44 | * the window title | |
5c5abfd2 NR |
45 | * @param width |
46 | * the window width | |
47 | * @param height | |
48 | * the window height | |
c8398c23 | 49 | */ |
5c5abfd2 NR |
50 | public TuiBasicWindow(TApplication app, String title, int width, int height) { |
51 | super(app, title, width, height); | |
c8398c23 NR |
52 | |
53 | this.app = app; | |
54 | ||
55 | keyBindings = new HashMap<TKeypress, TAction>(); | |
56 | closeListeners = new ArrayList<TAction>(); | |
57 | ||
58 | if (TuiLauncherJexer.FULLSCREEN) { | |
59 | setFullscreen(true); | |
60 | } | |
61 | } | |
62 | ||
63 | /** | |
64 | * Add a key binding, that is, describe a key to press and its action on he | |
65 | * window. | |
66 | * | |
67 | * @param key | |
68 | * the key to press | |
5c5abfd2 NR |
69 | * @param text |
70 | * the text to display for this command | |
c8398c23 NR |
71 | * @param action |
72 | * the action | |
73 | */ | |
5c5abfd2 | 74 | public void addKeyBinding(TKeypress key, String text, TAction action) { |
c8398c23 | 75 | keyBindings.put(key, action); |
5c5abfd2 NR |
76 | |
77 | TStatusBar statusbar = getStatusBar(); | |
78 | if (statusbar == null) { | |
79 | statusbar = newStatusBar(""); | |
80 | } | |
81 | ||
82 | statusbar.addShortcutKeypress(key, null, text); | |
c8398c23 NR |
83 | } |
84 | ||
85 | /** | |
86 | * Add a close listener on this window that will be called when the window | |
87 | * closes. | |
88 | * | |
89 | * @param listener | |
90 | * the listener | |
91 | */ | |
92 | public void addCloseListener(TAction listener) { | |
93 | closeListeners.add(listener); | |
94 | } | |
95 | ||
c8398c23 NR |
96 | @Override |
97 | public void onClose() { | |
98 | super.onClose(); | |
99 | for (TAction listener : closeListeners) { | |
100 | listener.DO(); | |
101 | } | |
102 | } | |
103 | ||
104 | @Override | |
105 | public void onKeypress(TKeypressEvent keypress) { | |
106 | if (keyBindings.containsKey(keypress.getKey())) { | |
107 | keyBindings.get(keypress.getKey()).DO(); | |
108 | } else { | |
109 | super.onKeypress(keypress); | |
110 | } | |
111 | } | |
112 | } |