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