From: Niki Roo Date: Sat, 28 Apr 2018 09:55:32 +0000 (+0200) Subject: Update support code for Jexer X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=5c5abfd29edd67f4c16932dd8aaf16ac4ccec4f2;p=jvcard.git Update support code for Jexer --- diff --git a/src/be/nikiroo/jvcard/launcher/Main.java b/src/be/nikiroo/jvcard/launcher/Main.java index 69ed7e9..a073e92 100644 --- a/src/be/nikiroo/jvcard/launcher/Main.java +++ b/src/be/nikiroo/jvcard/launcher/Main.java @@ -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 diff --git a/src/be/nikiroo/jvcard/tui/TuiLauncherJexer.java b/src/be/nikiroo/jvcard/tui/TuiLauncherJexer.java index b79488c..3f54633 100644 --- a/src/be/nikiroo/jvcard/tui/TuiLauncherJexer.java +++ b/src/be/nikiroo/jvcard/tui/TuiLauncherJexer.java @@ -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 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 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) { diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java index a87df61..404d146 100644 --- a/src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java +++ b/src/be/nikiroo/jvcard/tui/windows/TuiBasicWindow.java @@ -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 keyBindings; private List 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(); diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java index 827f7b9..31ffce7 100644 --- a/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java +++ b/src/be/nikiroo/jvcard/tui/windows/TuiBrowserWindow.java @@ -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 headers, List> lines) { - int prevLine = table.getSelectedLine(); + public void setData(List headers, List> rows) { + int prevRow = table.getSelectedRow(); int prevColumn = table.getSelectedColumn(); table.clear(); table.setHeaders(headers, showHeader); - for (List line : lines) { - table.addLine(line); + for (List 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 diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiContactListWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiContactListWindow.java index af6b08e..21a8ca3 100644 --- a/src/be/nikiroo/jvcard/tui/windows/TuiContactListWindow.java +++ b/src/be/nikiroo/jvcard/tui/windows/TuiContactListWindow.java @@ -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 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); } diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiContactWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiContactWindow.java index c8a31b8..f1a6c34 100644 --- a/src/be/nikiroo/jvcard/tui/windows/TuiContactWindow.java +++ b/src/be/nikiroo/jvcard/tui/windows/TuiContactWindow.java @@ -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); } }); diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiFileListWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiFileListWindow.java index f1feea5..f354a10 100644 --- a/src/be/nikiroo/jvcard/tui/windows/TuiFileListWindow.java +++ b/src/be/nikiroo/jvcard/tui/windows/TuiFileListWindow.java @@ -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 files; private List cards; - public TuiFileListWindow(TApplication app, List files) { - super(app, "Contacts", false); + public TuiFileListWindow(TApplication app, int width, int height, + List files) { + super(app, width, height, "Contacts", false); - this.app = app; this.files = files; cards = new ArrayList(); @@ -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); } diff --git a/src/be/nikiroo/jvcard/tui/windows/TuiRawContactWindow.java b/src/be/nikiroo/jvcard/tui/windows/TuiRawContactWindow.java index c784b80..e69ee37 100644 --- a/src/be/nikiroo/jvcard/tui/windows/TuiRawContactWindow.java +++ b/src/be/nikiroo/jvcard/tui/windows/TuiRawContactWindow.java @@ -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 headers = new ArrayList(); 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();