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