Update support code for Jexer
authorNiki Roo <niki@nikiroo.be>
Sat, 28 Apr 2018 09:55:32 +0000 (11:55 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 28 Apr 2018 09:55:32 +0000 (11:55 +0200)
src/be/nikiroo/jvcard/launcher/Main.java
src/be/nikiroo/jvcard/tui/TuiLauncherJexer.java
src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java
src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java
src/be/nikiroo/jvcard/tui/windows/TuiContactListWindow.java
src/be/nikiroo/jvcard/tui/windows/TuiContactWindow.java
src/be/nikiroo/jvcard/tui/windows/TuiFileListWindow.java
src/be/nikiroo/jvcard/tui/windows/TuiRawContactWindow.java

index 69ed7e9b4fb123e6dd855000603cd7c6ab677fce..a073e92921a09c0a41b12dc5af2b718de268f7d0 100644 (file)
@@ -57,7 +57,7 @@ public class Main {
        /**
         * Translate the given {@link StringId} into user text.
         * 
-        * @param stringId
+        * @param id
         *            the ID to translate
         * @param values
         *            the values to insert instead of the place holders in the
index b79488cdd00601ef7db46bf55827f580c1eddef9..3f54633122c445066192cc2c04b420a34e02faea 100644 (file)
@@ -1,6 +1,5 @@
 package be.nikiroo.jvcard.tui;
 
-import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
 
@@ -17,6 +16,8 @@ import be.nikiroo.jvcard.tui.windows.TuiFileListWindow;
 public class TuiLauncherJexer extends TApplication {
        /**
         * Application is in fullscreen mode, no windows.
+        * 
+        * TODO: make it an option
         */
        static public final boolean FULLSCREEN = true;
 
@@ -26,7 +27,9 @@ public class TuiLauncherJexer extends TApplication {
         *            emulator, null to automatically determine the best choice
         * @param files
         *            the files to show at startup
+        * 
         * @throws UnsupportedEncodingException
+        *             if an exception is thrown when creating the InputStreamReader
         */
        public TuiLauncherJexer(final Boolean textMode, final List<String> files)
                        throws UnsupportedEncodingException {
@@ -35,22 +38,24 @@ public class TuiLauncherJexer extends TApplication {
                addFileMenu();
                addWindowMenu();
 
-               // TODO investigate why that is
+               int width = getBackend().getScreen().getWidth();
+               int height = getBackend().getScreen().getHeight() - 2;
+
                if (backend(textMode) == BackendType.SWING) {
-                       new Thread(new Runnable() {
-                               @Override
-                               public void run() {
-                                       showMainWindow(files);
-                               }
-                       }).start();
-               } else {
-                       showMainWindow(files);
+                       // TODO: why does the size change after the FIRST window has been
+                       // created (SWING mode only?) ?
+                       // A problem with the graphical size not an exact number of
+                       // cols/lines?
+                       width--;
+                       height--;
                }
-       }
 
-       private void showMainWindow(final List<String> files) {
+               width = Math.max(1, width);
+               height = Math.max(1, height);
+
                TuiBrowserWindow main = new TuiFileListWindow(TuiLauncherJexer.this,
-                               files);
+                               width, height, files);
+
                main.addCloseListener(new TAction() {
                        @Override
                        public void DO() {
@@ -61,11 +66,8 @@ public class TuiLauncherJexer extends TApplication {
 
        /**
         * Start the TUI program.
-        * 
-        * @throws IOException
-        *             in case of IO error
         */
-       public void start() throws IOException {
+       public void start() {
                (new Thread(this)).start();
        }
 
@@ -73,7 +75,7 @@ public class TuiLauncherJexer extends TApplication {
         * Select the most appropriate backend.
         * 
         * @param textMode
-        *            NULL for auto-dection
+        *            NULL for auto-detection
         * @return the backend type to use
         */
        private static BackendType backend(Boolean textMode) {
index a87df613c6d2e67cba8f5b3f0b6186a8e4df385e..404d1460177eec84a0e74f896589d747e516c704 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Map;
 import jexer.TAction;
 import jexer.TApplication;
 import jexer.TKeypress;
+import jexer.TStatusBar;
 import jexer.TWindow;
 import jexer.event.TKeypressEvent;
 import be.nikiroo.jvcard.tui.TuiLauncherJexer;
@@ -22,6 +23,18 @@ public abstract class TuiBasicWindow extends TWindow {
        private Map<TKeypress, TAction> keyBindings;
        private List<TAction> closeListeners;
 
+       /**
+        * Create a new window with the given title.
+        * 
+        * @param parent
+        *            the parent {@link TuiBasicWindow}
+        * @param title
+        *            the window title
+        */
+       public TuiBasicWindow(TuiBasicWindow parent, String title) {
+               this(parent.app, title, parent.getWidth(), parent.getHeight());
+       }
+
        /**
         * Create a new window with the given title.
         * 
@@ -29,13 +42,13 @@ public abstract class TuiBasicWindow extends TWindow {
         *            the application that will manage this window
         * @param title
         *            the window title
+        * @param width
+        *            the window width
+        * @param height
+        *            the window height
         */
-       public TuiBasicWindow(TApplication app, String title) {
-               // Note: will not support screen with less than 10x10
-               super(app, title, //
-                               Math.min(36, app.getScreen().getWidth() - 9), //
-                               Math.min(16, app.getScreen().getHeight() - 9) //
-               );
+       public TuiBasicWindow(TApplication app, String title, int width, int height) {
+               super(app, title, width, height);
 
                this.app = app;
 
@@ -53,11 +66,20 @@ public abstract class TuiBasicWindow extends TWindow {
         * 
         * @param key
         *            the key to press
+        * @param text
+        *            the text to display for this command
         * @param action
         *            the action
         */
-       public void addKeyBinding(TKeypress key, TAction action) {
+       public void addKeyBinding(TKeypress key, String text, TAction action) {
                keyBindings.put(key, action);
+
+               TStatusBar statusbar = getStatusBar();
+               if (statusbar == null) {
+                       statusbar = newStatusBar("");
+               }
+
+               statusbar.addShortcutKeypress(key, null, text);
        }
 
        /**
@@ -71,13 +93,6 @@ public abstract class TuiBasicWindow extends TWindow {
                closeListeners.add(listener);
        }
 
-       /**
-        * Close the window.
-        */
-       public void close() {
-               app.closeWindow(this);
-       }
-
        @Override
        public void onClose() {
                super.onClose();
index 827f7b9f2a2f2559d31bd767b2b0e001e45330ea..31ffce79909b7aa121f3d2191a5618437c3b6f2e 100644 (file)
@@ -12,16 +12,26 @@ public abstract class TuiBrowserWindow extends TuiBasicWindow {
        private TTable table;
        private boolean showHeader;
 
-       public TuiBrowserWindow(TApplication app, String title, boolean showHeaders) {
-               super(app, title);
+       public TuiBrowserWindow(TuiBasicWindow parent, String title,
+                       boolean showHeaders) {
+               super(parent, title);
+               init(showHeaders);
+       }
+
+       public TuiBrowserWindow(TApplication app, int width, int height,
+                       String title, boolean showHeaders) {
+               super(app, title, width, height);
+               init(showHeaders);
+       }
 
+       private void init(boolean showHeaders) {
                this.showHeader = showHeaders;
 
                table = new TTable(this, 0, 0, getWidth() - 2, getHeight() - 2,
                                new TAction() {
                                        @Override
                                        public void DO() {
-                                               onAction(table.getSelectedLine(),
+                                               onAction(table.getSelectedRow(),
                                                                table.getSelectedColumn());
                                        }
                                }, null);
@@ -32,22 +42,22 @@ public abstract class TuiBrowserWindow extends TuiBasicWindow {
         * 
         * @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));
        }
@@ -59,19 +69,19 @@ public abstract class TuiBrowserWindow extends TuiBasicWindow {
         * @return -1 or the number of present items
         */
        public int size() {
-               return table.getNumberOfLines();
+               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
index af6b08edd496f471ceaa31ff9d0248cf44add9ad..21a8ca3c6cec2f904be935d622ffd4bad0006564 100644 (file)
@@ -15,17 +15,15 @@ import be.nikiroo.jvcard.resources.DisplayBundle;
 import be.nikiroo.jvcard.resources.DisplayOption;
 
 public class TuiContactListWindow extends TuiBrowserWindow {
-       private TApplication app;
        private Card card;
        private String filter;
        private List<String> formats;
        private int selectedFormat;
        private String format;
 
-       public TuiContactListWindow(TApplication app, Card card) {
-               super(app, "Contacts", false);
+       public TuiContactListWindow(TuiBasicWindow parent, Card card) {
+               super(parent, "Contacts", false);
 
-               this.app = app;
                this.card = card;
                this.selectedFormat = -1;
 
@@ -36,17 +34,17 @@ public class TuiContactListWindow extends TuiBrowserWindow {
                        formats.add(format);
                }
 
-               addKeyBinding(TKeypress.kbTab, new TAction() {
+               addKeyBinding(TKeypress.kbQ, "Quit", new TAction() {
                        @Override
                        public void DO() {
-                               switchFormat();
+                               close();
                        }
                });
-
-               addKeyBinding(TKeypress.kbQ, new TAction() {
+               
+               addKeyBinding(TKeypress.kbTab, "Switch format", new TAction() {
                        @Override
                        public void DO() {
-                               close();
+                               switchFormat();
                        }
                });
 
@@ -58,7 +56,7 @@ public class TuiContactListWindow extends TuiBrowserWindow {
        public void onAction(int selectedLine, int selectedColumn) {
                try {
                        @SuppressWarnings("unused")
-                       TWindow w = new TuiContactWindow(app, card.get(selectedLine));
+                       TWindow w = new TuiContactWindow(TuiContactListWindow.this, card.get(selectedLine));
                } catch (IndexOutOfBoundsException e) {
                        setMessage("Fail to get contact", true);
                }
index c8a31b895e00f8e3c725bf14af069c702b8d1509..f1a6c3439d4b3d4d1dfbc23b0bbae85f3743e982 100644 (file)
@@ -1,28 +1,28 @@
 package be.nikiroo.jvcard.tui.windows;
 
 import jexer.TAction;
-import jexer.TApplication;
 import jexer.TKeypress;
 import jexer.TLabel;
 import jexer.TWindow;
 import be.nikiroo.jvcard.Contact;
 
 public class TuiContactWindow extends TuiBasicWindow {
-       public TuiContactWindow(final TApplication app, final Contact contact) {
-               super(app, "Contact view");
+       public TuiContactWindow(final TuiBasicWindow parent, final Contact contact) {
+               super(parent, "Contact view");
 
-               addKeyBinding(TKeypress.kbQ, new TAction() {
+               addKeyBinding(TKeypress.kbQ, "Quit", new TAction() {
                        @Override
                        public void DO() {
-                               app.closeWindow(TuiContactWindow.this);
+                               parent.getApplication().closeWindow(TuiContactWindow.this);
                        }
                });
 
-               addKeyBinding(TKeypress.kbR, new TAction() {
+               addKeyBinding(TKeypress.kbR, "Raw view", new TAction() {
                        @Override
                        public void DO() {
                                @SuppressWarnings("unused")
-                               TWindow w = new TuiRawContactWindow(app, contact);
+                               TWindow w = new TuiRawContactWindow(TuiContactWindow.this,
+                                               contact);
                        }
                });
 
index f1feea5c021ead72fb6ae9c4c1f24694ed3eb76e..f354a1061c0c9bcbf36cd1fbb2addf2119149dfb 100644 (file)
@@ -17,14 +17,13 @@ import be.nikiroo.jvcard.launcher.Main;
 import be.nikiroo.jvcard.parsers.Format;
 
 public class TuiFileListWindow extends TuiBrowserWindow {
-       private TApplication app;
        private List<String> files;
        private List<CardResult> cards;
 
-       public TuiFileListWindow(TApplication app, List<String> files) {
-               super(app, "Contacts", false);
+       public TuiFileListWindow(TApplication app, int width, int height,
+                       List<String> files) {
+               super(app, width, height, "Contacts", false);
 
-               this.app = app;
                this.files = files;
 
                cards = new ArrayList<CardResult>();
@@ -41,7 +40,7 @@ public class TuiFileListWindow extends TuiBrowserWindow {
                        dataLines.add(listOfOneFile);
                }
 
-               addKeyBinding(TKeypress.kbQ, new TAction() {
+               addKeyBinding(TKeypress.kbQ, "Quit", new TAction() {
                        @Override
                        public void DO() {
                                close();
@@ -55,7 +54,8 @@ public class TuiFileListWindow extends TuiBrowserWindow {
        public void onAction(int selectedLine, int selectedColumn) {
                try {
                        @SuppressWarnings("unused")
-                       TWindow w = new TuiContactListWindow(app, getCard(selectedLine));
+                       TWindow w = new TuiContactListWindow(TuiFileListWindow.this,
+                                       getCard(selectedLine));
                } catch (IOException e) {
                        setMessage("Fail to get file: " + e.getMessage(), true);
                }
index c784b8091704723f582bc2ae3b4f2c1e10d7c475..e69ee37a7497f14e9bd20365bc0f5b2bf50c92b6 100644 (file)
@@ -11,8 +11,8 @@ import be.nikiroo.jvcard.Data;
 
 public class TuiRawContactWindow extends TuiBrowserWindow {
 
-       public TuiRawContactWindow(TApplication app, Contact contact) {
-               super(app, "Contact RAW mode", false);
+       public TuiRawContactWindow(TuiBasicWindow parent, Contact contact) {
+               super(parent, "Contact RAW mode", false);
 
                List<String> headers = new ArrayList<String>();
                headers.add("Name");
@@ -29,7 +29,7 @@ public class TuiRawContactWindow extends TuiBrowserWindow {
                        dataLines.add(dataLine);
                }
 
-               addKeyBinding(TKeypress.kbQ, new TAction() {
+               addKeyBinding(TKeypress.kbQ, "Quit", new TAction() {
                        @Override
                        public void DO() {
                                close();