Update support code for Jexer
[jvcard.git] / src / be / nikiroo / jvcard / tui / windows / TuiBrowserWindow.java
index 55cc4aa6b3dea79b74e1094ab50b5a52b58c0172..31ffce79909b7aa121f3d2191a5618437c3b6f2e 100644 (file)
@@ -1,45 +1,40 @@
 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;
+public abstract class TuiBrowserWindow extends TuiBasicWindow {
        private TTable table;
        private boolean showHeader;
-       private Map<TKeypress, TAction> 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);
+       public TuiBrowserWindow(TuiBasicWindow parent, String title,
+                       boolean showHeaders) {
+               super(parent, title);
+               init(showHeaders);
+       }
 
-               keyBindings = new HashMap<TKeypress, TAction>();
+       public TuiBrowserWindow(TApplication app, int width, int height,
+                       String title, boolean showHeaders) {
+               super(app, title, width, height);
+               init(showHeaders);
+       }
 
-               // TODO: fullscreen selection?
+       private void init(boolean showHeaders) {
+               this.showHeader = showHeaders;
 
-               // TODO: auto-maximize on FS, auto-resize on maximize
-               // setFullscreen(true);
-               maximize();
-               onResize(null);
+               table = new TTable(this, 0, 0, getWidth() - 2, getHeight() - 2,
+                               new TAction() {
+                                       @Override
+                                       public void DO() {
+                                               onAction(table.getSelectedRow(),
+                                                               table.getSelectedColumn());
+                                       }
+                               }, null);
        }
 
        /**
@@ -47,30 +42,26 @@ public abstract class TuiBrowserWindow extends TWindow {
         * 
         * @param headers
         *            the table headers (mandatory)
-        * @param lines
+        * @param rows
         *            the data to display
         */
-       public void setData(List<String> headers, List<List<String>> lines) {
-               int prevLine = table.getSelectedLine();
+       public void setData(List<String> headers, List<List<String>> rows) {
+               int prevRow = table.getSelectedRow();
                int prevColumn = table.getSelectedColumn();
 
                table.clear();
                table.setHeaders(headers, showHeader);
-               for (List<String> line : lines) {
-                       table.addLine(line);
+               for (List<String> row : rows) {
+                       table.addRow(row);
                }
 
                table.reflow();
 
-               table.setSelectedLine(Math.min(prevLine, table.getNumberOfLines() - 1));
+               table.setSelectedRow(Math.min(prevRow, table.getNumberOfRows() - 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.
@@ -78,42 +69,29 @@ public abstract class TuiBrowserWindow extends TWindow {
         * @return -1 or the number of present items
         */
        public int size() {
-               return table.getNumberOfLines();
-       }
-
-       /**
-        * Close the window.
-        */
-       public void close() {
-               app.closeWindow(this);
+               return table.getNumberOfRows();
        }
 
        /**
         * An item has been selected.
         * 
-        * @param selectedLine
-        *            the currently selected line
+        * @param selectedRow
+        *            the currently selected row
         * @param selectedColumn
         *            the currently selected column
         */
        @SuppressWarnings("unused")
-       public void onAction(int selectedLine, int selectedColumn) {
+       public void onAction(int selectedRow, 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);
+               // Will be NULL at creation time in super()
+               if (table != null) {
+                       table.setWidth(getWidth() - 2);
+                       table.setHeight(getHeight() - 2);
+                       table.reflow();
                }
        }
 }