Update lanterna, fix bugs, implement save...
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
1 package be.nikiroo.jvcard.tui.panes;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import com.googlecode.lanterna.input.KeyType;
7
8 import be.nikiroo.jvcard.Contact;
9 import be.nikiroo.jvcard.Data;
10 import be.nikiroo.jvcard.TypeInfo;
11 import be.nikiroo.jvcard.i18n.Trans;
12 import be.nikiroo.jvcard.tui.KeyAction;
13 import be.nikiroo.jvcard.tui.KeyAction.DataType;
14 import be.nikiroo.jvcard.tui.KeyAction.Mode;
15 import be.nikiroo.jvcard.tui.StringUtils;
16 import be.nikiroo.jvcard.tui.UiColors.Element;
17
18 public class ContactDetails extends MainContentList {
19 private Contact contact;
20 private int mode;
21
22 public ContactDetails(Contact contact) {
23 super(null, null);
24
25 this.contact = contact;
26 this.mode = 0;
27
28 for (int i = 0; i < contact.getContent().size(); i++) {
29 addItem("[detail line]");
30 }
31 }
32
33 @Override
34 protected List<TextPart> getLabel(int index, int width, boolean selected,
35 boolean focused) {
36 // TODO: from ini file?
37 int SIZE_COL_1 = 15;
38
39 Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
40 : Element.CONTACT_LINE;
41 Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
42 : Element.CONTACT_LINE_SEPARATOR;
43 Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED
44 : Element.CONTACT_LINE_DIRTY;
45
46 Data data = contact.getContent().get(index);
47
48 List<TextPart> parts = new LinkedList<TextPart>();
49 if (data.isDirty()) {
50 parts.add(new TextPart(" ", el));
51 parts.add(new TextPart("*", elDirty));
52 } else {
53 parts.add(new TextPart(" ", elSep));
54 }
55 String name = " " + data.getName() + " ";
56 String value = null;
57
58 StringBuilder valueBuilder = new StringBuilder(" ");
59 switch (mode) {
60 case 0:
61 valueBuilder.append(data.getValue());
62 if (data.getGroup() != null && data.getGroup().length() > 0) {
63 valueBuilder.append("(");
64 valueBuilder.append(data.getGroup());
65 valueBuilder.append(")");
66 }
67 break;
68 case 1:
69 for (TypeInfo type : data.getTypes()) {
70 if (valueBuilder.length() > 1)
71 valueBuilder.append(", ");
72 valueBuilder.append(type.getName());
73 valueBuilder.append(": ");
74 valueBuilder.append(type.getValue());
75 }
76 break;
77 }
78 valueBuilder.append(" ");
79
80 value = valueBuilder.toString();
81
82 name = StringUtils.padString(name, SIZE_COL_1);
83 value = StringUtils.padString(value, width - SIZE_COL_1
84 - getSeparator().length() - 2);
85
86 parts.add(new TextPart(name, el));
87 parts.add(new TextPart(getSeparator(), elSep));
88 parts.add(new TextPart(value, el));
89
90 return parts;
91 };
92
93 @Override
94 public DataType getDataType() {
95 return DataType.DATA;
96 }
97
98 @Override
99 public String getExitWarning() {
100 // TODO Auto-generated method stub
101 return null;
102 }
103
104 @Override
105 public List<KeyAction> getKeyBindings() {
106 // TODO Auto-generated method stub
107 List<KeyAction> actions = new LinkedList<KeyAction>();
108
109 // TODO: add, remove
110 actions.add(new KeyAction(Mode.EDIT_DETAIL, 'd', Trans.StringId.DUMMY) {
111 @Override
112 public Object getObject() {
113 return contact.getContent().get(getSelectedIndex());
114 }
115 });
116 actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
117 Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
118 @Override
119 public boolean onAction() {
120 mode++;
121 if (mode > 1)
122 mode = 0;
123
124 return false;
125 }
126 });
127
128 return actions;
129 }
130
131 @Override
132 public Mode getMode() {
133 return Mode.CONTACT_DETAILS;
134 }
135
136 @Override
137 public String getTitle() {
138 String title = null;
139
140 if (contact != null) {
141 title = contact.getPreferredDataValue("FN");
142 if (title == null || title.length() == 0)
143 title = contact.getPreferredDataValue("N");
144 }
145
146 return title;
147 }
148
149 @Override
150 public String move(int x, int y) {
151 // TODO Auto-generated method stub
152 return null;
153 }
154 }