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
.Bundles
;
12 import be
.nikiroo
.jvcard
.resources
.Trans
;
13 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
14 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
15 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
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',
96 Trans
.StringId
.KEY_ACTION_ADD
) {
98 public Object
getObject() {
103 public String
getQuestion() {
104 return Main
.trans(Trans
.StringId
.ASK_USER_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() {
128 Contact contact
= getSelectedContact();
129 String contactName
= "null";
131 contactName
= "" + contact
.getPreferredDataValue("FN");
133 return Main
.trans(Trans
.StringId
.CONFIRM_USER_DELETE_CONTACT
, contactName
);
137 public String
callback(String answer
) {
138 if (answer
.equalsIgnoreCase("y")) {
139 Contact contact
= getSelectedContact();
140 if (contact
!= null && contact
.delete()) {
145 String contactName
= "null";
147 contactName
= "" + contact
.getPreferredDataValue("FN");
149 return Main
.trans(Trans
.StringId
.ERR_CANNOT_DELETE_CONTACT
,
156 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 's',
157 Trans
.StringId
.KEY_ACTION_SAVE_CARD
) {
159 public Object
getObject() {
164 public String
getQuestion() {
165 return "Save changes? [Y/N]";
169 public String
callback(String answer
) {
170 if (answer
.equalsIgnoreCase("y")) {
173 if (card
!= null && card
.save())
175 } catch (IOException ioe
) {
176 ioe
.printStackTrace();
180 return "Cannot save to file";
188 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, KeyType
.Enter
,
189 Trans
.StringId
.KEY_ACTION_VIEW_CONTACT
) {
191 public Object
getObject() {
192 return getSelectedContact();
195 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
196 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
198 public boolean onAction() {
203 actions
.add(new KeyAction(Mode
.ASK_USER
, 'w',
204 Trans
.StringId
.KEY_ACTION_SEARCH
) {
207 public String
getQuestion() {
212 public String
getDefaultAnswer() {
217 public String
callback(String answer
) {
228 public DataType
getDataType() {
229 return DataType
.CARD
;
233 public String
getTitle() {
236 return card
.getName() + " [" + filter
+ "]";
237 return card
.getName();
244 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
246 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
248 Contact contact
= null;
249 if (index
> -1 && index
< contacts
.size())
250 contact
= contacts
.get(index
);
255 Element el
= (focused
&& selected
) ? Element
.CONTACT_LINE_SELECTED
256 : Element
.CONTACT_LINE
;
257 Element elSep
= (focused
&& selected
) ? Element
.CONTACT_LINE_SEPARATOR_SELECTED
258 : Element
.CONTACT_LINE_SEPARATOR
;
259 Element elDirty
= (focused
&& selected
) ? Element
.CONTACT_LINE_DIRTY_SELECTED
260 : Element
.CONTACT_LINE_DIRTY
;
262 width
-= 2; // dirty mark space
264 String
[] array
= contact
.toStringArray(format
, getSeparator(), " ",
265 width
, Main
.isUnicode());
267 if (contact
.isDirty()) {
268 parts
.add(new TextPart(" ", el
));
269 parts
.add(new TextPart("*", elDirty
));
271 parts
.add(new TextPart(" ", elSep
));
274 boolean separator
= false;
275 for (String str
: array
) {
276 parts
.add(new TextPart(str
, (separator ? elSep
: el
)));
277 separator
= !separator
;
284 * Return the currently selected {@link Contact}.
286 * @return the currently selected {@link Contact}
288 private Contact
getSelectedContact() {
289 int index
= getSelectedIndex();
290 if (index
> -1 && index
< contacts
.size())
291 return contacts
.get(index
);
295 private void switchFormat() {
296 if (formats
.size() == 0)
300 if (selectedFormat
>= formats
.size()) {
304 format
= formats
.get(selectedFormat
);