1 package be
.nikiroo
.jvcard
.tui
.panes
;
3 import java
.util
.LinkedList
;
6 import be
.nikiroo
.jvcard
.i18n
.Trans
.StringId
;
7 import be
.nikiroo
.jvcard
.tui
.Main
;
8 import be
.nikiroo
.jvcard
.tui
.StringUtils
;
9 import be
.nikiroo
.jvcard
.tui
.UiColors
;
10 import be
.nikiroo
.jvcard
.tui
.UiColors
.Element
;
12 import com
.googlecode
.lanterna
.TextColor
;
13 import com
.googlecode
.lanterna
.gui2
.AbstractListBox
.ListItemRenderer
;
14 import com
.googlecode
.lanterna
.gui2
.ActionListBox
;
15 import com
.googlecode
.lanterna
.gui2
.Direction
;
16 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
17 import com
.googlecode
.lanterna
.gui2
.TextGUIGraphics
;
19 abstract public class MainContentList
extends MainContent
implements Runnable
{
20 private ActionListBox lines
;
23 * This class represent a part of a text line to draw in this
24 * {@link MainContentList}.
29 public class TextPart
{
31 private Element element
;
33 public TextPart(String text
, Element element
) {
35 this.element
= element
;
38 public String
getText() {
42 public Element
getElement() {
46 public TextColor
getForegroundColor() {
48 return element
.getForegroundColor();
49 return Element
.DEFAULT
.getForegroundColor();
52 public TextColor
getBackgroundColor() {
54 return element
.getBackgroundColor();
55 return Element
.DEFAULT
.getBackgroundColor();
59 public MainContentList(final UiColors
.Element normalStyle
,
60 final UiColors
.Element selectedStyle
) {
61 super(Direction
.VERTICAL
);
63 lines
= new ActionListBox();
65 lines
.setListItemRenderer(new ListItemRenderer
<Runnable
, ActionListBox
>() {
67 * This is the main drawing method for a single list box item, it
68 * applies the current theme to setup the colors and then calls
69 * {@code getLabel(..)} and draws the result using the supplied
70 * {@code TextGUIGraphics}. The graphics object is created just for
71 * this item and is restricted so that it can only draw on the area
72 * this item is occupying. The top-left corner (0x0) should be the
73 * starting point when drawing the item.
76 * Graphics object to draw with
78 * List box we are drawing an item from
80 * Index of the item we are drawing
82 * The item we are drawing
84 * Will be set to {@code true} if the item is currently
85 * selected, otherwise {@code false}, but please notice
86 * what context 'selected' refers to here (see
87 * {@code setSelectedIndex})
89 * Will be set to {@code true} if the list box currently
90 * has input focus, otherwise {@code false}
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(index
,
99 lines
.getSize().getColumns() - 1, selected
, focused
);
102 for (TextPart part
: parts
) {
103 graphics
.setForegroundColor(part
.getForegroundColor());
104 graphics
.setBackgroundColor(part
.getBackgroundColor());
106 String label
= StringUtils
.sanitize(part
.getText(),
107 UiColors
.getInstance().isUnicode());
109 graphics
.putString(position
, 0, label
);
110 position
+= label
.length();
116 LinearLayout
.createLayoutData(LinearLayout
.Alignment
.Fill
));
120 * Add an item to this {@link MainContentList}.
125 public void addItem(String line
) {
126 lines
.addItem(line
, this);
130 * Clear all the items in this {@link MainContentList}
132 public void clearItems() {
137 * Get the index of the currently selected line.
141 public int getSelectedIndex() {
142 return lines
.getSelectedIndex();
146 * Change the index of the currently selected line.
151 public void setSelectedIndex(int index
) {
152 lines
.setSelectedIndex(index
);
156 * Return the default content separator for text fields.
158 * @return the separator
160 public String
getSeparator() {
161 return Main
.trans(StringId
.DEAULT_FIELD_SEPARATOR
);
171 public String
move(int x
, int y
) {
172 setSelectedIndex(getSelectedIndex() + x
);
178 public int getCount() {
179 return lines
.getItemCount();
183 * Return the representation of the selected line, in {@link TextPart}s.
188 * the max width of the line
190 * TRUE if the item is selected
192 * TRUE if the item is focused
194 * @return the text representation
196 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
198 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
200 if (selected
&& focused
) {
201 parts
.add(new TextPart("" + lines
.getItems().get(index
),
202 Element
.CONTACT_LINE_SELECTED
));
204 parts
.add(new TextPart("" + lines
.getItems().get(index
),
205 Element
.CONTACT_LINE
));