Update lanterna, fix bugs, implement save...
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / FileList.java
CommitLineData
fae07ea7
NR
1package be.nikiroo.jvcard.tui.panes;
2
3import java.io.File;
9c8baf0c 4import java.io.IOException;
bcb54330 5import java.util.ArrayList;
9c8baf0c 6import java.util.LinkedList;
fae07ea7
NR
7import java.util.List;
8
9c8baf0c
NR
9import be.nikiroo.jvcard.Card;
10import be.nikiroo.jvcard.i18n.Trans;
11import be.nikiroo.jvcard.parsers.Format;
fae07ea7 12import be.nikiroo.jvcard.tui.KeyAction;
fae07ea7
NR
13import be.nikiroo.jvcard.tui.KeyAction.DataType;
14import be.nikiroo.jvcard.tui.KeyAction.Mode;
bcb54330
NR
15import be.nikiroo.jvcard.tui.StringUtils;
16import be.nikiroo.jvcard.tui.UiColors;
17import be.nikiroo.jvcard.tui.UiColors.Element;
fae07ea7 18
9c8baf0c 19import com.googlecode.lanterna.input.KeyType;
fae07ea7
NR
20
21public class FileList extends MainContentList {
22 private List<File> files;
bcb54330 23 private List<Card> cards;
fae07ea7
NR
24
25 public FileList(List<File> files) {
26 super(UiColors.Element.CONTACT_LINE,
27 UiColors.Element.CONTACT_LINE_SELECTED);
28
29 setFiles(files);
30 }
31
32 /**
33 * Change the list of currently selected files.
34 *
35 * @param files
36 * the new files
37 */
38 public void setFiles(List<File> files) {
39 clearItems();
40 this.files = files;
bcb54330 41 cards = new ArrayList<Card>();
fae07ea7 42
fae07ea7
NR
43 for (File file : files) {
44 addItem(file.getName());
bcb54330 45 cards.add(null);
fae07ea7
NR
46 }
47
48 setSelectedIndex(0);
49 }
50
51 @Override
52 public DataType getDataType() {
53 return DataType.CARD_FILES;
54 }
55
56 @Override
bcb54330
NR
57 protected List<TextPart> getLabel(int index, int width, boolean selected,
58 boolean focused) {
59 // TODO: from ini file?
60 int SIZE_COL_1 = 3;
61
62 Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
63 : Element.CONTACT_LINE;
64 Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
65 : Element.CONTACT_LINE_SEPARATOR;
66
67 List<TextPart> parts = new LinkedList<TextPart>();
68
69 String count = "";
70 if (cards.get(index) != null)
71 count += cards.get(index).size();
72
73 String name = files.get(index).getName();
74
75 count = " " + StringUtils.padString(count, SIZE_COL_1) + " ";
76 name = " "
77 + StringUtils.padString(name, width - SIZE_COL_1
78 - getSeparator().length()) + " ";
79
80 parts.add(new TextPart(count, el));
81 parts.add(new TextPart(getSeparator(), elSep));
82 parts.add(new TextPart(name, el));
83
84 return parts;
85 };
fae07ea7
NR
86
87 @Override
88 public List<KeyAction> getKeyBindings() {
9c8baf0c
NR
89 List<KeyAction> actions = new LinkedList<KeyAction>();
90
91 // TODO del, save...
92 actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter,
93 Trans.StringId.KEY_ACTION_VIEW_CARD) {
94 @Override
95 public Object getObject() {
bcb54330
NR
96 int index = getSelectedIndex();
97
98 if (index < 0 || index >= cards.size())
99 return null;
100
101 if (cards.get(index) != null)
102 return cards.get(index);
103
104 File file = files.get(index);
9c8baf0c
NR
105 Format format = Format.Abook;
106 String ext = file.getName();
107 if (ext.contains(".")) {
108 String tab[] = ext.split("\\.");
109 if (tab.length > 1
110 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
111 format = Format.VCard21;
112 }
113 }
114 try {
bcb54330
NR
115 Card card = new Card(file, format);
116 cards.set(index, card);
117
118 invalidate();
119
120 return card;
9c8baf0c
NR
121 } catch (IOException ioe) {
122 ioe.printStackTrace();
123 return null;
124 }
125 }
126 });
127
128 return actions;
fae07ea7
NR
129 }
130
131 @Override
132 public Mode getMode() {
133 return Mode.FILE_LIST;
134 }
fae07ea7 135}