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