New launcher class to start all 3 modes:
[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
7da41ecd
NR
6import be.nikiroo.jvcard.launcher.Main;
7import be.nikiroo.jvcard.resources.StringUtils;
8import be.nikiroo.jvcard.resources.Trans.StringId;
9c8baf0c 9import be.nikiroo.jvcard.tui.UiColors.Element;
fae07ea7 10
9c8baf0c 11import com.googlecode.lanterna.TextColor;
296a0b75 12import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer;
fae07ea7
NR
13import com.googlecode.lanterna.gui2.ActionListBox;
14import com.googlecode.lanterna.gui2.Direction;
15import com.googlecode.lanterna.gui2.LinearLayout;
16import com.googlecode.lanterna.gui2.TextGUIGraphics;
fae07ea7
NR
17
18abstract public class MainContentList extends MainContent implements Runnable {
19 private ActionListBox lines;
20
9c8baf0c
NR
21 /**
22 * This class represent a part of a text line to draw in this
23 * {@link MainContentList}.
24 *
25 * @author niki
26 *
27 */
0b0b2b0f 28 public class TextPart {
9c8baf0c
NR
29 private String text;
30 private Element element;
31
32 public TextPart(String text, Element element) {
33 this.text = text;
34 this.element = element;
35 }
36
37 public String getText() {
38 return text;
39 }
40
41 public Element getElement() {
42 return element;
43 }
44
45 public TextColor getForegroundColor() {
46 if (element != null)
47 return element.getForegroundColor();
48 return Element.DEFAULT.getForegroundColor();
49 }
50
51 public TextColor getBackgroundColor() {
52 if (element != null)
53 return element.getBackgroundColor();
54 return Element.DEFAULT.getBackgroundColor();
55 }
56 }
57
2a96e7b2 58 public MainContentList() {
fae07ea7
NR
59 super(Direction.VERTICAL);
60
61 lines = new ActionListBox();
62
296a0b75
NR
63 lines.setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() {
64 /**
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.
72 *
73 * @param graphics
74 * Graphics object to draw with
75 * @param listBox
76 * List box we are drawing an item from
77 * @param index
78 * Index of the item we are drawing
79 * @param item
80 * The item we are drawing
81 * @param selected
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})
86 * @param focused
87 * Will be set to {@code true} if the list box currently
88 * has input focus, otherwise {@code false}
89 */
90 public void drawItem(TextGUIGraphics graphics,
91 ActionListBox listBox, int index, Runnable item,
92 boolean selected, boolean focused) {
93
94 // width "-1" to reserve space for the optional vertical
95 // scroll bar
96 List<TextPart> parts = MainContentList.this.getLabel(index,
97 lines.getSize().getColumns() - 1, selected, focused);
98
99 int position = 0;
100 for (TextPart part : parts) {
101 graphics.setForegroundColor(part.getForegroundColor());
102 graphics.setBackgroundColor(part.getBackgroundColor());
103
104 String label = StringUtils.sanitize(part.getText(),
7da41ecd 105 Main.isUnicode());
296a0b75
NR
106
107 graphics.putString(position, 0, label);
108 position += label.length();
109 }
110 }
111 });
112
113 addComponent(lines,
114 LinearLayout.createLayoutData(LinearLayout.Alignment.Fill));
fae07ea7
NR
115 }
116
117 /**
118 * Add an item to this {@link MainContentList}.
119 *
120 * @param line
121 * the item to add
122 */
123 public void addItem(String line) {
124 lines.addItem(line, this);
125 }
9c8baf0c 126
176a8327
NR
127 /**
128 * Delete the given item.
129 *
130 * Remark: it will only delete the first found instance if multiple
131 * instances of this item are present.
132 *
133 * @param line
134 * the line to delete
135 *
136 * @return TRUE if the item was deleted
137 */
138 public boolean removeItem(String line) {
139 boolean deleted = false;
140
141 List<Runnable> copy = lines.getItems();
142 for (int index = 0; index < copy.size(); index++) {
143 if (copy.get(index).toString().equals(line)) {
144 deleted = true;
145 copy.remove(index);
146 break;
147 }
148 }
149
150 int index = getSelectedIndex();
151 clearItems();
152 for (Runnable run : copy) {
153 addItem(run.toString());
154 }
155 setSelectedIndex(index);
156
157 return deleted;
158 }
159
fae07ea7
NR
160 /**
161 * Clear all the items in this {@link MainContentList}
162 */
163 public void clearItems() {
164 lines.clearItems();
165 }
166
167 /**
168 * Get the index of the currently selected line.
169 *
170 * @return the index
171 */
172 public int getSelectedIndex() {
173 return lines.getSelectedIndex();
174 }
175
176 /**
177 * Change the index of the currently selected line.
178 *
179 * @param index
180 * the new index
181 */
182 public void setSelectedIndex(int index) {
183 lines.setSelectedIndex(index);
184 }
296a0b75 185
bcb54330
NR
186 /**
187 * Return the default content separator for text fields.
188 *
189 * @return the separator
190 */
191 public String getSeparator() {
668268fc 192 return Main.trans(StringId.DEAULT_FIELD_SEPARATOR);
bcb54330 193 }
fae07ea7
NR
194
195 @Override
196 public void run() {
197 // item selected.
198 // ignore.
199 }
200
201 @Override
202 public String move(int x, int y) {
203 setSelectedIndex(getSelectedIndex() + x);
204 // TODO: y?
205 return null;
206 }
207
0b0b2b0f
NR
208 @Override
209 public int getCount() {
210 return lines.getItemCount();
211 }
212
fae07ea7 213 /**
9c8baf0c 214 * Return the representation of the selected line, in {@link TextPart}s.
fae07ea7
NR
215 *
216 * @param index
217 * the line index
218 * @param width
219 * the max width of the line
9c8baf0c
NR
220 * @param selected
221 * TRUE if the item is selected
222 * @param focused
223 * TRUE if the item is focused
fae07ea7
NR
224 *
225 * @return the text representation
226 */
9c8baf0c
NR
227 protected List<TextPart> getLabel(int index, int width, boolean selected,
228 boolean focused) {
229 List<TextPart> parts = new LinkedList<TextPart>();
230
231 if (selected && focused) {
232 parts.add(new TextPart("" + lines.getItems().get(index),
233 Element.CONTACT_LINE_SELECTED));
234 } else {
235 parts.add(new TextPart("" + lines.getItems().get(index),
236 Element.CONTACT_LINE));
237 }
238
239 return parts;
fae07ea7
NR
240 }
241}