X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fpanes%2FContactList.java;h=3bb6eac7095b093b006a3b4f865ce88905d749e3;hb=f04d8b1c4c3ed29d4d23cc076f307ef455b2dcb6;hp=3a943f900610a75a56cec552be84eff1d7f4d5d0;hpb=fae07ea7af01c64ca1a858db75a615555318d5e2;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactList.java b/src/be/nikiroo/jvcard/tui/panes/ContactList.java index 3a943f9..3bb6eac 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactList.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactList.java @@ -4,11 +4,13 @@ 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.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; @@ -50,11 +52,20 @@ public class ContactList extends MainContentList { setSelectedIndex(0); } + @Override + public void refreshData() { + int index = getSelectedIndex(); + setCard(card); + setSelectedIndex(index); + super.refreshData(); + } + @Override public String getExitWarning() { if (card != null && card.isDirty()) { - return "Some of your contact information is not saved"; + return "Ignore unsaved changes? [Y/N]"; } + return null; } @@ -62,24 +73,36 @@ public class ContactList extends MainContentList { public List getKeyBindings() { List actions = new LinkedList(); - // TODO del, save... - actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e', + // TODO add + actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'e', Trans.StringId.KEY_ACTION_EDIT_CONTACT) { @Override public Object getObject() { - int index = getSelectedIndex(); - return card.getContacts().get(index); + return getSelectedContact(); + } + }); + actions.add(new KeyAction(Mode.DELETE_CONTACT, 'd', + Trans.StringId.KEY_ACTION_DELETE_CONTACT) { + @Override + public Object getObject() { + return getSelectedContact(); + } + }); + actions.add(new KeyAction(Mode.SAVE_CARD, 's', + Trans.StringId.KEY_ACTION_SAVE_CARD) { + @Override + public Object getObject() { + return card; } }); actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter, Trans.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, + actions.add(new KeyAction(Mode.NONE, KeyType.Tab, Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { @Override public boolean onAction() { @@ -96,21 +119,65 @@ public class ContactList extends MainContentList { return DataType.CARD; } - @Override - public Mode getMode() { - return Mode.CONTACT_LIST; - } - @Override public String getTitle() { - // TODO Auto-generated method stub + if (card != null) { + return card.getName(); + } + return null; } @Override - protected String getLabel(int index, int width) { - // we could use: " ", "┃", "│"... - return card.getContacts().get(index).toString(format, " ┃ ", width); + protected List getLabel(int index, int width, boolean selected, + boolean focused) { + List parts = new LinkedList(); + + Contact contact = null; + if (index > -1 && index < card.size()) + contact = card.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; + + width -= 2; // dirty mark space + + String[] array = contact.toStringArray(format, getSeparator(), " ", + width, UiColors.getInstance().isUnicode()); + + if (contact.isDirty()) { + parts.add(new TextPart(" ", el)); + parts.add(new TextPart("*", elDirty)); + } else { + parts.add(new TextPart(" ", elSep)); + } + + boolean separator = false; + for (String str : array) { + parts.add(new TextPart(str, (separator ? elSep : el))); + separator = !separator; + } + + return parts; + } + + /** + * Return the currently selected {@link Contact}. + * + * @return the currently selected {@link Contact} + */ + private Contact getSelectedContact() { + int index = getSelectedIndex(); + if (index > -1 && index < card.size()) + return card.get(index); + return null; } private void switchFormat() {