Add text-image control and separate Edit/View contact
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
1 package be.nikiroo.jvcard.tui.panes;
2
3 import java.awt.Image;
4 import java.util.Base64;
5 import java.util.List;
6
7 import javax.swing.ImageIcon;
8
9 import be.nikiroo.jvcard.Contact;
10 import be.nikiroo.jvcard.Data;
11 import be.nikiroo.jvcard.TypeInfo;
12 import be.nikiroo.jvcard.tui.ImageText;
13 import be.nikiroo.jvcard.tui.KeyAction;
14 import be.nikiroo.jvcard.tui.KeyAction.DataType;
15
16 import com.googlecode.lanterna.TerminalSize;
17 import com.googlecode.lanterna.gui2.BorderLayout;
18 import com.googlecode.lanterna.gui2.Panel;
19 import com.googlecode.lanterna.gui2.TextBox;
20
21 public class ContactDetails extends MainContent {
22 private Contact contact;
23
24 @Override
25 public DataType getDataType() {
26 return DataType.DATA;
27 }
28
29 @Override
30 public List<KeyAction> getKeyBindings() {
31 // TODO Auto-generated method stub
32 return null;
33 }
34
35 public ContactDetails(Contact contact) {
36 this.contact = contact;
37
38 BorderLayout blayout = new BorderLayout();
39 setLayoutManager(blayout);
40
41 Panel top = new Panel();
42 if (contact != null) {
43 Data photo = contact.getPreferredData("PHOTO");
44 if (photo != null) {
45 TypeInfo encoding = null;
46 TypeInfo type = null;
47 for (TypeInfo info : photo.getTypes()) {
48 if (info.getName() != null) {
49 if (info.getName().equalsIgnoreCase("ENCODING"))
50 encoding = info;
51 if (info.getName().equalsIgnoreCase("TYPE"))
52 type = info;
53 }
54 }
55
56 if (encoding != null && encoding.getValue() != null
57 && encoding.getValue().equalsIgnoreCase("b")) {
58
59 Image img = new ImageIcon(Base64.getDecoder().decode(
60 photo.getValue())).getImage();
61
62 TerminalSize size = new TerminalSize(40, 20);
63 size = new TerminalSize(120, 50);
64
65 String str = new ImageText(img, size).getText();
66 top.addComponent(new TextBox(size, str));
67 }
68 }
69 }
70
71 addComponent(top, BorderLayout.Location.TOP);
72 }
73 }