Commit | Line | Data |
---|---|---|
fae07ea7 NR |
1 | package be.nikiroo.jvcard.tui.panes; |
2 | ||
9c8baf0c | 3 | import java.io.IOException; |
bcb54330 | 4 | import java.util.ArrayList; |
9c8baf0c | 5 | import java.util.LinkedList; |
fae07ea7 NR |
6 | import java.util.List; |
7 | ||
9c8baf0c | 8 | import be.nikiroo.jvcard.Card; |
7da41ecd NR |
9 | import be.nikiroo.jvcard.launcher.Main; |
10 | import be.nikiroo.jvcard.resources.StringUtils; | |
11 | import be.nikiroo.jvcard.resources.Trans; | |
fae07ea7 | 12 | import be.nikiroo.jvcard.tui.KeyAction; |
fae07ea7 NR |
13 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
14 | import be.nikiroo.jvcard.tui.KeyAction.Mode; | |
bcb54330 | 15 | import be.nikiroo.jvcard.tui.UiColors.Element; |
fae07ea7 | 16 | |
9c8baf0c | 17 | import com.googlecode.lanterna.input.KeyType; |
fae07ea7 NR |
18 | |
19 | public class FileList extends MainContentList { | |
7f82bf68 | 20 | private List<String> files; |
bcb54330 | 21 | private List<Card> cards; |
fae07ea7 | 22 | |
7f82bf68 | 23 | public FileList(List<String> files) { |
fae07ea7 NR |
24 | setFiles(files); |
25 | } | |
26 | ||
27 | /** | |
28 | * Change the list of currently selected files. | |
29 | * | |
30 | * @param files | |
31 | * the new files | |
32 | */ | |
7f82bf68 | 33 | public void setFiles(List<String> files) { |
fae07ea7 NR |
34 | clearItems(); |
35 | this.files = files; | |
bcb54330 | 36 | cards = new ArrayList<Card>(); |
fae07ea7 | 37 | |
7f82bf68 NR |
38 | for (String file : files) { |
39 | addItem(file); // TODO | |
bcb54330 | 40 | cards.add(null); |
fae07ea7 NR |
41 | } |
42 | ||
43 | setSelectedIndex(0); | |
44 | } | |
45 | ||
46 | @Override | |
47 | public DataType getDataType() { | |
48 | return DataType.CARD_FILES; | |
49 | } | |
50 | ||
51 | @Override | |
bcb54330 NR |
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 | ||
7f82bf68 NR |
68 | String name = files.get(index).replaceAll("\\\\", "/"); |
69 | int indexSl = name.lastIndexOf('/'); | |
70 | if (indexSl >= 0) { | |
71 | name = name.substring(indexSl + 1); | |
72 | } | |
bcb54330 | 73 | |
7da41ecd | 74 | name = StringUtils.sanitize(name, Main.isUnicode()); |
296a0b75 | 75 | |
bcb54330 NR |
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 | }; | |
fae07ea7 NR |
87 | |
88 | @Override | |
89 | public List<KeyAction> getKeyBindings() { | |
9c8baf0c NR |
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() { | |
bcb54330 NR |
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 | ||
7f82bf68 NR |
105 | String file = files.get(index); |
106 | ||
9c8baf0c | 107 | try { |
7da41ecd | 108 | Card card = Main.getCard(file); |
bcb54330 NR |
109 | cards.set(index, card); |
110 | ||
111 | invalidate(); | |
112 | ||
113 | return card; | |
9c8baf0c NR |
114 | } catch (IOException ioe) { |
115 | ioe.printStackTrace(); | |
116 | return null; | |
117 | } | |
118 | } | |
119 | }); | |
120 | ||
121 | return actions; | |
fae07ea7 | 122 | } |
fae07ea7 | 123 | } |