New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactList.java
1 package be.nikiroo.jvcard.tui.panes;
2
3 import java.io.IOException;
4 import java.util.LinkedList;
5 import java.util.List;
6
7 import be.nikiroo.jvcard.Card;
8 import be.nikiroo.jvcard.Contact;
9 import be.nikiroo.jvcard.Data;
10 import be.nikiroo.jvcard.launcher.Main;
11 import be.nikiroo.jvcard.resources.Bundles;
12 import be.nikiroo.jvcard.resources.Trans;
13 import be.nikiroo.jvcard.tui.KeyAction;
14 import be.nikiroo.jvcard.tui.KeyAction.DataType;
15 import be.nikiroo.jvcard.tui.KeyAction.Mode;
16 import be.nikiroo.jvcard.tui.UiColors.Element;
17
18 import com.googlecode.lanterna.input.KeyType;
19
20 public class ContactList extends MainContentList {
21 private Card card;
22 private List<Contact> contacts;
23 private String filter;
24
25 private List<String> formats;
26 private int selectedFormat;
27 private String format;
28
29 public ContactList(Card card) {
30 formats = new LinkedList<String>();
31 for (String format : Bundles.getBundle("display")
32 .getString("CONTACT_LIST_FORMAT").split(",")) {
33 formats.add(format);
34 }
35
36 selectedFormat = -1;
37 switchFormat();
38
39 setCard(card);
40 }
41
42 /**
43 * Change the currently displayed contacts card, only allowing those that
44 * satisfy the current filter.
45 *
46 * @param card
47 * the new {@link Card}
48 * @param filter
49 * the text filter or NULL for all contacts
50 */
51 public void setCard(Card card) {
52 clearItems();
53 this.card = card;
54 this.contacts = new LinkedList<Contact>();
55
56 if (card != null) {
57 for (Contact c : card) {
58 if (filter == null
59 || c.toString(format).toLowerCase()
60 .contains(filter.toLowerCase())) {
61 addItem("x");
62 contacts.add(c);
63 }
64 }
65 }
66
67 setSelectedIndex(0);
68 }
69
70 @Override
71 public void refreshData() {
72 int index = getSelectedIndex();
73 setCard(card);
74 if (index >= contacts.size())
75 index = contacts.size() - 1;
76 setSelectedIndex(index);
77
78 super.refreshData();
79 }
80
81 @Override
82 public String getExitWarning() {
83 if (card != null && card.isDirty()) {
84 return "Ignore unsaved changes? [Y/N]";
85 }
86
87 return null;
88 }
89
90 @Override
91 public List<KeyAction> getKeyBindings() {
92 List<KeyAction> actions = new LinkedList<KeyAction>();
93
94 // TODO ui
95 actions.add(new KeyAction(Mode.ASK_USER, 'a', Trans.StringId.DUMMY) {
96 @Override
97 public Object getObject() {
98 return card;
99 }
100
101 @Override
102 public String getQuestion() {
103 // TODO i18n
104 return "new contact name: ";
105 }
106
107 @Override
108 public String callback(String answer) {
109 if (answer.length() > 0) {
110 List<Data> datas = new LinkedList<Data>();
111 datas.add(new Data(null, "FN", answer, null));
112 getCard().add(new Contact(datas));
113 addItem("x");
114 }
115
116 return null;
117 }
118 });
119 actions.add(new KeyAction(Mode.ASK_USER_KEY, 'd',
120 Trans.StringId.KEY_ACTION_DELETE_CONTACT) {
121 @Override
122 public Object getObject() {
123 return getSelectedContact();
124 }
125
126 @Override
127 public String getQuestion() {
128 // TODO i18n
129 return "Delete contact? [Y/N]";
130 }
131
132 @Override
133 public String callback(String answer) {
134 if (answer.equalsIgnoreCase("y")) {
135 Contact contact = getSelectedContact();
136 if (contact != null && contact.delete()) {
137 removeItem("x");
138 return null;
139 }
140
141 // TODO i18n
142 return "Cannot delete contact";
143 }
144
145 return null;
146 }
147 });
148 actions.add(new KeyAction(Mode.ASK_USER_KEY, 's',
149 Trans.StringId.KEY_ACTION_SAVE_CARD) {
150 @Override
151 public Object getObject() {
152 return card;
153 }
154
155 @Override
156 public String getQuestion() {
157 return "Save changes? [Y/N]";
158 }
159
160 @Override
161 public String callback(String answer) {
162 if (answer.equalsIgnoreCase("y")) {
163 boolean ok = false;
164 try {
165 if (card != null && card.save())
166 ok = true;
167 } catch (IOException ioe) {
168 ioe.printStackTrace();
169 }
170
171 if (!ok) {
172 return "Cannot save to file";
173 }
174 }
175
176 return null;
177 }
178
179 });
180 actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter,
181 Trans.StringId.KEY_ACTION_VIEW_CONTACT) {
182 @Override
183 public Object getObject() {
184 return getSelectedContact();
185 }
186 });
187 actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
188 Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
189 @Override
190 public boolean onAction() {
191 switchFormat();
192 return false;
193 }
194 });
195 actions.add(new KeyAction(Mode.ASK_USER, 'w',
196 Trans.StringId.KEY_ACTION_SEARCH) {
197
198 @Override
199 public String getQuestion() {
200 return "Search:";
201 }
202
203 @Override
204 public String getDefaultAnswer() {
205 return filter;
206 }
207
208 @Override
209 public String callback(String answer) {
210 filter = answer;
211 setCard(card);
212 return null;
213 }
214 });
215
216 return actions;
217 }
218
219 @Override
220 public DataType getDataType() {
221 return DataType.CARD;
222 }
223
224 @Override
225 public String getTitle() {
226 if (card != null) {
227 if (filter != null)
228 return card.getName() + " [" + filter + "]";
229 return card.getName();
230 }
231
232 return null;
233 }
234
235 @Override
236 protected List<TextPart> getLabel(int index, int width, boolean selected,
237 boolean focused) {
238 List<TextPart> parts = new LinkedList<TextPart>();
239
240 Contact contact = null;
241 if (index > -1 && index < contacts.size())
242 contact = contacts.get(index);
243
244 if (contact == null)
245 return parts;
246
247 Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
248 : Element.CONTACT_LINE;
249 Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
250 : Element.CONTACT_LINE_SEPARATOR;
251 Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED
252 : Element.CONTACT_LINE_DIRTY;
253
254 width -= 2; // dirty mark space
255
256 String[] array = contact.toStringArray(format, getSeparator(), " ",
257 width, Main.isUnicode());
258
259 if (contact.isDirty()) {
260 parts.add(new TextPart(" ", el));
261 parts.add(new TextPart("*", elDirty));
262 } else {
263 parts.add(new TextPart(" ", elSep));
264 }
265
266 boolean separator = false;
267 for (String str : array) {
268 parts.add(new TextPart(str, (separator ? elSep : el)));
269 separator = !separator;
270 }
271
272 return parts;
273 }
274
275 /**
276 * Return the currently selected {@link Contact}.
277 *
278 * @return the currently selected {@link Contact}
279 */
280 private Contact getSelectedContact() {
281 int index = getSelectedIndex();
282 if (index > -1 && index < contacts.size())
283 return contacts.get(index);
284 return null;
285 }
286
287 private void switchFormat() {
288 if (formats.size() == 0)
289 return;
290
291 selectedFormat++;
292 if (selectedFormat >= formats.size()) {
293 selectedFormat = 0;
294 }
295
296 format = formats.get(selectedFormat);
297
298 invalidate();
299 }
300 }