GUI: fix keyboard up/down in lib
[fanfix.git] / src / be / nikiroo / jexer / TTableCellRendererWidget.java
CommitLineData
8f34a795
NR
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 David "Niki" ROULET
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author David ROULET [niki@nikiroo.be]
27 * @version 1
28 */
29package be.nikiroo.jexer;
30
31import java.util.HashMap;
32import java.util.Map;
33
34import jexer.TLabel;
35import jexer.TWidget;
36
37/**
38 * A simple {@link TTableCellRenderer} that display the values within a
39 * {@link TLabel}.
40 * <p>
41 * It supports a few different modes, see
42 * {@link TTableSimpleTextCellRenderer.CellRendererMode}.
43 *
44 * @author niki
45 */
46public class TTableCellRendererWidget extends TTableCellRenderer {
47 private boolean rightAlign;
48 private Map<String, TWidget> widgets = new HashMap<String, TWidget>();
49
50 /**
51 * Create a new renderer for normal text mode.
52 */
53 public TTableCellRendererWidget() {
54 this(CellRendererMode.NORMAL);
55 }
56
57 /**
58 * Create a new renderer of the given mode.
59 *
60 * @param mode
61 * the renderer mode
62 */
63 public TTableCellRendererWidget(CellRendererMode mode) {
64 this(mode, false);
65 }
66
67 /**
68 * Create a new renderer of the given mode.
69 *
70 * @param mode
71 * the renderer mode, cannot be NULL
72 */
73 public TTableCellRendererWidget(CellRendererMode mode, boolean rightAlign) {
74 super(mode);
75
76 this.rightAlign = rightAlign;
77 }
78
79 @Override
80 public void renderTableCell(TTable table, Object value, int rowIndex,
81 int colIndex, int y) {
82
83 String wkey = "[Row " + y + " " + getMode() + "]";
84 TWidget widget = widgets.get(wkey);
85
86 TTableColumn tcol = table.getColumns().get(colIndex);
87 boolean isSelected = table.getSelectedRow() == rowIndex;
88 boolean hasFocus = table.isAbsoluteActive();
89 int width = tcol.getWidth();
90
91 int xOffset = getXOffset(table, colIndex);
92
93 if (widget != null
94 && !updateTableCellRendererComponent(widget, value, isSelected,
95 hasFocus, y, xOffset, width)) {
96 table.removeChild(widget);
97 widget = null;
98 }
99
100 if (widget == null) {
101 widget = getTableCellRendererComponent(table, value, isSelected,
102 hasFocus, y, xOffset, width);
103 }
104
105 widgets.put(wkey, widget);
106 }
107
108 /**
109 * Create a new {@link TWidget} to represent the given value.
110 *
111 * @param table
112 * the parent {@link TTable}
113 * @param value
114 * the value to represent
115 * @param isSelected
116 * TRUE if selected
117 * @param hasFocus
118 * TRUE if focused
119 * @param row
120 * the row to draw it at
121 * @param column
122 * the column to draw it at
123 * @param width
124 * the width of the control
125 *
126 * @return the widget
127 */
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);
132 }
133
134 /**
135 * Update the content of the widget if at all possible.
136 *
137 * @param component
138 * the component to update
139 * @param value
140 * the value to represent
141 * @param isSelected
142 * TRUE if selected
143 * @param hasFocus
144 * TRUE if focused
145 * @param row
146 * the row to draw it at
147 * @param column
148 * the column to draw it at
149 * @param width
150 * the width of the control
151 *
152 * @return TRUE if the operation was possible, FALSE if it failed
153 */
154 protected boolean updateTableCellRendererComponent(TWidget component,
155 Object value, boolean isSelected, boolean hasFocus, int row,
156 int column, int width) {
157
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);
163 widget.setX(column);
164 widget.setY(row);
165 return true;
166 }
167
168 return false;
169 }
170}