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 for (Data data
: contact
.getData(field
)) {
135 if (data
.isPreferred()) {
136 infoPanel
.addComponent(UiColors
.createLabel(el
,
137 StringUtils
.padString(label
, labelSize
)
140 infoPanel
.addComponent(UiColors
.createLabel(
141 ColorOption
.VIEW_CONTACT_NORMAL
,
142 StringUtils
.padString(label
, labelSize
)
147 String val
= contact
.getPreferredDataValue(field
);
150 infoPanel
.addComponent(UiColors
.createLabel(el
,
151 StringUtils
.padString(label
, labelSize
) + val
));
155 infoPanel
.addComponent(UiColors
.createLabel(
156 ColorOption
.VIEW_CONTACT_NORMAL
,
157 StringUtils
.padString(label
, labelSize
)));
162 infoPanel
.addComponent(UiColors
.createLabel(
163 ColorOption
.VIEW_CONTACT_NORMAL
, ""));
165 String notes
= contact
.getPreferredDataValue("NOTE");
170 Data photo
= contact
.getPreferredData("PHOTO");
172 TypeInfo encoding
= null;
173 for (TypeInfo info
: photo
) {
174 if (info
.getName() != null) {
175 if (info
.getName().equalsIgnoreCase("ENCODING"))
177 // We don't check for the "TYPE" anymore, we just defer
182 if (encoding
!= null && encoding
.getValue() != null
183 && encoding
.getValue().equalsIgnoreCase("b")) {
186 image
= StringUtils
.toImage(photo
.getValue());
187 } catch (Exception e
) {
188 System
.err
.println("Cannot parse image for contact: "
189 + contact
.getPreferredDataValue("UID"));
199 public DataType
getDataType() {
200 return DataType
.DATA
;
204 public List
<KeyAction
> getKeyBindings() {
205 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
207 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
208 StringId
.KEY_ACTION_SWITCH_FORMAT
) {
210 public boolean onAction() {
211 if (txtImage
!= null) {
212 txtImage
.switchMode();
218 actions
.add(new KeyAction(Mode
.NONE
, 'i', StringId
.KEY_ACTION_INVERT
) {
220 public boolean onAction() {
221 if (txtImage
!= null) {
222 txtImage
.invertColor();
228 actions
.add(new KeyAction(Mode
.NONE
, 'f',
229 StringId
.KEY_ACTION_FULLSCREEN
) {
231 public boolean onAction() {
232 fullscreenImage
= !fullscreenImage
;
237 // TODO: add "normal" edit
238 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS_RAW
, 'r',
239 StringId
.KEY_ACTION_EDIT_CONTACT_RAW
) {
241 public Object
getObject() {
250 public synchronized Panel
setSize(TerminalSize size
) {
257 * Set the {@link Image} to render and refresh it to the current size
261 * the new {@link Image}
263 private void setImage(Image image
) {
266 if (txtImage
!= null && top
.containsComponent(txtImage
))
267 top
.removeComponent(txtImage
);
269 TerminalSize size
= getTxtSize();
271 if (txtImage
!= null)
272 txtImage
.setSize(size
);
274 txtImage
= new ImageTextControl(image
, size
);
278 top
.addComponent(txtImage
, BorderLayout
.Location
.LEFT
);
285 * Compute the size to use for the {@link Image} text rendering. Return NULL
288 * @return the {@link TerminalSize} to use or NULL if it is not possible
290 private TerminalSize
getTxtSize() {
291 if (image
!= null && getSize() != null && getSize().getColumns() > 0
292 && getSize().getRows() > 0) {
293 if (fullscreenImage
) {
296 // TODO: configure size?
297 int w
= getSize().getColumns() - 40;
298 int h
= getSize().getRows() - 9;
299 if (w
<= 0 || h
<= 0)
302 return new TerminalSize(w
, h
);