1 package be
.nikiroo
.jvcard
.tui
.panes
;
4 import java
.util
.LinkedList
;
7 import be
.nikiroo
.jvcard
.Contact
;
8 import be
.nikiroo
.jvcard
.Data
;
9 import be
.nikiroo
.jvcard
.TypeInfo
;
10 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
11 import be
.nikiroo
.jvcard
.resources
.bundles
.DisplayBundle
;
12 import be
.nikiroo
.jvcard
.resources
.enums
.DisplayOption
;
13 import be
.nikiroo
.jvcard
.resources
.enums
.ColorOption
;
14 import be
.nikiroo
.jvcard
.resources
.enums
.StringId
;
15 import be
.nikiroo
.jvcard
.tui
.ImageTextControl
;
16 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
17 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
18 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
19 import be
.nikiroo
.jvcard
.tui
.UiColors
;
21 import com
.googlecode
.lanterna
.TerminalSize
;
22 import com
.googlecode
.lanterna
.gui2
.BorderLayout
;
23 import com
.googlecode
.lanterna
.gui2
.Borders
;
24 import com
.googlecode
.lanterna
.gui2
.Direction
;
25 import com
.googlecode
.lanterna
.gui2
.Label
;
26 import com
.googlecode
.lanterna
.gui2
.LinearLayout
;
27 import com
.googlecode
.lanterna
.gui2
.Panel
;
28 import com
.googlecode
.lanterna
.input
.KeyType
;
30 public class ContactDetails
extends MainContent
{
31 private Contact contact
;
33 private ImageTextControl txtImage
;
35 private boolean fullscreenImage
;
36 private Panel infoPanel
;
39 // from .properties file:
40 private int labelSize
= -1;
41 private String infoFormat
= "";
45 public ContactDetails(Contact contact
) {
46 // Get the .properties info:
47 DisplayBundle map
= new DisplayBundle();
48 labelSize
= map
.getInteger(DisplayOption
.CONTACT_DETAILS_LABEL_WIDTH
,
50 infoFormat
= map
.getString(DisplayOption
.CONTACT_DETAILS_INFO
);
53 BorderLayout blayout
= new BorderLayout();
54 setLayoutManager(blayout
);
57 blayout
= new BorderLayout();
58 top
.setLayoutManager(blayout
);
60 infoPanel
= new Panel();
61 infoPanel
.setLayoutManager(new LinearLayout(Direction
.VERTICAL
));
62 top
.addComponent(infoPanel
, BorderLayout
.Location
.CENTER
);
64 Panel notePanel
= new Panel();
65 notePanel
.setLayoutManager(new LinearLayout(Direction
.HORIZONTAL
));
67 notePanel
.addComponent(UiColors
.createLabel(
68 ColorOption
.VIEW_CONTACT_NOTES_TITLE
, "Notes:"));
69 note
= UiColors
.createLabel(ColorOption
.VIEW_CONTACT_NORMAL
, "");
70 notePanel
.addComponent(note
);
74 addComponent(top
.withBorder(Borders
.doubleLineBevel()),
75 BorderLayout
.Location
.TOP
);
76 addComponent(notePanel
.withBorder(Borders
.singleLineBevel()),
77 BorderLayout
.Location
.CENTER
);
81 * Change the enclosed {@link Contact} from this {@link ContactDetails}.
82 * Also re-set the image.
85 * the new {@link Contact}
87 public void setContact(Contact contact
) {
88 this.contact
= contact
;
91 if (contact
!= null) {
92 infoPanel
.removeAllComponents();
94 String name
= contact
.getPreferredDataValue("FN");
95 infoPanel
.addComponent(UiColors
.createLabel(
96 ColorOption
.VIEW_CONTACT_NAME
, name
));
97 infoPanel
.addComponent(UiColors
.createLabel(
98 ColorOption
.VIEW_CONTACT_NORMAL
, ""));
101 String
[] infos
= infoFormat
.split("\\|");
102 for (String info
: infos
) {
103 // # - "=FIELD" will take the preferred value for this field
104 // # - "+FIELD" will take the preferred value for this field and
106 // # - "#FIELD" will take all the values with this field's name
107 // # - "*FIELD" will take all the values with this field's name,
108 // highlighting the preferred one
113 if (info
.contains("+") || info
.contains("#"))
115 if (info
.contains("*") || info
.contains("#"))
118 if (all
|| hl
|| info
.contains("=")) {
119 ColorOption el
= hl ? ColorOption
.VIEW_CONTACT_HIGHLIGHT
120 : ColorOption
.VIEW_CONTACT_NORMAL
;
122 int index
= info
.indexOf('=');
124 index
= info
.indexOf('+');
126 index
= info
.indexOf('#');
128 index
= info
.indexOf('*');
130 String label
= info
.substring(0, index
);
131 String field
= info
.substring(index
+ 1);
134 Data pref
= contact
.getPreferredData(field
);
135 for (Data data
: contact
.getData(field
)) {
137 infoPanel
.addComponent(UiColors
.createLabel(el
,
138 StringUtils
.padString(label
, labelSize
)
141 infoPanel
.addComponent(UiColors
.createLabel(
142 ColorOption
.VIEW_CONTACT_NORMAL
,
143 StringUtils
.padString(label
, labelSize
)
148 String val
= contact
.getPreferredDataValue(field
);
151 infoPanel
.addComponent(UiColors
.createLabel(el
,
152 StringUtils
.padString(label
, labelSize
) + val
));
156 infoPanel
.addComponent(UiColors
.createLabel(
157 ColorOption
.VIEW_CONTACT_NORMAL
,
158 StringUtils
.padString(label
, labelSize
)));
163 infoPanel
.addComponent(UiColors
.createLabel(
164 ColorOption
.VIEW_CONTACT_NORMAL
, ""));
166 String notes
= contact
.getPreferredDataValue("NOTE");
171 Data photo
= contact
.getPreferredData("PHOTO");
173 TypeInfo encoding
= null;
174 for (TypeInfo info
: photo
) {
175 if (info
.getName() != null) {
176 if (info
.getName().equalsIgnoreCase("ENCODING"))
178 // We don't check for the "TYPE" anymore, we just defer
183 if (encoding
!= null && encoding
.getValue() != null
184 && encoding
.getValue().equalsIgnoreCase("b")) {
187 image
= StringUtils
.toImage(photo
.getValue());
188 } catch (Exception e
) {
189 System
.err
.println("Cannot parse image for contact: "
190 + contact
.getPreferredDataValue("UID"));
200 public DataType
getDataType() {
201 return DataType
.DATA
;
205 public List
<KeyAction
> getKeyBindings() {
206 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
208 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
209 StringId
.KEY_ACTION_SWITCH_FORMAT
) {
211 public boolean onAction() {
212 if (txtImage
!= null) {
213 txtImage
.switchMode();
219 actions
.add(new KeyAction(Mode
.NONE
, 'i', StringId
.KEY_ACTION_INVERT
) {
221 public boolean onAction() {
222 if (txtImage
!= null) {
223 txtImage
.invertColor();
229 actions
.add(new KeyAction(Mode
.NONE
, 'f',
230 StringId
.KEY_ACTION_FULLSCREEN
) {
232 public boolean onAction() {
233 fullscreenImage
= !fullscreenImage
;
238 // TODO: add "normal" edit
239 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS_RAW
, 'r',
240 StringId
.KEY_ACTION_EDIT_CONTACT_RAW
) {
242 public Object
getObject() {
251 public synchronized Panel
setSize(TerminalSize size
) {
258 * Set the {@link Image} to render and refresh it to the current size
262 * the new {@link Image}
264 private void setImage(Image image
) {
267 if (txtImage
!= null && top
.containsComponent(txtImage
))
268 top
.removeComponent(txtImage
);
270 TerminalSize size
= getTxtSize();
272 if (txtImage
!= null)
273 txtImage
.setSize(size
);
275 txtImage
= new ImageTextControl(image
, size
);
279 top
.addComponent(txtImage
, BorderLayout
.Location
.LEFT
);
286 * Compute the size to use for the {@link Image} text rendering. Return NULL
289 * @return the {@link TerminalSize} to use or NULL if it is not possible
291 private TerminalSize
getTxtSize() {
292 if (image
!= null && getSize() != null && getSize().getColumns() > 0
293 && getSize().getRows() > 0) {
294 if (fullscreenImage
) {
297 // TODO: configure size?
298 int w
= getSize().getColumns() - 40;
299 int h
= getSize().getRows() - 9;
300 if (w
<= 0 || h
<= 0)
303 return new TerminalSize(w
, h
);