New launcher class to start all 3 modes:
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / FileList.java
index 993007066ff75da71552bbc403c0a3280910e069..e7632e02344514fa4ecdb8a29dc89b77d6b7f75f 100644 (file)
@@ -1,22 +1,26 @@
 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;
 
+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.UiColors;
 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.gui2.Label;
+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);
        }
 
@@ -26,13 +30,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);
@@ -44,26 +49,75 @@ 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, 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<KeyAction> getKeyBindings() {
-               // TODO Auto-generated method stub
-               return null;
-       }
+               List<KeyAction> actions = new LinkedList<KeyAction>();
 
-       @Override
-       public Mode getMode() {
-               return Mode.FILE_LIST;
-       }
+               // 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();
 
-       @Override
-       public String getTitle() {
-               // TODO Auto-generated method stub
-               return null;
-       }
+                               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;
+       }
 }