New Jexer TUI now working (still needs work)
[jvcard.git] / src / be / nikiroo / jvcard / tui / windows / TuiBrowserWindow.java
1 package be.nikiroo.jvcard.tui.windows;
2
3 import java.util.List;
4
5 import jexer.TAction;
6 import jexer.TApplication;
7 import jexer.TTable;
8 import jexer.event.TResizeEvent;
9 import be.nikiroo.jvcard.tui.panes.MainContent;
10
11 public abstract class TuiBrowserWindow extends TuiBasicWindow {
12 private TTable table;
13 private boolean showHeader;
14
15 public TuiBrowserWindow(TApplication app, String title, boolean showHeaders) {
16 super(app, title);
17
18 this.showHeader = showHeaders;
19
20 table = new TTable(this, 0, 0, getWidth() - 2, getHeight() - 2,
21 new TAction() {
22 @Override
23 public void DO() {
24 onAction(table.getSelectedLine(),
25 table.getSelectedColumn());
26 }
27 }, null);
28 }
29
30 /**
31 * Change the currently displayed data.
32 *
33 * @param headers
34 * the table headers (mandatory)
35 * @param lines
36 * the data to display
37 */
38 public void setData(List<String> headers, List<List<String>> lines) {
39 int prevLine = table.getSelectedLine();
40 int prevColumn = table.getSelectedColumn();
41
42 table.clear();
43 table.setHeaders(headers, showHeader);
44 for (List<String> line : lines) {
45 table.addLine(line);
46 }
47
48 table.reflow();
49
50 table.setSelectedLine(Math.min(prevLine, table.getNumberOfLines() - 1));
51 table.setSelectedColumn(Math.min(prevColumn,
52 table.getNumberOfColumns() - 1));
53 }
54
55 /**
56 * Return the number of items in this {@link MainContent}, or -1 if this
57 * {@link MainContent} is not countable.
58 *
59 * @return -1 or the number of present items
60 */
61 public int size() {
62 return table.getNumberOfLines();
63 }
64
65 /**
66 * An item has been selected.
67 *
68 * @param selectedLine
69 * the currently selected line
70 * @param selectedColumn
71 * the currently selected column
72 */
73 @SuppressWarnings("unused")
74 public void onAction(int selectedLine, int selectedColumn) {
75 }
76
77 @Override
78 public void onResize(TResizeEvent resize) {
79 super.onResize(resize);
80 // Will be NULL at creation time in super()
81 if (table != null) {
82 table.setWidth(getWidth() - 2);
83 table.setHeight(getHeight() - 2);
84 table.reflow();
85 }
86 }
87 }