Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package com.googlecode.lanterna.gui2.table; |
2 | ||
3 | import com.googlecode.lanterna.TerminalTextUtils; | |
4 | import com.googlecode.lanterna.TerminalSize; | |
5 | import com.googlecode.lanterna.graphics.ThemeDefinition; | |
6 | import com.googlecode.lanterna.gui2.TextGUIGraphics; | |
7 | ||
8 | /** | |
9 | * Default implementation of {@code TableCellRenderer} | |
10 | * @param <V> Type of data stored in each table cell | |
11 | * @author Martin | |
12 | */ | |
13 | public class DefaultTableCellRenderer<V> implements TableCellRenderer<V> { | |
14 | @Override | |
15 | public TerminalSize getPreferredSize(Table<V> table, V cell, int columnIndex, int rowIndex) { | |
16 | String[] lines = getContent(cell); | |
17 | int maxWidth = 0; | |
18 | for(String line: lines) { | |
19 | int length = TerminalTextUtils.getColumnWidth(line); | |
20 | if(maxWidth < length) { | |
21 | maxWidth = length; | |
22 | } | |
23 | } | |
24 | return new TerminalSize(maxWidth, lines.length); | |
25 | } | |
26 | ||
27 | @Override | |
28 | public void drawCell(Table<V> table, V cell, int columnIndex, int rowIndex, TextGUIGraphics textGUIGraphics) { | |
29 | ThemeDefinition themeDefinition = textGUIGraphics.getThemeDefinition(Table.class); | |
30 | if((table.getSelectedColumn() == columnIndex && table.getSelectedRow() == rowIndex) || | |
31 | (table.getSelectedRow() == rowIndex && !table.isCellSelection())) { | |
32 | if(table.isFocused()) { | |
33 | textGUIGraphics.applyThemeStyle(themeDefinition.getActive()); | |
34 | } | |
35 | else { | |
36 | textGUIGraphics.applyThemeStyle(themeDefinition.getSelected()); | |
37 | } | |
38 | textGUIGraphics.fill(' '); //Make sure to fill the whole cell first | |
39 | } | |
40 | else { | |
41 | textGUIGraphics.applyThemeStyle(themeDefinition.getNormal()); | |
42 | } | |
43 | String[] lines = getContent(cell); | |
44 | int rowCount = 0; | |
45 | for(String line: lines) { | |
46 | textGUIGraphics.putString(0, rowCount++, line); | |
47 | } | |
48 | } | |
49 | ||
50 | private String[] getContent(V cell) { | |
51 | String[] lines; | |
52 | if(cell == null) { | |
53 | lines = new String[] { "" }; | |
54 | } | |
55 | else { | |
56 | lines = cell.toString().split("\r?\n"); | |
57 | } | |
58 | return lines; | |
59 | } | |
60 | } |