1 package be
.nikiroo
.jvcard
.tui
.panes
;
4 import java
.util
.LinkedList
;
7 import javax
.xml
.bind
.DatatypeConverter
;
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}.
65 * Also re-set the image.
68 * the new {@link Contact}
70 public void setContact(Contact contact
) {
71 this.contact
= contact
;
74 if (contact
!= null) {
75 infoPanel
.removeAllComponents();
77 String name
= contact
.getPreferredDataValue("FN");
78 if (name
== null || name
.length() == 0) {
79 // TODO format it ourself
80 name
= contact
.getPreferredDataValue("N");
83 // TODO: i18n + do it properly
84 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NAME
87 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
89 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
90 .createLabel("Phone: "
91 + contact
.getPreferredDataValue("TEL")));
92 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
93 .createLabel("eMail: "
94 + contact
.getPreferredDataValue("EMAIL")));
95 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
98 String notes
= contact
.getPreferredDataValue("NOTE");
101 note
.setText(notes
.replaceAll("\\\\n", "\n"));
103 Data photo
= contact
.getPreferredData("PHOTO");
105 TypeInfo encoding
= null;
106 for (int index
= 0; index
< photo
.size(); index
++) {
107 TypeInfo info
= photo
.get(index
);
108 if (info
.getName() != null) {
109 if (info
.getName().equalsIgnoreCase("ENCODING"))
111 // We don't check for the "TYPE" anymore, we just defer
116 if (encoding
!= null && encoding
.getValue() != null
117 && encoding
.getValue().equalsIgnoreCase("b")) {
119 image
= new ImageIcon(DatatypeConverter
.parseBase64Binary(
120 photo
.getValue())).getImage();
129 public DataType
getDataType() {
130 return DataType
.DATA
;
134 public List
<KeyAction
> getKeyBindings() {
135 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
138 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
139 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
141 public boolean onAction() {
142 if (txtImage
!= null) {
143 txtImage
.switchMode();
149 actions
.add(new KeyAction(Mode
.NONE
, 'i',
150 Trans
.StringId
.KEY_ACTION_INVERT
) {
152 public boolean onAction() {
153 if (txtImage
!= null) {
154 txtImage
.invertColor();
160 actions
.add(new KeyAction(Mode
.NONE
, 'f',
161 Trans
.StringId
.KEY_ACTION_FULLSCREEN
) {
163 public boolean onAction() {
164 fullscreenImage
= !fullscreenImage
;
169 // TODO: add "normal" edit and remove this one into RAW edit
170 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS_RAW
, 'e',
171 Trans
.StringId
.KEY_ACTION_EDIT_CONTACT
) {
173 public Object
getObject() {
182 public synchronized Panel
setSize(TerminalSize size
) {
189 * Set the {@link Image} to render and refresh it to the current size
193 * the new {@link Image}
195 private void setImage(Image image
) {
198 if (txtImage
!= null && top
.containsComponent(txtImage
))
199 top
.removeComponent(txtImage
);
201 TerminalSize size
= getTxtSize();
203 if (txtImage
!= null)
204 txtImage
.setSize(size
);
206 txtImage
= new ImageTextControl(image
, size
);
210 top
.addComponent(txtImage
, BorderLayout
.Location
.LEFT
);
217 * Compute the size to use for the {@link Image} text rendering. Return NULL
220 * @return the {@link TerminalSize} to use or NULL if it is not possible
222 private TerminalSize
getTxtSize() {
223 if (image
!= null && getSize() != null && getSize().getColumns() > 0
224 && getSize().getRows() > 0) {
225 if (fullscreenImage
) {
228 // TODO: configure size?
229 int w
= getSize().getColumns() - 40;
230 int h
= getSize().getRows() - 5;
231 if (w
<= 0 || h
<= 0)
234 return new TerminalSize(w
, h
);