New Jexer TUI now working (still needs work)
authorNiki Roo <niki@nikiroo.be>
Thu, 13 Jul 2017 05:52:34 +0000 (07:52 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 13 Jul 2017 05:52:34 +0000 (07:52 +0200)
src/be/nikiroo/jvcard/tui/TuiLauncherJexer.java
src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java [new file with mode: 0644]
src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java
src/be/nikiroo/jvcard/tui/windows/TuiContactWindow.java

index 445fc787895543a3ec9485436f35dc513181e847..b79488cdd00601ef7db46bf55827f580c1eddef9 100644 (file)
@@ -4,8 +4,9 @@ import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.List;
 
+import jexer.TAction;
 import jexer.TApplication;
-import jexer.TWindow;
+import be.nikiroo.jvcard.tui.windows.TuiBrowserWindow;
 import be.nikiroo.jvcard.tui.windows.TuiFileListWindow;
 
 /**
@@ -14,6 +15,10 @@ import be.nikiroo.jvcard.tui.windows.TuiFileListWindow;
  * @author niki
  */
 public class TuiLauncherJexer extends TApplication {
+       /**
+        * Application is in fullscreen mode, no windows.
+        */
+       static public final boolean FULLSCREEN = true;
 
        /**
         * @param textMode
@@ -30,8 +35,28 @@ public class TuiLauncherJexer extends TApplication {
                addFileMenu();
                addWindowMenu();
 
-               @SuppressWarnings("unused")
-               TWindow w = new TuiFileListWindow(this, files);
+               // TODO investigate why that is
+               if (backend(textMode) == BackendType.SWING) {
+                       new Thread(new Runnable() {
+                               @Override
+                               public void run() {
+                                       showMainWindow(files);
+                               }
+                       }).start();
+               } else {
+                       showMainWindow(files);
+               }
+       }
+
+       private void showMainWindow(final List<String> files) {
+               TuiBrowserWindow main = new TuiFileListWindow(TuiLauncherJexer.this,
+                               files);
+               main.addCloseListener(new TAction() {
+                       @Override
+                       public void DO() {
+                               TuiLauncherJexer.this.exit(false);
+                       }
+               });
        }
 
        /**
diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java
new file mode 100644 (file)
index 0000000..a87df61
--- /dev/null
@@ -0,0 +1,97 @@
+package be.nikiroo.jvcard.tui.windows;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jexer.TAction;
+import jexer.TApplication;
+import jexer.TKeypress;
+import jexer.TWindow;
+import jexer.event.TKeypressEvent;
+import be.nikiroo.jvcard.tui.TuiLauncherJexer;
+
+/**
+ * A basic window for using with jVCard.
+ * 
+ * @author niki
+ */
+public abstract class TuiBasicWindow extends TWindow {
+       private TApplication app;
+       private Map<TKeypress, TAction> keyBindings;
+       private List<TAction> closeListeners;
+
+       /**
+        * Create a new window with the given title.
+        * 
+        * @param app
+        *            the application that will manage this window
+        * @param title
+        *            the window title
+        */
+       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) //
+               );
+
+               this.app = app;
+
+               keyBindings = new HashMap<TKeypress, TAction>();
+               closeListeners = new ArrayList<TAction>();
+
+               if (TuiLauncherJexer.FULLSCREEN) {
+                       setFullscreen(true);
+               }
+       }
+
+       /**
+        * Add a key binding, that is, describe a key to press and its action on he
+        * window.
+        * 
+        * @param key
+        *            the key to press
+        * @param action
+        *            the action
+        */
+       public void addKeyBinding(TKeypress key, TAction action) {
+               keyBindings.put(key, action);
+       }
+
+       /**
+        * Add a close listener on this window that will be called when the window
+        * closes.
+        * 
+        * @param listener
+        *            the listener
+        */
+       public void addCloseListener(TAction listener) {
+               closeListeners.add(listener);
+       }
+
+       /**
+        * Close the window.
+        */
+       public void close() {
+               app.closeWindow(this);
+       }
+
+       @Override
+       public void onClose() {
+               super.onClose();
+               for (TAction listener : closeListeners) {
+                       listener.DO();
+               }
+       }
+
+       @Override
+       public void onKeypress(TKeypressEvent keypress) {
+               if (keyBindings.containsKey(keypress.getKey())) {
+                       keyBindings.get(keypress.getKey()).DO();
+               } else {
+                       super.onKeypress(keypress);
+               }
+       }
+}
index 55cc4aa6b3dea79b74e1094ab50b5a52b58c0172..827f7b9f2a2f2559d31bd767b2b0e001e45330ea 100644 (file)
@@ -1,45 +1,30 @@
 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);
+               super(app, title);
 
-               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<TKeypress, TAction>();
-
-               // TODO: fullscreen selection?
-
-               // 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.getSelectedLine(),
+                                                               table.getSelectedColumn());
+                                       }
+                               }, null);
        }
 
        /**
@@ -67,10 +52,6 @@ public abstract class TuiBrowserWindow extends TWindow {
                                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.
@@ -81,13 +62,6 @@ public abstract class TuiBrowserWindow extends TWindow {
                return table.getNumberOfLines();
        }
 
-       /**
-        * Close the window.
-        */
-       public void close() {
-               app.closeWindow(this);
-       }
-
        /**
         * An item has been selected.
         * 
@@ -103,17 +77,11 @@ public abstract class TuiBrowserWindow extends TWindow {
        @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();
                }
        }
 }
index bc4cb587febadec0a67fe5c12c5afc0155eba9b5..c8a31b895e00f8e3c725bf14af069c702b8d1509 100644 (file)
@@ -1,32 +1,24 @@
 package be.nikiroo.jvcard.tui.windows;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import jexer.TAction;
 import jexer.TApplication;
 import jexer.TKeypress;
 import jexer.TLabel;
 import jexer.TWindow;
-import jexer.event.TKeypressEvent;
 import be.nikiroo.jvcard.Contact;
 
-public class TuiContactWindow extends TWindow {
-       private Map<TKeypress, TAction> keyBindings;
-
+public class TuiContactWindow extends TuiBasicWindow {
        public TuiContactWindow(final TApplication app, final Contact contact) {
-               super(app, "Contact view", 40, 20);
+               super(app, "Contact view");
 
-               keyBindings = new HashMap<TKeypress, TAction>();
-
-               keyBindings.put(TKeypress.kbQ, new TAction() {
+               addKeyBinding(TKeypress.kbQ, new TAction() {
                        @Override
                        public void DO() {
                                app.closeWindow(TuiContactWindow.this);
                        }
                });
 
-               keyBindings.put(TKeypress.kbR, new TAction() {
+               addKeyBinding(TKeypress.kbR, new TAction() {
                        @Override
                        public void DO() {
                                @SuppressWarnings("unused")
@@ -36,21 +28,5 @@ public class TuiContactWindow extends TWindow {
 
                @SuppressWarnings("unused")
                TLabel l = new TLabel(this, "'r' to see raw view", 0, 0);
-
-               // TODO: fullscreen selection?
-
-               // TODO: auto-maximize on FS, auto-resize on maximize
-               // setFullscreen(true);
-               maximize();
-               onResize(null);
-       }
-
-       @Override
-       public void onKeypress(TKeypressEvent keypress) {
-               if (keyBindings.containsKey(keypress.getKey())) {
-                       keyBindings.get(keypress.getKey()).DO();
-               } else {
-                       super.onKeypress(keypress);
-               }
        }
 }