X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTTableWidget.java;h=c5d96ddb2991511fcf083b3ac4cad4d17c70a94b;hb=2e1384cc5018840e683bf29f705c5c08125325a6;hp=480e8289c2e8def0f1be139675bb963544857c60;hpb=1dac6b8d395e2bf3c1b58915f8f4f481d9e46793;p=fanfix.git diff --git a/src/jexer/TTableWidget.java b/src/jexer/TTableWidget.java index 480e828..c5d96dd 100644 --- a/src/jexer/TTableWidget.java +++ b/src/jexer/TTableWidget.java @@ -31,8 +31,10 @@ package jexer; import java.util.ArrayList; import java.util.List; +import jexer.bits.CellAttributes; import jexer.event.TKeypressEvent; import jexer.event.TMenuEvent; +import jexer.event.TResizeEvent; import jexer.menu.TMenu; import static jexer.TKeypress.*; @@ -113,12 +115,22 @@ public class TTableWidget extends TWidget { /** * If true, highlight the entire row of the currently-selected cell. */ - private boolean highlightRow = false; + private boolean highlightRow = true; /** * If true, highlight the entire column of the currently-selected cell. */ - private boolean highlightColumn = false; + private boolean highlightColumn = true; + + /** + * If true, show the row labels as the first column. + */ + private boolean showRowLabels = true; + + /** + * If true, show the column labels as the first row. + */ + private boolean showColumnLabels = true; /** * Column represents a column of cells. @@ -145,6 +157,24 @@ public class TTableWidget extends TWidget { */ private Border border = Border.NONE; + /** + * Constructor sets label to lettered column. + * + * @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. + */ + Column(int col) { + StringBuilder sb = new StringBuilder(); + for (;;) { + sb.append((char) ('A' + (col % 26))); + if (col < 26) { + break; + } + col /= 26; + } + label = sb.reverse().toString(); + } + /** * Add an entry to this column. * @@ -190,6 +220,15 @@ public class TTableWidget extends TWidget { */ private Border border = Border.NONE; + /** + * Constructor sets label to numbered row. + * + * @param row row number to use for this row + */ + Row(final int row) { + label = Integer.toString(row); + } + /** * Add an entry to this column. * @@ -270,10 +309,8 @@ public class TTableWidget extends TWidget { this.column = column; this.row = row; - field = addField(0, 0, width - 1, false); + field = addField(0, 0, width, false); field.setEnabled(false); - field.setActiveColorKey("ttable.active"); - field.setInactiveColorKey("ttable.inactive"); field.setBackgroundChar(' '); } @@ -330,25 +367,26 @@ public class TTableWidget extends TWidget { public void draw() { TTableWidget table = (TTableWidget) getParent(); - field.setActiveColorKey("ttable.active"); - field.setInactiveColorKey("ttable.inactive"); - if (isAbsoluteActive()) { - if (table.selectedColumn == column) { - if ((table.selectedRow == row) - || (table.highlightColumn == true) - ) { - field.setActiveColorKey("ttable.active"); - field.setInactiveColorKey("ttable.active"); - } - } else if (table.selectedRow == row) { - if ((table.selectedColumn == column) - || (table.highlightRow == true) - ) { - field.setActiveColorKey("ttable.active"); - field.setInactiveColorKey("ttable.active"); - } + if (isEditing) { + field.setActiveColorKey("tfield.active"); + field.setInactiveColorKey("tfield.inactive"); + } else { + field.setActiveColorKey("ttable.selected"); + field.setInactiveColorKey("ttable.selected"); } + } else if (((table.selectedColumn == column) + && ((table.selectedRow == row) + || (table.highlightColumn == true))) + || ((table.selectedRow == row) + && ((table.selectedColumn == column) + || (table.highlightRow == true))) + ) { + field.setActiveColorKey("ttable.active"); + field.setInactiveColorKey("ttable.active"); + } else { + field.setActiveColorKey("ttable.active"); + field.setInactiveColorKey("ttable.inactive"); } super.draw(); @@ -397,8 +435,8 @@ public class TTableWidget extends TWidget { super(parent, x, y, width, height); // Initialize the starting row and column. - rows.add(new Row()); - columns.add(new Column()); + rows.add(new Row(0)); + columns.add(new Column(0)); // Place a grid of cells that fit in this space. int row = 0; @@ -412,16 +450,18 @@ public class TTableWidget extends TWidget { rows.get(row).add(cell); columns.get(column).add(cell); if ((i == 0) && (j + columns.get(0).width < width)) { - columns.add(new Column()); + columns.add(new Column(column + 1)); } column++; } if (i + rows.get(0).height < height) { - rows.add(new Row()); + rows.add(new Row(row + 1)); } row++; } activate(columns.get(selectedColumn).get(selectedRow)); + + alignGrid(); } // ------------------------------------------------------------------------ @@ -442,7 +482,8 @@ public class TTableWidget extends TWidget { // grid context. return; } - + + // If editing, pass to that cell and do nothing else. if (getSelectedCell().isEditing) { super.onKeypress(keypress); return; @@ -486,6 +527,22 @@ public class TTableWidget extends TWidget { // Pass to the Cell. super.onKeypress(keypress); } + + // We may have scrolled off screen. Reset positions as needed to + // make the newly selected cell visible. + alignGrid(); + } + + /** + * Handle widget resize events. + * + * @param event resize event + */ + @Override + public void onResize(final TResizeEvent event) { + super.onResize(event); + + alignGrid(); } /** @@ -519,6 +576,12 @@ public class TTableWidget extends TWidget { cell.setWidth(columns.get(selectedColumn).width); cell.field.setWidth(columns.get(selectedColumn).width - 1); } + for (int i = selectedColumn + 1; i < columns.size(); i++) { + for (Cell cell: columns.get(i).cells) { + cell.setX(cell.getX() - 1); + } + } + alignGrid(); break; case TMenu.MID_TABLE_COLUMN_WIDEN: columns.get(selectedColumn).width++; @@ -526,6 +589,12 @@ public class TTableWidget extends TWidget { cell.setWidth(columns.get(selectedColumn).width); cell.field.setWidth(columns.get(selectedColumn).width - 1); } + for (int i = selectedColumn + 1; i < columns.size(); i++) { + for (Cell cell: columns.get(i).cells) { + cell.setX(cell.getX() + 1); + } + } + alignGrid(); break; case TMenu.MID_TABLE_FILE_SAVE_CSV: case TMenu.MID_TABLE_FILE_SAVE_TEXT: @@ -539,6 +608,43 @@ public class TTableWidget extends TWidget { // TWidget ---------------------------------------------------------------- // ------------------------------------------------------------------------ + /** + * Draw the table row/column headings, and borders. + */ + @Override + public void draw() { + CellAttributes headingColor = getTheme().getColor("ttable.heading"); + CellAttributes headingColorSelected = getTheme().getColor("ttable.heading.selected"); + CellAttributes borderColor = getTheme().getColor("ttable.border"); + + // Column labels. + if (showColumnLabels == true) { + for (int i = left; i < columns.size(); i++) { + if (columns.get(i).get(top).isVisible() == false) { + break; + } + putStringXY(columns.get(i).get(top).getX(), 0, + String.format(" %-6s ", columns.get(i).label), + (i == selectedColumn ? headingColorSelected : headingColor)); + } + } + + // Row labels. + if (showRowLabels == true) { + for (int i = top; i < rows.size(); i++) { + if (rows.get(i).get(left).isVisible() == false) { + break; + } + putStringXY(0, rows.get(i).get(left).getY(), + String.format(" %-6s ", rows.get(i).label), + (i == selectedRow ? headingColorSelected : headingColor)); + } + } + + // Now draw the window borders. + super.draw(); + } + // ------------------------------------------------------------------------ // TTable ----------------------------------------------------------------- // ------------------------------------------------------------------------ @@ -583,4 +689,223 @@ public class TTableWidget extends TWidget { return rows.get(selectedRow); } + /** + * Get the full horizontal width of this table. + * + * @return the width required to render the entire table + */ + public int getMaximumWidth() { + int totalWidth = 0; + if (showRowLabels == true) { + // For now, all row labels are 8 cells wide. TODO: make this + // adjustable. + totalWidth += 8; + } + for (Cell cell: getSelectedRow().cells) { + totalWidth += cell.getWidth() + 1; + } + return totalWidth; + } + + /** + * Get the full vertical height of this table. + * + * @return the height required to render the entire table + */ + public int getMaximumHeight() { + int totalHeight = 0; + if (showColumnLabels == true) { + // For now, all column labels are 1 cell tall. TODO: make this + // adjustable. + totalHeight += 1; + } + for (Cell cell: getSelectedColumn().cells) { + totalHeight += cell.getHeight(); + // TODO: handle top/bottom borders. + } + return totalHeight; + } + + /** + * Align the grid so that the selected cell is fully visible. + */ + private void alignGrid() { + + /* + * We start by assuming that all cells are visible, and then mark as + * invisible those that are outside the viewable area. + */ + for (int x = 0; x < columns.size(); x++) { + for (int y = 0; y < rows.size(); y++) { + rows.get(y).cells.get(x).setVisible(true); + } + } + + // 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 + // adjustable. + leftCellX += 8; + } + Row row = getSelectedRow(); + Cell selectedColumnCell = null; + for (int i = 0; i < row.cells.size(); i++) { + if (i == selectedColumn) { + selectedColumnCell = row.cells.get(i); + break; + } + leftCellX += row.cells.get(i).getWidth() + 1; + } + // There should always be a selected column. + assert (selectedColumnCell != null); + + int excessWidth = leftCellX + selectedColumnCell.getWidth() + 1 - getWidth(); + if (excessWidth > 0) { + leftCellX -= excessWidth; + } + if (leftCellX < 0) { + if (showRowLabels == true) { + leftCellX = 8; + } else { + leftCellX = 0; + } + } + + /* + * leftCellX now contains the basic left offset necessary to draw the + * cells such that the selected cell (column) is fully visible within + * this widget's given width. Or, if the widget is too narrow to + * display the full cell, leftCellX is 0 or 8. + * + * Now reset all of the X positions of the other cells so that the + * selected cell X is leftCellX. + */ + for (int y = 0; y < rows.size(); y++) { + // All cells to the left of selected cell. + int newCellX = leftCellX; + left = selectedColumn; + for (int x = selectedColumn - 1; x >= 0; x--) { + newCellX -= rows.get(y).cells.get(x).getWidth() + 1; + if (newCellX - rows.get(y).cells.get(x).getWidth() + 1 >= 0) { + rows.get(y).cells.get(x).setVisible(true); + rows.get(y).cells.get(x).setX(newCellX); + left--; + } else { + // This cell won't be visible. + rows.get(y).cells.get(x).setVisible(false); + } + } + + // Selected cell. + rows.get(y).cells.get(selectedColumn).setX(leftCellX); + assert (rows.get(y).cells.get(selectedColumn).isVisible()); + + // All cells to the right of selected cell. + newCellX = leftCellX + selectedColumnCell.getWidth() + 1; + for (int x = selectedColumn + 1; x < columns.size(); x++) { + if (newCellX <= getWidth()) { + rows.get(y).cells.get(x).setVisible(true); + rows.get(y).cells.get(x).setX(newCellX); + } else { + // This cell won't be visible. + rows.get(y).cells.get(x).setVisible(false); + } + newCellX += rows.get(y).cells.get(x).getWidth() + 1; + } + } + + // Adjust Y locations to be visible ----------------------------------- + // 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 + // adjustable. + topCellY += 1; + } + Column column = getSelectedColumn(); + Cell selectedRowCell = null; + for (int i = 0; i < column.cells.size(); i++) { + if (i == selectedRow) { + selectedRowCell = column.cells.get(i); + break; + } + topCellY += column.cells.get(i).getHeight(); + // TODO: if a border is selected, add 1 to topCellY. + } + // There should always be a selected row. + assert (selectedRowCell != null); + + int excessHeight = topCellY + selectedRowCell.getHeight() - getHeight() - 1; + if (showColumnLabels == true) { + excessHeight += 1; + } + if (excessHeight > 0) { + topCellY -= excessHeight; + } + if (topCellY < 0) { + if (showColumnLabels == true) { + topCellY = 1; + } else { + topCellY = 0; + } + } + + /* + * topCellY now contains the basic top offset necessary to draw the + * cells such that the selected cell (row) is fully visible within + * this widget's given height. Or, if the widget is too short to + * display the full cell, topCellY is 0 or 1. + * + * Now reset all of the Y positions of the other cells so that the + * 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; + } + + // All cells above the selected cell. + int newCellY = topCellY; + top = selectedRow; + for (int y = selectedRow - 1; y >= 0; y--) { + newCellY -= rows.get(y).cells.get(x).getHeight(); + if (newCellY >= (showColumnLabels == true ? 1 : 0)) { + rows.get(y).cells.get(x).setVisible(true); + rows.get(y).cells.get(x).setY(newCellY); + top--; + } else { + // This cell won't be visible. + rows.get(y).cells.get(x).setVisible(false); + } + } + + // 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(); + for (int y = selectedRow + 1; y < rows.size(); y++) { + if (newCellY <= getHeight()) { + rows.get(y).cells.get(x).setVisible(true); + rows.get(y).cells.get(x).setY(newCellY); + } else { + // This cell won't be visible. + rows.get(y).cells.get(x).setVisible(false); + } + newCellY += rows.get(y).cells.get(x).getHeight(); + } + } + + } + }