1 package be
.nikiroo
.jvcard
.tui
.panes
;
4 import java
.util
.Base64
;
5 import java
.util
.LinkedList
;
8 import javax
.swing
.ImageIcon
;
10 import be
.nikiroo
.jvcard
.Contact
;
11 import be
.nikiroo
.jvcard
.Data
;
12 import be
.nikiroo
.jvcard
.TypeInfo
;
13 import be
.nikiroo
.jvcard
.i18n
.Trans
;
14 import be
.nikiroo
.jvcard
.tui
.ImageTextControl
;
15 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
16 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
17 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
18 import be
.nikiroo
.jvcard
.tui
.UiColors
;
20 import com
.googlecode
.lanterna
.TerminalSize
;
21 import com
.googlecode
.lanterna
.gui2
.BorderLayout
;
22 import com
.googlecode
.lanterna
.gui2
.Direction
;
23 import com
.googlecode
.lanterna
.gui2
.Label
;
24 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
25 import com
.googlecode
.lanterna
.gui2
.Panel
;
26 import com
.googlecode
.lanterna
.input
.KeyType
;
28 public class ContactDetails
extends MainContent
{
29 private Contact contact
;
31 private ImageTextControl txtImage
;
33 private boolean fullscreenImage
;
34 private Panel infoPanel
;
37 public ContactDetails(Contact contact
) {
38 BorderLayout blayout
= new BorderLayout();
39 setLayoutManager(blayout
);
42 blayout
= new BorderLayout();
43 top
.setLayoutManager(blayout
);
45 infoPanel
= new Panel();
46 infoPanel
.setLayoutManager(new LinearLayout(Direction
.VERTICAL
));
47 top
.addComponent(infoPanel
, BorderLayout
.Location
.CENTER
);
49 Panel notePanel
= new Panel();
50 notePanel
.setLayoutManager(new LinearLayout(Direction
.HORIZONTAL
));
52 notePanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NOTES_TITLE
53 .createLabel("Notes:"));
54 note
= UiColors
.Element
.VIEW_CONTACT_NORMAL
.createLabel("");
55 notePanel
.addComponent(note
);
59 addComponent(top
, BorderLayout
.Location
.TOP
);
60 addComponent(notePanel
, BorderLayout
.Location
.CENTER
);
64 * Change the enclosed {@link Contact} from this {@link ContactDetails}.
67 * the new {@link Contact}
69 public void setContact(Contact contact
) {
70 if (this.contact
== contact
)
73 this.contact
= contact
;
75 if (contact
== null) {
78 infoPanel
.removeAllComponents();
80 String name
= contact
.getPreferredDataValue("FN");
81 if (name
== null || name
.length() == 0) {
82 // TODO format it ourself
83 name
= contact
.getPreferredDataValue("N");
86 // TODO: i18n + do it properly
87 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NAME
90 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
92 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
93 .createLabel("Phone: "
94 + contact
.getPreferredDataValue("TEL")));
95 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
96 .createLabel("eMail: "
97 + contact
.getPreferredDataValue("EMAIL")));
98 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
101 String notes
= contact
.getPreferredDataValue("NOTE");
104 note
.setText(notes
.replaceAll("\\\\n", "\n"));
106 Data photo
= contact
.getPreferredData("PHOTO");
108 TypeInfo encoding
= null;
109 TypeInfo type
= null;
110 for (int index
= 0; index
< photo
.size(); index
++) {
111 TypeInfo info
= photo
.get(index
);
112 if (info
.getName() != null) {
113 if (info
.getName().equalsIgnoreCase("ENCODING"))
115 if (info
.getName().equalsIgnoreCase("TYPE"))
120 if (encoding
!= null && encoding
.getValue() != null
121 && encoding
.getValue().equalsIgnoreCase("b")) {
123 image
= new ImageIcon(Base64
.getDecoder().decode(
124 photo
.getValue())).getImage();
133 public DataType
getDataType() {
134 return DataType
.DATA
;
138 public List
<KeyAction
> getKeyBindings() {
139 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
142 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
143 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
145 public boolean onAction() {
146 if (txtImage
!= null) {
147 txtImage
.switchMode();
153 actions
.add(new KeyAction(Mode
.NONE
, 'i',
154 Trans
.StringId
.KEY_ACTION_INVERT
) {
156 public boolean onAction() {
157 if (txtImage
!= null) {
158 txtImage
.invertColor();
164 actions
.add(new KeyAction(Mode
.NONE
, 'f',
165 Trans
.StringId
.KEY_ACTION_FULLSCREEN
) {
167 public boolean onAction() {
168 fullscreenImage
= !fullscreenImage
;
178 public synchronized Panel
setSize(TerminalSize size
) {
185 * Set the {@link Image} to render and refresh it to the current size
189 * the new {@link Image}
191 private void setImage(Image image
) {
194 if (txtImage
!= null && top
.containsComponent(txtImage
))
195 top
.removeComponent(txtImage
);
197 TerminalSize size
= getTxtSize();
199 if (txtImage
!= null)
200 txtImage
.setSize(size
);
202 txtImage
= new ImageTextControl(image
, size
);
206 top
.addComponent(txtImage
, BorderLayout
.Location
.LEFT
);
213 * Compute the size to use for the {@link Image} text rendering. Return NULL
216 * @return the {@link TerminalSize} to use or NULL if it is not possible
218 private TerminalSize
getTxtSize() {
219 if (image
!= null && getSize() != null && getSize().getColumns() > 0
220 && getSize().getRows() > 0) {
221 if (fullscreenImage
) {
224 // TODO: configure size?
225 int w
= getSize().getColumns() - 40;
226 int h
= getSize().getRows() - 5;
227 if (w
<= 0 || h
<= 0)
230 return new TerminalSize(w
, h
);