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 (int i
= 0; i
< card
.size(); i
++) {
58 Contact c
= card
.get(i
);
60 || c
.toString(format
).toLowerCase()
61 .contains(filter
.toLowerCase())) {
72 public void refreshData() {
73 int index
= getSelectedIndex();
75 if (index
>= contacts
.size())
76 index
= contacts
.size() - 1;
77 setSelectedIndex(index
);
83 public String
getExitWarning() {
84 if (card
!= null && card
.isDirty()) {
85 return "Ignore unsaved changes? [Y/N]";
92 public List
<KeyAction
> getKeyBindings() {
93 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
96 actions
.add(new KeyAction(Mode
.ASK_USER
, 'a', Trans
.StringId
.DUMMY
) {
98 public Object
getObject() {
103 public String
getQuestion() {
105 return "new contact name: ";
109 public String
callback(String answer
) {
110 if (answer
.length() > 0) {
111 List
<Data
> datas
= new LinkedList
<Data
>();
112 datas
.add(new Data(null, "FN", answer
, null));
113 getCard().add(new Contact(datas
));
120 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 'd',
121 Trans
.StringId
.KEY_ACTION_DELETE_CONTACT
) {
123 public Object
getObject() {
124 return getSelectedContact();
128 public String
getQuestion() {
130 return "Delete contact? [Y/N]";
134 public String
callback(String answer
) {
135 if (answer
.equalsIgnoreCase("y")) {
136 Contact contact
= getSelectedContact();
137 if (contact
!= null && contact
.delete()) {
143 return "Cannot delete contact";
149 actions
.add(new KeyAction(Mode
.ASK_USER_KEY
, 's',
150 Trans
.StringId
.KEY_ACTION_SAVE_CARD
) {
152 public Object
getObject() {
157 public String
getQuestion() {
158 return "Save changes? [Y/N]";
162 public String
callback(String answer
) {
163 if (answer
.equalsIgnoreCase("y")) {
166 if (card
!= null && card
.save())
168 } catch (IOException ioe
) {
169 ioe
.printStackTrace();
173 return "Cannot save to file";
181 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS
, KeyType
.Enter
,
182 Trans
.StringId
.KEY_ACTION_VIEW_CONTACT
) {
184 public Object
getObject() {
185 return getSelectedContact();
188 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
189 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
191 public boolean onAction() {
196 actions
.add(new KeyAction(Mode
.ASK_USER
, 'w',
197 Trans
.StringId
.KEY_ACTION_SEARCH
) {
200 public String
getQuestion() {
205 public String
getDefaultAnswer() {
210 public String
callback(String answer
) {
221 public DataType
getDataType() {
222 return DataType
.CARD
;
226 public String
getTitle() {
229 return card
.getName() + " [" + filter
+ "]";
230 return card
.getName();
237 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
239 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
241 Contact contact
= null;
242 if (index
> -1 && index
< contacts
.size())
243 contact
= contacts
.get(index
);
248 Element el
= (focused
&& selected
) ? Element
.CONTACT_LINE_SELECTED
249 : Element
.CONTACT_LINE
;
250 Element elSep
= (focused
&& selected
) ? Element
.CONTACT_LINE_SEPARATOR_SELECTED
251 : Element
.CONTACT_LINE_SEPARATOR
;
252 Element elDirty
= (focused
&& selected
) ? Element
.CONTACT_LINE_DIRTY_SELECTED
253 : Element
.CONTACT_LINE_DIRTY
;
255 width
-= 2; // dirty mark space
257 String
[] array
= contact
.toStringArray(format
, getSeparator(), " ",
258 width
, UiColors
.getInstance().isUnicode());
260 if (contact
.isDirty()) {
261 parts
.add(new TextPart(" ", el
));
262 parts
.add(new TextPart("*", elDirty
));
264 parts
.add(new TextPart(" ", elSep
));
267 boolean separator
= false;
268 for (String str
: array
) {
269 parts
.add(new TextPart(str
, (separator ? elSep
: el
)));
270 separator
= !separator
;
277 * Return the currently selected {@link Contact}.
279 * @return the currently selected {@link Contact}
281 private Contact
getSelectedContact() {
282 int index
= getSelectedIndex();
283 if (index
> -1 && index
< contacts
.size())
284 return contacts
.get(index
);
288 private void switchFormat() {
289 if (formats
.size() == 0)
293 if (selectedFormat
>= formats
.size()) {
297 format
= formats
.get(selectedFormat
);