| 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 | super(UiColors.Element.CONTACT_LINE, |
| 27 | UiColors.Element.CONTACT_LINE_SELECTED); |
| 28 | |
| 29 | setFiles(files); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Change the list of currently selected files. |
| 34 | * |
| 35 | * @param files |
| 36 | * the new files |
| 37 | */ |
| 38 | public void setFiles(List<File> files) { |
| 39 | clearItems(); |
| 40 | this.files = files; |
| 41 | cards = new ArrayList<Card>(); |
| 42 | |
| 43 | for (File file : files) { |
| 44 | addItem(file.getName()); |
| 45 | cards.add(null); |
| 46 | } |
| 47 | |
| 48 | setSelectedIndex(0); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public DataType getDataType() { |
| 53 | return DataType.CARD_FILES; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected List<TextPart> getLabel(int index, int width, boolean selected, |
| 58 | boolean focused) { |
| 59 | // TODO: from ini file? |
| 60 | int SIZE_COL_1 = 3; |
| 61 | |
| 62 | Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED |
| 63 | : Element.CONTACT_LINE; |
| 64 | Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED |
| 65 | : Element.CONTACT_LINE_SEPARATOR; |
| 66 | |
| 67 | List<TextPart> parts = new LinkedList<TextPart>(); |
| 68 | |
| 69 | String count = ""; |
| 70 | if (cards.get(index) != null) |
| 71 | count += cards.get(index).size(); |
| 72 | |
| 73 | String name = files.get(index).getName(); |
| 74 | |
| 75 | name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode()); |
| 76 | |
| 77 | count = " " + StringUtils.padString(count, SIZE_COL_1) + " "; |
| 78 | name = " " |
| 79 | + StringUtils.padString(name, width - SIZE_COL_1 |
| 80 | - getSeparator().length()) + " "; |
| 81 | |
| 82 | parts.add(new TextPart(count, el)); |
| 83 | parts.add(new TextPart(getSeparator(), elSep)); |
| 84 | parts.add(new TextPart(name, el)); |
| 85 | |
| 86 | return parts; |
| 87 | }; |
| 88 | |
| 89 | @Override |
| 90 | public List<KeyAction> getKeyBindings() { |
| 91 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
| 92 | |
| 93 | // TODO del, save... |
| 94 | actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter, |
| 95 | Trans.StringId.KEY_ACTION_VIEW_CARD) { |
| 96 | @Override |
| 97 | public Object getObject() { |
| 98 | int index = getSelectedIndex(); |
| 99 | |
| 100 | if (index < 0 || index >= cards.size()) |
| 101 | return null; |
| 102 | |
| 103 | if (cards.get(index) != null) |
| 104 | return cards.get(index); |
| 105 | |
| 106 | File file = files.get(index); |
| 107 | Format format = Format.Abook; |
| 108 | String ext = file.getName(); |
| 109 | if (ext.contains(".")) { |
| 110 | String tab[] = ext.split("\\."); |
| 111 | if (tab.length > 1 |
| 112 | && tab[tab.length - 1].equalsIgnoreCase("vcf")) { |
| 113 | format = Format.VCard21; |
| 114 | } |
| 115 | } |
| 116 | try { |
| 117 | Card card = new Card(file, format); |
| 118 | cards.set(index, card); |
| 119 | |
| 120 | invalidate(); |
| 121 | |
| 122 | return card; |
| 123 | } catch (IOException ioe) { |
| 124 | ioe.printStackTrace(); |
| 125 | return null; |
| 126 | } |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | return actions; |
| 131 | } |
| 132 | } |