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