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