| 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(TuiBasicWindow parent, String title, |
| 16 | boolean showHeaders) { |
| 17 | super(parent, title); |
| 18 | init(showHeaders); |
| 19 | } |
| 20 | |
| 21 | public TuiBrowserWindow(TApplication app, int width, int height, |
| 22 | String title, boolean showHeaders) { |
| 23 | super(app, title, width, height); |
| 24 | init(showHeaders); |
| 25 | } |
| 26 | |
| 27 | private void init(boolean showHeaders) { |
| 28 | this.showHeader = showHeaders; |
| 29 | |
| 30 | table = new TTable(this, 0, 0, getWidth() - 2, getHeight() - 2, |
| 31 | new TAction() { |
| 32 | @Override |
| 33 | public void DO() { |
| 34 | onAction(table.getSelectedRow(), |
| 35 | table.getSelectedColumn()); |
| 36 | } |
| 37 | }, null); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Change the currently displayed data. |
| 42 | * |
| 43 | * @param headers |
| 44 | * the table headers (mandatory) |
| 45 | * @param rows |
| 46 | * the data to display |
| 47 | */ |
| 48 | public void setData(List<String> headers, List<List<String>> rows) { |
| 49 | int prevRow = table.getSelectedRow(); |
| 50 | int prevColumn = table.getSelectedColumn(); |
| 51 | |
| 52 | table.clear(); |
| 53 | table.setHeaders(headers, showHeader); |
| 54 | for (List<String> row : rows) { |
| 55 | table.addRow(row); |
| 56 | } |
| 57 | |
| 58 | table.reflow(); |
| 59 | |
| 60 | table.setSelectedRow(Math.min(prevRow, table.getNumberOfRows() - 1)); |
| 61 | table.setSelectedColumn(Math.min(prevColumn, |
| 62 | table.getNumberOfColumns() - 1)); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return the number of items in this {@link MainContent}, or -1 if this |
| 67 | * {@link MainContent} is not countable. |
| 68 | * |
| 69 | * @return -1 or the number of present items |
| 70 | */ |
| 71 | public int size() { |
| 72 | return table.getNumberOfRows(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * An item has been selected. |
| 77 | * |
| 78 | * @param selectedRow |
| 79 | * the currently selected row |
| 80 | * @param selectedColumn |
| 81 | * the currently selected column |
| 82 | */ |
| 83 | @SuppressWarnings("unused") |
| 84 | public void onAction(int selectedRow, int selectedColumn) { |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onResize(TResizeEvent resize) { |
| 89 | super.onResize(resize); |
| 90 | // Will be NULL at creation time in super() |
| 91 | if (table != null) { |
| 92 | table.setWidth(getWidth() - 2); |
| 93 | table.setHeight(getHeight() - 2); |
| 94 | table.reflow(); |
| 95 | } |
| 96 | } |
| 97 | } |