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 java
.util
.HashMap
;
38 * A simple {@link TTableCellRenderer} that display the values within a
41 * It supports a few different modes, see
42 * {@link TTableSimpleTextCellRenderer.CellRendererMode}.
46 public class TTableCellRendererWidget
extends TTableCellRenderer
{
47 private boolean rightAlign
;
48 private Map
<String
, TWidget
> widgets
= new HashMap
<String
, TWidget
>();
51 * Create a new renderer for normal text mode.
53 public TTableCellRendererWidget() {
54 this(CellRendererMode
.NORMAL
);
58 * Create a new renderer of the given mode.
63 public TTableCellRendererWidget(CellRendererMode mode
) {
68 * Create a new renderer of the given mode.
71 * the renderer mode, cannot be NULL
73 public TTableCellRendererWidget(CellRendererMode mode
, boolean rightAlign
) {
76 this.rightAlign
= rightAlign
;
80 public void renderTableCell(TTable table
, Object value
, int rowIndex
,
81 int colIndex
, int y
) {
83 String wkey
= "[Row " + y
+ " " + getMode() + "]";
84 TWidget widget
= widgets
.get(wkey
);
86 TTableColumn tcol
= table
.getColumns().get(colIndex
);
87 boolean isSelected
= table
.getSelectedRow() == rowIndex
;
88 boolean hasFocus
= table
.isAbsoluteActive();
89 int width
= tcol
.getWidth();
91 int xOffset
= getXOffset(table
, colIndex
);
94 && !updateTableCellRendererComponent(widget
, value
, isSelected
,
95 hasFocus
, y
, xOffset
, width
)) {
96 table
.removeChild(widget
);
100 if (widget
== null) {
101 widget
= getTableCellRendererComponent(table
, value
, isSelected
,
102 hasFocus
, y
, xOffset
, width
);
105 widgets
.put(wkey
, widget
);
109 * Create a new {@link TWidget} to represent the given value.
112 * the parent {@link TTable}
114 * the value to represent
120 * the row to draw it at
122 * the column to draw it at
124 * the width of the control
128 protected TWidget
getTableCellRendererComponent(TTable table
, Object value
,
129 boolean isSelected
, boolean hasFocus
, int row
, int column
, int width
) {
130 return new TLabel(table
, asText(value
, width
, rightAlign
), column
, row
,
131 getColorKey(isSelected
, hasFocus
), false);
135 * Update the content of the widget if at all possible.
138 * the component to update
140 * the value to represent
146 * the row to draw it at
148 * the column to draw it at
150 * the width of the control
152 * @return TRUE if the operation was possible, FALSE if it failed
154 protected boolean updateTableCellRendererComponent(TWidget component
,
155 Object value
, boolean isSelected
, boolean hasFocus
, int row
,
156 int column
, int width
) {
158 if (component
instanceof TLabel
) {
159 TLabel widget
= (TLabel
) component
;
160 widget
.setLabel(asText(value
, width
, rightAlign
));
161 widget
.setColorKey(getColorKey(isSelected
, hasFocus
));
162 widget
.setWidth(width
);