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
.i18n
.Trans
;
11 import be
.nikiroo
.jvcard
.resources
.Bundles
;
12 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
13 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
14 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
15 import be
.nikiroo
.jvcard
.tui
.UiColors
;
16 import be
.nikiroo
.jvcard
.tui
.UiColors
.Element
;
18 import com
.googlecode
.lanterna
.input
.KeyType
;
20 public class ContactList
extends MainContentList
{
22 private List
<Contact
> contacts
;
23 private String filter
;
25 private List
<String
> formats
;
26 private int selectedFormat
;
27 private String format
;
29 public ContactList(Card card
) {
30 formats
= new LinkedList
<String
>();
31 for (String format
: Bundles
.getBundle("display")
32 .getString("CONTACT_LIST_FORMAT").split(",")) {
43 * Change the currently displayed contacts card, only allowing those that
44 * satisfy the current filter.
47 * the new {@link Card}
49 * the text filter or NULL for all contacts
51 public void setCard(Card card
) {
54 this.contacts
= new LinkedList
<Contact
>();
57 for (Contact c
: card
) {
59 || c
.toString(format
).toLowerCase()
60 .contains(filter
.toLowerCase())) {
71 public void refreshData() {
72 int index
= getSelectedIndex();
74 if (index
>= contacts
.size())
75 index
= contacts
.size() - 1;
76 setSelectedIndex(index
);
82 public String
getExitWarning() {
83 if (card
!= null && card
.isDirty()) {
84 return "Ignore unsaved changes? [Y/N]";
91 public List
<KeyAction
> getKeyBindings() {
92 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
95 actions
.add(new KeyAction(Mode
.ASK_USER
, 'a', Trans
.StringId
.DUMMY
) {
97 public Object
getObject() {
102 public String
getQuestion() {
104 return "new contact name: ";
108 public String
callback(String answer
) {
109 if (answer
.length() > 0) {
110 List
<Data
> datas
= new LinkedList
<Data
>();
111 datas
.add(new Data(null, "FN", answer
, null));
112 getCard().add(new Contact(datas
));
119 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 'd',
120 Trans
.StringId
.KEY_ACTION_DELETE_CONTACT
) {
122 public Object
getObject() {
123 return getSelectedContact();
127 public String
getQuestion() {
129 return "Delete contact? [Y/N]";
133 public String
callback(String answer
) {
134 if (answer
.equalsIgnoreCase("y")) {
135 Contact contact
= getSelectedContact();
136 if (contact
!= null && contact
.delete()) {
142 return "Cannot delete contact";
148 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 's',
149 Trans
.StringId
.KEY_ACTION_SAVE_CARD
) {
151 public Object
getObject() {
156 public String
getQuestion() {
157 return "Save changes? [Y/N]";
161 public String
callback(String answer
) {
162 if (answer
.equalsIgnoreCase("y")) {
165 if (card
!= null && card
.save())
167 } catch (IOException ioe
) {
168 ioe
.printStackTrace();
172 return "Cannot save to file";
180 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, KeyType
.Enter
,
181 Trans
.StringId
.KEY_ACTION_VIEW_CONTACT
) {
183 public Object
getObject() {
184 return getSelectedContact();
187 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
188 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
190 public boolean onAction() {
195 actions
.add(new KeyAction(Mode
.ASK_USER
, 'w',
196 Trans
.StringId
.KEY_ACTION_SEARCH
) {
199 public String
getQuestion() {
204 public String
getDefaultAnswer() {
209 public String
callback(String answer
) {
220 public DataType
getDataType() {
221 return DataType
.CARD
;
225 public String
getTitle() {
228 return card
.getName() + " [" + filter
+ "]";
229 return card
.getName();
236 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
238 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
240 Contact contact
= null;
241 if (index
> -1 && index
< contacts
.size())
242 contact
= contacts
.get(index
);
247 Element el
= (focused
&& selected
) ? Element
.CONTACT_LINE_SELECTED
248 : Element
.CONTACT_LINE
;
249 Element elSep
= (focused
&& selected
) ? Element
.CONTACT_LINE_SEPARATOR_SELECTED
250 : Element
.CONTACT_LINE_SEPARATOR
;
251 Element elDirty
= (focused
&& selected
) ? Element
.CONTACT_LINE_DIRTY_SELECTED
252 : Element
.CONTACT_LINE_DIRTY
;
254 width
-= 2; // dirty mark space
256 String
[] array
= contact
.toStringArray(format
, getSeparator(), " ",
257 width
, UiColors
.getInstance().isUnicode());
259 if (contact
.isDirty()) {
260 parts
.add(new TextPart(" ", el
));
261 parts
.add(new TextPart("*", elDirty
));
263 parts
.add(new TextPart(" ", elSep
));
266 boolean separator
= false;
267 for (String str
: array
) {
268 parts
.add(new TextPart(str
, (separator ? elSep
: el
)));
269 separator
= !separator
;
276 * Return the currently selected {@link Contact}.
278 * @return the currently selected {@link Contact}
280 private Contact
getSelectedContact() {
281 int index
= getSelectedIndex();
282 if (index
> -1 && index
< contacts
.size())
283 return contacts
.get(index
);
287 private void switchFormat() {
288 if (formats
.size() == 0)
292 if (selectedFormat
>= formats
.size()) {
296 format
= formats
.get(selectedFormat
);