package be.nikiroo.jvcard.tui.panes; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import be.nikiroo.jvcard.Card; import be.nikiroo.jvcard.launcher.Main; import be.nikiroo.jvcard.resources.StringUtils; import be.nikiroo.jvcard.resources.Trans; import be.nikiroo.jvcard.tui.KeyAction; import be.nikiroo.jvcard.tui.KeyAction.DataType; import be.nikiroo.jvcard.tui.KeyAction.Mode; import be.nikiroo.jvcard.tui.UiColors.Element; import com.googlecode.lanterna.input.KeyType; public class FileList extends MainContentList { private List files; private List cards; public FileList(List files) { setFiles(files); } /** * Change the list of currently selected files. * * @param files * the new files */ public void setFiles(List files) { clearItems(); this.files = files; cards = new ArrayList(); for (String file : files) { addItem(file); // TODO cards.add(null); } setSelectedIndex(0); } @Override public DataType getDataType() { return DataType.CARD_FILES; } @Override protected List getLabel(int index, int width, boolean selected, boolean focused) { // TODO: from ini file? int SIZE_COL_1 = 3; Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED : Element.CONTACT_LINE; Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED : Element.CONTACT_LINE_SEPARATOR; List parts = new LinkedList(); String count = ""; if (cards.get(index) != null) count += cards.get(index).size(); String name = files.get(index).replaceAll("\\\\", "/"); int indexSl = name.lastIndexOf('/'); if (indexSl >= 0) { name = name.substring(indexSl + 1); } name = StringUtils.sanitize(name, Main.isUnicode()); count = " " + StringUtils.padString(count, SIZE_COL_1) + " "; name = " " + StringUtils.padString(name, width - SIZE_COL_1 - getSeparator().length()) + " "; parts.add(new TextPart(count, el)); parts.add(new TextPart(getSeparator(), elSep)); parts.add(new TextPart(name, el)); return parts; }; @Override public List getKeyBindings() { List actions = new LinkedList(); // TODO del, save... actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter, Trans.StringId.KEY_ACTION_VIEW_CARD) { @Override public Object getObject() { int index = getSelectedIndex(); if (index < 0 || index >= cards.size()) return null; if (cards.get(index) != null) return cards.get(index); String file = files.get(index); try { Card card = Main.getCard(file); cards.set(index, card); invalidate(); return card; } catch (IOException ioe) { ioe.printStackTrace(); return null; } } }); return actions; } }