package be.nikiroo.jvcard.tui.panes;
+import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class ContactList extends MainContentList {
private Card card;
+ private List<Contact> contacts;
+ private String filter;
private List<String> formats = new LinkedList<String>();
private int selectedFormat = -1;
private String format = "";
public ContactList(Card card) {
- super(UiColors.Element.CONTACT_LINE,
- UiColors.Element.CONTACT_LINE_SELECTED);
-
// TODO: should get that in an INI file
formats.add("NICKNAME@3|FN@+|EMAIL@30");
formats.add("FN@+|EMAIL@40");
}
/**
- * Change the currently displayed contacts card.
+ * Change the currently displayed contacts card, only allowing those that
+ * satisfy the current filter.
*
* @param card
* the new {@link Card}
+ * @param filter
+ * the text filter or NULL for all contacts
*/
public void setCard(Card card) {
clearItems();
this.card = card;
+ this.contacts = new LinkedList<Contact>();
if (card != null) {
- for (int i = 0; i < card.getContacts().size(); i++) {
- addItem("[contact line]");
+ for (int i = 0; i < card.size(); i++) {
+ Contact c = card.get(i);
+ if (filter == null
+ || c.toString(format).toLowerCase()
+ .contains(filter.toLowerCase())) {
+ addItem("[contact line]");
+ contacts.add(c);
+ }
}
}
public void refreshData() {
int index = getSelectedIndex();
setCard(card);
+ if (index >= contacts.size())
+ index = contacts.size() - 1;
setSelectedIndex(index);
+
super.refreshData();
}
List<KeyAction> actions = new LinkedList<KeyAction>();
// TODO add
- actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e',
+ actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'e',
Trans.StringId.KEY_ACTION_EDIT_CONTACT) {
@Override
public Object getObject() {
return getSelectedContact();
}
});
- actions.add(new KeyAction(Mode.DELETE_CONTACT, 'd',
+ actions.add(new KeyAction(Mode.ASK_USER_KEY, 'd',
Trans.StringId.KEY_ACTION_DELETE_CONTACT) {
@Override
public Object getObject() {
return getSelectedContact();
}
+
+ @Override
+ public String getQuestion() {
+ // TODO i18n
+ return "Delete contact? [Y/N]";
+ }
+
+ @Override
+ public String callback(String answer) {
+ if (answer.equalsIgnoreCase("y")) {
+ Contact contact = getSelectedContact();
+ if (contact != null && contact.delete()) {
+ return null;
+ }
+
+ // TODO i18n
+ return "Cannot delete contact";
+ }
+
+ return null;
+ }
});
- actions.add(new KeyAction(Mode.SAVE_CARD, 's',
+ actions.add(new KeyAction(Mode.ASK_USER_KEY, 's',
Trans.StringId.KEY_ACTION_SAVE_CARD) {
@Override
public Object getObject() {
return card;
}
+
+ @Override
+ public String getQuestion() {
+ return "Save changes? [Y/N]";
+ }
+
+ @Override
+ public String callback(String answer) {
+ if (answer.equalsIgnoreCase("y")) {
+ boolean ok = false;
+ try {
+ if (card != null && card.save())
+ ok = true;
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ }
+
+ if (!ok) {
+ return "Cannot save to file";
+ }
+ }
+
+ return null;
+ }
+
});
actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter,
Trans.StringId.KEY_ACTION_VIEW_CONTACT) {
return false;
}
});
+ actions.add(new KeyAction(Mode.ASK_USER, 'w',
+ Trans.StringId.KEY_ACTION_SEARCH) {
+
+ @Override
+ public String getQuestion() {
+ return "Search:";
+ }
+
+ @Override
+ public String getDefaultAnswer() {
+ return filter;
+ }
+
+ @Override
+ public String callback(String answer) {
+ filter = answer;
+ setCard(card);
+ return null;
+ }
+ });
return actions;
}
return DataType.CARD;
}
- @Override
- public Mode getMode() {
- return Mode.CONTACT_LIST;
- }
-
@Override
public String getTitle() {
if (card != null) {
+ if (filter != null)
+ return card.getName() + " [" + filter + "]";
return card.getName();
}
List<TextPart> parts = new LinkedList<TextPart>();
Contact contact = null;
- if (index > -1 && index < card.size())
- contact = card.get(index);
+ if (index > -1 && index < contacts.size())
+ contact = contacts.get(index);
if (contact == null)
return parts;
*/
private Contact getSelectedContact() {
int index = getSelectedIndex();
- if (index > -1 && index < card.size())
- return card.get(index);
+ if (index > -1 && index < contacts.size())
+ return contacts.get(index);
return null;
}