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