Update lanterna, fix bugs, implement save...
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / MainContentList.java
index 137a68dd210833e6a8613c125d87fa289609e200..38b775c2998af5986cf06e31440bd663b42d90d5 100644 (file)
@@ -1,7 +1,12 @@
 package be.nikiroo.jvcard.tui.panes;
 
+import java.util.LinkedList;
+import java.util.List;
+
 import be.nikiroo.jvcard.tui.UiColors;
+import be.nikiroo.jvcard.tui.UiColors.Element;
 
+import com.googlecode.lanterna.TextColor;
 import com.googlecode.lanterna.gui2.ActionListBox;
 import com.googlecode.lanterna.gui2.Direction;
 import com.googlecode.lanterna.gui2.LinearLayout;
@@ -11,6 +16,43 @@ import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer;
 abstract public class MainContentList extends MainContent implements Runnable {
        private ActionListBox lines;
 
+       /**
+        * This class represent a part of a text line to draw in this
+        * {@link MainContentList}.
+        * 
+        * @author niki
+        * 
+        */
+       public class TextPart {
+               private String text;
+               private Element element;
+
+               public TextPart(String text, Element element) {
+                       this.text = text;
+                       this.element = element;
+               }
+
+               public String getText() {
+                       return text;
+               }
+
+               public Element getElement() {
+                       return element;
+               }
+
+               public TextColor getForegroundColor() {
+                       if (element != null)
+                               return element.getForegroundColor();
+                       return Element.DEFAULT.getForegroundColor();
+               }
+
+               public TextColor getBackgroundColor() {
+                       if (element != null)
+                               return element.getBackgroundColor();
+                       return Element.DEFAULT.getBackgroundColor();
+               }
+       }
+
        public MainContentList(final UiColors.Element normalStyle,
                        final UiColors.Element selectedStyle) {
                super(Direction.VERTICAL);
@@ -51,27 +93,23 @@ abstract public class MainContentList extends MainContent implements Runnable {
                                                        ActionListBox listBox, int index, Runnable item,
                                                        boolean selected, boolean focused) {
 
-                                               if (selected && focused) {
-                                                       graphics.setForegroundColor(selectedStyle
-                                                                       .getForegroundColor());
-                                                       graphics.setBackgroundColor(selectedStyle
-                                                                       .getBackgroundColor());
-                                               } else {
-                                                       graphics.setForegroundColor(normalStyle
+                                               // width "-1" to reserve space for the optional vertical
+                                               // scroll bar
+                                               List<TextPart> parts = MainContentList.this.getLabel(
+                                                               index, lines.getSize().getColumns() - 1,
+                                                               selected, focused);
+
+                                               int position = 0;
+                                               for (TextPart part : parts) {
+                                                       graphics.setForegroundColor(part
                                                                        .getForegroundColor());
-                                                       graphics.setBackgroundColor(normalStyle
+                                                       graphics.setBackgroundColor(part
                                                                        .getBackgroundColor());
-                                               }
-
-                                               // original impl:
-                                               // String label = getLabel(listBox, index, item);
-                                               // label = TerminalTextUtils.fitString(label,
-                                               // graphics.getSize().getColumns());
+                                                       String label = part.getText();
 
-                                               // TODO: why +5 ?? padding problem?
-                                               String label = MainContentList.this.getLabel(index,
-                                                               lines.getSize().getColumns() + 5);
-                                               graphics.putString(0, 0, label);
+                                                       graphics.putString(position, 0, label);
+                                                       position += label.length();
+                                               }
                                        }
                                });
 
@@ -88,7 +126,7 @@ abstract public class MainContentList extends MainContent implements Runnable {
        public void addItem(String line) {
                lines.addItem(line, this);
        }
-       
+
        /**
         * Clear all the items in this {@link MainContentList}
         */
@@ -114,6 +152,17 @@ abstract public class MainContentList extends MainContent implements Runnable {
        public void setSelectedIndex(int index) {
                lines.setSelectedIndex(index);
        }
+       
+       
+       /**
+        * Return the default content separator for text fields.
+        * 
+        * @return the separator
+        */
+       public String getSeparator() {
+               // we could use: " ", "┃", "│"...
+               return "┃";
+       }
 
        @Override
        public void run() {
@@ -128,17 +177,37 @@ abstract public class MainContentList extends MainContent implements Runnable {
                return null;
        }
 
+       @Override
+       public int getCount() {
+               return lines.getItemCount();
+       }
+
        /**
-        * Return the text representation of the selected line.
+        * Return the representation of the selected line, in {@link TextPart}s.
         * 
         * @param index
         *            the line index
         * @param width
         *            the max width of the line
+        * @param selected
+        *            TRUE if the item is selected
+        * @param focused
+        *            TRUE if the item is focused
         * 
         * @return the text representation
         */
-       protected String getLabel(int index, int width) {
-               return "" + lines.getItems().get(index);
+       protected List<TextPart> getLabel(int index, int width, boolean selected,
+                       boolean focused) {
+               List<TextPart> parts = new LinkedList<TextPart>();
+
+               if (selected && focused) {
+                       parts.add(new TextPart("" + lines.getItems().get(index),
+                                       Element.CONTACT_LINE_SELECTED));
+               } else {
+                       parts.add(new TextPart("" + lines.getItems().get(index),
+                                       Element.CONTACT_LINE));
+               }
+
+               return parts;
        }
 }