1 package be
.nikiroo
.jvcard
.tui
.panes
;
3 import be
.nikiroo
.jvcard
.tui
.UiColors
;
5 import com
.googlecode
.lanterna
.gui2
.ActionListBox
;
6 import com
.googlecode
.lanterna
.gui2
.Direction
;
7 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
8 import com
.googlecode
.lanterna
.gui2
.TextGUIGraphics
;
9 import com
.googlecode
.lanterna
.gui2
.AbstractListBox
.ListItemRenderer
;
11 abstract public class MainContentList
extends MainContent
implements Runnable
{
12 private ActionListBox lines
;
14 public MainContentList(final UiColors
.Element normalStyle
,
15 final UiColors
.Element selectedStyle
) {
16 super(Direction
.VERTICAL
);
18 lines
= new ActionListBox();
21 .setListItemRenderer(new ListItemRenderer
<Runnable
, ActionListBox
>() {
23 * This is the main drawing method for a single list box
24 * item, it applies the current theme to setup the colors
25 * and then calls {@code getLabel(..)} and draws the result
26 * using the supplied {@code TextGUIGraphics}. The graphics
27 * object is created just for this item and is restricted so
28 * that it can only draw on the area this item is occupying.
29 * The top-left corner (0x0) should be the starting point
30 * when drawing the item.
33 * Graphics object to draw with
35 * List box we are drawing an item from
37 * Index of the item we are drawing
39 * The item we are drawing
41 * Will be set to {@code true} if the item is
42 * currently selected, otherwise {@code false},
43 * but please notice what context 'selected'
44 * refers to here (see {@code setSelectedIndex})
46 * Will be set to {@code true} if the list box
47 * currently has input focus, otherwise {@code
50 public void drawItem(TextGUIGraphics graphics
,
51 ActionListBox listBox
, int index
, Runnable item
,
52 boolean selected
, boolean focused
) {
54 if (selected
&& focused
) {
55 graphics
.setForegroundColor(selectedStyle
56 .getForegroundColor());
57 graphics
.setBackgroundColor(selectedStyle
58 .getBackgroundColor());
60 graphics
.setForegroundColor(normalStyle
61 .getForegroundColor());
62 graphics
.setBackgroundColor(normalStyle
63 .getBackgroundColor());
67 // String label = getLabel(listBox, index, item);
68 // label = TerminalTextUtils.fitString(label,
69 // graphics.getSize().getColumns());
71 // TODO: why +5 ?? padding problem?
72 String label
= MainContentList
.this.getLabel(index
,
73 lines
.getSize().getColumns() + 5);
74 graphics
.putString(0, 0, label
);
78 addComponent(lines
, LinearLayout
79 .createLayoutData(LinearLayout
.Alignment
.Fill
));
83 * Add an item to this {@link MainContentList}.
88 public void addItem(String line
) {
89 lines
.addItem(line
, this);
93 * Clear all the items in this {@link MainContentList}
95 public void clearItems() {
100 * Get the index of the currently selected line.
104 public int getSelectedIndex() {
105 return lines
.getSelectedIndex();
109 * Change the index of the currently selected line.
114 public void setSelectedIndex(int index
) {
115 lines
.setSelectedIndex(index
);
125 public String
move(int x
, int y
) {
126 setSelectedIndex(getSelectedIndex() + x
);
132 * Return the text representation of the selected line.
137 * the max width of the line
139 * @return the text representation
141 protected String
getLabel(int index
, int width
) {
142 return "" + lines
.getItems().get(index
);