Move MainContent and its derivative to a new package, rework ContactList
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / MainContentList.java
1 package be.nikiroo.jvcard.tui.panes;
2
3 import be.nikiroo.jvcard.tui.UiColors;
4
5 import com.googlecode.lanterna.gui2.ActionListBox;
6 import com.googlecode.lanterna.gui2.Direction;
7 import com.googlecode.lanterna.gui2.LinearLayout;
8 import com.googlecode.lanterna.gui2.TextGUIGraphics;
9 import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer;
10
11 abstract public class MainContentList extends MainContent implements Runnable {
12 private ActionListBox lines;
13
14 public MainContentList(final UiColors.Element normalStyle,
15 final UiColors.Element selectedStyle) {
16 super(Direction.VERTICAL);
17
18 lines = new ActionListBox();
19
20 lines
21 .setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() {
22 /**
23 * This is the main drawing method for a single list box
24 * item, it applies the current theme to setup the colors
25 * and then calls {@code getLabel(..)} and draws the result
26 * using the supplied {@code TextGUIGraphics}. The graphics
27 * object is created just for this item and is restricted so
28 * that it can only draw on the area this item is occupying.
29 * The top-left corner (0x0) should be the starting point
30 * when drawing the item.
31 *
32 * @param graphics
33 * Graphics object to draw with
34 * @param listBox
35 * List box we are drawing an item from
36 * @param index
37 * Index of the item we are drawing
38 * @param item
39 * The item we are drawing
40 * @param selected
41 * Will be set to {@code true} if the item is
42 * currently selected, otherwise {@code false},
43 * but please notice what context 'selected'
44 * refers to here (see {@code setSelectedIndex})
45 * @param focused
46 * Will be set to {@code true} if the list box
47 * currently has input focus, otherwise {@code
48 * false}
49 */
50 public void drawItem(TextGUIGraphics graphics,
51 ActionListBox listBox, int index, Runnable item,
52 boolean selected, boolean focused) {
53
54 if (selected && focused) {
55 graphics.setForegroundColor(selectedStyle
56 .getForegroundColor());
57 graphics.setBackgroundColor(selectedStyle
58 .getBackgroundColor());
59 } else {
60 graphics.setForegroundColor(normalStyle
61 .getForegroundColor());
62 graphics.setBackgroundColor(normalStyle
63 .getBackgroundColor());
64 }
65
66 // original impl:
67 // String label = getLabel(listBox, index, item);
68 // label = TerminalTextUtils.fitString(label,
69 // graphics.getSize().getColumns());
70
71 // TODO: why +5 ?? padding problem?
72 String label = MainContentList.this.getLabel(index,
73 lines.getSize().getColumns() + 5);
74 graphics.putString(0, 0, label);
75 }
76 });
77
78 addComponent(lines, LinearLayout
79 .createLayoutData(LinearLayout.Alignment.Fill));
80 }
81
82 /**
83 * Add an item to this {@link MainContentList}.
84 *
85 * @param line
86 * the item to add
87 */
88 public void addItem(String line) {
89 lines.addItem(line, this);
90 }
91
92 /**
93 * Clear all the items in this {@link MainContentList}
94 */
95 public void clearItems() {
96 lines.clearItems();
97 }
98
99 /**
100 * Get the index of the currently selected line.
101 *
102 * @return the index
103 */
104 public int getSelectedIndex() {
105 return lines.getSelectedIndex();
106 }
107
108 /**
109 * Change the index of the currently selected line.
110 *
111 * @param index
112 * the new index
113 */
114 public void setSelectedIndex(int index) {
115 lines.setSelectedIndex(index);
116 }
117
118 @Override
119 public void run() {
120 // item selected.
121 // ignore.
122 }
123
124 @Override
125 public String move(int x, int y) {
126 setSelectedIndex(getSelectedIndex() + x);
127 // TODO: y?
128 return null;
129 }
130
131 /**
132 * Return the text representation of the selected line.
133 *
134 * @param index
135 * the line index
136 * @param width
137 * the max width of the line
138 *
139 * @return the text representation
140 */
141 protected String getLabel(int index, int width) {
142 return "" + lines.getItems().get(index);
143 }
144 }