Commit | Line | Data |
---|---|---|
10dd1e38 NR |
1 | package be.nikiroo.jvcard.tui.windows; |
2 | ||
3 | import java.util.HashMap; | |
4 | import java.util.List; | |
5 | import java.util.Map; | |
6 | ||
7 | import jexer.TAction; | |
8 | import jexer.TApplication; | |
9 | import jexer.TKeypress; | |
10 | import jexer.TTable; | |
11 | import jexer.TWindow; | |
12 | import jexer.event.TKeypressEvent; | |
13 | import jexer.event.TResizeEvent; | |
14 | import be.nikiroo.jvcard.tui.panes.MainContent; | |
15 | ||
16 | public abstract class TuiBrowserWindow extends TWindow { | |
17 | private TApplication app; | |
18 | private TTable table; | |
19 | private boolean showHeader; | |
20 | private Map<TKeypress, TAction> keyBindings; | |
21 | ||
22 | public TuiBrowserWindow(TApplication app, String title, boolean showHeaders) { | |
23 | super(app, title, 10, 10); | |
24 | ||
25 | this.app = app; | |
26 | this.showHeader = showHeaders; | |
27 | ||
28 | table = new TTable(this, 0, 0, getWidth(), getHeight(), new TAction() { | |
29 | @Override | |
30 | public void DO() { | |
31 | onAction(table.getSelectedLine(), table.getSelectedColumn()); | |
32 | } | |
33 | }, null); | |
34 | ||
35 | keyBindings = new HashMap<TKeypress, TAction>(); | |
36 | ||
37 | // TODO: fullscreen selection? | |
38 | ||
39 | // TODO: auto-maximize on FS, auto-resize on maximize | |
40 | // setFullscreen(true); | |
41 | maximize(); | |
42 | onResize(null); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Change the currently displayed data. | |
47 | * | |
48 | * @param headers | |
49 | * the table headers (mandatory) | |
50 | * @param lines | |
51 | * the data to display | |
52 | */ | |
53 | public void setData(List<String> headers, List<List<String>> lines) { | |
54 | int prevLine = table.getSelectedLine(); | |
55 | int prevColumn = table.getSelectedColumn(); | |
56 | ||
57 | table.clear(); | |
58 | table.setHeaders(headers, showHeader); | |
59 | for (List<String> line : lines) { | |
60 | table.addLine(line); | |
61 | } | |
62 | ||
63 | table.reflow(); | |
64 | ||
65 | table.setSelectedLine(Math.min(prevLine, table.getNumberOfLines() - 1)); | |
66 | table.setSelectedColumn(Math.min(prevColumn, | |
67 | table.getNumberOfColumns() - 1)); | |
68 | } | |
69 | ||
70 | public void addKeyBinding(TKeypress key, TAction action) { | |
71 | keyBindings.put(key, action); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Return the number of items in this {@link MainContent}, or -1 if this | |
76 | * {@link MainContent} is not countable. | |
77 | * | |
78 | * @return -1 or the number of present items | |
79 | */ | |
80 | public int size() { | |
81 | return table.getNumberOfLines(); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Close the window. | |
86 | */ | |
87 | public void close() { | |
88 | app.closeWindow(this); | |
89 | } | |
90 | ||
91 | /** | |
92 | * An item has been selected. | |
93 | * | |
94 | * @param selectedLine | |
95 | * the currently selected line | |
96 | * @param selectedColumn | |
97 | * the currently selected column | |
98 | */ | |
99 | @SuppressWarnings("unused") | |
100 | public void onAction(int selectedLine, int selectedColumn) { | |
101 | } | |
102 | ||
103 | @Override | |
104 | public void onResize(TResizeEvent resize) { | |
105 | super.onResize(resize); | |
106 | table.setWidth(getWidth()); | |
107 | table.setHeight(getHeight()); | |
108 | table.reflow(); | |
109 | } | |
110 | ||
111 | @Override | |
112 | public void onKeypress(TKeypressEvent keypress) { | |
113 | if (keyBindings.containsKey(keypress.getKey())) { | |
114 | keyBindings.get(keypress.getKey()).DO(); | |
115 | } else { | |
116 | super.onKeypress(keypress); | |
117 | } | |
118 | } | |
119 | } |