+
+ cellI = 0;
+ for (Cell cell: row.cells) {
+
+ for (int i = 0; i < columns.get(cellI).width; i++) {
+ if (row.bottomBorder == Border.SINGLE) {
+ writer.write("\u2500");
+ }
+ if (row.bottomBorder == Border.DOUBLE) {
+ writer.write("\u2550");
+ }
+ if (row.bottomBorder == Border.THICK) {
+ writer.write("\u2501");
+ }
+ }
+
+ if ((rowI < rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right tee.
+ writer.write("\u2524");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right tee (double).
+ writer.write("\u2561");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right tee (thick).
+ writer.write("\u2525");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right bottom corner.
+ writer.write("\u2518");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right bottom corner (double).
+ writer.write("\u255b");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI == columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit right bottom corner (thick).
+ writer.write("\u2519");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit intersection.
+ writer.write("\u253c");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit intersection (double).
+ writer.write("\u256a");
+ }
+ if ((rowI < rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit intersection (thick).
+ writer.write("\u253f");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.SINGLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit bottom tee.
+ writer.write("\u2534");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.DOUBLE)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit bottom tee (double).
+ writer.write("\u2567");
+ }
+ if ((rowI == rows.size() - 1)
+ && (cellI < columns.size() - 1)
+ && (row.bottomBorder == Border.THICK)
+ && (columns.get(cellI).rightBorder == Border.SINGLE)
+ ) {
+ // Emit bottom tee (thick).
+ writer.write("\u2537");
+ }
+
+ cellI++;
+ }
+
+ writer.write("\n");
+ rowI++;
+ }
+ } finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+ }
+
+ /**
+ * 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(idx).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(row);
+ 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 = row + 1;
+ if (idx < rows.size()) {
+ insertRowAt(idx);
+ activate(columns.get(selectedColumn).get(selectedRow));
+ return;
+ }
+
+ // row 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(row).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(idx).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(column);
+ 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 = column + 1;
+ if (idx < columns.size()) {
+ insertColumnAt(idx);
+ activate(columns.get(selectedColumn).get(selectedRow));
+ return;
+ }
+
+ // column 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(column).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);