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
.ColorOption
;
13 import be
.nikiroo
.jvcard
.resources
.enums
.DisplayOption
;
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
.LinearLayout
;
26 import com
.googlecode
.lanterna
.gui2
.Panel
;
27 import com
.googlecode
.lanterna
.gui2
.TextBox
;
28 import com
.googlecode
.lanterna
.gui2
.TextBox
.Style
;
29 import com
.googlecode
.lanterna
.input
.KeyType
;
31 public class ContactDetails
extends MainContent
{
32 private Contact contact
;
34 private ImageTextControl txtImage
;
36 private boolean fullscreenImage
;
37 private Panel infoPanel
;
40 // from .properties file:
41 private int labelSize
= -1;
42 private String infoFormat
= "";
46 public ContactDetails(Contact contact
) {
47 // Get the .properties info:
48 DisplayBundle map
= new DisplayBundle();
49 labelSize
= map
.getInteger(DisplayOption
.CONTACT_DETAILS_LABEL_WIDTH
,
51 infoFormat
= map
.getString(DisplayOption
.CONTACT_DETAILS_INFO
);
54 BorderLayout blayout
= new BorderLayout();
55 setLayoutManager(blayout
);
58 blayout
= new BorderLayout();
59 top
.setLayoutManager(blayout
);
61 infoPanel
= new Panel();
62 infoPanel
.setLayoutManager(new LinearLayout(Direction
.VERTICAL
));
63 top
.addComponent(infoPanel
, BorderLayout
.Location
.CENTER
);
65 Panel notePanel
= new Panel();
66 notePanel
.setLayoutManager(new LinearLayout(Direction
.HORIZONTAL
));
68 notePanel
.addComponent(UiColors
.createLabel(
69 ColorOption
.VIEW_CONTACT_NOTES_TITLE
, "Notes:"));
70 // 10000x10000 is probably enough or "max"
71 note
= new TextBox(new TerminalSize(10000, 10000), Style
.MULTI_LINE
);
72 note
.setReadOnly(true);
73 notePanel
.addComponent(note
);
74 note
.setVerticalFocusSwitching(false);
75 note
.setHorizontalFocusSwitching(false);
79 addComponent(top
.withBorder(Borders
.doubleLineBevel()),
80 BorderLayout
.Location
.TOP
);
81 addComponent(notePanel
.withBorder(Borders
.singleLineBevel()),
82 BorderLayout
.Location
.CENTER
);
86 * Change the enclosed {@link Contact} from this {@link ContactDetails}.
87 * Also re-set the image.
90 * the new {@link Contact}
92 public void setContact(Contact contact
) {
93 this.contact
= contact
;
96 if (contact
!= null) {
97 infoPanel
.removeAllComponents();
99 String name
= contact
.getPreferredDataValue("FN");
100 infoPanel
.addComponent(UiColors
.createLabel(
101 ColorOption
.VIEW_CONTACT_NAME
, name
));
102 infoPanel
.addComponent(UiColors
.createLabel(
103 ColorOption
.VIEW_CONTACT_NORMAL
, ""));
106 String
[] infos
= infoFormat
.split("\\|");
107 for (String info
: infos
) {
108 // # - "=FIELD" will take the preferred value for this field
109 // # - "+FIELD" will take the preferred value for this field and
111 // # - "#FIELD" will take all the values with this field's name
112 // # - "*FIELD" will take all the values with this field's name,
113 // highlighting the preferred one
118 if (info
.contains("+") || info
.contains("#"))
120 if (info
.contains("*") || info
.contains("#"))
123 if (all
|| hl
|| info
.contains("=")) {
124 ColorOption el
= hl ? ColorOption
.VIEW_CONTACT_HIGHLIGHT
125 : ColorOption
.VIEW_CONTACT_NORMAL
;
127 int index
= info
.indexOf('=');
129 index
= info
.indexOf('+');
131 index
= info
.indexOf('#');
133 index
= info
.indexOf('*');
135 String label
= info
.substring(0, index
);
136 String field
= info
.substring(index
+ 1);
139 Data pref
= contact
.getPreferredData(field
);
140 for (Data data
: contact
.getData(field
)) {
142 infoPanel
.addComponent(UiColors
.createLabel(el
,
143 StringUtils
.padString(label
, labelSize
)
146 infoPanel
.addComponent(UiColors
.createLabel(
147 ColorOption
.VIEW_CONTACT_NORMAL
,
148 StringUtils
.padString(label
, labelSize
)
153 String val
= contact
.getPreferredDataValue(field
);
156 infoPanel
.addComponent(UiColors
.createLabel(el
,
157 StringUtils
.padString(label
, labelSize
) + val
));
161 infoPanel
.addComponent(UiColors
.createLabel(
162 ColorOption
.VIEW_CONTACT_NORMAL
,
163 StringUtils
.padString(label
, labelSize
)));
168 infoPanel
.addComponent(UiColors
.createLabel(
169 ColorOption
.VIEW_CONTACT_NORMAL
, ""));
171 String notes
= contact
.getPreferredDataValue("NOTE");
176 Data photo
= contact
.getPreferredData("PHOTO");
178 TypeInfo encoding
= null;
179 for (TypeInfo info
: photo
) {
180 if (info
.getName() != null) {
181 if (info
.getName().equalsIgnoreCase("ENCODING"))
183 // We don't check for the "TYPE" anymore, we just defer
188 if (encoding
!= null && encoding
.getValue() != null
189 && encoding
.getValue().equalsIgnoreCase("b")) {
192 image
= StringUtils
.toImage(photo
.getValue());
193 } catch (Exception e
) {
194 System
.err
.println("Cannot parse image for contact: "
195 + contact
.getPreferredDataValue("UID"));
205 public DataType
getDataType() {
206 return DataType
.DATA
;
210 public List
<KeyAction
> getKeyBindings() {
211 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
213 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
214 StringId
.KEY_ACTION_SWITCH_FORMAT
) {
216 public boolean onAction() {
217 if (txtImage
!= null) {
218 txtImage
.switchMode();
224 actions
.add(new KeyAction(Mode
.NONE
, 'i', StringId
.KEY_ACTION_INVERT
) {
226 public boolean onAction() {
227 if (txtImage
!= null) {
228 txtImage
.invertColor();
234 actions
.add(new KeyAction(Mode
.NONE
, 'f',
235 StringId
.KEY_ACTION_FULLSCREEN
) {
237 public boolean onAction() {
238 fullscreenImage
= !fullscreenImage
;
243 // TODO: add "normal" edit
244 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS_RAW
, 'r',
245 StringId
.KEY_ACTION_EDIT_CONTACT_RAW
) {
247 public Object
getObject() {
256 public synchronized Panel
setSize(TerminalSize size
) {
263 * Set the {@link Image} to render and refresh it to the current size
267 * the new {@link Image}
269 private void setImage(Image image
) {
272 if (txtImage
!= null && top
.containsComponent(txtImage
))
273 top
.removeComponent(txtImage
);
275 TerminalSize size
= getTxtSize();
277 if (txtImage
!= null)
278 txtImage
.setSize(size
);
280 txtImage
= new ImageTextControl(image
, size
);
284 top
.addComponent(txtImage
, BorderLayout
.Location
.LEFT
);
291 * Compute the size to use for the {@link Image} text rendering. Return NULL
294 * @return the {@link TerminalSize} to use or NULL if it is not possible
296 private TerminalSize
getTxtSize() {
297 if (image
!= null && getSize() != null && getSize().getColumns() > 0
298 && getSize().getRows() > 0) {
299 if (fullscreenImage
) {
302 // TODO: configure size?
303 int w
= getSize().getColumns() - 40;
304 int h
= getSize().getRows() - 9;
305 if (w
<= 0 || h
<= 0)
308 return new TerminalSize(w
, h
);