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