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
.i18n
.Trans
;
10 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
11 import be
.nikiroo
.jvcard
.tui
.UiColors
;
12 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
13 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
14 import be
.nikiroo
.jvcard
.tui
.UiColors
.Element
;
16 import com
.googlecode
.lanterna
.input
.KeyType
;
18 public class ContactList
extends MainContentList
{
20 private List
<Contact
> contacts
;
21 private String filter
;
23 private List
<String
> formats
= new LinkedList
<String
>();
24 private int selectedFormat
= -1;
25 private String format
= "";
27 public ContactList(Card card
) {
28 // TODO: should get that in an INI file
29 formats
.add("NICKNAME@3|FN@+|EMAIL@30");
30 formats
.add("FN@+|EMAIL@40");
37 * Change the currently displayed contacts card, only allowing those that
38 * satisfy the current filter.
41 * the new {@link Card}
43 * the text filter or NULL for all contacts
45 public void setCard(Card card
) {
48 this.contacts
= new LinkedList
<Contact
>();
51 for (int i
= 0; i
< card
.size(); i
++) {
52 Contact c
= card
.get(i
);
54 || c
.toString(format
).toLowerCase()
55 .contains(filter
.toLowerCase())) {
56 addItem("[contact line]");
66 public void refreshData() {
67 int index
= getSelectedIndex();
69 if (index
>= contacts
.size())
70 index
= contacts
.size() - 1;
71 setSelectedIndex(index
);
77 public String
getExitWarning() {
78 if (card
!= null && card
.isDirty()) {
79 return "Ignore unsaved changes? [Y/N]";
86 public List
<KeyAction
> getKeyBindings() {
87 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
90 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS_RAW
, 'e',
91 Trans
.StringId
.KEY_ACTION_EDIT_CONTACT
) {
93 public Object
getObject() {
94 return getSelectedContact();
97 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 'd',
98 Trans
.StringId
.KEY_ACTION_DELETE_CONTACT
) {
100 public Object
getObject() {
101 return getSelectedContact();
105 public String
getQuestion() {
107 return "Delete contact? [Y/N]";
111 public String
callback(String answer
) {
112 if (answer
.equalsIgnoreCase("y")) {
113 Contact contact
= getSelectedContact();
114 if (contact
!= null && contact
.delete()) {
119 return "Cannot delete contact";
125 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 's',
126 Trans
.StringId
.KEY_ACTION_SAVE_CARD
) {
128 public Object
getObject() {
133 public String
getQuestion() {
134 return "Save changes? [Y/N]";
138 public String
callback(String answer
) {
139 if (answer
.equalsIgnoreCase("y")) {
142 if (card
!= null && card
.save())
144 } catch (IOException ioe
) {
145 ioe
.printStackTrace();
149 return "Cannot save to file";
157 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, KeyType
.Enter
,
158 Trans
.StringId
.KEY_ACTION_VIEW_CONTACT
) {
160 public Object
getObject() {
161 return getSelectedContact();
164 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
165 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
167 public boolean onAction() {
172 actions
.add(new KeyAction(Mode
.ASK_USER
, 'w',
173 Trans
.StringId
.KEY_ACTION_SEARCH
) {
176 public String
getQuestion() {
181 public String
getDefaultAnswer() {
186 public String
callback(String answer
) {
197 public DataType
getDataType() {
198 return DataType
.CARD
;
202 public String
getTitle() {
205 return card
.getName() + " [" + filter
+ "]";
206 return card
.getName();
213 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
215 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
217 Contact contact
= null;
218 if (index
> -1 && index
< contacts
.size())
219 contact
= contacts
.get(index
);
224 Element el
= (focused
&& selected
) ? Element
.CONTACT_LINE_SELECTED
225 : Element
.CONTACT_LINE
;
226 Element elSep
= (focused
&& selected
) ? Element
.CONTACT_LINE_SEPARATOR_SELECTED
227 : Element
.CONTACT_LINE_SEPARATOR
;
228 Element elDirty
= (focused
&& selected
) ? Element
.CONTACT_LINE_DIRTY_SELECTED
229 : Element
.CONTACT_LINE_DIRTY
;
231 width
-= 2; // dirty mark space
233 String
[] array
= contact
.toStringArray(format
, getSeparator(), " ",
234 width
, UiColors
.getInstance().isUnicode());
236 if (contact
.isDirty()) {
237 parts
.add(new TextPart(" ", el
));
238 parts
.add(new TextPart("*", elDirty
));
240 parts
.add(new TextPart(" ", elSep
));
243 boolean separator
= false;
244 for (String str
: array
) {
245 parts
.add(new TextPart(str
, (separator ? elSep
: el
)));
246 separator
= !separator
;
253 * Return the currently selected {@link Contact}.
255 * @return the currently selected {@link Contact}
257 private Contact
getSelectedContact() {
258 int index
= getSelectedIndex();
259 if (index
> -1 && index
< contacts
.size())
260 return contacts
.get(index
);
264 private void switchFormat() {
265 if (formats
.size() == 0)
269 if (selectedFormat
>= formats
.size()) {
273 format
= formats
.get(selectedFormat
);