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 public 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 // width "-1" to reserve space for the optional vertical
98 List
<TextPart
> parts
= MainContentList
.this.getLabel(
99 index
, lines
.getSize().getColumns() - 1,
103 for (TextPart part
: parts
) {
104 graphics
.setForegroundColor(part
105 .getForegroundColor());
106 graphics
.setBackgroundColor(part
107 .getBackgroundColor());
108 String label
= part
.getText();
110 graphics
.putString(position
, 0, label
);
111 position
+= label
.length();
116 addComponent(lines
, LinearLayout
117 .createLayoutData(LinearLayout
.Alignment
.Fill
));
121 * Add an item to this {@link MainContentList}.
126 public void addItem(String line
) {
127 lines
.addItem(line
, this);
131 * Clear all the items in this {@link MainContentList}
133 public void clearItems() {
138 * Get the index of the currently selected line.
142 public int getSelectedIndex() {
143 return lines
.getSelectedIndex();
147 * Change the index of the currently selected line.
152 public void setSelectedIndex(int index
) {
153 lines
.setSelectedIndex(index
);
158 * Return the default content separator for text fields.
160 * @return the separator
162 public String
getSeparator() {
163 // we could use: " ", "┃", "│"...
174 public String
move(int x
, int y
) {
175 setSelectedIndex(getSelectedIndex() + x
);
181 public int getCount() {
182 return lines
.getItemCount();
186 * Return the representation of the selected line, in {@link TextPart}s.
191 * the max width of the line
193 * TRUE if the item is selected
195 * TRUE if the item is focused
197 * @return the text representation
199 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
201 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
203 if (selected
&& focused
) {
204 parts
.add(new TextPart("" + lines
.getItems().get(index
),
205 Element
.CONTACT_LINE_SELECTED
));
207 parts
.add(new TextPart("" + lines
.getItems().get(index
),
208 Element
.CONTACT_LINE
));