ttable delete up/left
[fanfix.git] / src / jexer / TTableWidget.java
index c001109a93d966732b27d264ba4a58b1b17dba9d..f25f429d52b58bc916dab54329296a12686d9e2c 100644 (file)
@@ -801,7 +801,7 @@ public class TTableWidget extends TWidget {
     public void onResize(final TResizeEvent event) {
         super.onResize(event);
 
-        alignGrid();
+        bottomRightCorner();
     }
 
     /**
@@ -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:
@@ -1276,6 +1276,24 @@ public class TTableWidget extends TWidget {
         return rows.size();
     }
 
+
+    /**
+     * Push top and left to the bottom-most right corner of the available
+     * grid.
+     */
+    private void bottomRightCorner() {
+        int viewColumns = getWidth();
+        if (showRowLabels == true) {
+            viewColumns -= ROW_LABEL_WIDTH;
+        }
+
+        // Set left and top such that the table stays on screen if possible.
+        top = rows.size() - getHeight();
+        left = columns.size() - (getWidth() / (viewColumns / (COLUMN_DEFAULT_WIDTH + 1)));
+        // Now ensure the selection is visible.
+        alignGrid();
+    }
+
     /**
      * Align the grid so that the selected cell is fully visible.
      */
@@ -1743,7 +1761,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--;
+        }
+        bottomRightCorner();
     }
 
     /**
@@ -1828,21 +1871,54 @@ 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--;
+        }
+        bottomRightCorner();
     }
 
     /**
      * Delete the selected cell, shifting cells over to the left.
      */
     public void deleteCellShiftLeft() {
-        // TODO
+        // All we do is copy the text from every cell in this row over.
+        for (int i = selectedColumn + 1; i < columns.size(); i++) {
+            setCellText(i - 1, selectedRow, getCellText(i, selectedRow));
+        }
+        setCellText(columns.size() - 1, selectedRow, "");
     }
 
     /**
      * Delete the selected cell, shifting cells from below up.
      */
     public void deleteCellShiftUp() {
-        // TODO
+        // All we do is copy the text from every cell in this column up.
+        for (int i = selectedRow + 1; i < rows.size(); i++) {
+            setCellText(selectedColumn, i - 1, getCellText(selectedColumn, i));
+        }
+        setCellText(selectedColumn, rows.size() - 1, "");
     }
 
     /**