From 77961919f5b88f024863cf33ffdfe8f3f9e98d83 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sun, 28 Jul 2019 12:53:29 -0500 Subject: [PATCH] ttable navigation minimally working --- src/jexer/TApplication.java | 22 ++++++++++ src/jexer/TApplication.properties | 1 + src/jexer/TTableWidget.java | 50 ++++++++++++++++------ src/jexer/TTableWindow.java | 8 ++++ src/jexer/bits/ColorTheme.java | 4 +- src/jexer/menu/TMenu.java | 71 ++++++++++++++++++++++--------- src/jexer/menu/TMenu.properties | 4 ++ src/jexer/menu/TMenuItem.java | 24 +++++++++++ 8 files changed, 149 insertions(+), 35 deletions(-) diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index 41fbea5..199fa1b 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -2923,6 +2923,21 @@ public class TApplication implements Runnable { } } + /** + * Get the menu item associated with this ID. + * + * @param id the menu item ID + * @return the menu item, or null if not found + */ + public final TMenuItem getMenuItem(final int id) { + for (TMenuItem item: menuItems) { + if (item.getId() == id) { + return item; + } + } + return null; + } + /** * Recompute menu x positions based on their title length. */ @@ -3101,6 +3116,13 @@ public class TApplication implements Runnable { */ public final TMenu addTableMenu() { TMenu tableMenu = addMenu(i18n.getString("tableMenuTitle")); + TSubMenu viewMenu = tableMenu.addSubMenu(i18n. + getString("tableSubMenuView")); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_ROW_LABELS, false); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS, false); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW, false); + viewMenu.addDefaultItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN, false); + TSubMenu borderMenu = tableMenu.addSubMenu(i18n. getString("tableSubMenuBorders")); borderMenu.addDefaultItem(TMenu.MID_TABLE_BORDER_NONE, false); diff --git a/src/jexer/TApplication.properties b/src/jexer/TApplication.properties index 5c50457..299c6a3 100644 --- a/src/jexer/TApplication.properties +++ b/src/jexer/TApplication.properties @@ -12,6 +12,7 @@ helpMenuTitle=&Help helpMenuStatus=Access online help tableMenuTitle=&Table +tableSubMenuView=&View tableSubMenuBorders=&Borders tableSubMenuDelete=&Delete tableSubMenuInsert=&Insert diff --git a/src/jexer/TTableWidget.java b/src/jexer/TTableWidget.java index c5d96dd..bd53977 100644 --- a/src/jexer/TTableWidget.java +++ b/src/jexer/TTableWidget.java @@ -389,6 +389,8 @@ public class TTableWidget extends TWidget { field.setInactiveColorKey("ttable.inactive"); } + assert (isVisible() == true); + super.draw(); } @@ -462,6 +464,18 @@ public class TTableWidget extends TWidget { activate(columns.get(selectedColumn).get(selectedRow)); alignGrid(); + + // Set the menu to match the flags. + getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS). + setChecked(showRowLabels); + getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS). + setChecked(showColumnLabels); + getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW). + setChecked(highlightRow); + getApplication().getMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN). + setChecked(highlightColumn); + + } // ------------------------------------------------------------------------ @@ -553,6 +567,18 @@ public class TTableWidget extends TWidget { @Override public void onMenu(final TMenuEvent menu) { switch (menu.getId()) { + case TMenu.MID_TABLE_VIEW_ROW_LABELS: + showRowLabels = getApplication().getMenuItem(menu.getId()).getChecked(); + break; + case TMenu.MID_TABLE_VIEW_COLUMN_LABELS: + showColumnLabels = getApplication().getMenuItem(menu.getId()).getChecked(); + break; + case TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW: + highlightRow = getApplication().getMenuItem(menu.getId()).getChecked(); + break; + case TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN: + highlightColumn = getApplication().getMenuItem(menu.getId()).getChecked(); + break; case TMenu.MID_TABLE_BORDER_NONE: case TMenu.MID_TABLE_BORDER_ALL: case TMenu.MID_TABLE_BORDER_RIGHT: @@ -602,6 +628,8 @@ public class TTableWidget extends TWidget { default: super.onMenu(menu); } + + alignGrid(); } // ------------------------------------------------------------------------ @@ -609,12 +637,12 @@ public class TTableWidget extends TWidget { // ------------------------------------------------------------------------ /** - * Draw the table row/column headings, and borders. + * Draw the table row/column labels, and borders. */ @Override public void draw() { - CellAttributes headingColor = getTheme().getColor("ttable.heading"); - CellAttributes headingColorSelected = getTheme().getColor("ttable.heading.selected"); + CellAttributes labelColor = getTheme().getColor("ttable.label"); + CellAttributes labelColorSelected = getTheme().getColor("ttable.label.selected"); CellAttributes borderColor = getTheme().getColor("ttable.border"); // Column labels. @@ -625,7 +653,7 @@ public class TTableWidget extends TWidget { } putStringXY(columns.get(i).get(top).getX(), 0, String.format(" %-6s ", columns.get(i).label), - (i == selectedColumn ? headingColorSelected : headingColor)); + (i == selectedColumn ? labelColorSelected : labelColor)); } } @@ -637,7 +665,7 @@ public class TTableWidget extends TWidget { } putStringXY(0, rows.get(i).get(left).getY(), String.format(" %-6s ", rows.get(i).label), - (i == selectedRow ? headingColorSelected : headingColor)); + (i == selectedRow ? labelColorSelected : labelColor)); } } @@ -744,7 +772,6 @@ public class TTableWidget extends TWidget { // Adjust X locations to be visible ----------------------------------- // Determine if we need to shift left or right. - int width = getMaximumWidth(); int leftCellX = 0; if (showRowLabels == true) { // For now, all row labels are 8 cells wide. TODO: make this @@ -822,7 +849,6 @@ public class TTableWidget extends TWidget { // The same logic as above, but applied to the column Y. // Determine if we need to shift up or down. - int height = getMaximumHeight(); int topCellY = 0; if (showColumnLabels == true) { // For now, all column labels are 1 cell high. TODO: make this @@ -867,10 +893,11 @@ public class TTableWidget extends TWidget { * selected cell Y is topCellY. */ for (int x = 0; x < columns.size(); x++) { - if (columns.get(x).cells.get(0).isVisible() == false) { - // This entire column will not be visible, as determined by - // the width checks above. Do no further processing. - break; + + if (columns.get(x).get(0).isVisible() == false) { + // This column won't be visible as determined by the checks + // above, just continue to the next. + continue; } // All cells above the selected cell. @@ -890,7 +917,6 @@ public class TTableWidget extends TWidget { // Selected cell. columns.get(x).cells.get(selectedRow).setY(topCellY); - assert (columns.get(x).cells.get(selectedRow).isVisible()); // All cells below the selected cell. newCellY = topCellY + selectedRowCell.getHeight(); diff --git a/src/jexer/TTableWindow.java b/src/jexer/TTableWindow.java index 507dde7..33b8116 100644 --- a/src/jexer/TTableWindow.java +++ b/src/jexer/TTableWindow.java @@ -88,6 +88,10 @@ public class TTableWindow extends TScrollableWindow { public void onFocus() { // Enable the table menu items. getApplication().enableMenuItem(TMenu.MID_CUT); + getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); + getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS); + getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW); + getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_NONE); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_ALL); getApplication().enableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT); @@ -117,6 +121,10 @@ public class TTableWindow extends TScrollableWindow { public void onUnfocus() { // Disable the table menu items. getApplication().disableMenuItem(TMenu.MID_CUT); + getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); + getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_COLUMN_LABELS); + getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_ROW); + getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_HIGHLIGHT_COLUMN); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_NONE); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_ALL); getApplication().disableMenuItem(TMenu.MID_TABLE_BORDER_RIGHT); diff --git a/src/jexer/bits/ColorTheme.java b/src/jexer/bits/ColorTheme.java index 86fc9b2..701ea75 100644 --- a/src/jexer/bits/ColorTheme.java +++ b/src/jexer/bits/ColorTheme.java @@ -654,12 +654,12 @@ public class ColorTheme { color.setForeColor(Color.BLACK); color.setBackColor(Color.WHITE); color.setBold(false); - colors.put("ttable.heading", color); + colors.put("ttable.label", color); color = new CellAttributes(); color.setForeColor(Color.BLUE); color.setBackColor(Color.WHITE); color.setBold(false); - colors.put("ttable.heading.selected", color); + colors.put("ttable.label.selected", color); color = new CellAttributes(); color.setForeColor(Color.WHITE); color.setBackColor(Color.BLUE); diff --git a/src/jexer/menu/TMenu.java b/src/jexer/menu/TMenu.java index 1bcc800..1420456 100644 --- a/src/jexer/menu/TMenu.java +++ b/src/jexer/menu/TMenu.java @@ -102,26 +102,30 @@ public class TMenu extends TWindow { public static final int MID_ABOUT = 56; // Table menu - public static final int MID_TABLE_BORDER_NONE = 60; - public static final int MID_TABLE_BORDER_ALL = 61; - public static final int MID_TABLE_BORDER_RIGHT = 62; - public static final int MID_TABLE_BORDER_LEFT = 63; - public static final int MID_TABLE_BORDER_TOP = 64; - public static final int MID_TABLE_BORDER_BOTTOM = 65; - public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM = 66; - public static final int MID_TABLE_BORDER_THICK_BOTTOM = 67; - public static final int MID_TABLE_DELETE_LEFT = 68; - public static final int MID_TABLE_DELETE_UP = 69; - public static final int MID_TABLE_DELETE_ROW = 70; - public static final int MID_TABLE_DELETE_COLUMN = 71; - public static final int MID_TABLE_INSERT_LEFT = 72; - public static final int MID_TABLE_INSERT_RIGHT = 73; - public static final int MID_TABLE_INSERT_ABOVE = 74; - public static final int MID_TABLE_INSERT_BELOW = 75; - public static final int MID_TABLE_COLUMN_NARROW = 76; - public static final int MID_TABLE_COLUMN_WIDEN = 77; - public static final int MID_TABLE_FILE_SAVE_CSV = 78; - public static final int MID_TABLE_FILE_SAVE_TEXT = 79; + public static final int MID_TABLE_VIEW_ROW_LABELS = 60; + public static final int MID_TABLE_VIEW_COLUMN_LABELS = 61; + public static final int MID_TABLE_VIEW_HIGHLIGHT_ROW = 62; + public static final int MID_TABLE_VIEW_HIGHLIGHT_COLUMN = 63; + public static final int MID_TABLE_BORDER_NONE = 64; + public static final int MID_TABLE_BORDER_ALL = 65; + public static final int MID_TABLE_BORDER_RIGHT = 66; + public static final int MID_TABLE_BORDER_LEFT = 67; + public static final int MID_TABLE_BORDER_TOP = 68; + public static final int MID_TABLE_BORDER_BOTTOM = 69; + public static final int MID_TABLE_BORDER_DOUBLE_BOTTOM = 70; + public static final int MID_TABLE_BORDER_THICK_BOTTOM = 71; + public static final int MID_TABLE_DELETE_LEFT = 72; + public static final int MID_TABLE_DELETE_UP = 73; + public static final int MID_TABLE_DELETE_ROW = 74; + public static final int MID_TABLE_DELETE_COLUMN = 75; + public static final int MID_TABLE_INSERT_LEFT = 76; + public static final int MID_TABLE_INSERT_RIGHT = 77; + public static final int MID_TABLE_INSERT_ABOVE = 78; + public static final int MID_TABLE_INSERT_BELOW = 79; + public static final int MID_TABLE_COLUMN_NARROW = 80; + public static final int MID_TABLE_COLUMN_WIDEN = 81; + public static final int MID_TABLE_FILE_SAVE_CSV = 82; + public static final int MID_TABLE_FILE_SAVE_TEXT = 83; // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- @@ -530,6 +534,8 @@ public class TMenu extends TWindow { String label; TKeypress key = null; + boolean checkable = false; + boolean checked = false; switch (id) { @@ -649,6 +655,27 @@ public class TMenu extends TWindow { label = i18n.getString("menuHelpAbout"); break; + case MID_TABLE_VIEW_ROW_LABELS: + label = i18n.getString("menuTableViewRowLabels"); + checkable = true; + checked = true; + break; + case MID_TABLE_VIEW_COLUMN_LABELS: + label = i18n.getString("menuTableViewColumnLabels"); + checkable = true; + checked = true; + break; + case MID_TABLE_VIEW_HIGHLIGHT_ROW: + label = i18n.getString("menuTableViewHighlightRow"); + checkable = true; + checked = true; + break; + case MID_TABLE_VIEW_HIGHLIGHT_COLUMN: + label = i18n.getString("menuTableViewHighlightColumn"); + checkable = true; + checked = true; + break; + case MID_TABLE_BORDER_NONE: label = i18n.getString("menuTableBorderNone"); break; @@ -716,7 +743,9 @@ public class TMenu extends TWindow { throw new IllegalArgumentException("Invalid menu ID: " + id); } - return addItemInternal(id, label, key, enabled); + TMenuItem item = addItemInternal(id, label, key, enabled); + item.setCheckable(checkable); + return item; } /** diff --git a/src/jexer/menu/TMenu.properties b/src/jexer/menu/TMenu.properties index c64da18..3274157 100644 --- a/src/jexer/menu/TMenu.properties +++ b/src/jexer/menu/TMenu.properties @@ -26,6 +26,10 @@ menuHelpHelp=&Help on help menuHelpActive=Active &file... menuHelpAbout=&About... +menuTableViewRowLabels=&Row Labels +menuTableViewColumnLabels=&Column Labels +menuTableViewHighlightRow=Highlight Selected R&ow +menuTableViewHighlightColumn=Highlight Selected Co&lumn menuTableBorderNone=&None menuTableBorderAll=&All menuTableBorderRight=&Right diff --git a/src/jexer/menu/TMenuItem.java b/src/jexer/menu/TMenuItem.java index 6e455d8..bd149ab 100644 --- a/src/jexer/menu/TMenuItem.java +++ b/src/jexer/menu/TMenuItem.java @@ -263,6 +263,30 @@ public class TMenuItem extends TWidget { this.checkable = checkable; } + /** + * Get checkable flag. + * + * @return true if this menu item is both checkable and checked + */ + public final boolean getChecked() { + return ((checkable == true) && (checked == true)); + } + + /** + * Set checked flag. Note that setting checked on an item checkable will + * do nothing. + * + * @param checked if true, and if this menu item is checkable, then + * getChecked() will return true + */ + public final void setChecked(final boolean checked) { + if (checkable) { + this.checked = checked; + } else { + this.checked = false; + } + } + /** * Get the mnemonic string for this menu item. * -- 2.27.0