i18n: rework of the system + French translation
[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.i18n.Trans.StringId;
7 import be.nikiroo.jvcard.tui.Main;
8 import be.nikiroo.jvcard.tui.StringUtils;
9 import be.nikiroo.jvcard.tui.UiColors;
10 import be.nikiroo.jvcard.tui.UiColors.Element;
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 Element element;
32
33 public TextPart(String text, Element element) {
34 this.text = text;
35 this.element = element;
36 }
37
38 public String getText() {
39 return text;
40 }
41
42 public Element getElement() {
43 return element;
44 }
45
46 public TextColor getForegroundColor() {
47 if (element != null)
48 return element.getForegroundColor();
49 return Element.DEFAULT.getForegroundColor();
50 }
51
52 public TextColor getBackgroundColor() {
53 if (element != null)
54 return element.getBackgroundColor();
55 return Element.DEFAULT.getBackgroundColor();
56 }
57 }
58
59 public MainContentList(final UiColors.Element normalStyle,
60 final UiColors.Element selectedStyle) {
61 super(Direction.VERTICAL);
62
63 lines = new ActionListBox();
64
65 lines.setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() {
66 /**
67 * This is the main drawing method for a single list box item, it
68 * applies the current theme to setup the colors and then calls
69 * {@code getLabel(..)} and draws the result using the supplied
70 * {@code TextGUIGraphics}. The graphics object is created just for
71 * this item and is restricted so that it can only draw on the area
72 * this item is occupying. The top-left corner (0x0) should be the
73 * starting point when drawing the item.
74 *
75 * @param graphics
76 * Graphics object to draw with
77 * @param listBox
78 * List box we are drawing an item from
79 * @param index
80 * Index of the item we are drawing
81 * @param item
82 * The item we are drawing
83 * @param selected
84 * Will be set to {@code true} if the item is currently
85 * selected, otherwise {@code false}, but please notice
86 * what context 'selected' refers to here (see
87 * {@code setSelectedIndex})
88 * @param focused
89 * Will be set to {@code true} if the list box currently
90 * has input focus, otherwise {@code false}
91 */
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 UiColors.getInstance().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 * Clear all the items in this {@link MainContentList}
131 */
132 public void clearItems() {
133 lines.clearItems();
134 }
135
136 /**
137 * Get the index of the currently selected line.
138 *
139 * @return the index
140 */
141 public int getSelectedIndex() {
142 return lines.getSelectedIndex();
143 }
144
145 /**
146 * Change the index of the currently selected line.
147 *
148 * @param index
149 * the new index
150 */
151 public void setSelectedIndex(int index) {
152 lines.setSelectedIndex(index);
153 }
154
155 /**
156 * Return the default content separator for text fields.
157 *
158 * @return the separator
159 */
160 public String getSeparator() {
161 return Main.trans(StringId.DEAULT_FIELD_SEPARATOR);
162 }
163
164 @Override
165 public void run() {
166 // item selected.
167 // ignore.
168 }
169
170 @Override
171 public String move(int x, int y) {
172 setSelectedIndex(getSelectedIndex() + x);
173 // TODO: y?
174 return null;
175 }
176
177 @Override
178 public int getCount() {
179 return lines.getItemCount();
180 }
181
182 /**
183 * Return the representation of the selected line, in {@link TextPart}s.
184 *
185 * @param index
186 * the line index
187 * @param width
188 * the max width of the line
189 * @param selected
190 * TRUE if the item is selected
191 * @param focused
192 * TRUE if the item is focused
193 *
194 * @return the text representation
195 */
196 protected List<TextPart> getLabel(int index, int width, boolean selected,
197 boolean focused) {
198 List<TextPart> parts = new LinkedList<TextPart>();
199
200 if (selected && focused) {
201 parts.add(new TextPart("" + lines.getItems().get(index),
202 Element.CONTACT_LINE_SELECTED));
203 } else {
204 parts.add(new TextPart("" + lines.getItems().get(index),
205 Element.CONTACT_LINE));
206 }
207
208 return parts;
209 }
210 }