X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjexer%2FTTableWidget.java;h=2f498150d03b6019dc52e4d61ba0f3d50246dcd1;hb=f528c340199b03ccc2e0d2835b6156562686a202;hp=ef5857fa48cfa0275bd1441c1014f484591477c5;hpb=11cedc9ab61111659c8d2cca5ff85303ec18e53b;p=fanfix.git diff --git a/src/jexer/TTableWidget.java b/src/jexer/TTableWidget.java index ef5857f..2f49815 100644 --- a/src/jexer/TTableWidget.java +++ b/src/jexer/TTableWidget.java @@ -31,13 +31,12 @@ package jexer; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.ResourceBundle; import jexer.bits.CellAttributes; import jexer.event.TKeypressEvent; -import jexer.event.TMenuEvent; import jexer.event.TMouseEvent; import jexer.event.TResizeEvent; -import jexer.menu.TMenu; import static jexer.TKeypress.*; /** @@ -50,6 +49,11 @@ import static jexer.TKeypress.*; */ public class TTableWidget extends TWidget { + /** + * Translated strings. + */ + private static final ResourceBundle i18n = ResourceBundle.getBundle(TTableWidget.class.getName()); + // ------------------------------------------------------------------------ // Constants -------------------------------------------------------------- // ------------------------------------------------------------------------ @@ -74,12 +78,16 @@ public class TTableWidget extends TWidget { DOUBLE, /** - * Thick bar: \u258C (vertical, left half block) and \u2580 - * (horizontal, upper block). + * Thick bar: \u2503 (vertical heavy) and \u2501 (horizontal heavy). */ THICK, } + /** + * If true, put a grid of numbers in the cells. + */ + private static final boolean DEBUG = false; + /** * Row label width. */ @@ -98,12 +106,12 @@ public class TTableWidget extends TWidget { /** * Extra rows to add. */ - private static final int EXTRA_ROWS = 10; + private static final int EXTRA_ROWS = (DEBUG ? 10 : 0); /** * Extra columns to add. */ - private static final int EXTRA_COLUMNS = 10 * (8 + 1); + private static final int EXTRA_COLUMNS = (DEBUG ? 10 * (8 + 1) : 0); // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- @@ -206,15 +214,7 @@ public class TTableWidget extends TWidget { * "A", column 1 will be "B", column 26 will be "AA", and so on. */ Column(int col) { - StringBuilder sb = new StringBuilder(); - for (;;) { - sb.append((char) ('A' + (col % 26))); - if (col < 26) { - break; - } - col /= 26; - } - label = sb.reverse().toString(); + label = makeColumnLabel(col); } /** @@ -370,6 +370,11 @@ public class TTableWidget extends TWidget { */ private boolean isEditing = false; + /** + * If true, the cell is read-only (non-editable). + */ + private boolean readOnly = false; + /** * Text of field before editing. */ @@ -476,16 +481,24 @@ public class TTableWidget extends TWidget { public void onKeypress(final TKeypressEvent keypress) { // System.err.println("Cell onKeypress: " + keypress); + if (readOnly) { + // Read only: do nothing. + return; + } + if (isEditing) { if (keypress.equals(kbEsc)) { // ESC cancels the edit. - field.setText(fieldText); - isEditing = false; - field.setEnabled(false); + cancelEdit(); return; } if (keypress.equals(kbEnter)) { // Enter ends editing. + + // Pass down to field first so that it can execute + // enterAction if specified. + super.onKeypress(keypress); + fieldText = field.getText(); isEditing = false; field.setEnabled(false); @@ -565,6 +578,28 @@ public class TTableWidget extends TWidget { field.setText(text); } + /** + * Cancel any pending edit. + */ + public void cancelEdit() { + // Cancel any pending edit. + if (fieldText != null) { + field.setText(fieldText); + } + isEditing = false; + field.setEnabled(false); + } + + /** + * Set an entire column of cells read-only (non-editable) or not. + * + * @param readOnly if true, the cells will be non-editable + */ + public void setReadOnly(final boolean readOnly) { + cancelEdit(); + this.readOnly = readOnly; + } + } // ------------------------------------------------------------------------ @@ -596,12 +631,13 @@ public class TTableWidget extends TWidget { for (int j = 0; j < width + EXTRA_COLUMNS; j += columns.get(0).width) { - Cell cell = new Cell(this, 0, 0, /* j, i, */ columns.get(0).width, + Cell cell = new Cell(this, 0, 0, columns.get(0).width, rows.get(0).height, column, row); - // DEBUG: set a grid of cell index labels - // TODO: remove this - cell.setText("" + row + " " + column); + if (DEBUG) { + // For debugging: set a grid of cell index labels. + cell.setText("" + row + " " + column); + } rows.get(row).add(cell); columns.get(column).add(cell); if ((i == 0) && @@ -626,18 +662,6 @@ 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); - - } // ------------------------------------------------------------------------ @@ -769,79 +793,7 @@ public class TTableWidget extends TWidget { public void onResize(final TResizeEvent event) { super.onResize(event); - alignGrid(); - } - - /** - * Handle posted menu events. - * - * @param menu menu event - */ - @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: - case TMenu.MID_TABLE_BORDER_LEFT: - case TMenu.MID_TABLE_BORDER_TOP: - case TMenu.MID_TABLE_BORDER_BOTTOM: - case TMenu.MID_TABLE_BORDER_DOUBLE_BOTTOM: - case TMenu.MID_TABLE_BORDER_THICK_BOTTOM: - case TMenu.MID_TABLE_DELETE_LEFT: - case TMenu.MID_TABLE_DELETE_UP: - case TMenu.MID_TABLE_DELETE_ROW: - case TMenu.MID_TABLE_DELETE_COLUMN: - case TMenu.MID_TABLE_INSERT_LEFT: - case TMenu.MID_TABLE_INSERT_RIGHT: - case TMenu.MID_TABLE_INSERT_ABOVE: - case TMenu.MID_TABLE_INSERT_BELOW: - break; - case TMenu.MID_TABLE_COLUMN_NARROW: - columns.get(selectedColumn).width--; - for (Cell cell: getSelectedColumn().cells) { - cell.setWidth(columns.get(selectedColumn).width); - cell.field.setWidth(columns.get(selectedColumn).width); - } - for (int i = selectedColumn + 1; i < columns.size(); i++) { - columns.get(i).setX(columns.get(i).getX() - 1); - } - alignGrid(); - break; - case TMenu.MID_TABLE_COLUMN_WIDEN: - columns.get(selectedColumn).width++; - for (Cell cell: getSelectedColumn().cells) { - cell.setWidth(columns.get(selectedColumn).width); - cell.field.setWidth(columns.get(selectedColumn).width); - } - for (int i = selectedColumn + 1; i < columns.size(); i++) { - columns.get(i).setX(columns.get(i).getX() + 1); - } - alignGrid(); - break; - case TMenu.MID_TABLE_FILE_SAVE_CSV: - // TODO - break; - case TMenu.MID_TABLE_FILE_SAVE_TEXT: - // TODO - break; - default: - super.onMenu(menu); - } - - alignGrid(); + bottomRightCorner(); } // ------------------------------------------------------------------------ @@ -883,6 +835,129 @@ public class TTableWidget extends TWidget { } } + // Draw vertical borders. + if (leftBorder == Border.SINGLE) { + vLineXY((showRowLabels ? ROW_LABEL_WIDTH : 0), + (topBorder == Border.NONE ? 0 : 1) + + (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0), + getHeight(), '\u2502', borderColor); + } + for (int i = left; i < columns.size(); i++) { + if (columns.get(i).get(top).isVisible() == false) { + break; + } + if (columns.get(i).rightBorder == Border.SINGLE) { + vLineXY(columns.get(i).getX() + columns.get(i).width, + (topBorder == Border.NONE ? 0 : 1) + + (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0), + getHeight(), '\u2502', borderColor); + } + } + + // Draw horizontal borders. + if (topBorder == Border.SINGLE) { + hLineXY((showRowLabels ? ROW_LABEL_WIDTH : 0), + (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0), + getWidth(), '\u2500', borderColor); + } + for (int i = top; i < rows.size(); i++) { + if (rows.get(i).get(left).isVisible() == false) { + break; + } + if (rows.get(i).bottomBorder == Border.SINGLE) { + hLineXY((leftBorder == Border.NONE ? 0 : 1) + + (showRowLabels ? ROW_LABEL_WIDTH : 0), + rows.get(i).getY() + rows.get(i).height - 1, + getWidth(), '\u2500', borderColor); + } else if (rows.get(i).bottomBorder == Border.DOUBLE) { + hLineXY((leftBorder == Border.NONE ? 0 : 1) + + (showRowLabels ? ROW_LABEL_WIDTH : 0), + rows.get(i).getY() + rows.get(i).height - 1, + getWidth(), '\u2550', borderColor); + } else if (rows.get(i).bottomBorder == Border.THICK) { + hLineXY((leftBorder == Border.NONE ? 0 : 1) + + (showRowLabels ? ROW_LABEL_WIDTH : 0), + rows.get(i).getY() + rows.get(i).height - 1, + getWidth(), '\u2501', borderColor); + } + } + // Top-left corner if needed + if ((topBorder == Border.SINGLE) && (leftBorder == Border.SINGLE)) { + putCharXY((showRowLabels ? ROW_LABEL_WIDTH : 0), + (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0), + '\u250c', borderColor); + } + + // Now draw the correct corners + for (int i = top; i < rows.size(); i++) { + if (rows.get(i).get(left).isVisible() == false) { + break; + } + for (int j = left; j < columns.size(); j++) { + if (columns.get(j).get(i).isVisible() == false) { + break; + } + if ((i == top) && (topBorder == Border.SINGLE) + && (columns.get(j).rightBorder == Border.SINGLE) + ) { + // Top tee + putCharXY(columns.get(j).getX() + columns.get(j).width, + (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0), + '\u252c', borderColor); + } + if ((j == left) && (leftBorder == Border.SINGLE) + && (rows.get(i).bottomBorder == Border.SINGLE) + ) { + // Left tee + putCharXY((showRowLabels ? ROW_LABEL_WIDTH : 0), + rows.get(i).getY() + rows.get(i).height - 1, + '\u251c', borderColor); + } + if ((columns.get(j).rightBorder == Border.SINGLE) + && (rows.get(i).bottomBorder == Border.SINGLE) + ) { + // Intersection of single bars + putCharXY(columns.get(j).getX() + columns.get(j).width, + rows.get(i).getY() + rows.get(i).height - 1, + '\u253c', borderColor); + } + if ((j == left) && (leftBorder == Border.SINGLE) + && (rows.get(i).bottomBorder == Border.DOUBLE) + ) { + // Left tee: single bar vertical, double bar horizontal + putCharXY((showRowLabels ? ROW_LABEL_WIDTH : 0), + rows.get(i).getY() + rows.get(i).height - 1, + '\u255e', borderColor); + } + if ((j == left) && (leftBorder == Border.SINGLE) + && (rows.get(i).bottomBorder == Border.THICK) + ) { + // Left tee: single bar vertical, thick bar horizontal + putCharXY((showRowLabels ? ROW_LABEL_WIDTH : 0), + rows.get(i).getY() + rows.get(i).height - 1, + '\u251d', borderColor); + } + if ((columns.get(j).rightBorder == Border.SINGLE) + && (rows.get(i).bottomBorder == Border.DOUBLE) + ) { + // Intersection: single bar vertical, double bar + // horizontal + putCharXY(columns.get(j).getX() + columns.get(j).width, + rows.get(i).getY() + rows.get(i).height - 1, + '\u256a', borderColor); + } + if ((columns.get(j).rightBorder == Border.SINGLE) + && (rows.get(i).bottomBorder == Border.THICK) + ) { + // Intersection: single bar vertical, thick bar + // horizontal + putCharXY(columns.get(j).getX() + columns.get(j).width, + rows.get(i).getY() + rows.get(i).height - 1, + '\u253f', borderColor); + } + } + } + // Now draw the window borders. super.draw(); } @@ -891,6 +966,24 @@ public class TTableWidget extends TWidget { // TTable ----------------------------------------------------------------- // ------------------------------------------------------------------------ + /** + * Generate the default letter name for a column number. + * + * @param col column number to use for this column. Column 0 will be + * "A", column 1 will be "B", column 26 will be "AA", and so on. + */ + private String makeColumnLabel(int col) { + StringBuilder sb = new StringBuilder(); + for (;;) { + sb.append((char) ('A' + (col % 26))); + if (col < 26) { + break; + } + col /= 26; + } + return sb.reverse().toString(); + } + /** * Get the currently-selected cell. * @@ -979,6 +1072,78 @@ public class TTableWidget extends TWidget { alignGrid(); } + /** + * Get the highlight row flag. + * + * @return true if the selected row is highlighted + */ + public boolean getHighlightRow() { + return highlightRow; + } + + /** + * Set the highlight row flag. + * + * @param highlightRow if true, the selected row will be highlighted + */ + public void setHighlightRow(final boolean highlightRow) { + this.highlightRow = highlightRow; + } + + /** + * Get the highlight column flag. + * + * @return true if the selected column is highlighted + */ + public boolean getHighlightColumn() { + return highlightColumn; + } + + /** + * Set the highlight column flag. + * + * @param highlightColumn if true, the selected column will be highlighted + */ + public void setHighlightColumn(final boolean highlightColumn) { + this.highlightColumn = highlightColumn; + } + + /** + * Get the show row labels flag. + * + * @return true if row labels are shown + */ + public boolean getShowRowLabels() { + return showRowLabels; + } + + /** + * Set the show row labels flag. + * + * @param showRowLabels if true, the row labels will be shown + */ + public void setShowRowLabels(final boolean showRowLabels) { + this.showRowLabels = showRowLabels; + } + + /** + * Get the show column labels flag. + * + * @return true if column labels are shown + */ + public boolean getShowColumnLabels() { + return showColumnLabels; + } + + /** + * Set the show column labels flag. + * + * @param showColumnLabels if true, the column labels will be shown + */ + public void setShowColumnLabels(final boolean showColumnLabels) { + this.showColumnLabels = showColumnLabels; + } + /** * Get the number of columns. * @@ -997,6 +1162,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. */ @@ -1045,6 +1228,9 @@ public class TTableWidget extends TWidget { boolean done = false; while (!done) { int rightCellX = (showRowLabels ? ROW_LABEL_WIDTH : 0); + if (leftBorder != Border.NONE) { + rightCellX++; + } int maxCellX = rightCellX + viewColumns; right = left; boolean selectedIsVisible = false; @@ -1085,6 +1271,9 @@ public class TTableWidget extends TWidget { // We have the left/right range correct, set cell visibility and // column X positions. int leftCellX = showRowLabels ? ROW_LABEL_WIDTH : 0; + if (leftBorder != Border.NONE) { + leftCellX++; + } for (int x = 0; x < columns.size(); x++) { if ((x < left) || (x > right)) { for (int i = 0; i < rows.size(); i++) { @@ -1105,6 +1294,9 @@ public class TTableWidget extends TWidget { done = false; while (!done) { int bottomCellY = (showColumnLabels ? COLUMN_LABEL_HEIGHT : 0); + if (topBorder != Border.NONE) { + bottomCellY++; + } int maxCellY = bottomCellY + viewRows; bottom = top; for (int y = top; y < rows.size(); y++) { @@ -1127,6 +1319,9 @@ public class TTableWidget extends TWidget { // We have the top/bottom range correct, set cell visibility and // row Y positions. int topCellY = showColumnLabels ? COLUMN_LABEL_HEIGHT : 0; + if (topBorder != Border.NONE) { + topCellY++; + } for (int y = 0; y < rows.size(); y++) { if ((y < top) || (y > bottom)) { for (int i = 0; i < columns.size(); i++) { @@ -1142,16 +1337,690 @@ public class TTableWidget extends TWidget { topCellY += rows.get(y).height; } + // Last thing: cancel any edits that are not the selected cell. + for (int y = 0; y < rows.size(); y++) { + for (int x = 0; x < columns.size(); x++) { + if ((x == selectedColumn) && (y == selectedRow)) { + continue; + } + rows.get(y).get(x).cancelEdit(); + } + } + } + + /** + * Save contents to file in CSV format. + * + * @param filename file to save to + * @throws IOException if a java.io operation throws + */ + public void saveToCsvFilename(final String filename) throws IOException { + // TODO } /** - * Save contents to file. + * Save contents to file in text format with lines. * * @param filename file to save to * @throws IOException if a java.io operation throws */ - public void saveToFilename(final String filename) throws IOException { + public void saveToTextFilename(final String filename) throws IOException { // TODO } + /** + * Set the selected cell location. + * + * @param column the selected cell location column + * @param row the selected cell location row + */ + public void setSelectedCell(final int column, final int row) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + selectedColumn = column; + selectedRow = row; + alignGrid(); + } + + /** + * Get a particular cell. + * + * @param column the cell column + * @param row the cell row + * @return the cell + */ + public Cell getCell(final int column, final int row) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + return rows.get(row).get(column); + } + + /** + * Get the text of a particular cell. + * + * @param column the cell column + * @param row the cell row + * @return the text in the cell + */ + public String getCellText(final int column, final int row) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + return rows.get(row).get(column).getText(); + } + + /** + * Set the text of a particular cell. + * + * @param column the cell column + * @param row the cell row + * @param text the text to put into the cell + */ + public void setCellText(final int column, final int row, + final String text) { + + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + rows.get(row).get(column).setText(text); + } + + /** + * Set the action to perform when the user presses enter on a particular + * cell. + * + * @param column the cell column + * @param row the cell row + * @param action the action to perform when the user presses enter on the + * cell + */ + public void setCellEnterAction(final int column, final int row, + final TAction action) { + + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + rows.get(row).get(column).field.setEnterAction(action); + } + + /** + * Set the action to perform when the user updates a particular cell. + * + * @param column the cell column + * @param row the cell row + * @param action the action to perform when the user updates the cell + */ + public void setCellUpdateAction(final int column, final int row, + final TAction action) { + + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + rows.get(row).get(column).field.setUpdateAction(action); + } + + /** + * Get the width of a column. + * + * @param column the column number + * @return the width of the column + */ + public int getColumnWidth(final int column) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + return columns.get(column).width; + } + + /** + * Set the width of a column. + * + * @param column the column number + * @param width the new width of the column + */ + public void setColumnWidth(final int column, final int width) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + + if (width < 4) { + // Columns may not be smaller than 4 cells wide. + return; + } + + int delta = width - columns.get(column).width; + columns.get(column).width = width; + for (Cell cell: columns.get(column).cells) { + cell.setWidth(columns.get(column).width); + cell.field.setWidth(columns.get(column).width); + } + for (int i = column + 1; i < columns.size(); i++) { + columns.get(i).setX(columns.get(i).getX() + delta); + } + if (column == columns.size() - 1) { + bottomRightCorner(); + } else { + alignGrid(); + } + } + + /** + * Get the label of a column. + * + * @param column the column number + * @return the label of the column + */ + public String getColumnLabel(final int column) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + return columns.get(column).label; + } + + /** + * Set the label of a column. + * + * @param column the column number + * @param label the new label of the column + */ + public void setColumnLabel(final int column, final String label) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + columns.get(column).label = label; + } + + /** + * Get the label of a row. + * + * @param row the row number + * @return the label of the row + */ + public String getRowLabel(final int row) { + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + return rows.get(row).label; + } + + /** + * Set the label of a row. + * + * @param row the row number + * @param label the new label of the row + */ + public void setRowLabel(final int row, final String label) { + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + rows.get(row).label = label; + } + + /** + * Insert one row at a particular index. + * + * @param idx the row number + */ + private void insertRowAt(final int idx) { + Row newRow = new Row(idx); + for (int i = 0; i < columns.size(); i++) { + Cell cell = new Cell(this, columns.get(i).getX(), + rows.get(selectedRow).getY(), COLUMN_DEFAULT_WIDTH, 1, i, idx); + newRow.add(cell); + columns.get(i).cells.add(idx, cell); + } + rows.add(idx, newRow); + + for (int x = 0; x < columns.size(); x++) { + for (int y = idx; y < rows.size(); y++) { + columns.get(x).get(y).row = y; + columns.get(x).get(y).column = x; + } + } + for (int i = idx + 1; i < rows.size(); i++) { + String oldRowLabel = Integer.toString(i - 1); + if (rows.get(i).label.equals(oldRowLabel)) { + rows.get(i).label = Integer.toString(i); + } + } + alignGrid(); + } + + /** + * Insert one row above a particular row. + * + * @param row the row number + */ + public void insertRowAbove(final int row) { + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + insertRowAt(selectedRow); + selectedRow++; + activate(columns.get(selectedColumn).get(selectedRow)); + } + + /** + * Insert one row below a particular row. + * + * @param row the row number + */ + public void insertRowBelow(final int row) { + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + int idx = selectedRow + 1; + if (idx < rows.size()) { + insertRowAt(idx); + activate(columns.get(selectedColumn).get(selectedRow)); + return; + } + + // selectedRow is the last row, we need to perform an append. + Row newRow = new Row(idx); + for (int i = 0; i < columns.size(); i++) { + Cell cell = new Cell(this, columns.get(i).getX(), + rows.get(selectedRow).getY(), COLUMN_DEFAULT_WIDTH, 1, i, idx); + newRow.add(cell); + columns.get(i).cells.add(cell); + } + rows.add(newRow); + alignGrid(); + activate(columns.get(selectedColumn).get(selectedRow)); + } + + /** + * Delete a particular row. + * + * @param row the row number + */ + public void deleteRow(final int row) { + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + 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--; + } + activate(columns.get(selectedColumn).get(selectedRow)); + bottomRightCorner(); + } + + /** + * Insert one column at a particular index. + * + * @param idx the column number + */ + private void insertColumnAt(final int idx) { + Column newColumn = new Column(idx); + for (int i = 0; i < rows.size(); i++) { + Cell cell = new Cell(this, columns.get(selectedColumn).getX(), + rows.get(i).getY(), COLUMN_DEFAULT_WIDTH, 1, idx, i); + newColumn.add(cell); + rows.get(i).cells.add(idx, cell); + } + columns.add(idx, newColumn); + + for (int x = idx; 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 = idx + 1; i < columns.size(); i++) { + String oldColumnLabel = makeColumnLabel(i - 1); + if (columns.get(i).label.equals(oldColumnLabel)) { + columns.get(i).label = makeColumnLabel(i); + } + } + alignGrid(); + } + + /** + * Insert one column to the left of a particular column. + * + * @param column the column number + */ + public void insertColumnLeft(final int column) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + insertColumnAt(selectedColumn); + selectedColumn++; + activate(columns.get(selectedColumn).get(selectedRow)); + } + + /** + * Insert one column to the right of a particular column. + * + * @param column the column number + */ + public void insertColumnRight(final int column) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + int idx = selectedColumn + 1; + if (idx < columns.size()) { + insertColumnAt(idx); + activate(columns.get(selectedColumn).get(selectedRow)); + return; + } + + // selectedColumn is the last column, we need to perform an append. + Column newColumn = new Column(idx); + for (int i = 0; i < rows.size(); i++) { + Cell cell = new Cell(this, columns.get(selectedColumn).getX(), + rows.get(i).getY(), COLUMN_DEFAULT_WIDTH, 1, idx, i); + newColumn.add(cell); + rows.get(i).cells.add(cell); + } + columns.add(newColumn); + alignGrid(); + activate(columns.get(selectedColumn).get(selectedRow)); + } + + /** + * Delete a particular column. + * + * @param column the column number + */ + public void deleteColumn(final int column) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + 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--; + } + activate(columns.get(selectedColumn).get(selectedRow)); + bottomRightCorner(); + } + + /** + * Delete the selected cell, shifting cells over to the left. + */ + public void deleteCellShiftLeft() { + // 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() { + // 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, ""); + } + + /** + * Set a particular cell read-only (non-editable) or not. + * + * @param column the cell column + * @param row the cell row + * @param readOnly if true, the cell will be non-editable + */ + public void setCellReadOnly(final int column, final int row, + final boolean readOnly) { + + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + rows.get(row).get(column).setReadOnly(readOnly); + } + + /** + * Set an entire row of cells read-only (non-editable) or not. + * + * @param row the row number + * @param readOnly if true, the cells will be non-editable + */ + public void setRowReadOnly(final int row, final boolean readOnly) { + if ((row < 0) || (row > rows.size() - 1)) { + throw new IndexOutOfBoundsException("Row count is " + + rows.size() + ", requested index " + row); + } + for (Cell cell: rows.get(row).cells) { + cell.setReadOnly(readOnly); + } + } + + /** + * Set an entire column of cells read-only (non-editable) or not. + * + * @param column the column number + * @param readOnly if true, the cells will be non-editable + */ + public void setColumnReadOnly(final int column, final boolean readOnly) { + if ((column < 0) || (column > columns.size() - 1)) { + throw new IndexOutOfBoundsException("Column count is " + + columns.size() + ", requested index " + column); + } + for (Cell cell: columns.get(column).cells) { + cell.setReadOnly(readOnly); + } + } + + /** + * Set all borders across the entire table to Border.NONE. + */ + public void setBorderAllNone() { + topBorder = Border.NONE; + leftBorder = Border.NONE; + for (int i = 0; i < columns.size(); i++) { + columns.get(i).rightBorder = Border.NONE; + } + for (int i = 0; i < rows.size(); i++) { + rows.get(i).bottomBorder = Border.NONE; + rows.get(i).height = 1; + } + bottomRightCorner(); + } + + /** + * Set all borders across the entire table to Border.SINGLE. + */ + public void setBorderAllSingle() { + topBorder = Border.SINGLE; + leftBorder = Border.SINGLE; + for (int i = 0; i < columns.size(); i++) { + columns.get(i).rightBorder = Border.SINGLE; + } + for (int i = 0; i < rows.size(); i++) { + rows.get(i).bottomBorder = Border.SINGLE; + rows.get(i).height = 2; + } + alignGrid(); + } + + /** + * Set all borders around the selected cell to Border.NONE. + */ + public void setBorderCellNone() { + if (selectedRow == 0) { + topBorder = Border.NONE; + } + if (selectedColumn == 0) { + leftBorder = Border.NONE; + } + columns.get(selectedColumn).rightBorder = Border.NONE; + rows.get(selectedRow).bottomBorder = Border.NONE; + rows.get(selectedRow).height = 1; + bottomRightCorner(); + } + + /** + * Set all borders around the selected cell to Border.SINGLE. + */ + public void setBorderCellSingle() { + if (selectedRow == 0) { + topBorder = Border.SINGLE; + } + if (selectedColumn == 0) { + leftBorder = Border.SINGLE; + } + columns.get(selectedColumn).rightBorder = Border.SINGLE; + rows.get(selectedRow).bottomBorder = Border.SINGLE; + rows.get(selectedRow).height = 2; + alignGrid(); + } + + /** + * Set the column border to the right of the selected cell to + * Border.SINGLE. + */ + public void setBorderColumnRightSingle() { + columns.get(selectedColumn).rightBorder = Border.SINGLE; + alignGrid(); + } + + /** + * Set the column border to the right of the selected cell to + * Border.SINGLE. + */ + public void setBorderColumnLeftSingle() { + if (selectedColumn == 0) { + leftBorder = Border.SINGLE; + } else { + columns.get(selectedColumn - 1).rightBorder = Border.SINGLE; + } + alignGrid(); + } + + /** + * Set the row border above the selected cell to Border.SINGLE. + */ + public void setBorderRowAboveSingle() { + if (selectedRow == 0) { + topBorder = Border.SINGLE; + } else { + rows.get(selectedRow - 1).bottomBorder = Border.SINGLE; + rows.get(selectedRow - 1).height = 2; + } + alignGrid(); + } + + /** + * Set the row border below the selected cell to Border.SINGLE. + */ + public void setBorderRowBelowSingle() { + rows.get(selectedRow).bottomBorder = Border.SINGLE; + rows.get(selectedRow).height = 2; + alignGrid(); + } + + /** + * Set the row border below the selected cell to Border.DOUBLE. + */ + public void setBorderRowBelowDouble() { + rows.get(selectedRow).bottomBorder = Border.DOUBLE; + rows.get(selectedRow).height = 2; + alignGrid(); + } + + /** + * Set the row border below the selected cell to Border.THICK. + */ + public void setBorderRowBelowThick() { + rows.get(selectedRow).bottomBorder = Border.THICK; + rows.get(selectedRow).height = 2; + alignGrid(); + } + }