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