| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import be.nikiroo.jvcard.Card; |
| 7 | import be.nikiroo.jvcard.Contact; |
| 8 | import be.nikiroo.jvcard.i18n.Trans; |
| 9 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
| 10 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
| 11 | |
| 12 | import com.googlecode.lanterna.gui2.ActionListBox; |
| 13 | import com.googlecode.lanterna.gui2.Direction; |
| 14 | import com.googlecode.lanterna.gui2.Interactable; |
| 15 | import com.googlecode.lanterna.gui2.LinearLayout; |
| 16 | import com.googlecode.lanterna.gui2.TextGUIGraphics; |
| 17 | import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer; |
| 18 | import com.googlecode.lanterna.input.KeyType; |
| 19 | |
| 20 | public class ContactList extends MainContent implements Runnable { |
| 21 | private Card card; |
| 22 | private ActionListBox lines; |
| 23 | |
| 24 | private List<String> formats = new LinkedList<String>(); |
| 25 | private int selectedFormat = -1; |
| 26 | private String format = ""; |
| 27 | |
| 28 | public ContactList(Card card) { |
| 29 | super(Direction.VERTICAL); |
| 30 | |
| 31 | // TODO: should get that in an INI file |
| 32 | formats.add("NICKNAME@3|FN@+|EMAIL@30"); |
| 33 | formats.add("FN@+|EMAIL@40"); |
| 34 | switchFormat(); |
| 35 | |
| 36 | lines = new ActionListBox(); |
| 37 | |
| 38 | lines |
| 39 | .setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() { |
| 40 | /** |
| 41 | * This is the main drawing method for a single list box |
| 42 | * item, it applies the current theme to setup the colors |
| 43 | * and then calls {@code getLabel(..)} and draws the result |
| 44 | * using the supplied {@code TextGUIGraphics}. The graphics |
| 45 | * object is created just for this item and is restricted so |
| 46 | * that it can only draw on the area this item is occupying. |
| 47 | * The top-left corner (0x0) should be the starting point |
| 48 | * when drawing the item. |
| 49 | * |
| 50 | * @param graphics |
| 51 | * Graphics object to draw with |
| 52 | * @param listBox |
| 53 | * List box we are drawing an item from |
| 54 | * @param index |
| 55 | * Index of the item we are drawing |
| 56 | * @param item |
| 57 | * The item we are drawing |
| 58 | * @param selected |
| 59 | * Will be set to {@code true} if the item is |
| 60 | * currently selected, otherwise {@code false}, |
| 61 | * but please notice what context 'selected' |
| 62 | * refers to here (see {@code setSelectedIndex}) |
| 63 | * @param focused |
| 64 | * Will be set to {@code true} if the list box |
| 65 | * currently has input focus, otherwise {@code |
| 66 | * false} |
| 67 | */ |
| 68 | public void drawItem(TextGUIGraphics graphics, |
| 69 | ActionListBox listBox, int index, Runnable item, |
| 70 | boolean selected, boolean focused) { |
| 71 | |
| 72 | if (selected && focused) { |
| 73 | graphics |
| 74 | .setForegroundColor(UiColors.Element.CONTACT_LINE_SELECTED |
| 75 | .getForegroundColor()); |
| 76 | graphics |
| 77 | .setBackgroundColor(UiColors.Element.CONTACT_LINE_SELECTED |
| 78 | .getBackgroundColor()); |
| 79 | } else { |
| 80 | graphics |
| 81 | .setForegroundColor(UiColors.Element.CONTACT_LINE |
| 82 | .getForegroundColor()); |
| 83 | graphics |
| 84 | .setBackgroundColor(UiColors.Element.CONTACT_LINE |
| 85 | .getBackgroundColor()); |
| 86 | } |
| 87 | |
| 88 | String label = getLabel(listBox, index, item); |
| 89 | // label = TerminalTextUtils.fitString(label, |
| 90 | // graphics.getSize().getColumns()); |
| 91 | |
| 92 | Contact c = ContactList.this.card.getContacts().get( |
| 93 | index); |
| 94 | |
| 95 | // we could use: " ", "┃", "│"... |
| 96 | //TODO: why +5 ?? padding problem? |
| 97 | label = c.toString(format, " ┃ ", lines.getSize().getColumns() + 5); |
| 98 | |
| 99 | graphics.putString(0, 0, label); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | addComponent(lines, LinearLayout |
| 104 | .createLayoutData(LinearLayout.Alignment.Fill)); |
| 105 | |
| 106 | setCard(card); |
| 107 | } |
| 108 | |
| 109 | private void switchFormat() { |
| 110 | if (formats.size() == 0) |
| 111 | return; |
| 112 | |
| 113 | selectedFormat++; |
| 114 | if (selectedFormat >= formats.size()) { |
| 115 | selectedFormat = 0; |
| 116 | } |
| 117 | |
| 118 | format = formats.get(selectedFormat); |
| 119 | |
| 120 | if (lines != null) |
| 121 | lines.invalidate(); |
| 122 | } |
| 123 | |
| 124 | public void setCard(Card card) { |
| 125 | lines.clearItems(); |
| 126 | this.card = card; |
| 127 | |
| 128 | if (card != null) { |
| 129 | for (int i = 0; i < card.getContacts().size(); i++) { |
| 130 | lines.addItem("[contact line]", this); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | lines.setSelectedIndex(0); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void run() { |
| 139 | // TODO: item selected. |
| 140 | // should we do something? |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String getExitWarning() { |
| 145 | if (card != null && card.isDirty()) { |
| 146 | return "Some of your contact information is not saved"; |
| 147 | } |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public List<KeyAction> getKeyBindings() { |
| 153 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 154 | |
| 155 | // TODO del, save... |
| 156 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e', |
| 157 | Trans.StringId.KEY_ACTION_EDIT_CONTACT) { |
| 158 | @Override |
| 159 | public Object getObject() { |
| 160 | int index = lines.getSelectedIndex(); |
| 161 | return card.getContacts().get(index); |
| 162 | } |
| 163 | }); |
| 164 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter, |
| 165 | Trans.StringId.KEY_ACTION_VIEW_CONTACT) { |
| 166 | @Override |
| 167 | public Object getObject() { |
| 168 | int index = lines.getSelectedIndex(); |
| 169 | return card.getContacts().get(index); |
| 170 | } |
| 171 | }); |
| 172 | actions.add(new KeyAction(Mode.SWICTH_FORMAT, KeyType.Tab, |
| 173 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { |
| 174 | @Override |
| 175 | public boolean onAction() { |
| 176 | switchFormat(); |
| 177 | return false; |
| 178 | } |
| 179 | }); |
| 180 | |
| 181 | return actions; |
| 182 | } |
| 183 | |
| 184 | public DataType getDataType() { |
| 185 | return DataType.CARD; |
| 186 | } |
| 187 | |
| 188 | public Mode getMode() { |
| 189 | return Mode.CONTACT_LIST; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public String move(int x, int y) { |
| 194 | lines.setSelectedIndex(lines.getSelectedIndex() + x); |
| 195 | // TODO: y? |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public String getTitle() { |
| 201 | // TODO Auto-generated method stub |
| 202 | return null; |
| 203 | } |
| 204 | } |