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