Commit | Line | Data |
---|---|---|
fae07ea7 NR |
1 | package be.nikiroo.jvcard.tui.panes; |
2 | ||
bcb54330 | 3 | import java.util.LinkedList; |
fae07ea7 NR |
4 | import java.util.List; |
5 | ||
6 | import be.nikiroo.jvcard.Contact; | |
7 | import be.nikiroo.jvcard.Data; | |
bcb54330 NR |
8 | import be.nikiroo.jvcard.TypeInfo; |
9 | import be.nikiroo.jvcard.i18n.Trans; | |
fae07ea7 NR |
10 | import be.nikiroo.jvcard.tui.KeyAction; |
11 | import be.nikiroo.jvcard.tui.KeyAction.DataType; | |
12 | import be.nikiroo.jvcard.tui.KeyAction.Mode; | |
bcb54330 | 13 | import be.nikiroo.jvcard.tui.StringUtils; |
78e4af97 | 14 | import be.nikiroo.jvcard.tui.UiColors; |
bcb54330 | 15 | import be.nikiroo.jvcard.tui.UiColors.Element; |
fae07ea7 | 16 | |
78e4af97 NR |
17 | import com.googlecode.lanterna.input.KeyType; |
18 | ||
296a0b75 | 19 | public class ContactDetailsRaw extends MainContentList { |
fae07ea7 | 20 | private Contact contact; |
bcb54330 | 21 | private int mode; |
fae07ea7 | 22 | |
296a0b75 | 23 | public ContactDetailsRaw(Contact contact) { |
bcb54330 | 24 | super(null, null); |
fae07ea7 NR |
25 | |
26 | this.contact = contact; | |
bcb54330 | 27 | this.mode = 0; |
fae07ea7 | 28 | |
78e4af97 | 29 | for (int i = 0; i < contact.size(); i++) { |
bcb54330 | 30 | addItem("[detail line]"); |
fae07ea7 NR |
31 | } |
32 | } | |
33 | ||
bcb54330 NR |
34 | @Override |
35 | protected List<TextPart> getLabel(int index, int width, boolean selected, | |
36 | boolean focused) { | |
37 | // TODO: from ini file? | |
38 | int SIZE_COL_1 = 15; | |
39 | ||
40 | Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED | |
41 | : Element.CONTACT_LINE; | |
42 | Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED | |
43 | : Element.CONTACT_LINE_SEPARATOR; | |
44 | Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED | |
45 | : Element.CONTACT_LINE_DIRTY; | |
46 | ||
78e4af97 | 47 | Data data = contact.get(index); |
bcb54330 NR |
48 | |
49 | List<TextPart> parts = new LinkedList<TextPart>(); | |
50 | if (data.isDirty()) { | |
51 | parts.add(new TextPart(" ", el)); | |
52 | parts.add(new TextPart("*", elDirty)); | |
53 | } else { | |
54 | parts.add(new TextPart(" ", elSep)); | |
55 | } | |
56 | String name = " " + data.getName() + " "; | |
57 | String value = null; | |
58 | ||
59 | StringBuilder valueBuilder = new StringBuilder(" "); | |
60 | switch (mode) { | |
61 | case 0: | |
62 | valueBuilder.append(data.getValue()); | |
63 | if (data.getGroup() != null && data.getGroup().length() > 0) { | |
64 | valueBuilder.append("("); | |
65 | valueBuilder.append(data.getGroup()); | |
66 | valueBuilder.append(")"); | |
67 | } | |
68 | break; | |
69 | case 1: | |
78e4af97 NR |
70 | for (int indexType = 0; indexType < data.size(); indexType++) { |
71 | TypeInfo type = data.get(indexType); | |
bcb54330 NR |
72 | if (valueBuilder.length() > 1) |
73 | valueBuilder.append(", "); | |
74 | valueBuilder.append(type.getName()); | |
75 | valueBuilder.append(": "); | |
76 | valueBuilder.append(type.getValue()); | |
77 | } | |
78 | break; | |
79 | } | |
80 | valueBuilder.append(" "); | |
81 | ||
82 | value = valueBuilder.toString(); | |
83 | ||
296a0b75 NR |
84 | name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode()); |
85 | value = StringUtils.sanitize(value, UiColors.getInstance().isUnicode()); | |
86 | ||
bcb54330 NR |
87 | name = StringUtils.padString(name, SIZE_COL_1); |
88 | value = StringUtils.padString(value, width - SIZE_COL_1 | |
89 | - getSeparator().length() - 2); | |
90 | ||
91 | parts.add(new TextPart(name, el)); | |
92 | parts.add(new TextPart(getSeparator(), elSep)); | |
93 | parts.add(new TextPart(value, el)); | |
94 | ||
95 | return parts; | |
96 | }; | |
97 | ||
fae07ea7 NR |
98 | @Override |
99 | public DataType getDataType() { | |
bcb54330 | 100 | return DataType.DATA; |
fae07ea7 | 101 | } |
ae22c247 | 102 | |
fae07ea7 NR |
103 | @Override |
104 | public List<KeyAction> getKeyBindings() { | |
105 | // TODO Auto-generated method stub | |
bcb54330 NR |
106 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
107 | ||
108 | // TODO: add, remove | |
78e4af97 | 109 | actions.add(new KeyAction(Mode.ASK_USER, KeyType.Enter, |
ae22c247 | 110 | Trans.StringId.DUMMY) { |
bcb54330 NR |
111 | @Override |
112 | public Object getObject() { | |
78e4af97 | 113 | return contact.get(getSelectedIndex()); |
bcb54330 | 114 | } |
ae22c247 NR |
115 | |
116 | @Override | |
117 | public String getQuestion() { | |
118 | Data data = getData(); | |
119 | if (data != null) { | |
120 | return data.getName(); | |
121 | } | |
122 | ||
123 | return null; | |
124 | } | |
125 | ||
126 | @Override | |
127 | public String getDefaultAnswer() { | |
128 | Data data = getData(); | |
129 | if (data != null) { | |
130 | return data.getValue(); | |
131 | } | |
132 | ||
133 | return null; | |
134 | } | |
135 | ||
136 | @Override | |
137 | public String callback(String answer) { | |
138 | Data data = getData(); | |
139 | if (data != null) { | |
140 | data.setValue(answer); | |
141 | return null; | |
142 | } | |
143 | ||
144 | // TODO: i18n | |
145 | return "Cannot modify value"; | |
146 | } | |
bcb54330 NR |
147 | }); |
148 | actions.add(new KeyAction(Mode.NONE, KeyType.Tab, | |
149 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { | |
150 | @Override | |
151 | public boolean onAction() { | |
152 | mode++; | |
153 | if (mode > 1) | |
154 | mode = 0; | |
155 | ||
156 | return false; | |
157 | } | |
158 | }); | |
159 | ||
160 | return actions; | |
fae07ea7 NR |
161 | } |
162 | ||
fae07ea7 NR |
163 | @Override |
164 | public String getTitle() { | |
0b0b2b0f NR |
165 | String title = null; |
166 | ||
167 | if (contact != null) { | |
168 | title = contact.getPreferredDataValue("FN"); | |
169 | if (title == null || title.length() == 0) | |
170 | title = contact.getPreferredDataValue("N"); | |
171 | } | |
172 | ||
173 | return title; | |
fae07ea7 NR |
174 | } |
175 | ||
176 | @Override | |
177 | public String move(int x, int y) { | |
178 | // TODO Auto-generated method stub | |
179 | return null; | |
180 | } | |
181 | } |