1 package be
.nikiroo
.jvcard
.tui
.panes
;
3 import java
.util
.LinkedList
;
6 import be
.nikiroo
.jvcard
.launcher
.Main
;
7 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
8 import be
.nikiroo
.jvcard
.resources
.Trans
.StringId
;
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() {
59 super(Direction
.VERTICAL
);
61 lines
= new ActionListBox();
63 lines
.setListItemRenderer(new ListItemRenderer
<Runnable
, ActionListBox
>() {
65 * This is the main drawing method for a single list box item, it
66 * applies the current theme to setup the colors and then calls
67 * {@code getLabel(..)} and draws the result using the supplied
68 * {@code TextGUIGraphics}. The graphics object is created just for
69 * this item and is restricted so that it can only draw on the area
70 * this item is occupying. The top-left corner (0x0) should be the
71 * starting point when drawing the item.
74 * Graphics object to draw with
76 * List box we are drawing an item from
78 * Index of the item we are drawing
80 * The item we are drawing
82 * Will be set to {@code true} if the item is currently
83 * selected, otherwise {@code false}, but please notice
84 * what context 'selected' refers to here (see
85 * {@code setSelectedIndex})
87 * Will be set to {@code true} if the list box currently
88 * has input focus, otherwise {@code false}
90 public void drawItem(TextGUIGraphics graphics
,
91 ActionListBox listBox
, int index
, Runnable item
,
92 boolean selected
, boolean focused
) {
94 // width "-1" to reserve space for the optional vertical
96 List
<TextPart
> parts
= MainContentList
.this.getLabel(index
,
97 lines
.getSize().getColumns() - 1, selected
, focused
);
100 for (TextPart part
: parts
) {
101 graphics
.setForegroundColor(part
.getForegroundColor());
102 graphics
.setBackgroundColor(part
.getBackgroundColor());
104 String label
= StringUtils
.sanitize(part
.getText(),
107 graphics
.putString(position
, 0, label
);
108 position
+= label
.length();
114 LinearLayout
.createLayoutData(LinearLayout
.Alignment
.Fill
));
118 * Add an item to this {@link MainContentList}.
123 public void addItem(String line
) {
124 lines
.addItem(line
, this);
128 * Delete the given item.
130 * Remark: it will only delete the first found instance if multiple
131 * instances of this item are present.
136 * @return TRUE if the item was deleted
138 public boolean removeItem(String line
) {
139 boolean deleted
= false;
141 List
<Runnable
> copy
= lines
.getItems();
142 for (int index
= 0; index
< copy
.size(); index
++) {
143 if (copy
.get(index
).toString().equals(line
)) {
150 int index
= getSelectedIndex();
152 for (Runnable run
: copy
) {
153 addItem(run
.toString());
155 setSelectedIndex(index
);
161 * Clear all the items in this {@link MainContentList}
163 public void clearItems() {
168 * Get the index of the currently selected line.
172 public int getSelectedIndex() {
173 return lines
.getSelectedIndex();
177 * Change the index of the currently selected line.
182 public void setSelectedIndex(int index
) {
183 lines
.setSelectedIndex(index
);
187 * Return the default content separator for text fields.
189 * @return the separator
191 public String
getSeparator() {
192 return Main
.trans(StringId
.DEAULT_FIELD_SEPARATOR
);
202 public String
move(int x
, int y
) {
203 setSelectedIndex(getSelectedIndex() + x
);
209 public int getCount() {
210 return lines
.getItemCount();
214 * Return the representation of the selected line, in {@link TextPart}s.
219 * the max width of the line
221 * TRUE if the item is selected
223 * TRUE if the item is focused
225 * @return the text representation
227 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
229 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
231 if (selected
&& focused
) {
232 parts
.add(new TextPart("" + lines
.getItems().get(index
),
233 Element
.CONTACT_LINE_SELECTED
));
235 parts
.add(new TextPart("" + lines
.getItems().get(index
),
236 Element
.CONTACT_LINE
));