1 package be
.nikiroo
.jvcard
.tui
.panes
;
3 import java
.io
.IOException
;
4 import java
.util
.LinkedList
;
7 import be
.nikiroo
.jvcard
.Card
;
8 import be
.nikiroo
.jvcard
.Contact
;
9 import be
.nikiroo
.jvcard
.Data
;
10 import be
.nikiroo
.jvcard
.launcher
.Main
;
11 import be
.nikiroo
.jvcard
.resources
.ColorOption
;
12 import be
.nikiroo
.jvcard
.resources
.DisplayBundle
;
13 import be
.nikiroo
.jvcard
.resources
.DisplayOption
;
14 import be
.nikiroo
.jvcard
.resources
.StringId
;
15 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
16 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
17 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
19 import com
.googlecode
.lanterna
.input
.KeyType
;
21 public class ContactList
extends MainContentList
{
23 private List
<Contact
> contacts
;
24 private String filter
;
26 private List
<String
> formats
;
27 private int selectedFormat
;
28 private String format
;
30 public ContactList(Card card
) {
31 DisplayBundle map
= new DisplayBundle();
32 formats
= new LinkedList
<String
>();
33 for (String format
: map
.getString(DisplayOption
.CONTACT_LIST_FORMAT
)
45 * Change the currently displayed contacts card, only allowing those that
46 * satisfy the current filter.
49 * the new {@link Card}
51 * the text filter or NULL for all contacts
53 public void setCard(Card card
) {
56 this.contacts
= new LinkedList
<Contact
>();
59 for (Contact c
: card
) {
61 || c
.toString(format
, "|").toLowerCase()
62 .contains(filter
.toLowerCase())) {
73 public void refreshData() {
74 int index
= getSelectedIndex();
76 if (index
>= contacts
.size())
77 index
= contacts
.size() - 1;
78 setSelectedIndex(index
);
84 public String
getExitWarning() {
85 if (card
!= null && card
.isDirty()) {
86 return "Ignore unsaved changes? [Y/N]";
93 public List
<KeyAction
> getKeyBindings() {
94 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
97 actions
.add(new KeyAction(Mode
.ASK_USER
, 'a',
98 StringId
.KEY_ACTION_ADD
) {
100 public Object
getObject() {
105 public String
getQuestion() {
106 return Main
.trans(StringId
.ASK_USER_CONTACT_NAME
);
110 public String
callback(String answer
) {
111 if (answer
.length() > 0) {
112 List
<Data
> datas
= new LinkedList
<Data
>();
113 datas
.add(new Data(null, "FN", answer
, null));
114 getCard().add(new Contact(datas
));
121 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 'd',
122 StringId
.KEY_ACTION_DELETE
) {
124 public Object
getObject() {
125 return getSelectedContact();
129 public String
getQuestion() {
130 Contact contact
= getSelectedContact();
131 String contactName
= "null";
133 contactName
= "" + contact
.getPreferredDataValue("FN");
136 StringId
.CONFIRM_USER_DELETE_CONTACT
,
141 public String
callback(String answer
) {
142 if (answer
.equalsIgnoreCase("y")) {
143 Contact contact
= getSelectedContact();
144 if (contact
!= null && contact
.delete()) {
149 String contactName
= "null";
151 contactName
= "" + contact
.getPreferredDataValue("FN");
154 StringId
.ERR_CANNOT_DELETE_CONTACT
,
161 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 's',
162 StringId
.KEY_ACTION_SAVE_CARD
) {
164 public Object
getObject() {
169 public String
getQuestion() {
170 return "Save changes? [Y/N]";
174 public String
callback(String answer
) {
175 if (answer
.equalsIgnoreCase("y")) {
178 if (card
!= null && card
.save())
180 } catch (IOException ioe
) {
181 ioe
.printStackTrace();
185 return "Cannot save to file";
193 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, KeyType
.Enter
,
194 StringId
.KEY_ACTION_VIEW_CONTACT
) {
196 public Object
getObject() {
197 return getSelectedContact();
200 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
201 StringId
.KEY_ACTION_SWITCH_FORMAT
) {
203 public boolean onAction() {
208 actions
.add(new KeyAction(Mode
.ASK_USER
, 'w',
209 StringId
.KEY_ACTION_SEARCH
) {
212 public String
getQuestion() {
217 public String
getDefaultAnswer() {
222 public String
callback(String answer
) {
233 public DataType
getDataType() {
234 return DataType
.CARD
;
238 public String
getTitle() {
241 return card
.getName() + " [" + filter
+ "]";
242 return card
.getName();
249 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
251 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
253 Contact contact
= null;
254 if (index
> -1 && index
< contacts
.size())
255 contact
= contacts
.get(index
);
260 ColorOption el
= (focused
&& selected
) ? ColorOption
.CONTACT_LINE_SELECTED
261 : ColorOption
.CONTACT_LINE
;
262 ColorOption elSep
= (focused
&& selected
) ? ColorOption
.CONTACT_LINE_SEPARATOR_SELECTED
263 : ColorOption
.CONTACT_LINE_SEPARATOR
;
264 ColorOption elDirty
= (focused
&& selected
) ? ColorOption
.CONTACT_LINE_DIRTY_SELECTED
265 : ColorOption
.CONTACT_LINE_DIRTY
;
267 width
-= 2; // dirty mark space
269 String
[] array
= contact
.toStringArray(format
, getSeparator(), " ",
270 width
, Main
.isUnicode());
272 if (contact
.isDirty()) {
273 parts
.add(new TextPart(" ", el
));
274 parts
.add(new TextPart("*", elDirty
));
276 parts
.add(new TextPart(" ", elSep
));
279 boolean separator
= false;
280 for (String str
: array
) {
281 parts
.add(new TextPart(str
, (separator ? elSep
: el
)));
282 separator
= !separator
;
289 * Return the currently selected {@link Contact}.
291 * @return the currently selected {@link Contact}
293 private Contact
getSelectedContact() {
294 int index
= getSelectedIndex();
295 if (index
> -1 && index
< contacts
.size())
296 return contacts
.get(index
);
300 private void switchFormat() {
301 if (formats
.size() == 0)
305 if (selectedFormat
>= formats
.size()) {
309 format
= formats
.get(selectedFormat
);