2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 David "Niki" ROULET
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 David ROULET [niki@nikiroo.be]
29 package be
.nikiroo
.jexer
;
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.ColorTheme
;
35 * A {@link TTable} cell renderer allows you to customize the way a single cell
36 * will be displayed on screen.
38 * It can be used in a {@link TTable} for the haeders or the separators or in a
39 * {@link TTableColumn} for the data.
43 abstract public class TTableCellRenderer
{
44 private CellRendererMode mode
;
47 * The simple renderer mode.
51 public enum CellRendererMode
{
52 /** Normal text mode */
54 /** Only display a separator */
56 /** Header text mode */
58 /** Both HEADER and SEPARATOR at once */
62 * This mode represents a separator.
64 * @return TRUE for separators
66 public boolean isSeparator() {
67 return this == SEPARATOR
|| this == HEADER_SEPARATOR
;
71 * This mode represents a header.
73 * @return TRUE for headers
75 public boolean isHeader() {
76 return this == HEADER
|| this == HEADER_SEPARATOR
;
81 * Create a new renderer of the given mode.
84 * the renderer mode, cannot be NULL
86 public TTableCellRenderer(CellRendererMode mode
) {
88 throw new IllegalArgumentException(
89 "Cannot create a renderer of type NULL");
96 * Render the given value.
99 * the table to write on
103 * the row index in the table
105 * the column index in the table
107 * the Y position at which to draw this row
109 abstract public void renderTableCell(TTable table
, Object value
,
110 int rowIndex
, int colIndex
, int y
);
113 * The mode of this {@link TTableCellRenderer}.
117 public CellRendererMode
getMode() {
122 * The cell attributes to use for the given state.
125 * the color theme to use
127 * TRUE if the cell is selected
129 * TRUE if the cell has focus
131 * @return the attributes
133 public CellAttributes
getCellAttributes(ColorTheme theme
,
134 boolean isSelected
, boolean hasFocus
) {
135 return theme
.getColor(getColorKey(isSelected
, hasFocus
));
139 * Measure the width of the value.
142 * the value to measure
146 public int getWidthOf(Object value
) {
147 if (getMode().isSeparator()) {
148 return asText(null, 0, false).length();
150 return ("" + value
).length();
154 * The colour to use for the given state, specified as a Jexer colour key.
157 * TRUE if the cell is selected
159 * TRUE if the cell has focus
161 * @return the colour key
163 protected String
getColorKey(boolean isSelected
, boolean hasFocus
) {
164 if (mode
.isHeader()) {
168 String colorKey
= "tlist";
170 colorKey
+= ".selected";
171 } else if (!hasFocus
) {
172 colorKey
+= ".inactive";
179 * Return the X offset to use to draw a column at the given index.
182 * the table to draw into
188 protected int getXOffset(TTable table
, int colIndex
) {
189 int xOffset
= -table
.getHorizontalValue();
190 for (int i
= 0; i
<= colIndex
; i
++) {
191 TTableColumn tcol
= table
.getColumns().get(i
);
192 xOffset
+= tcol
.getWidth();
194 xOffset
+= table
.getSeparatorRenderer().getWidthOf(null);
198 TTableColumn tcol
= table
.getColumns().get(colIndex
);
199 if (!getMode().isSeparator()) {
200 xOffset
-= tcol
.getWidth();
207 * Return the text to use (usually the converted-to-text value, except for
208 * the special separator mode).
211 * the value to get the text of
213 * the width we should tale
215 * the text to the right
217 * @return the {@link String} to display
219 protected String
asText(Object value
, int width
, boolean rightAlign
) {
220 if (getMode().isSeparator()) {
221 // some nice characters for the separator: ┃ │ |
232 format
= "%-" + width
+ "s";
235 format
= "%" + width
+ "s";
238 return String
.format(format
, value
);