cac03e2426c615b9e61b2420d049408ed4d15f67
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
.StringUtils
;
8 import be
.nikiroo
.jvcard
.tui
.UiColors
;
9 import be
.nikiroo
.jvcard
.tui
.UiColors
.Element
;
11 import com
.googlecode
.lanterna
.TextColor
;
12 import com
.googlecode
.lanterna
.gui2
.AbstractListBox
.ListItemRenderer
;
13 import com
.googlecode
.lanterna
.gui2
.ActionListBox
;
14 import com
.googlecode
.lanterna
.gui2
.Direction
;
15 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
16 import com
.googlecode
.lanterna
.gui2
.TextGUIGraphics
;
18 abstract public class MainContentList
extends MainContent
implements Runnable
{
19 private ActionListBox lines
;
22 * This class represent a part of a text line to draw in this
23 * {@link MainContentList}.
28 public class TextPart
{
30 private Element element
;
32 public TextPart(String text
, Element element
) {
34 this.element
= element
;
37 public String
getText() {
41 public Element
getElement() {
45 public TextColor
getForegroundColor() {
47 return element
.getForegroundColor();
48 return Element
.DEFAULT
.getForegroundColor();
51 public TextColor
getBackgroundColor() {
53 return element
.getBackgroundColor();
54 return Element
.DEFAULT
.getBackgroundColor();
58 public MainContentList(final UiColors
.Element normalStyle
,
59 final UiColors
.Element selectedStyle
) {
60 super(Direction
.VERTICAL
);
62 lines
= new ActionListBox();
64 lines
.setListItemRenderer(new ListItemRenderer
<Runnable
, ActionListBox
>() {
66 * This is the main drawing method for a single list box item, it
67 * applies the current theme to setup the colors and then calls
68 * {@code getLabel(..)} and draws the result using the supplied
69 * {@code TextGUIGraphics}. The graphics object is created just for
70 * this item and is restricted so that it can only draw on the area
71 * this item is occupying. The top-left corner (0x0) should be the
72 * starting point 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 currently
84 * selected, otherwise {@code false}, but please notice
85 * what context 'selected' refers to here (see
86 * {@code setSelectedIndex})
88 * Will be set to {@code true} if the list box currently
89 * has input focus, otherwise {@code false}
91 public void drawItem(TextGUIGraphics graphics
,
92 ActionListBox listBox
, int index
, Runnable item
,
93 boolean selected
, boolean focused
) {
95 // width "-1" to reserve space for the optional vertical
97 List
<TextPart
> parts
= MainContentList
.this.getLabel(index
,
98 lines
.getSize().getColumns() - 1, selected
, focused
);
101 for (TextPart part
: parts
) {
102 graphics
.setForegroundColor(part
.getForegroundColor());
103 graphics
.setBackgroundColor(part
.getBackgroundColor());
105 String label
= StringUtils
.sanitize(part
.getText(),
106 UiColors
.getInstance().isUnicode());
108 graphics
.putString(position
, 0, label
);
109 position
+= label
.length();
115 LinearLayout
.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
);
155 * Return the default content separator for text fields.
157 * @return the separator
159 public String
getSeparator() {
160 return StringId
.DEAULT_FIELD_SEPARATOR
.trans();
170 public String
move(int x
, int y
) {
171 setSelectedIndex(getSelectedIndex() + x
);
177 public int getCount() {
178 return lines
.getItemCount();
182 * Return the representation of the selected line, in {@link TextPart}s.
187 * the max width of the line
189 * TRUE if the item is selected
191 * TRUE if the item is focused
193 * @return the text representation
195 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
197 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
199 if (selected
&& focused
) {
200 parts
.add(new TextPart("" + lines
.getItems().get(index
),
201 Element
.CONTACT_LINE_SELECTED
));
203 parts
.add(new TextPart("" + lines
.getItems().get(index
),
204 Element
.CONTACT_LINE
));