Commit | Line | Data |
---|---|---|
fae07ea7 NR |
1 | package be.nikiroo.jvcard.tui.panes; |
2 | ||
3 | import java.util.LinkedList; | |
4 | import java.util.List; | |
5 | ||
6 | import be.nikiroo.jvcard.Card; | |
0b0b2b0f | 7 | import be.nikiroo.jvcard.Contact; |
fae07ea7 NR |
8 | import be.nikiroo.jvcard.i18n.Trans; |
9 | import be.nikiroo.jvcard.tui.KeyAction; | |
10 | import be.nikiroo.jvcard.tui.UiColors; | |
11 | import be.nikiroo.jvcard.tui.KeyAction.DataType; | |
12 | import be.nikiroo.jvcard.tui.KeyAction.Mode; | |
9c8baf0c | 13 | import be.nikiroo.jvcard.tui.UiColors.Element; |
fae07ea7 NR |
14 | |
15 | import com.googlecode.lanterna.input.KeyType; | |
16 | ||
17 | public class ContactList extends MainContentList { | |
18 | private Card card; | |
19 | ||
20 | private List<String> formats = new LinkedList<String>(); | |
21 | private int selectedFormat = -1; | |
22 | private String format = ""; | |
23 | ||
24 | public ContactList(Card card) { | |
25 | super(UiColors.Element.CONTACT_LINE, | |
26 | UiColors.Element.CONTACT_LINE_SELECTED); | |
27 | ||
28 | // TODO: should get that in an INI file | |
29 | formats.add("NICKNAME@3|FN@+|EMAIL@30"); | |
30 | formats.add("FN@+|EMAIL@40"); | |
31 | switchFormat(); | |
32 | ||
33 | setCard(card); | |
34 | } | |
35 | ||
36 | /** | |
37 | * Change the currently displayed contacts card. | |
38 | * | |
39 | * @param card | |
40 | * the new {@link Card} | |
41 | */ | |
42 | public void setCard(Card card) { | |
43 | clearItems(); | |
44 | this.card = card; | |
45 | ||
46 | if (card != null) { | |
47 | for (int i = 0; i < card.getContacts().size(); i++) { | |
48 | addItem("[contact line]"); | |
49 | } | |
50 | } | |
51 | ||
52 | setSelectedIndex(0); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public String getExitWarning() { | |
57 | if (card != null && card.isDirty()) { | |
0b0b2b0f NR |
58 | //TODO: save? [y/n] instead |
59 | return "Some of your contact information is not saved; ignore? [Y/N]"; | |
fae07ea7 | 60 | } |
0b0b2b0f | 61 | |
fae07ea7 NR |
62 | return null; |
63 | } | |
64 | ||
65 | @Override | |
66 | public List<KeyAction> getKeyBindings() { | |
67 | List<KeyAction> actions = new LinkedList<KeyAction>(); | |
68 | ||
69 | // TODO del, save... | |
0b0b2b0f NR |
70 | // TODO: remove |
71 | actions.add(new KeyAction(Mode.NONE, 'd', Trans.StringId.DUMMY) { | |
72 | @Override | |
73 | public boolean onAction() { | |
74 | //TODO dummy action | |
75 | int index = getSelectedIndex(); | |
76 | Contact c = card.getContacts().get(index); | |
77 | c.updateFrom(c); | |
78 | return false; | |
79 | } | |
80 | }); | |
fae07ea7 NR |
81 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, 'e', |
82 | Trans.StringId.KEY_ACTION_EDIT_CONTACT) { | |
83 | @Override | |
84 | public Object getObject() { | |
85 | int index = getSelectedIndex(); | |
86 | return card.getContacts().get(index); | |
87 | } | |
88 | }); | |
89 | actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter, | |
90 | Trans.StringId.KEY_ACTION_VIEW_CONTACT) { | |
91 | @Override | |
92 | public Object getObject() { | |
93 | int index = getSelectedIndex(); | |
94 | return card.getContacts().get(index); | |
95 | } | |
96 | }); | |
97 | actions.add(new KeyAction(Mode.SWICTH_FORMAT, KeyType.Tab, | |
98 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { | |
99 | @Override | |
100 | public boolean onAction() { | |
101 | switchFormat(); | |
102 | return false; | |
103 | } | |
104 | }); | |
105 | ||
106 | return actions; | |
107 | } | |
108 | ||
109 | @Override | |
110 | public DataType getDataType() { | |
111 | return DataType.CARD; | |
112 | } | |
113 | ||
114 | @Override | |
115 | public Mode getMode() { | |
116 | return Mode.CONTACT_LIST; | |
117 | } | |
118 | ||
119 | @Override | |
120 | public String getTitle() { | |
0b0b2b0f NR |
121 | if (card != null) { |
122 | return card.getName(); | |
123 | } | |
124 | ||
fae07ea7 NR |
125 | return null; |
126 | } | |
127 | ||
128 | @Override | |
9c8baf0c NR |
129 | protected List<TextPart> getLabel(int index, int width, boolean selected, |
130 | boolean focused) { | |
0b0b2b0f | 131 | Contact c = card.getContacts().get(index); |
9c8baf0c NR |
132 | |
133 | Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED | |
134 | : Element.CONTACT_LINE; | |
135 | Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED | |
136 | : Element.CONTACT_LINE_SEPARATOR; | |
0b0b2b0f NR |
137 | Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED |
138 | : Element.CONTACT_LINE_DIRTY; | |
9c8baf0c | 139 | |
0b0b2b0f | 140 | width -= 2; // dirty mark space |
9c8baf0c | 141 | |
fae07ea7 | 142 | // we could use: " ", "┃", "│"... |
0b0b2b0f NR |
143 | String[] array = c.toStringArray(format, "┃", " ", width); |
144 | ||
145 | List<TextPart> parts = new LinkedList<TextPart>(); | |
146 | if (c.isDirty()) { | |
147 | parts.add(new TextPart(" ", el)); | |
148 | parts.add(new TextPart("*", elDirty)); | |
149 | } else { | |
150 | parts.add(new TextPart(" ", elSep)); | |
9c8baf0c NR |
151 | } |
152 | ||
0b0b2b0f NR |
153 | boolean separator = false; |
154 | for (String str : array) { | |
155 | parts.add(new TextPart(str, (separator ? elSep : el))); | |
156 | separator = !separator; | |
157 | } | |
9c8baf0c NR |
158 | |
159 | return parts; | |
fae07ea7 NR |
160 | } |
161 | ||
162 | private void switchFormat() { | |
163 | if (formats.size() == 0) | |
164 | return; | |
165 | ||
166 | selectedFormat++; | |
167 | if (selectedFormat >= formats.size()) { | |
168 | selectedFormat = 0; | |
169 | } | |
170 | ||
171 | format = formats.get(selectedFormat); | |
172 | ||
173 | invalidate(); | |
174 | } | |
175 | } |