1 package be
.nikiroo
.jvcard
.tui
.panes
;
3 import java
.util
.LinkedList
;
6 import be
.nikiroo
.jvcard
.tui
.UiColors
;
7 import be
.nikiroo
.jvcard
.tui
.UiColors
.Element
;
9 import com
.googlecode
.lanterna
.TextColor
;
10 import com
.googlecode
.lanterna
.gui2
.ActionListBox
;
11 import com
.googlecode
.lanterna
.gui2
.Direction
;
12 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
13 import com
.googlecode
.lanterna
.gui2
.TextGUIGraphics
;
14 import com
.googlecode
.lanterna
.gui2
.AbstractListBox
.ListItemRenderer
;
16 abstract public class MainContentList
extends MainContent
implements Runnable
{
17 private ActionListBox lines
;
20 * This class represent a part of a text line to draw in this
21 * {@link MainContentList}.
26 protected class TextPart
{
28 private Element element
;
30 public TextPart(String text
, Element element
) {
32 this.element
= element
;
35 public String
getText() {
39 public Element
getElement() {
43 public TextColor
getForegroundColor() {
45 return element
.getForegroundColor();
46 return Element
.DEFAULT
.getForegroundColor();
49 public TextColor
getBackgroundColor() {
51 return element
.getBackgroundColor();
52 return Element
.DEFAULT
.getBackgroundColor();
56 public MainContentList(final UiColors
.Element normalStyle
,
57 final UiColors
.Element selectedStyle
) {
58 super(Direction
.VERTICAL
);
60 lines
= new ActionListBox();
63 .setListItemRenderer(new ListItemRenderer
<Runnable
, ActionListBox
>() {
65 * This is the main drawing method for a single list box
66 * item, it applies the current theme to setup the colors
67 * and then calls {@code getLabel(..)} and draws the result
68 * using the supplied {@code TextGUIGraphics}. The graphics
69 * object is created just for this item and is restricted so
70 * that it can only draw on the area this item is occupying.
71 * The top-left corner (0x0) should be the starting point
72 * when drawing the item.
75 * Graphics object to draw with
77 * List box we are drawing an item from
79 * Index of the item we are drawing
81 * The item we are drawing
83 * Will be set to {@code true} if the item is
84 * currently selected, otherwise {@code false},
85 * but please notice what context 'selected'
86 * refers to here (see {@code setSelectedIndex})
88 * Will be set to {@code true} if the list box
89 * currently has input focus, otherwise {@code
92 public void drawItem(TextGUIGraphics graphics
,
93 ActionListBox listBox
, int index
, Runnable item
,
94 boolean selected
, boolean focused
) {
96 // TODO: why +5 ?? padding problem?
97 List
<TextPart
> parts
= MainContentList
.this.getLabel(
98 index
, lines
.getSize().getColumns() + 5,
102 for (TextPart part
: parts
) {
103 graphics
.setForegroundColor(part
104 .getForegroundColor());
105 graphics
.setBackgroundColor(part
106 .getBackgroundColor());
107 String label
= part
.getText();
108 graphics
.putString(position
, 0, label
);
109 position
+= label
.length();
114 addComponent(lines
, LinearLayout
115 .createLayoutData(LinearLayout
.Alignment
.Fill
));
119 * Add an item to this {@link MainContentList}.
124 public void addItem(String line
) {
125 lines
.addItem(line
, this);
129 * Clear all the items in this {@link MainContentList}
131 public void clearItems() {
136 * Get the index of the currently selected line.
140 public int getSelectedIndex() {
141 return lines
.getSelectedIndex();
145 * Change the index of the currently selected line.
150 public void setSelectedIndex(int index
) {
151 lines
.setSelectedIndex(index
);
161 public String
move(int x
, int y
) {
162 setSelectedIndex(getSelectedIndex() + x
);
168 * Return the representation of the selected line, in {@link TextPart}s.
173 * the max width of the line
175 * TRUE if the item is selected
177 * TRUE if the item is focused
179 * @return the text representation
181 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
183 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
185 if (selected
&& focused
) {
186 parts
.add(new TextPart("" + lines
.getItems().get(index
),
187 Element
.CONTACT_LINE_SELECTED
));
189 parts
.add(new TextPart("" + lines
.getItems().get(index
),
190 Element
.CONTACT_LINE
));