Commit | Line | Data |
---|---|---|
f04d8b1c NR |
1 | package be.nikiroo.jvcard.tui.panes; |
2 | ||
3 | import java.awt.Image; | |
25232463 | 4 | import java.util.LinkedList; |
f04d8b1c | 5 | import java.util.List; |
3634193b NR |
6 | import java.util.MissingResourceException; |
7 | import java.util.ResourceBundle; | |
f04d8b1c | 8 | |
f04d8b1c NR |
9 | import be.nikiroo.jvcard.Contact; |
10 | import be.nikiroo.jvcard.Data; | |
11 | import be.nikiroo.jvcard.TypeInfo; | |
3634193b NR |
12 | import be.nikiroo.jvcard.resources.Bundles; |
13 | import be.nikiroo.jvcard.resources.StringUtils; | |
7da41ecd | 14 | import be.nikiroo.jvcard.resources.Trans; |
25232463 | 15 | import be.nikiroo.jvcard.tui.ImageTextControl; |
f04d8b1c NR |
16 | import be.nikiroo.jvcard.tui.KeyAction; |
17 | import be.nikiroo.jvcard.tui.KeyAction.DataType; | |
25232463 | 18 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
f82bad11 | 19 | import be.nikiroo.jvcard.tui.UiColors; |
f04d8b1c NR |
20 | |
21 | import com.googlecode.lanterna.TerminalSize; | |
22 | import com.googlecode.lanterna.gui2.BorderLayout; | |
3634193b | 23 | import com.googlecode.lanterna.gui2.Borders; |
f82bad11 NR |
24 | import com.googlecode.lanterna.gui2.Direction; |
25 | import com.googlecode.lanterna.gui2.Label; | |
26 | import com.googlecode.lanterna.gui2.LinearLayout; | |
f04d8b1c | 27 | import com.googlecode.lanterna.gui2.Panel; |
25232463 | 28 | import com.googlecode.lanterna.input.KeyType; |
f04d8b1c NR |
29 | |
30 | public class ContactDetails extends MainContent { | |
31 | private Contact contact; | |
ae22c247 | 32 | private Panel top; |
f82bad11 | 33 | private ImageTextControl txtImage; |
ae22c247 NR |
34 | private Image image; |
35 | private boolean fullscreenImage; | |
f82bad11 NR |
36 | private Panel infoPanel; |
37 | private Label note; | |
3634193b | 38 | ResourceBundle map; |
f04d8b1c NR |
39 | |
40 | public ContactDetails(Contact contact) { | |
3634193b NR |
41 | map = Bundles.getBundle("display"); |
42 | ||
f04d8b1c NR |
43 | BorderLayout blayout = new BorderLayout(); |
44 | setLayoutManager(blayout); | |
45 | ||
ae22c247 | 46 | top = new Panel(); |
f82bad11 NR |
47 | blayout = new BorderLayout(); |
48 | top.setLayoutManager(blayout); | |
49 | ||
50 | infoPanel = new Panel(); | |
51 | infoPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL)); | |
52 | top.addComponent(infoPanel, BorderLayout.Location.CENTER); | |
53 | ||
54 | Panel notePanel = new Panel(); | |
55 | notePanel.setLayoutManager(new LinearLayout(Direction.HORIZONTAL)); | |
56 | ||
57 | notePanel.addComponent(UiColors.Element.VIEW_CONTACT_NOTES_TITLE | |
58 | .createLabel("Notes:")); | |
59 | note = UiColors.Element.VIEW_CONTACT_NORMAL.createLabel(""); | |
60 | notePanel.addComponent(note); | |
61 | ||
ae22c247 | 62 | setContact(contact); |
f82bad11 | 63 | |
3634193b NR |
64 | addComponent(top.withBorder(Borders.doubleLineBevel()), |
65 | BorderLayout.Location.TOP); | |
66 | addComponent(notePanel.withBorder(Borders.singleLineBevel()), | |
67 | BorderLayout.Location.CENTER); | |
ae22c247 NR |
68 | } |
69 | ||
f82bad11 NR |
70 | /** |
71 | * Change the enclosed {@link Contact} from this {@link ContactDetails}. | |
2a96e7b2 | 72 | * Also re-set the image. |
f82bad11 NR |
73 | * |
74 | * @param contact | |
75 | * the new {@link Contact} | |
76 | */ | |
ae22c247 | 77 | public void setContact(Contact contact) { |
ae22c247 | 78 | this.contact = contact; |
2a96e7b2 | 79 | image = null; |
ae22c247 | 80 | |
2a96e7b2 | 81 | if (contact != null) { |
f82bad11 NR |
82 | infoPanel.removeAllComponents(); |
83 | ||
84 | String name = contact.getPreferredDataValue("FN"); | |
85 | if (name == null || name.length() == 0) { | |
86 | // TODO format it ourself | |
87 | name = contact.getPreferredDataValue("N"); | |
88 | } | |
89 | ||
f82bad11 NR |
90 | infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NAME |
91 | .createLabel(name)); | |
f82bad11 NR |
92 | infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL |
93 | .createLabel("")); | |
3634193b NR |
94 | |
95 | // List of infos: | |
96 | int labelSize = -1; | |
97 | try { | |
98 | labelSize = Integer.parseInt(map | |
99 | .getString("CONTACT_DETAILS_LABEL_WIDTH")); | |
100 | ||
101 | } catch (NumberFormatException e) { | |
102 | e.printStackTrace(); | |
103 | labelSize = -1; | |
104 | } catch (MissingResourceException e) { | |
105 | labelSize = -1; | |
106 | } | |
107 | ||
108 | String infoFormat = ""; | |
109 | try { | |
110 | infoFormat = map.getString("CONTACT_DETAILS_INFO"); | |
111 | } catch (MissingResourceException e) { | |
112 | e.printStackTrace(); | |
113 | } | |
114 | ||
115 | String[] infos = infoFormat.split("\\|"); | |
116 | for (String info : infos) { | |
117 | // # - "=FIELD" will take the preferred value for this field | |
118 | // # - "+FIELD" will take the preferred value for this field and | |
119 | // highlight it | |
120 | // # - "#FIELD" will take all the values with this field's name | |
121 | // # - "*FIELD" will take all the values with this field's name, | |
122 | // highlighting the preferred one | |
123 | // # | |
124 | ||
125 | boolean hl = false; | |
126 | boolean all = false; | |
127 | if (info.contains("+") || info.contains("#")) | |
128 | hl = true; | |
129 | if (info.contains("*") || info.contains("#")) | |
130 | all = true; | |
131 | ||
132 | if (all || hl || info.contains("=")) { | |
133 | UiColors.Element el = hl ? UiColors.Element.VIEW_CONTACT_HIGHLIGHT | |
134 | : UiColors.Element.VIEW_CONTACT_NORMAL; | |
135 | ||
136 | int index = info.indexOf('='); | |
137 | if (index < 0) | |
138 | index = info.indexOf('+'); | |
139 | if (index < 0) | |
140 | index = info.indexOf('#'); | |
141 | if (index < 0) | |
142 | index = info.indexOf('*'); | |
143 | ||
144 | String label = info.substring(0, index); | |
145 | String field = info.substring(index + 1); | |
146 | ||
147 | if (all) { | |
148 | for (Data data : contact.getData(field)) { | |
149 | if (data.isPreferred()) { | |
aecb3399 NR |
150 | infoPanel.addComponent(el |
151 | .createLabel(StringUtils.padString( | |
152 | label, labelSize) | |
153 | + data.toString())); | |
3634193b NR |
154 | } else { |
155 | infoPanel | |
aecb3399 NR |
156 | .addComponent(UiColors.Element.VIEW_CONTACT_NORMAL |
157 | .createLabel(StringUtils | |
158 | .padString(label, | |
159 | labelSize) | |
160 | + data.toString())); | |
3634193b NR |
161 | } |
162 | } | |
163 | } else { | |
aecb3399 NR |
164 | String val = contact.getPreferredDataValue(field); |
165 | if (val == null) | |
166 | val = ""; | |
3634193b | 167 | infoPanel.addComponent(el.createLabel(StringUtils |
aecb3399 | 168 | .padString(label, labelSize) + val)); |
3634193b NR |
169 | } |
170 | } else { | |
171 | String label = info; | |
172 | infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL | |
173 | .createLabel(StringUtils | |
174 | .padString(label, labelSize))); | |
175 | } | |
176 | } | |
177 | // end of list | |
178 | ||
f82bad11 NR |
179 | infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL |
180 | .createLabel("")); | |
181 | ||
182 | String notes = contact.getPreferredDataValue("NOTE"); | |
183 | if (notes == null) | |
184 | notes = ""; | |
aecb3399 | 185 | note.setText(notes); |
f82bad11 | 186 | |
f04d8b1c NR |
187 | Data photo = contact.getPreferredData("PHOTO"); |
188 | if (photo != null) { | |
189 | TypeInfo encoding = null; | |
26d254a3 | 190 | for (TypeInfo info : photo) { |
f04d8b1c NR |
191 | if (info.getName() != null) { |
192 | if (info.getName().equalsIgnoreCase("ENCODING")) | |
193 | encoding = info; | |
2a96e7b2 NR |
194 | // We don't check for the "TYPE" anymore, we just defer |
195 | // it to ImageIcon | |
f04d8b1c NR |
196 | } |
197 | } | |
198 | ||
199 | if (encoding != null && encoding.getValue() != null | |
200 | && encoding.getValue().equalsIgnoreCase("b")) { | |
201 | ||
1c03abaf | 202 | try { |
26d254a3 | 203 | image = StringUtils.toImage(photo.getValue()); |
1c03abaf NR |
204 | } catch (Exception e) { |
205 | System.err.println("Cannot parse image for contact: " | |
206 | + contact.getPreferredDataValue("UID")); | |
207 | } | |
f04d8b1c NR |
208 | } |
209 | } | |
210 | } | |
211 | ||
f82bad11 | 212 | setImage(image); |
f04d8b1c | 213 | } |
25232463 NR |
214 | |
215 | @Override | |
216 | public DataType getDataType() { | |
217 | return DataType.DATA; | |
218 | } | |
219 | ||
220 | @Override | |
221 | public List<KeyAction> getKeyBindings() { | |
222 | List<KeyAction> actions = new LinkedList<KeyAction>(); | |
223 | ||
25232463 NR |
224 | actions.add(new KeyAction(Mode.NONE, KeyType.Tab, |
225 | Trans.StringId.KEY_ACTION_SWITCH_FORMAT) { | |
226 | @Override | |
227 | public boolean onAction() { | |
f82bad11 NR |
228 | if (txtImage != null) { |
229 | txtImage.switchMode(); | |
25232463 NR |
230 | } |
231 | ||
232 | return false; | |
233 | } | |
234 | }); | |
235 | actions.add(new KeyAction(Mode.NONE, 'i', | |
ae22c247 | 236 | Trans.StringId.KEY_ACTION_INVERT) { |
25232463 NR |
237 | @Override |
238 | public boolean onAction() { | |
f82bad11 NR |
239 | if (txtImage != null) { |
240 | txtImage.invertColor(); | |
25232463 NR |
241 | } |
242 | ||
243 | return false; | |
244 | } | |
245 | }); | |
ae22c247 NR |
246 | actions.add(new KeyAction(Mode.NONE, 'f', |
247 | Trans.StringId.KEY_ACTION_FULLSCREEN) { | |
248 | @Override | |
249 | public boolean onAction() { | |
250 | fullscreenImage = !fullscreenImage; | |
251 | setImage(image); | |
252 | return false; | |
253 | } | |
254 | }); | |
3634193b NR |
255 | // TODO: add "normal" edit |
256 | actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r', | |
257 | Trans.StringId.KEY_ACTION_EDIT_CONTACT_RAW) { | |
176a8327 NR |
258 | @Override |
259 | public Object getObject() { | |
260 | return contact; | |
261 | } | |
262 | }); | |
25232463 NR |
263 | |
264 | return actions; | |
265 | } | |
ae22c247 NR |
266 | |
267 | @Override | |
268 | public synchronized Panel setSize(TerminalSize size) { | |
269 | super.setSize(size); | |
270 | setImage(image); | |
271 | return this; | |
272 | } | |
273 | ||
274 | /** | |
f82bad11 NR |
275 | * Set the {@link Image} to render and refresh it to the current size |
276 | * constraints. | |
ae22c247 NR |
277 | * |
278 | * @param image | |
279 | * the new {@link Image} | |
280 | */ | |
281 | private void setImage(Image image) { | |
282 | this.image = image; | |
283 | ||
f82bad11 NR |
284 | if (txtImage != null && top.containsComponent(txtImage)) |
285 | top.removeComponent(txtImage); | |
286 | ||
ae22c247 NR |
287 | TerminalSize size = getTxtSize(); |
288 | if (size != null) { | |
f82bad11 NR |
289 | if (txtImage != null) |
290 | txtImage.setSize(size); | |
ae22c247 | 291 | else |
f82bad11 | 292 | txtImage = new ImageTextControl(image, size); |
ae22c247 NR |
293 | } |
294 | ||
f82bad11 NR |
295 | if (size != null) { |
296 | top.addComponent(txtImage, BorderLayout.Location.LEFT); | |
297 | } | |
ae22c247 | 298 | |
f82bad11 | 299 | invalidate(); |
ae22c247 NR |
300 | } |
301 | ||
302 | /** | |
303 | * Compute the size to use for the {@link Image} text rendering. Return NULL | |
304 | * in case of error. | |
305 | * | |
306 | * @return the {@link TerminalSize} to use or NULL if it is not possible | |
307 | */ | |
308 | private TerminalSize getTxtSize() { | |
309 | if (image != null && getSize() != null && getSize().getColumns() > 0 | |
310 | && getSize().getRows() > 0) { | |
311 | if (fullscreenImage) { | |
312 | return getSize(); | |
313 | } else { | |
f82bad11 NR |
314 | // TODO: configure size? |
315 | int w = getSize().getColumns() - 40; | |
aecb3399 | 316 | int h = getSize().getRows() - 9; |
f82bad11 NR |
317 | if (w <= 0 || h <= 0) |
318 | return null; | |
319 | ||
320 | return new TerminalSize(w, h); | |
ae22c247 NR |
321 | } |
322 | } | |
323 | ||
324 | return null; | |
325 | } | |
f04d8b1c | 326 | } |