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