| 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.tui.KeyAction; |
| 13 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
| 14 | import be.nikiroo.jvcard.tui.KeyAction.Mode; |
| 15 | import be.nikiroo.jvcard.tui.StringUtils; |
| 16 | import be.nikiroo.jvcard.tui.UiColors; |
| 17 | import be.nikiroo.jvcard.tui.UiColors.Element; |
| 18 | |
| 19 | import com.googlecode.lanterna.input.KeyType; |
| 20 | |
| 21 | public class FileList extends MainContentList { |
| 22 | private List<File> files; |
| 23 | private List<Card> cards; |
| 24 | |
| 25 | public FileList(List<File> files) { |
| 26 | setFiles(files); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Change the list of currently selected files. |
| 31 | * |
| 32 | * @param files |
| 33 | * the new files |
| 34 | */ |
| 35 | public void setFiles(List<File> files) { |
| 36 | clearItems(); |
| 37 | this.files = files; |
| 38 | cards = new ArrayList<Card>(); |
| 39 | |
| 40 | for (File file : files) { |
| 41 | addItem(file.getName()); |
| 42 | cards.add(null); |
| 43 | } |
| 44 | |
| 45 | setSelectedIndex(0); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public DataType getDataType() { |
| 50 | return DataType.CARD_FILES; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 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 | |
| 70 | String name = files.get(index).getName(); |
| 71 | |
| 72 | name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode()); |
| 73 | |
| 74 | count = " " + StringUtils.padString(count, SIZE_COL_1) + " "; |
| 75 | name = " " |
| 76 | + StringUtils.padString(name, width - SIZE_COL_1 |
| 77 | - getSeparator().length()) + " "; |
| 78 | |
| 79 | parts.add(new TextPart(count, el)); |
| 80 | parts.add(new TextPart(getSeparator(), elSep)); |
| 81 | parts.add(new TextPart(name, el)); |
| 82 | |
| 83 | return parts; |
| 84 | }; |
| 85 | |
| 86 | @Override |
| 87 | public List<KeyAction> getKeyBindings() { |
| 88 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 89 | |
| 90 | // TODO del, save... |
| 91 | actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter, |
| 92 | Trans.StringId.KEY_ACTION_VIEW_CARD) { |
| 93 | @Override |
| 94 | public Object getObject() { |
| 95 | int index = getSelectedIndex(); |
| 96 | |
| 97 | if (index < 0 || index >= cards.size()) |
| 98 | return null; |
| 99 | |
| 100 | if (cards.get(index) != null) |
| 101 | return cards.get(index); |
| 102 | |
| 103 | File file = files.get(index); |
| 104 | Format format = Format.Abook; |
| 105 | String ext = file.getName(); |
| 106 | if (ext.contains(".")) { |
| 107 | String tab[] = ext.split("\\."); |
| 108 | if (tab.length > 1 |
| 109 | && tab[tab.length - 1].equalsIgnoreCase("vcf")) { |
| 110 | format = Format.VCard21; |
| 111 | } |
| 112 | } |
| 113 | try { |
| 114 | Card card = new Card(file, format); |
| 115 | cards.set(index, card); |
| 116 | |
| 117 | invalidate(); |
| 118 | |
| 119 | return card; |
| 120 | } catch (IOException ioe) { |
| 121 | ioe.printStackTrace(); |
| 122 | return null; |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | return actions; |
| 128 | } |
| 129 | } |