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