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