Resources: now allow "--config" and external .properties files
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / FileList.java
index 79f530e0604f38318d6b3283ce1dc75ce9530f88..75f9352cf92f389060276e67c1983b7f811068c3 100644 (file)
@@ -2,6 +2,7 @@ package be.nikiroo.jvcard.tui.panes;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -9,19 +10,19 @@ import be.nikiroo.jvcard.Card;
 import be.nikiroo.jvcard.i18n.Trans;
 import be.nikiroo.jvcard.parsers.Format;
 import be.nikiroo.jvcard.tui.KeyAction;
-import be.nikiroo.jvcard.tui.UiColors;
 import be.nikiroo.jvcard.tui.KeyAction.DataType;
 import be.nikiroo.jvcard.tui.KeyAction.Mode;
+import be.nikiroo.jvcard.tui.StringUtils;
+import be.nikiroo.jvcard.tui.UiColors;
+import be.nikiroo.jvcard.tui.UiColors.Element;
 
 import com.googlecode.lanterna.input.KeyType;
 
 public class FileList extends MainContentList {
-       private List<File> files;
-
-       public FileList(List<File> files) {
-               super(UiColors.Element.CONTACT_LINE,
-                               UiColors.Element.CONTACT_LINE_SELECTED);
+       private List<String> files;
+       private List<Card> cards;
 
+       public FileList(List<String> files) {
                setFiles(files);
        }
 
@@ -31,13 +32,14 @@ public class FileList extends MainContentList {
         * @param files
         *            the new files
         */
-       public void setFiles(List<File> files) {
+       public void setFiles(List<String> files) {
                clearItems();
                this.files = files;
+               cards = new ArrayList<Card>();
 
-               // TODO
-               for (File file : files) {
-                       addItem(file.getName());
+               for (String file : files) {
+                       addItem(file); // TODO
+                       cards.add(null);
                }
 
                setSelectedIndex(0);
@@ -49,10 +51,41 @@ public class FileList extends MainContentList {
        }
 
        @Override
-       public String getExitWarning() {
-               // TODO Auto-generated method stub
-               return null;
-       }
+       protected List<TextPart> 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<TextPart> parts = new LinkedList<TextPart>();
+
+               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, UiColors.getInstance().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<KeyAction> getKeyBindings() {
@@ -63,18 +96,23 @@ public class FileList extends MainContentList {
                                Trans.StringId.KEY_ACTION_VIEW_CARD) {
                        @Override
                        public Object getObject() {
-                               File file = files.get(getSelectedIndex());
-                               Format format = Format.Abook;
-                               String ext = file.getName();
-                               if (ext.contains(".")) {
-                                       String tab[] = ext.split("\\.");
-                                       if (tab.length > 1
-                                                       && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
-                                               format = Format.VCard21;
-                                       }
-                               }
+                               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 {
-                                       return new Card(file, format);
+                                       Card card = FileList.getCard(file);
+                                       cards.set(index, card);
+
+                                       invalidate();
+
+                                       return card;
                                } catch (IOException ioe) {
                                        ioe.printStackTrace();
                                        return null;
@@ -85,15 +123,24 @@ public class FileList extends MainContentList {
                return actions;
        }
 
-       @Override
-       public Mode getMode() {
-               return Mode.FILE_LIST;
-       }
+       static private Card getCard(String input) throws IOException {
+               Format format = Format.Abook;
+               String ext = input;
+               if (ext.contains(".")) {
+                       String tab[] = ext.split("\\.");
+                       if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
+                               format = Format.VCard21;
+                       }
+               }
 
-       @Override
-       public String getTitle() {
-               // TODO Auto-generated method stub
-               return null;
-       }
+               Card card = null;
+               try {
+                       card = new Card(new File(input), format);
+               } catch (IOException ioe) {
+                       ioe.printStackTrace();
+                       throw ioe;
+               }
 
+               return card;
+       }
 }