KeyAction management now more generic
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
CommitLineData
f04d8b1c
NR
1package be.nikiroo.jvcard.tui.panes;
2
3import java.awt.Image;
4import java.util.Base64;
25232463 5import java.util.LinkedList;
f04d8b1c
NR
6import java.util.List;
7
8import javax.swing.ImageIcon;
9
10import be.nikiroo.jvcard.Contact;
11import be.nikiroo.jvcard.Data;
12import be.nikiroo.jvcard.TypeInfo;
25232463
NR
13import be.nikiroo.jvcard.i18n.Trans;
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;
f04d8b1c
NR
18
19import com.googlecode.lanterna.TerminalSize;
20import com.googlecode.lanterna.gui2.BorderLayout;
21import com.googlecode.lanterna.gui2.Panel;
25232463 22import com.googlecode.lanterna.input.KeyType;
f04d8b1c
NR
23
24public class ContactDetails extends MainContent {
25 private Contact contact;
ae22c247 26 private Panel top;
25232463 27 private ImageTextControl txt;
ae22c247
NR
28 private Image image;
29 private boolean fullscreenImage;
f04d8b1c
NR
30
31 public ContactDetails(Contact contact) {
32 this.contact = contact;
33
34 BorderLayout blayout = new BorderLayout();
35 setLayoutManager(blayout);
36
ae22c247
NR
37 top = new Panel();
38 setContact(contact);
39 addComponent(top, BorderLayout.Location.TOP);
40 }
41
42 public void setContact(Contact contact) {
43 Image img = null;
44 this.contact = contact;
45
f04d8b1c
NR
46 if (contact != null) {
47 Data photo = contact.getPreferredData("PHOTO");
48 if (photo != null) {
49 TypeInfo encoding = null;
50 TypeInfo type = null;
51 for (TypeInfo info : photo.getTypes()) {
52 if (info.getName() != null) {
53 if (info.getName().equalsIgnoreCase("ENCODING"))
54 encoding = info;
55 if (info.getName().equalsIgnoreCase("TYPE"))
56 type = info;
57 }
58 }
59
60 if (encoding != null && encoding.getValue() != null
61 && encoding.getValue().equalsIgnoreCase("b")) {
62
ae22c247 63 img = new ImageIcon(Base64.getDecoder().decode(
f04d8b1c 64 photo.getValue())).getImage();
f04d8b1c
NR
65 }
66 }
67 }
68
ae22c247 69 setImage(img);
f04d8b1c 70 }
25232463
NR
71
72 @Override
73 public DataType getDataType() {
74 return DataType.DATA;
75 }
76
77 @Override
78 public List<KeyAction> getKeyBindings() {
79 List<KeyAction> actions = new LinkedList<KeyAction>();
80
81 // TODO
82 actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
83 Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
84 @Override
85 public boolean onAction() {
86 if (txt != null) {
87 txt.switchMode();
88 }
89
90 return false;
91 }
92 });
93 actions.add(new KeyAction(Mode.NONE, 'i',
ae22c247 94 Trans.StringId.KEY_ACTION_INVERT) {
25232463
NR
95 @Override
96 public boolean onAction() {
97 if (txt != null) {
98 txt.invertColor();
99 }
100
101 return false;
102 }
103 });
ae22c247
NR
104 actions.add(new KeyAction(Mode.NONE, 'f',
105 Trans.StringId.KEY_ACTION_FULLSCREEN) {
106 @Override
107 public boolean onAction() {
108 fullscreenImage = !fullscreenImage;
109 setImage(image);
110 return false;
111 }
112 });
25232463
NR
113
114 return actions;
115 }
ae22c247
NR
116
117 @Override
118 public synchronized Panel setSize(TerminalSize size) {
119 super.setSize(size);
120 setImage(image);
121 return this;
122 }
123
124 /**
125 * Set the {@link Image} to render.
126 *
127 * @param image
128 * the new {@link Image}
129 */
130 private void setImage(Image image) {
131 this.image = image;
132
133 TerminalSize size = getTxtSize();
134 if (size != null) {
135 if (txt != null)
136 txt.setSize(size);
137 else
138 txt = new ImageTextControl(image, size);
139 }
140
141 if (top.getChildCount() > 0)
142 top.removeAllComponents();
143
144 if (size != null)
145 top.addComponent(txt);
146 }
147
148 /**
149 * Compute the size to use for the {@link Image} text rendering. Return NULL
150 * in case of error.
151 *
152 * @return the {@link TerminalSize} to use or NULL if it is not possible
153 */
154 private TerminalSize getTxtSize() {
155 if (image != null && getSize() != null && getSize().getColumns() > 0
156 && getSize().getRows() > 0) {
157 if (fullscreenImage) {
158 return getSize();
159 } else {
160 // TODO:
161 return new TerminalSize(40, 20);
162 }
163 }
164
165 return null;
166 }
f04d8b1c 167}