1 package be
.nikiroo
.jvcard
.tui
.panes
;
4 import java
.util
.LinkedList
;
6 import java
.util
.MissingResourceException
;
7 import java
.util
.ResourceBundle
;
9 import be
.nikiroo
.jvcard
.Contact
;
10 import be
.nikiroo
.jvcard
.Data
;
11 import be
.nikiroo
.jvcard
.TypeInfo
;
12 import be
.nikiroo
.jvcard
.resources
.Bundles
;
13 import be
.nikiroo
.jvcard
.resources
.StringUtils
;
14 import be
.nikiroo
.jvcard
.resources
.Trans
;
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
= "";
44 public ContactDetails(Contact contact
) {
45 // Get the .properties info:
46 ResourceBundle map
= Bundles
.getBundle("display");
49 labelSize
= Integer
.parseInt(map
50 .getString("CONTACT_DETAILS_LABEL_WIDTH"));
52 } catch (NumberFormatException e
) {
55 } catch (MissingResourceException e
) {
60 infoFormat
= map
.getString("CONTACT_DETAILS_INFO");
61 } catch (MissingResourceException e
) {
66 BorderLayout blayout
= new BorderLayout();
67 setLayoutManager(blayout
);
70 blayout
= new BorderLayout();
71 top
.setLayoutManager(blayout
);
73 infoPanel
= new Panel();
74 infoPanel
.setLayoutManager(new LinearLayout(Direction
.VERTICAL
));
75 top
.addComponent(infoPanel
, BorderLayout
.Location
.CENTER
);
77 Panel notePanel
= new Panel();
78 notePanel
.setLayoutManager(new LinearLayout(Direction
.HORIZONTAL
));
80 notePanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NOTES_TITLE
81 .createLabel("Notes:"));
82 note
= UiColors
.Element
.VIEW_CONTACT_NORMAL
.createLabel("");
83 notePanel
.addComponent(note
);
87 addComponent(top
.withBorder(Borders
.doubleLineBevel()),
88 BorderLayout
.Location
.TOP
);
89 addComponent(notePanel
.withBorder(Borders
.singleLineBevel()),
90 BorderLayout
.Location
.CENTER
);
94 * Change the enclosed {@link Contact} from this {@link ContactDetails}.
95 * Also re-set the image.
98 * the new {@link Contact}
100 public void setContact(Contact contact
) {
101 this.contact
= contact
;
104 if (contact
!= null) {
105 infoPanel
.removeAllComponents();
107 String name
= contact
.getPreferredDataValue("FN");
108 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NAME
110 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
114 String
[] infos
= infoFormat
.split("\\|");
115 for (String info
: infos
) {
116 // # - "=FIELD" will take the preferred value for this field
117 // # - "+FIELD" will take the preferred value for this field and
119 // # - "#FIELD" will take all the values with this field's name
120 // # - "*FIELD" will take all the values with this field's name,
121 // highlighting the preferred one
126 if (info
.contains("+") || info
.contains("#"))
128 if (info
.contains("*") || info
.contains("#"))
131 if (all
|| hl
|| info
.contains("=")) {
132 UiColors
.Element el
= hl ? UiColors
.Element
.VIEW_CONTACT_HIGHLIGHT
133 : UiColors
.Element
.VIEW_CONTACT_NORMAL
;
135 int index
= info
.indexOf('=');
137 index
= info
.indexOf('+');
139 index
= info
.indexOf('#');
141 index
= info
.indexOf('*');
143 String label
= info
.substring(0, index
);
144 String field
= info
.substring(index
+ 1);
147 for (Data data
: contact
.getData(field
)) {
148 if (data
.isPreferred()) {
149 infoPanel
.addComponent(el
150 .createLabel(StringUtils
.padString(
155 .addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
156 .createLabel(StringUtils
163 String val
= contact
.getPreferredDataValue(field
);
166 infoPanel
.addComponent(el
.createLabel(StringUtils
167 .padString(label
, labelSize
) + val
));
171 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
172 .createLabel(StringUtils
173 .padString(label
, labelSize
)));
178 infoPanel
.addComponent(UiColors
.Element
.VIEW_CONTACT_NORMAL
181 String notes
= contact
.getPreferredDataValue("NOTE");
186 Data photo
= contact
.getPreferredData("PHOTO");
188 TypeInfo encoding
= null;
189 for (TypeInfo info
: photo
) {
190 if (info
.getName() != null) {
191 if (info
.getName().equalsIgnoreCase("ENCODING"))
193 // We don't check for the "TYPE" anymore, we just defer
198 if (encoding
!= null && encoding
.getValue() != null
199 && encoding
.getValue().equalsIgnoreCase("b")) {
202 image
= StringUtils
.toImage(photo
.getValue());
203 } catch (Exception e
) {
204 System
.err
.println("Cannot parse image for contact: "
205 + contact
.getPreferredDataValue("UID"));
215 public DataType
getDataType() {
216 return DataType
.DATA
;
220 public List
<KeyAction
> getKeyBindings() {
221 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
223 actions
.add(new KeyAction(Mode
.NONE
, KeyType
.Tab
,
224 Trans
.StringId
.KEY_ACTION_SWITCH_FORMAT
) {
226 public boolean onAction() {
227 if (txtImage
!= null) {
228 txtImage
.switchMode();
234 actions
.add(new KeyAction(Mode
.NONE
, 'i',
235 Trans
.StringId
.KEY_ACTION_INVERT
) {
237 public boolean onAction() {
238 if (txtImage
!= null) {
239 txtImage
.invertColor();
245 actions
.add(new KeyAction(Mode
.NONE
, 'f',
246 Trans
.StringId
.KEY_ACTION_FULLSCREEN
) {
248 public boolean onAction() {
249 fullscreenImage
= !fullscreenImage
;
254 // TODO: add "normal" edit
255 actions
.add(new KeyAction(Mode
.CONTACT_DETAILS_RAW
, 'r',
256 Trans
.StringId
.KEY_ACTION_EDIT_CONTACT_RAW
) {
258 public Object
getObject() {
267 public synchronized Panel
setSize(TerminalSize size
) {
274 * Set the {@link Image} to render and refresh it to the current size
278 * the new {@link Image}
280 private void setImage(Image image
) {
283 if (txtImage
!= null && top
.containsComponent(txtImage
))
284 top
.removeComponent(txtImage
);
286 TerminalSize size
= getTxtSize();
288 if (txtImage
!= null)
289 txtImage
.setSize(size
);
291 txtImage
= new ImageTextControl(image
, size
);
295 top
.addComponent(txtImage
, BorderLayout
.Location
.LEFT
);
302 * Compute the size to use for the {@link Image} text rendering. Return NULL
305 * @return the {@link TerminalSize} to use or NULL if it is not possible
307 private TerminalSize
getTxtSize() {
308 if (image
!= null && getSize() != null && getSize().getColumns() > 0
309 && getSize().getRows() > 0) {
310 if (fullscreenImage
) {
313 // TODO: configure size?
314 int w
= getSize().getColumns() - 40;
315 int h
= getSize().getRows() - 9;
316 if (w
<= 0 || h
<= 0)
319 return new TerminalSize(w
, h
);