| 1 | package be.nikiroo.jvcard.tui.panes; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import be.nikiroo.jvcard.Card; |
| 7 | import be.nikiroo.jvcard.i18n.Trans; |
| 8 | import be.nikiroo.jvcard.tui.KeyAction; |
| 9 | import be.nikiroo.jvcard.tui.UiColors; |
| 10 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
| 11 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
| 12 | |
| 13 | import com.googlecode.lanterna.input.KeyType; |
| 14 | |
| 15 | public class ContactList extends MainContentList { |
| 16 | private Card card; |
| 17 | |
| 18 | private List<String> formats = new LinkedList<String>(); |
| 19 | private int selectedFormat = -1; |
| 20 | private String format = ""; |
| 21 | |
| 22 | public ContactList(Card card) { |
| 23 | super(UiColors.Element.CONTACT_LINE, |
| 24 | UiColors.Element.CONTACT_LINE_SELECTED); |
| 25 | |
| 26 | // TODO: should get that in an INI file |
| 27 | formats.add("NICKNAME@3|FN@+|EMAIL@30"); |
| 28 | formats.add("FN@+|EMAIL@40"); |
| 29 | switchFormat(); |
| 30 | |
| 31 | setCard(card); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Change the currently displayed contacts card. |
| 36 | * |
| 37 | * @param card |
| 38 | * the new {@link Card} |
| 39 | */ |
| 40 | public void setCard(Card card) { |
| 41 | clearItems(); |
| 42 | this.card = card; |
| 43 | |
| 44 | if (card != null) { |
| 45 | for (int i = 0; i < card.getContacts().size(); i++) { |
| 46 | addItem("[contact line]"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | setSelectedIndex(0); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public String getExitWarning() { |
| 55 | if (card != null && card.isDirty()) { |
| 56 | return "Some of your contact information is not saved"; |
| 57 | } |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public List<KeyAction> getKeyBindings() { |
| 63 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 64 | |
| 65 | // TODO del, save... |
| 66 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e', |
| 67 | Trans.StringId.KEY_ACTION_EDIT_CONTACT) { |
| 68 | @Override |
| 69 | public Object getObject() { |
| 70 | int index = getSelectedIndex(); |
| 71 | return card.getContacts().get(index); |
| 72 | } |
| 73 | }); |
| 74 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter, |
| 75 | Trans.StringId.KEY_ACTION_VIEW_CONTACT) { |
| 76 | @Override |
| 77 | public Object getObject() { |
| 78 | int index = getSelectedIndex(); |
| 79 | return card.getContacts().get(index); |
| 80 | } |
| 81 | }); |
| 82 | actions.add(new KeyAction(Mode.SWICTH_FORMAT, KeyType.Tab, |
| 83 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { |
| 84 | @Override |
| 85 | public boolean onAction() { |
| 86 | switchFormat(); |
| 87 | return false; |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | return actions; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public DataType getDataType() { |
| 96 | return DataType.CARD; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Mode getMode() { |
| 101 | return Mode.CONTACT_LIST; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String getTitle() { |
| 106 | // TODO Auto-generated method stub |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected String getLabel(int index, int width) { |
| 112 | // we could use: " ", "┃", "│"... |
| 113 | return card.getContacts().get(index).toString(format, " ┃ ", width); |
| 114 | } |
| 115 | |
| 116 | private void switchFormat() { |
| 117 | if (formats.size() == 0) |
| 118 | return; |
| 119 | |
| 120 | selectedFormat++; |
| 121 | if (selectedFormat >= formats.size()) { |
| 122 | selectedFormat = 0; |
| 123 | } |
| 124 | |
| 125 | format = formats.get(selectedFormat); |
| 126 | |
| 127 | invalidate(); |
| 128 | } |
| 129 | } |