2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.util
.ArrayList
;
32 import java
.util
.List
;
34 import jexer
.bits
.CellAttributes
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMenuEvent
;
37 import jexer
.event
.TResizeEvent
;
38 import jexer
.menu
.TMenu
;
39 import static jexer
.TKeypress
.*;
42 * TTableWidget is used to display and edit regular two-dimensional tables of
45 * This class was inspired by a TTable implementation originally developed by
46 * David "Niki" ROULET [niki@nikiroo.be], made available under MIT at
47 * https://github.com/nikiroo/jexer/tree/ttable_pull.
49 public class TTableWidget
extends TWidget
{
51 // ------------------------------------------------------------------------
52 // Constants --------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Available borders for cells.
65 * Single bar: \u2502 (vertical) and \u2500 (horizontal).
70 * Double bar: \u2551 (vertical) and \u2550 (horizontal).
75 * Thick bar: \u258C (vertical, left half block) and \u2580
76 * (horizontal, upper block).
81 // ------------------------------------------------------------------------
82 // Variables --------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 * The underlying data, organized as columns.
88 private ArrayList
<Column
> columns
= new ArrayList
<Column
>();
91 * The underlying data, organized as rows.
93 private ArrayList
<Row
> rows
= new ArrayList
<Row
>();
96 * The row in model corresponding to the top-left visible cell.
101 * The column in model corresponding to the top-left visible cell.
103 private int left
= 0;
106 * The row in model corresponding to the currently selected cell.
108 private int selectedRow
= 0;
111 * The column in model corresponding to the currently selected cell.
113 private int selectedColumn
= 0;
116 * If true, highlight the entire row of the currently-selected cell.
118 private boolean highlightRow
= true;
121 * If true, highlight the entire column of the currently-selected cell.
123 private boolean highlightColumn
= true;
126 * If true, show the row labels as the first column.
128 private boolean showRowLabels
= true;
131 * If true, show the column labels as the first row.
133 private boolean showColumnLabels
= true;
136 * Column represents a column of cells.
138 public class Column
{
143 private int width
= 8;
146 * The cells of this column.
148 private ArrayList
<Cell
> cells
= new ArrayList
<Cell
>();
153 private String label
= "";
156 * The border for this column.
158 private Border border
= Border
.NONE
;
161 * Constructor sets label to lettered column.
163 * @param col column number to use for this column. Column 0 will be
164 * "A", column 1 will be "B", column 26 will be "AA", and so on.
167 StringBuilder sb
= new StringBuilder();
169 sb
.append((char) ('A' + (col
% 26)));
175 label
= sb
.reverse().toString();
179 * Add an entry to this column.
181 * @param cell the cell to add
183 public void add(final Cell cell
) {
188 * Get an entry from this column.
190 * @param row the entry index to get
191 * @return the cell at row
193 public Cell
get(final int row
) {
194 return cells
.get(row
);
199 * Row represents a row of cells.
206 private int height
= 1;
209 * The cells of this row.
211 private ArrayList
<Cell
> cells
= new ArrayList
<Cell
>();
216 private String label
= "";
219 * The border for this row.
221 private Border border
= Border
.NONE
;
224 * Constructor sets label to numbered row.
226 * @param row row number to use for this row
229 label
= Integer
.toString(row
);
233 * Add an entry to this column.
235 * @param cell the cell to add
237 public void add(final Cell cell
) {
242 * Get an entry from this row.
244 * @param column the entry index to get
245 * @return the cell at column
247 public Cell
get(final int column
) {
248 return cells
.get(column
);
254 * Cell represents an editable cell in the table. Normally, navigation
255 * to a cell only highlights it; pressing Enter or F2 will switch to
258 public class Cell
extends TWidget
{
260 // --------------------------------------------------------------------
261 // Variables ----------------------------------------------------------
262 // --------------------------------------------------------------------
265 * The field containing the cell's data.
267 private TField field
;
270 * The column of this cell.
275 * The row of this cell.
280 * If true, the cell is being edited.
282 private boolean isEditing
= false;
285 * Text of field before editing.
287 private String fieldText
;
289 // --------------------------------------------------------------------
290 // Constructors -------------------------------------------------------
291 // --------------------------------------------------------------------
294 * Public constructor.
296 * @param parent parent widget
297 * @param x column relative to parent
298 * @param y row relative to parent
299 * @param width width of widget
300 * @param height height of widget
301 * @param column column index of this cell
302 * @param row row index of this cell
304 public Cell(final TTableWidget parent
, final int x
, final int y
,
305 final int width
, final int height
, final int column
,
308 super(parent
, x
, y
, width
, height
);
309 this.column
= column
;
312 field
= addField(0, 0, width
, false);
313 field
.setEnabled(false);
314 field
.setBackgroundChar(' ');
317 // --------------------------------------------------------------------
318 // Event handlers -----------------------------------------------------
319 // --------------------------------------------------------------------
324 * @param keypress keystroke event
327 public void onKeypress(final TKeypressEvent keypress
) {
328 // System.err.println("Cell onKeypress: " + keypress);
331 if (keypress
.equals(kbEsc
)) {
332 // ESC cancels the edit.
333 field
.setText(fieldText
);
335 field
.setEnabled(false);
338 if (keypress
.equals(kbEnter
)) {
339 // Enter ends editing.
340 fieldText
= field
.getText();
342 field
.setEnabled(false);
345 // Pass down to field.
346 super.onKeypress(keypress
);
349 if (keypress
.equals(kbEnter
) || keypress
.equals(kbF2
)) {
350 // Enter or F2 starts editing.
351 fieldText
= field
.getText();
353 field
.setEnabled(true);
359 // --------------------------------------------------------------------
360 // TWidget ------------------------------------------------------------
361 // --------------------------------------------------------------------
368 TTableWidget table
= (TTableWidget
) getParent();
370 if (isAbsoluteActive()) {
372 field
.setActiveColorKey("tfield.active");
373 field
.setInactiveColorKey("tfield.inactive");
375 field
.setActiveColorKey("ttable.selected");
376 field
.setInactiveColorKey("ttable.selected");
378 } else if (((table
.selectedColumn
== column
)
379 && ((table
.selectedRow
== row
)
380 || (table
.highlightColumn
== true)))
381 || ((table
.selectedRow
== row
)
382 && ((table
.selectedColumn
== column
)
383 || (table
.highlightRow
== true)))
385 field
.setActiveColorKey("ttable.active");
386 field
.setInactiveColorKey("ttable.active");
388 field
.setActiveColorKey("ttable.active");
389 field
.setInactiveColorKey("ttable.inactive");
395 // --------------------------------------------------------------------
396 // TTable.Cell --------------------------------------------------------
397 // --------------------------------------------------------------------
404 public final String
getText() {
405 return field
.getText();
411 * @param text the new field text
413 public void setText(final String text
) {
419 // ------------------------------------------------------------------------
420 // Constructors -----------------------------------------------------------
421 // ------------------------------------------------------------------------
424 * Public constructor.
426 * @param parent parent widget
427 * @param x column relative to parent
428 * @param y row relative to parent
429 * @param width width of widget
430 * @param height height of widget
432 public TTableWidget(final TWidget parent
, final int x
, final int y
,
433 final int width
, final int height
) {
435 super(parent
, x
, y
, width
, height
);
437 // Initialize the starting row and column.
438 rows
.add(new Row(0));
439 columns
.add(new Column(0));
441 // Place a grid of cells that fit in this space.
443 for (int i
= 0; i
< height
; i
+= rows
.get(0).height
) {
445 for (int j
= 0; j
< width
; j
+= columns
.get(0).width
) {
446 Cell cell
= new Cell(this, j
, i
, columns
.get(0).width
,
447 rows
.get(0).height
, column
, row
);
449 cell
.setText("" + row
+ " " + column
);
450 rows
.get(row
).add(cell
);
451 columns
.get(column
).add(cell
);
452 if ((i
== 0) && (j
+ columns
.get(0).width
< width
)) {
453 columns
.add(new Column(column
+ 1));
457 if (i
+ rows
.get(0).height
< height
) {
458 rows
.add(new Row(row
+ 1));
462 activate(columns
.get(selectedColumn
).get(selectedRow
));
467 // ------------------------------------------------------------------------
468 // Event handlers ---------------------------------------------------------
469 // ------------------------------------------------------------------------
474 * @param keypress keystroke event
477 public void onKeypress(final TKeypressEvent keypress
) {
478 if (keypress
.equals(kbTab
)
479 || keypress
.equals(kbShiftTab
)
481 // Squash tab and back-tab. They don't make sense in the TTable
486 // If editing, pass to that cell and do nothing else.
487 if (getSelectedCell().isEditing
) {
488 super.onKeypress(keypress
);
492 if (keypress
.equals(kbLeft
)) {
493 if (selectedColumn
> 0) {
496 activate(columns
.get(selectedColumn
).get(selectedRow
));
497 } else if (keypress
.equals(kbRight
)) {
498 if (selectedColumn
< columns
.size() - 1) {
501 activate(columns
.get(selectedColumn
).get(selectedRow
));
502 } else if (keypress
.equals(kbUp
)) {
503 if (selectedRow
> 0) {
506 activate(columns
.get(selectedColumn
).get(selectedRow
));
507 } else if (keypress
.equals(kbDown
)) {
508 if (selectedRow
< rows
.size() - 1) {
511 activate(columns
.get(selectedColumn
).get(selectedRow
));
512 } else if (keypress
.equals(kbHome
)) {
514 activate(columns
.get(selectedColumn
).get(selectedRow
));
515 } else if (keypress
.equals(kbEnd
)) {
516 selectedColumn
= columns
.size() - 1;
517 activate(columns
.get(selectedColumn
).get(selectedRow
));
518 } else if (keypress
.equals(kbPgUp
)) {
520 } else if (keypress
.equals(kbPgDn
)) {
522 } else if (keypress
.equals(kbCtrlHome
)) {
524 } else if (keypress
.equals(kbCtrlEnd
)) {
528 super.onKeypress(keypress
);
531 // We may have scrolled off screen. Reset positions as needed to
532 // make the newly selected cell visible.
537 * Handle widget resize events.
539 * @param event resize event
542 public void onResize(final TResizeEvent event
) {
543 super.onResize(event
);
549 * Handle posted menu events.
551 * @param menu menu event
554 public void onMenu(final TMenuEvent menu
) {
555 switch (menu
.getId()) {
556 case TMenu
.MID_TABLE_BORDER_NONE
:
557 case TMenu
.MID_TABLE_BORDER_ALL
:
558 case TMenu
.MID_TABLE_BORDER_RIGHT
:
559 case TMenu
.MID_TABLE_BORDER_LEFT
:
560 case TMenu
.MID_TABLE_BORDER_TOP
:
561 case TMenu
.MID_TABLE_BORDER_BOTTOM
:
562 case TMenu
.MID_TABLE_BORDER_DOUBLE_BOTTOM
:
563 case TMenu
.MID_TABLE_BORDER_THICK_BOTTOM
:
564 case TMenu
.MID_TABLE_DELETE_LEFT
:
565 case TMenu
.MID_TABLE_DELETE_UP
:
566 case TMenu
.MID_TABLE_DELETE_ROW
:
567 case TMenu
.MID_TABLE_DELETE_COLUMN
:
568 case TMenu
.MID_TABLE_INSERT_LEFT
:
569 case TMenu
.MID_TABLE_INSERT_RIGHT
:
570 case TMenu
.MID_TABLE_INSERT_ABOVE
:
571 case TMenu
.MID_TABLE_INSERT_BELOW
:
573 case TMenu
.MID_TABLE_COLUMN_NARROW
:
574 columns
.get(selectedColumn
).width
--;
575 for (Cell cell
: getSelectedColumn().cells
) {
576 cell
.setWidth(columns
.get(selectedColumn
).width
);
577 cell
.field
.setWidth(columns
.get(selectedColumn
).width
- 1);
579 for (int i
= selectedColumn
+ 1; i
< columns
.size(); i
++) {
580 for (Cell cell
: columns
.get(i
).cells
) {
581 cell
.setX(cell
.getX() - 1);
586 case TMenu
.MID_TABLE_COLUMN_WIDEN
:
587 columns
.get(selectedColumn
).width
++;
588 for (Cell cell
: getSelectedColumn().cells
) {
589 cell
.setWidth(columns
.get(selectedColumn
).width
);
590 cell
.field
.setWidth(columns
.get(selectedColumn
).width
- 1);
592 for (int i
= selectedColumn
+ 1; i
< columns
.size(); i
++) {
593 for (Cell cell
: columns
.get(i
).cells
) {
594 cell
.setX(cell
.getX() + 1);
599 case TMenu
.MID_TABLE_FILE_SAVE_CSV
:
600 case TMenu
.MID_TABLE_FILE_SAVE_TEXT
:
607 // ------------------------------------------------------------------------
608 // TWidget ----------------------------------------------------------------
609 // ------------------------------------------------------------------------
612 * Draw the table row/column headings, and borders.
616 CellAttributes headingColor
= getTheme().getColor("ttable.heading");
617 CellAttributes headingColorSelected
= getTheme().getColor("ttable.heading.selected");
618 CellAttributes borderColor
= getTheme().getColor("ttable.border");
621 if (showColumnLabels
== true) {
622 for (int i
= left
; i
< columns
.size(); i
++) {
623 if (columns
.get(i
).get(top
).isVisible() == false) {
626 putStringXY(columns
.get(i
).get(top
).getX(), 0,
627 String
.format(" %-6s ", columns
.get(i
).label
),
628 (i
== selectedColumn ? headingColorSelected
: headingColor
));
633 if (showRowLabels
== true) {
634 for (int i
= top
; i
< rows
.size(); i
++) {
635 if (rows
.get(i
).get(left
).isVisible() == false) {
638 putStringXY(0, rows
.get(i
).get(left
).getY(),
639 String
.format(" %-6s ", rows
.get(i
).label
),
640 (i
== selectedRow ? headingColorSelected
: headingColor
));
644 // Now draw the window borders.
648 // ------------------------------------------------------------------------
649 // TTable -----------------------------------------------------------------
650 // ------------------------------------------------------------------------
653 * Get the currently-selected cell.
655 * @return the selected cell
657 public Cell
getSelectedCell() {
658 assert (rows
.get(selectedRow
) != null);
659 assert (rows
.get(selectedRow
).get(selectedColumn
) != null);
660 assert (columns
.get(selectedColumn
) != null);
661 assert (columns
.get(selectedColumn
).get(selectedRow
) != null);
662 assert (rows
.get(selectedRow
).get(selectedColumn
) ==
663 columns
.get(selectedColumn
).get(selectedRow
));
665 return (columns
.get(selectedColumn
).get(selectedRow
));
669 * Get the currently-selected column.
671 * @return the selected column
673 public Column
getSelectedColumn() {
674 assert (selectedColumn
>= 0);
675 assert (columns
.size() > selectedColumn
);
676 assert (columns
.get(selectedColumn
) != null);
677 return columns
.get(selectedColumn
);
681 * Get the currently-selected row.
683 * @return the selected row
685 public Row
getSelectedRow() {
686 assert (selectedRow
>= 0);
687 assert (rows
.size() > selectedRow
);
688 assert (rows
.get(selectedRow
) != null);
689 return rows
.get(selectedRow
);
693 * Get the full horizontal width of this table.
695 * @return the width required to render the entire table
697 public int getMaximumWidth() {
699 if (showRowLabels
== true) {
700 // For now, all row labels are 8 cells wide. TODO: make this
704 for (Cell cell
: getSelectedRow().cells
) {
705 totalWidth
+= cell
.getWidth() + 1;
711 * Get the full vertical height of this table.
713 * @return the height required to render the entire table
715 public int getMaximumHeight() {
717 if (showColumnLabels
== true) {
718 // For now, all column labels are 1 cell tall. TODO: make this
722 for (Cell cell
: getSelectedColumn().cells
) {
723 totalHeight
+= cell
.getHeight();
724 // TODO: handle top/bottom borders.
730 * Align the grid so that the selected cell is fully visible.
732 private void alignGrid() {
735 * We start by assuming that all cells are visible, and then mark as
736 * invisible those that are outside the viewable area.
738 for (int x
= 0; x
< columns
.size(); x
++) {
739 for (int y
= 0; y
< rows
.size(); y
++) {
740 rows
.get(y
).cells
.get(x
).setVisible(true);
744 // Adjust X locations to be visible -----------------------------------
746 // Determine if we need to shift left or right.
747 int width
= getMaximumWidth();
749 if (showRowLabels
== true) {
750 // For now, all row labels are 8 cells wide. TODO: make this
754 Row row
= getSelectedRow();
755 Cell selectedColumnCell
= null;
756 for (int i
= 0; i
< row
.cells
.size(); i
++) {
757 if (i
== selectedColumn
) {
758 selectedColumnCell
= row
.cells
.get(i
);
761 leftCellX
+= row
.cells
.get(i
).getWidth() + 1;
763 // There should always be a selected column.
764 assert (selectedColumnCell
!= null);
766 int excessWidth
= leftCellX
+ selectedColumnCell
.getWidth() + 1 - getWidth();
767 if (excessWidth
> 0) {
768 leftCellX
-= excessWidth
;
771 if (showRowLabels
== true) {
779 * leftCellX now contains the basic left offset necessary to draw the
780 * cells such that the selected cell (column) is fully visible within
781 * this widget's given width. Or, if the widget is too narrow to
782 * display the full cell, leftCellX is 0 or 8.
784 * Now reset all of the X positions of the other cells so that the
785 * selected cell X is leftCellX.
787 for (int y
= 0; y
< rows
.size(); y
++) {
788 // All cells to the left of selected cell.
789 int newCellX
= leftCellX
;
790 left
= selectedColumn
;
791 for (int x
= selectedColumn
- 1; x
>= 0; x
--) {
792 newCellX
-= rows
.get(y
).cells
.get(x
).getWidth() + 1;
793 if (newCellX
- rows
.get(y
).cells
.get(x
).getWidth() + 1 >= 0) {
794 rows
.get(y
).cells
.get(x
).setVisible(true);
795 rows
.get(y
).cells
.get(x
).setX(newCellX
);
798 // This cell won't be visible.
799 rows
.get(y
).cells
.get(x
).setVisible(false);
804 rows
.get(y
).cells
.get(selectedColumn
).setX(leftCellX
);
805 assert (rows
.get(y
).cells
.get(selectedColumn
).isVisible());
807 // All cells to the right of selected cell.
808 newCellX
= leftCellX
+ selectedColumnCell
.getWidth() + 1;
809 for (int x
= selectedColumn
+ 1; x
< columns
.size(); x
++) {
810 if (newCellX
<= getWidth()) {
811 rows
.get(y
).cells
.get(x
).setVisible(true);
812 rows
.get(y
).cells
.get(x
).setX(newCellX
);
814 // This cell won't be visible.
815 rows
.get(y
).cells
.get(x
).setVisible(false);
817 newCellX
+= rows
.get(y
).cells
.get(x
).getWidth() + 1;
821 // Adjust Y locations to be visible -----------------------------------
822 // The same logic as above, but applied to the column Y.
824 // Determine if we need to shift up or down.
825 int height
= getMaximumHeight();
827 if (showColumnLabels
== true) {
828 // For now, all column labels are 1 cell high. TODO: make this
832 Column column
= getSelectedColumn();
833 Cell selectedRowCell
= null;
834 for (int i
= 0; i
< column
.cells
.size(); i
++) {
835 if (i
== selectedRow
) {
836 selectedRowCell
= column
.cells
.get(i
);
839 topCellY
+= column
.cells
.get(i
).getHeight();
840 // TODO: if a border is selected, add 1 to topCellY.
842 // There should always be a selected row.
843 assert (selectedRowCell
!= null);
845 int excessHeight
= topCellY
+ selectedRowCell
.getHeight() - getHeight() - 1;
846 if (showColumnLabels
== true) {
849 if (excessHeight
> 0) {
850 topCellY
-= excessHeight
;
853 if (showColumnLabels
== true) {
861 * topCellY now contains the basic top offset necessary to draw the
862 * cells such that the selected cell (row) is fully visible within
863 * this widget's given height. Or, if the widget is too short to
864 * display the full cell, topCellY is 0 or 1.
866 * Now reset all of the Y positions of the other cells so that the
867 * selected cell Y is topCellY.
869 for (int x
= 0; x
< columns
.size(); x
++) {
870 if (columns
.get(x
).cells
.get(0).isVisible() == false) {
871 // This entire column will not be visible, as determined by
872 // the width checks above. Do no further processing.
876 // All cells above the selected cell.
877 int newCellY
= topCellY
;
879 for (int y
= selectedRow
- 1; y
>= 0; y
--) {
880 newCellY
-= rows
.get(y
).cells
.get(x
).getHeight();
881 if (newCellY
>= (showColumnLabels
== true ?
1 : 0)) {
882 rows
.get(y
).cells
.get(x
).setVisible(true);
883 rows
.get(y
).cells
.get(x
).setY(newCellY
);
886 // This cell won't be visible.
887 rows
.get(y
).cells
.get(x
).setVisible(false);
892 columns
.get(x
).cells
.get(selectedRow
).setY(topCellY
);
893 assert (columns
.get(x
).cells
.get(selectedRow
).isVisible());
895 // All cells below the selected cell.
896 newCellY
= topCellY
+ selectedRowCell
.getHeight();
897 for (int y
= selectedRow
+ 1; y
< rows
.size(); y
++) {
898 if (newCellY
<= getHeight()) {
899 rows
.get(y
).cells
.get(x
).setVisible(true);
900 rows
.get(y
).cells
.get(x
).setY(newCellY
);
902 // This cell won't be visible.
903 rows
.get(y
).cells
.get(x
).setVisible(false);
905 newCellY
+= rows
.get(y
).cells
.get(x
).getHeight();