X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fwindows%2FTuiBrowserWindow.java;fp=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fwindows%2FTuiBrowserWindow.java;h=55cc4aa6b3dea79b74e1094ab50b5a52b58c0172;hp=0000000000000000000000000000000000000000;hb=10dd1e387d6a1834596ae70f48cf905d7b302131;hpb=d5260eeb873fcf2ef9855dedcd9e2a3a3a990582 diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java new file mode 100644 index 0000000..55cc4aa --- /dev/null +++ b/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java @@ -0,0 +1,119 @@ +package be.nikiroo.jvcard.tui.windows; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import jexer.TAction; +import jexer.TApplication; +import jexer.TKeypress; +import jexer.TTable; +import jexer.TWindow; +import jexer.event.TKeypressEvent; +import jexer.event.TResizeEvent; +import be.nikiroo.jvcard.tui.panes.MainContent; + +public abstract class TuiBrowserWindow extends TWindow { + private TApplication app; + private TTable table; + private boolean showHeader; + private Map keyBindings; + + public TuiBrowserWindow(TApplication app, String title, boolean showHeaders) { + super(app, title, 10, 10); + + this.app = app; + this.showHeader = showHeaders; + + table = new TTable(this, 0, 0, getWidth(), getHeight(), new TAction() { + @Override + public void DO() { + onAction(table.getSelectedLine(), table.getSelectedColumn()); + } + }, null); + + keyBindings = new HashMap(); + + // TODO: fullscreen selection? + + // TODO: auto-maximize on FS, auto-resize on maximize + // setFullscreen(true); + maximize(); + onResize(null); + } + + /** + * Change the currently displayed data. + * + * @param headers + * the table headers (mandatory) + * @param lines + * the data to display + */ + public void setData(List headers, List> lines) { + int prevLine = table.getSelectedLine(); + int prevColumn = table.getSelectedColumn(); + + table.clear(); + table.setHeaders(headers, showHeader); + for (List line : lines) { + table.addLine(line); + } + + table.reflow(); + + table.setSelectedLine(Math.min(prevLine, table.getNumberOfLines() - 1)); + table.setSelectedColumn(Math.min(prevColumn, + table.getNumberOfColumns() - 1)); + } + + public void addKeyBinding(TKeypress key, TAction action) { + keyBindings.put(key, action); + } + + /** + * Return the number of items in this {@link MainContent}, or -1 if this + * {@link MainContent} is not countable. + * + * @return -1 or the number of present items + */ + public int size() { + return table.getNumberOfLines(); + } + + /** + * Close the window. + */ + public void close() { + app.closeWindow(this); + } + + /** + * An item has been selected. + * + * @param selectedLine + * the currently selected line + * @param selectedColumn + * the currently selected column + */ + @SuppressWarnings("unused") + public void onAction(int selectedLine, int selectedColumn) { + } + + @Override + public void onResize(TResizeEvent resize) { + super.onResize(resize); + table.setWidth(getWidth()); + table.setHeight(getHeight()); + table.reflow(); + } + + @Override + public void onKeypress(TKeypressEvent keypress) { + if (keyBindings.containsKey(keypress.getKey())) { + keyBindings.get(keypress.getKey()).DO(); + } else { + super.onKeypress(keypress); + } + } +}