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; | |
0b0b2b0f | 7 | import be.nikiroo.jvcard.Contact; |
fae07ea7 NR |
8 | import be.nikiroo.jvcard.i18n.Trans; |
9 | import be.nikiroo.jvcard.tui.KeyAction; | |
10 | import be.nikiroo.jvcard.tui.UiColors; | |
11 | import be.nikiroo.jvcard.tui.KeyAction.DataType; | |
12 | import be.nikiroo.jvcard.tui.KeyAction.Mode; | |
9c8baf0c | 13 | import be.nikiroo.jvcard.tui.UiColors.Element; |
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 | ||
bcb54330 NR |
55 | @Override |
56 | public void refreshData() { | |
57 | int index = getSelectedIndex(); | |
58 | setCard(card); | |
59 | setSelectedIndex(index); | |
60 | super.refreshData(); | |
61 | } | |
62 | ||
fae07ea7 NR |
63 | @Override |
64 | public String getExitWarning() { | |
65 | if (card != null && card.isDirty()) { | |
bcb54330 | 66 | return "Ignore unsaved changes? [Y/N]"; |
fae07ea7 | 67 | } |
bcb54330 | 68 | |
fae07ea7 NR |
69 | return null; |
70 | } | |
71 | ||
72 | @Override | |
73 | public List<KeyAction> getKeyBindings() { | |
74 | List<KeyAction> actions = new LinkedList<KeyAction>(); | |
75 | ||
bcb54330 NR |
76 | // TODO add, del, save... |
77 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e', | |
78 | Trans.StringId.KEY_ACTION_EDIT_CONTACT) { | |
0b0b2b0f | 79 | @Override |
bcb54330 NR |
80 | public Object getObject() { |
81 | return getSelectedContact(); | |
0b0b2b0f NR |
82 | } |
83 | }); | |
bcb54330 NR |
84 | actions.add(new KeyAction(Mode.DELETE_CONTACT, 'd', |
85 | Trans.StringId.KEY_ACTION_DELETE_CONTACT) { | |
86 | @Override | |
87 | public Object getObject() { | |
88 | return getSelectedContact(); | |
89 | } | |
90 | }); | |
91 | actions.add(new KeyAction(Mode.SAVE_CARD, 's', | |
92 | Trans.StringId.KEY_ACTION_SAVE_CARD) { | |
fae07ea7 NR |
93 | @Override |
94 | public Object getObject() { | |
bcb54330 | 95 | return card; |
fae07ea7 NR |
96 | } |
97 | }); | |
98 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter, | |
99 | Trans.StringId.KEY_ACTION_VIEW_CONTACT) { | |
100 | @Override | |
101 | public Object getObject() { | |
bcb54330 | 102 | return getSelectedContact(); |
fae07ea7 NR |
103 | } |
104 | }); | |
bcb54330 | 105 | actions.add(new KeyAction(Mode.NONE, KeyType.Tab, |
fae07ea7 NR |
106 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { |
107 | @Override | |
108 | public boolean onAction() { | |
109 | switchFormat(); | |
110 | return false; | |
111 | } | |
112 | }); | |
113 | ||
114 | return actions; | |
115 | } | |
116 | ||
117 | @Override | |
118 | public DataType getDataType() { | |
119 | return DataType.CARD; | |
120 | } | |
121 | ||
122 | @Override | |
123 | public Mode getMode() { | |
124 | return Mode.CONTACT_LIST; | |
125 | } | |
126 | ||
127 | @Override | |
128 | public String getTitle() { | |
0b0b2b0f NR |
129 | if (card != null) { |
130 | return card.getName(); | |
131 | } | |
132 | ||
fae07ea7 NR |
133 | return null; |
134 | } | |
135 | ||
136 | @Override | |
9c8baf0c NR |
137 | protected List<TextPart> getLabel(int index, int width, boolean selected, |
138 | boolean focused) { | |
bcb54330 NR |
139 | List<TextPart> parts = new LinkedList<TextPart>(); |
140 | ||
141 | Contact contact = null; | |
142 | if (index > -1 && index < card.size()) | |
143 | contact = card.get(index); | |
144 | ||
145 | if (contact == null) | |
146 | return parts; | |
9c8baf0c NR |
147 | |
148 | Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED | |
149 | : Element.CONTACT_LINE; | |
150 | Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED | |
151 | : Element.CONTACT_LINE_SEPARATOR; | |
0b0b2b0f NR |
152 | Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED |
153 | : Element.CONTACT_LINE_DIRTY; | |
9c8baf0c | 154 | |
0b0b2b0f | 155 | width -= 2; // dirty mark space |
9c8baf0c | 156 | |
bcb54330 NR |
157 | String[] array = contact.toStringArray(format, getSeparator(), " ", |
158 | width); | |
0b0b2b0f | 159 | |
bcb54330 | 160 | if (contact.isDirty()) { |
0b0b2b0f NR |
161 | parts.add(new TextPart(" ", el)); |
162 | parts.add(new TextPart("*", elDirty)); | |
163 | } else { | |
164 | parts.add(new TextPart(" ", elSep)); | |
9c8baf0c NR |
165 | } |
166 | ||
0b0b2b0f NR |
167 | boolean separator = false; |
168 | for (String str : array) { | |
169 | parts.add(new TextPart(str, (separator ? elSep : el))); | |
170 | separator = !separator; | |
171 | } | |
9c8baf0c NR |
172 | |
173 | return parts; | |
fae07ea7 NR |
174 | } |
175 | ||
bcb54330 NR |
176 | /** |
177 | * Return the currently selected {@link Contact}. | |
178 | * | |
179 | * @return the currently selected {@link Contact} | |
180 | */ | |
181 | private Contact getSelectedContact() { | |
182 | int index = getSelectedIndex(); | |
183 | if (index > -1 && index < card.size()) | |
184 | return card.get(index); | |
185 | return null; | |
186 | } | |
187 | ||
fae07ea7 NR |
188 | private void switchFormat() { |
189 | if (formats.size() == 0) | |
190 | return; | |
191 | ||
192 | selectedFormat++; | |
193 | if (selectedFormat >= formats.size()) { | |
194 | selectedFormat = 0; | |
195 | } | |
196 | ||
197 | format = formats.get(selectedFormat); | |
198 | ||
199 | invalidate(); | |
200 | } | |
201 | } |