Resources system rewrite + new "--save-config DIR" option
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / MainContentList.java
index 83654198f08c127f75d6ab6f1f06de66ca7372d3..9e5f8ec0e8278108d0aee9d971e6b1d04378f592 100644 (file)
@@ -3,15 +3,18 @@ package be.nikiroo.jvcard.tui.panes;
 import java.util.LinkedList;
 import java.util.List;
 
+import be.nikiroo.jvcard.launcher.Main;
+import be.nikiroo.jvcard.resources.StringUtils;
+import be.nikiroo.jvcard.resources.enums.ColorOption;
+import be.nikiroo.jvcard.resources.enums.StringId;
 import be.nikiroo.jvcard.tui.UiColors;
-import be.nikiroo.jvcard.tui.UiColors.Element;
 
 import com.googlecode.lanterna.TextColor;
+import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer;
 import com.googlecode.lanterna.gui2.ActionListBox;
 import com.googlecode.lanterna.gui2.Direction;
 import com.googlecode.lanterna.gui2.LinearLayout;
 import com.googlecode.lanterna.gui2.TextGUIGraphics;
-import com.googlecode.lanterna.gui2.AbstractListBox.ListItemRenderer;
 
 abstract public class MainContentList extends MainContent implements Runnable {
        private ActionListBox lines;
@@ -25,9 +28,9 @@ abstract public class MainContentList extends MainContent implements Runnable {
         */
        public class TextPart {
                private String text;
-               private Element element;
+               private ColorOption element;
 
-               public TextPart(String text, Element element) {
+               public TextPart(String text, ColorOption element) {
                        this.text = text;
                        this.element = element;
                }
@@ -36,85 +39,80 @@ abstract public class MainContentList extends MainContent implements Runnable {
                        return text;
                }
 
-               public Element getElement() {
+               public ColorOption getElement() {
                        return element;
                }
 
                public TextColor getForegroundColor() {
                        if (element != null)
-                               return element.getForegroundColor();
-                       return Element.DEFAULT.getForegroundColor();
+                               return UiColors.getForegroundColor(element);
+                       return UiColors.getForegroundColor(ColorOption.DEFAULT);
                }
 
                public TextColor getBackgroundColor() {
                        if (element != null)
-                               return element.getBackgroundColor();
-                       return Element.DEFAULT.getBackgroundColor();
+                               return UiColors.getBackgroundColor(element);
+                       return UiColors.getBackgroundColor(ColorOption.DEFAULT);
                }
        }
 
-       public MainContentList(final UiColors.Element normalStyle,
-                       final UiColors.Element selectedStyle) {
+       public MainContentList() {
                super(Direction.VERTICAL);
 
                lines = new ActionListBox();
 
-               lines
-                               .setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() {
-                                       /**
-                                        * This is the main drawing method for a single list box
-                                        * item, it applies the current theme to setup the colors
-                                        * and then calls {@code getLabel(..)} and draws the result
-                                        * using the supplied {@code TextGUIGraphics}. The graphics
-                                        * object is created just for this item and is restricted so
-                                        * that it can only draw on the area this item is occupying.
-                                        * The top-left corner (0x0) should be the starting point
-                                        * when drawing the item.
-                                        * 
-                                        * @param graphics
-                                        *            Graphics object to draw with
-                                        * @param listBox
-                                        *            List box we are drawing an item from
-                                        * @param index
-                                        *            Index of the item we are drawing
-                                        * @param item
-                                        *            The item we are drawing
-                                        * @param selected
-                                        *            Will be set to {@code true} if the item is
-                                        *            currently selected, otherwise {@code false},
-                                        *            but please notice what context 'selected'
-                                        *            refers to here (see {@code setSelectedIndex})
-                                        * @param focused
-                                        *            Will be set to {@code true} if the list box
-                                        *            currently has input focus, otherwise {@code
-                                        *            false}
-                                        */
-                                       public void drawItem(TextGUIGraphics graphics,
-                                                       ActionListBox listBox, int index, Runnable item,
-                                                       boolean selected, boolean focused) {
-
-                                               // 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(part
-                                                                       .getBackgroundColor());
-                                                       String label = part.getText();
-
-                                                       graphics.putString(position, 0, label);
-                                                       position += label.length();
-                                               }
-                                       }
-                               });
-
-               addComponent(lines, LinearLayout
-                               .createLayoutData(LinearLayout.Alignment.Fill));
+               lines.setListItemRenderer(new ListItemRenderer<Runnable, ActionListBox>() {
+                       /**
+                        * This is the main drawing method for a single list box item, it
+                        * applies the current theme to setup the colors and then calls
+                        * {@code getLabel(..)} and draws the result using the supplied
+                        * {@code TextGUIGraphics}. The graphics object is created just for
+                        * this item and is restricted so that it can only draw on the area
+                        * this item is occupying. The top-left corner (0x0) should be the
+                        * starting point when drawing the item.
+                        * 
+                        * @param graphics
+                        *            Graphics object to draw with
+                        * @param listBox
+                        *            List box we are drawing an item from
+                        * @param index
+                        *            Index of the item we are drawing
+                        * @param item
+                        *            The item we are drawing
+                        * @param selected
+                        *            Will be set to {@code true} if the item is currently
+                        *            selected, otherwise {@code false}, but please notice
+                        *            what context 'selected' refers to here (see
+                        *            {@code setSelectedIndex})
+                        * @param focused
+                        *            Will be set to {@code true} if the list box currently
+                        *            has input focus, otherwise {@code false}
+                        */
+                       public void drawItem(TextGUIGraphics graphics,
+                                       ActionListBox listBox, int index, Runnable item,
+                                       boolean selected, boolean focused) {
+
+                               // 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(part.getBackgroundColor());
+
+                                       String label = StringUtils.sanitize(part.getText(),
+                                                       Main.isUnicode());
+
+                                       graphics.putString(position, 0, label);
+                                       position += label.length();
+                               }
+                       }
+               });
+
+               addComponent(lines,
+                               LinearLayout.createLayoutData(LinearLayout.Alignment.Fill));
        }
 
        /**
@@ -127,6 +125,39 @@ abstract public class MainContentList extends MainContent implements Runnable {
                lines.addItem(line, this);
        }
 
+       /**
+        * Delete the given item.
+        * 
+        * Remark: it will only delete the first found instance if multiple
+        * instances of this item are present.
+        * 
+        * @param line
+        *            the line to delete
+        * 
+        * @return TRUE if the item was deleted
+        */
+       public boolean removeItem(String line) {
+               boolean deleted = false;
+
+               List<Runnable> copy = lines.getItems();
+               for (int index = 0; index < copy.size(); index++) {
+                       if (copy.get(index).toString().equals(line)) {
+                               deleted = true;
+                               copy.remove(index);
+                               break;
+                       }
+               }
+
+               int index = getSelectedIndex();
+               clearItems();
+               for (Runnable run : copy) {
+                       addItem(run.toString());
+               }
+               setSelectedIndex(index);
+
+               return deleted;
+       }
+
        /**
         * Clear all the items in this {@link MainContentList}
         */
@@ -153,6 +184,15 @@ abstract public class MainContentList extends MainContent implements Runnable {
                lines.setSelectedIndex(index);
        }
 
+       /**
+        * Return the default content separator for text fields.
+        * 
+        * @return the separator
+        */
+       public String getSeparator() {
+               return Main.trans(StringId.DEAULT_FIELD_SEPARATOR);
+       }
+
        @Override
        public void run() {
                // item selected.
@@ -191,10 +231,10 @@ abstract public class MainContentList extends MainContent implements Runnable {
 
                if (selected && focused) {
                        parts.add(new TextPart("" + lines.getItems().get(index),
-                                       Element.CONTACT_LINE_SELECTED));
+                                       ColorOption.CONTACT_LINE_SELECTED));
                } else {
                        parts.add(new TextPart("" + lines.getItems().get(index),
-                                       Element.CONTACT_LINE));
+                                       ColorOption.CONTACT_LINE));
                }
 
                return parts;