Some changes to support Files
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / MainContentList.java
CommitLineData
fae07ea7
NR
1package be.nikiroo.jvcard.tui.panes;
2
9c8baf0c
NR
3import java.util.LinkedList;
4import java.util.List;
5
fae07ea7 6import be.nikiroo.jvcard.tui.UiColors;
9c8baf0c 7import be.nikiroo.jvcard.tui.UiColors.Element;
fae07ea7 8
9c8baf0c 9import com.googlecode.lanterna.TextColor;
fae07ea7
NR
10import com.googlecode.lanterna.gui2.ActionListBox;
11import com.googlecode.lanterna.gui2.Direction;
12import com.googlecode.lanterna.gui2.LinearLayout;
13import com.googlecode.lanterna.gui2.TextGUIGraphics;
14import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer;
15
16abstract public class MainContentList extends MainContent implements Runnable {
17 private ActionListBox lines;
18
9c8baf0c
NR
19 /**
20 * This class represent a part of a text line to draw in this
21 * {@link MainContentList}.
22 *
23 * @author niki
24 *
25 */
26 protected class TextPart {
27 private String text;
28 private Element element;
29
30 public TextPart(String text, Element element) {
31 this.text = text;
32 this.element = element;
33 }
34
35 public String getText() {
36 return text;
37 }
38
39 public Element getElement() {
40 return element;
41 }
42
43 public TextColor getForegroundColor() {
44 if (element != null)
45 return element.getForegroundColor();
46 return Element.DEFAULT.getForegroundColor();
47 }
48
49 public TextColor getBackgroundColor() {
50 if (element != null)
51 return element.getBackgroundColor();
52 return Element.DEFAULT.getBackgroundColor();
53 }
54 }
55
fae07ea7
NR
56 public MainContentList(final UiColors.Element normalStyle,
57 final UiColors.Element selectedStyle) {
58 super(Direction.VERTICAL);
59
60 lines = new ActionListBox();
61
62 lines
63 .setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() {
64 /**
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.
73 *
74 * @param graphics
75 * Graphics object to draw with
76 * @param listBox
77 * List box we are drawing an item from
78 * @param index
79 * Index of the item we are drawing
80 * @param item
81 * The item we are drawing
82 * @param selected
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})
87 * @param focused
88 * Will be set to {@code true} if the list box
89 * currently has input focus, otherwise {@code
90 * false}
91 */
92 public void drawItem(TextGUIGraphics graphics,
93 ActionListBox listBox, int index, Runnable item,
94 boolean selected, boolean focused) {
95
9c8baf0c
NR
96 // TODO: why +5 ?? padding problem?
97 List<TextPart> parts = MainContentList.this.getLabel(
98 index, lines.getSize().getColumns() + 5,
99 selected, focused);
100
101 int position = 0;
102 for (TextPart part : parts) {
103 graphics.setForegroundColor(part
fae07ea7 104 .getForegroundColor());
9c8baf0c 105 graphics.setBackgroundColor(part
fae07ea7 106 .getBackgroundColor());
9c8baf0c
NR
107 String label = part.getText();
108 graphics.putString(position, 0, label);
109 position += label.length();
fae07ea7 110 }
fae07ea7
NR
111 }
112 });
113
114 addComponent(lines, LinearLayout
115 .createLayoutData(LinearLayout.Alignment.Fill));
116 }
117
118 /**
119 * Add an item to this {@link MainContentList}.
120 *
121 * @param line
122 * the item to add
123 */
124 public void addItem(String line) {
125 lines.addItem(line, this);
126 }
9c8baf0c 127
fae07ea7
NR
128 /**
129 * Clear all the items in this {@link MainContentList}
130 */
131 public void clearItems() {
132 lines.clearItems();
133 }
134
135 /**
136 * Get the index of the currently selected line.
137 *
138 * @return the index
139 */
140 public int getSelectedIndex() {
141 return lines.getSelectedIndex();
142 }
143
144 /**
145 * Change the index of the currently selected line.
146 *
147 * @param index
148 * the new index
149 */
150 public void setSelectedIndex(int index) {
151 lines.setSelectedIndex(index);
152 }
153
154 @Override
155 public void run() {
156 // item selected.
157 // ignore.
158 }
159
160 @Override
161 public String move(int x, int y) {
162 setSelectedIndex(getSelectedIndex() + x);
163 // TODO: y?
164 return null;
165 }
166
167 /**
9c8baf0c 168 * Return the representation of the selected line, in {@link TextPart}s.
fae07ea7
NR
169 *
170 * @param index
171 * the line index
172 * @param width
173 * the max width of the line
9c8baf0c
NR
174 * @param selected
175 * TRUE if the item is selected
176 * @param focused
177 * TRUE if the item is focused
fae07ea7
NR
178 *
179 * @return the text representation
180 */
9c8baf0c
NR
181 protected List<TextPart> getLabel(int index, int width, boolean selected,
182 boolean focused) {
183 List<TextPart> parts = new LinkedList<TextPart>();
184
185 if (selected && focused) {
186 parts.add(new TextPart("" + lines.getItems().get(index),
187 Element.CONTACT_LINE_SELECTED));
188 } else {
189 parts.add(new TextPart("" + lines.getItems().get(index),
190 Element.CONTACT_LINE));
191 }
192
193 return parts;
fae07ea7
NR
194 }
195}