From ad5849667b51ab27d7e4f31ba62b0447c94f1715 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Wed, 31 Jul 2019 22:04:09 -0500 Subject: [PATCH] delete column/row --- src/jexer/TTableWidget.java | 56 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/jexer/TTableWidget.java b/src/jexer/TTableWidget.java index c001109a..548acf64 100644 --- a/src/jexer/TTableWidget.java +++ b/src/jexer/TTableWidget.java @@ -925,7 +925,7 @@ public class TTableWidget extends TWidget { activate(columns.get(selectedColumn).get(selectedRow)); break; case TMenu.MID_TABLE_DELETE_ROW: - deleteRow(selectedColumn); + deleteRow(selectedRow); activate(columns.get(selectedColumn).get(selectedRow)); break; case TMenu.MID_TABLE_DELETE_COLUMN: @@ -1743,7 +1743,32 @@ public class TTableWidget extends TWidget { throw new IndexOutOfBoundsException("Row count is " + rows.size() + ", requested index " + row); } - // TODO + if (rows.size() == 1) { + // Don't delete the last row. + return; + } + for (int i = 0; i < columns.size(); i++) { + Cell cell = columns.get(i).cells.remove(row); + getChildren().remove(cell); + } + rows.remove(row); + + for (int x = 0; x < columns.size(); x++) { + for (int y = row; y < rows.size(); y++) { + columns.get(x).get(y).row = y; + columns.get(x).get(y).column = x; + } + } + for (int i = row; i < rows.size(); i++) { + String oldRowLabel = Integer.toString(i + 1); + if (rows.get(i).label.equals(oldRowLabel)) { + rows.get(i).label = Integer.toString(i); + } + } + if (selectedRow == rows.size()) { + selectedRow--; + } + alignGrid(); } /** @@ -1828,7 +1853,32 @@ public class TTableWidget extends TWidget { throw new IndexOutOfBoundsException("Column count is " + columns.size() + ", requested index " + column); } - // TODO + if (columns.size() == 1) { + // Don't delete the last column. + return; + } + for (int i = 0; i < rows.size(); i++) { + Cell cell = rows.get(i).cells.remove(column); + getChildren().remove(cell); + } + columns.remove(column); + + for (int x = column; x < columns.size(); x++) { + for (int y = 0; y < rows.size(); y++) { + columns.get(x).get(y).row = y; + columns.get(x).get(y).column = x; + } + } + for (int i = column; i < columns.size(); i++) { + String oldColumnLabel = makeColumnLabel(i + 1); + if (columns.get(i).label.equals(oldColumnLabel)) { + columns.get(i).label = makeColumnLabel(i); + } + } + if (selectedColumn == columns.size()) { + selectedColumn--; + } + alignGrid(); } /** -- 2.27.0