X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fpanes%2FContactList.java;h=023b834b85556fd666e713a9ab5d350e51998ab8;hb=e119a1c1a924998b9315e46c96b1c750aab1deb9;hp=370aba7cd5adf2e98177ef2d8cc86701c250c15d;hpb=0b0b2b0ff1f5e21f7b0feb955b4b54855fb3d508;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactList.java b/src/be/nikiroo/jvcard/tui/panes/ContactList.java index 370aba7..023b834 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactList.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactList.java @@ -1,64 +1,91 @@ package be.nikiroo.jvcard.tui.panes; +import java.io.IOException; import java.util.LinkedList; import java.util.List; import be.nikiroo.jvcard.Card; import be.nikiroo.jvcard.Contact; -import be.nikiroo.jvcard.i18n.Trans; +import be.nikiroo.jvcard.Data; +import be.nikiroo.jvcard.launcher.Main; +import be.nikiroo.jvcard.resources.bundles.DisplayBundle; +import be.nikiroo.jvcard.resources.enums.DisplayOption; +import be.nikiroo.jvcard.resources.enums.ColorOption; +import be.nikiroo.jvcard.resources.enums.StringId; import be.nikiroo.jvcard.tui.KeyAction; -import be.nikiroo.jvcard.tui.UiColors; import be.nikiroo.jvcard.tui.KeyAction.DataType; import be.nikiroo.jvcard.tui.KeyAction.Mode; -import be.nikiroo.jvcard.tui.UiColors.Element; import com.googlecode.lanterna.input.KeyType; public class ContactList extends MainContentList { private Card card; + private List contacts; + private String filter; - private List formats = new LinkedList(); - private int selectedFormat = -1; - private String format = ""; + private List formats; + private int selectedFormat; + private String format; public ContactList(Card card) { - super(UiColors.Element.CONTACT_LINE, - UiColors.Element.CONTACT_LINE_SELECTED); + DisplayBundle map = new DisplayBundle(); + formats = new LinkedList(); + for (String format : map.getString(DisplayOption.CONTACT_LIST_FORMAT) + .split(",")) { + formats.add(format); + } - // TODO: should get that in an INI file - formats.add("NICKNAME@3|FN@+|EMAIL@30"); - formats.add("FN@+|EMAIL@40"); + selectedFormat = -1; switchFormat(); setCard(card); } /** - * 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(); if (card != null) { - for (int i = 0; i < card.getContacts().size(); i++) { - addItem("[contact line]"); + for (Contact c : card) { + if (filter == null + || c.toString(format, "|").toLowerCase() + .contains(filter.toLowerCase())) { + addItem("x"); + contacts.add(c); + } } } setSelectedIndex(0); } + @Override + public void refreshData() { + int index = getSelectedIndex(); + setCard(card); + if (index >= contacts.size()) + index = contacts.size() - 1; + setSelectedIndex(index); + + super.refreshData(); + } + @Override public String getExitWarning() { if (card != null && card.isDirty()) { - //TODO: save? [y/n] instead - return "Some of your contact information is not saved; ignore? [Y/N]"; + return "Ignore unsaved changes? [Y/N]"; } - + return null; } @@ -66,42 +93,138 @@ public class ContactList extends MainContentList { public List getKeyBindings() { List actions = new LinkedList(); - // TODO del, save... - // TODO: remove - actions.add(new KeyAction(Mode.NONE, 'd', Trans.StringId.DUMMY) { + // TODO ui + actions.add(new KeyAction(Mode.ASK_USER, 'a', + StringId.KEY_ACTION_ADD) { @Override - public boolean onAction() { - //TODO dummy action - int index = getSelectedIndex(); - Contact c = card.getContacts().get(index); - c.updateFrom(c); - return false; + public Object getObject() { + return card; + } + + @Override + public String getQuestion() { + return Main.trans(StringId.ASK_USER_CONTACT_NAME); + } + + @Override + public String callback(String answer) { + if (answer.length() > 0) { + List datas = new LinkedList(); + datas.add(new Data(null, "FN", answer, null)); + getCard().add(new Contact(datas)); + addItem("x"); + } + + return null; } }); - actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e', - Trans.StringId.KEY_ACTION_EDIT_CONTACT) { + actions.add(new KeyAction(Mode.ASK_USER_KEY, 'd', + StringId.KEY_ACTION_DELETE_CONTACT) { @Override public Object getObject() { - int index = getSelectedIndex(); - return card.getContacts().get(index); + return getSelectedContact(); + } + + @Override + public String getQuestion() { + Contact contact = getSelectedContact(); + String contactName = "null"; + if (contact != null) + contactName = "" + contact.getPreferredDataValue("FN"); + + return Main.trans( + StringId.CONFIRM_USER_DELETE_CONTACT, + contactName); + } + + @Override + public String callback(String answer) { + if (answer.equalsIgnoreCase("y")) { + Contact contact = getSelectedContact(); + if (contact != null && contact.delete()) { + removeItem("x"); + return null; + } + + String contactName = "null"; + if (contact != null) + contactName = "" + contact.getPreferredDataValue("FN"); + + return Main.trans( + StringId.ERR_CANNOT_DELETE_CONTACT, + contactName); + } + + return null; } }); + actions.add(new KeyAction(Mode.ASK_USER_KEY, 's', + 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) { + StringId.KEY_ACTION_VIEW_CONTACT) { @Override public Object getObject() { - int index = getSelectedIndex(); - return card.getContacts().get(index); + return getSelectedContact(); } }); - actions.add(new KeyAction(Mode.SWICTH_FORMAT, KeyType.Tab, - Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { + actions.add(new KeyAction(Mode.NONE, KeyType.Tab, + StringId.KEY_ACTION_SWITCH_FORMAT) { @Override public boolean onAction() { switchFormat(); return false; } }); + actions.add(new KeyAction(Mode.ASK_USER, 'w', + 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; } @@ -111,14 +234,11 @@ public class ContactList extends MainContentList { 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(); } @@ -128,22 +248,28 @@ public class ContactList extends MainContentList { @Override protected List getLabel(int index, int width, boolean selected, boolean focused) { - Contact c = card.getContacts().get(index); + List parts = new LinkedList(); + + Contact contact = null; + if (index > -1 && index < contacts.size()) + contact = contacts.get(index); + + if (contact == null) + return parts; - Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED - : Element.CONTACT_LINE; - Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED - : Element.CONTACT_LINE_SEPARATOR; - Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED - : Element.CONTACT_LINE_DIRTY; + ColorOption el = (focused && selected) ? ColorOption.CONTACT_LINE_SELECTED + : ColorOption.CONTACT_LINE; + ColorOption elSep = (focused && selected) ? ColorOption.CONTACT_LINE_SEPARATOR_SELECTED + : ColorOption.CONTACT_LINE_SEPARATOR; + ColorOption elDirty = (focused && selected) ? ColorOption.CONTACT_LINE_DIRTY_SELECTED + : ColorOption.CONTACT_LINE_DIRTY; width -= 2; // dirty mark space - // we could use: " ", "┃", "│"... - String[] array = c.toStringArray(format, "┃", " ", width); + String[] array = contact.toStringArray(format, getSeparator(), " ", + width, Main.isUnicode()); - List parts = new LinkedList(); - if (c.isDirty()) { + if (contact.isDirty()) { parts.add(new TextPart(" ", el)); parts.add(new TextPart("*", elDirty)); } else { @@ -159,6 +285,18 @@ public class ContactList extends MainContentList { return parts; } + /** + * Return the currently selected {@link Contact}. + * + * @return the currently selected {@link Contact} + */ + private Contact getSelectedContact() { + int index = getSelectedIndex(); + if (index > -1 && index < contacts.size()) + return contacts.get(index); + return null; + } + private void switchFormat() { if (formats.size() == 0) return;