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