X-Git-Url: http://git.nikiroo.be/?p=jvcard.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2Fpanes%2FMainContentList.java;h=e4c40ba0bc5aaa1a4031d736c79a4867814a60c5;hp=137a68dd210833e6a8613c125d87fa289609e200;hb=7da41ecd30228908bf2afcd07ff7943ab59d4c01;hpb=fae07ea7af01c64ca1a858db75a615555318d5e2 diff --git a/src/be/nikiroo/jvcard/tui/panes/MainContentList.java b/src/be/nikiroo/jvcard/tui/panes/MainContentList.java index 137a68d..e4c40ba 100644 --- a/src/be/nikiroo/jvcard/tui/panes/MainContentList.java +++ b/src/be/nikiroo/jvcard/tui/panes/MainContentList.java @@ -1,82 +1,117 @@ package be.nikiroo.jvcard.tui.panes; -import be.nikiroo.jvcard.tui.UiColors; +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.Trans.StringId; +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; - public MainContentList(final UiColors.Element normalStyle, - final UiColors.Element selectedStyle) { + /** + * 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() { super(Direction.VERTICAL); lines = new ActionListBox(); - lines - .setListItemRenderer(new ListItemRenderer() { - /** - * 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) { - - if (selected && focused) { - graphics.setForegroundColor(selectedStyle - .getForegroundColor()); - graphics.setBackgroundColor(selectedStyle - .getBackgroundColor()); - } else { - graphics.setForegroundColor(normalStyle - .getForegroundColor()); - graphics.setBackgroundColor(normalStyle - .getBackgroundColor()); - } - - // original impl: - // String label = getLabel(listBox, index, item); - // label = TerminalTextUtils.fitString(label, - // graphics.getSize().getColumns()); - - // TODO: why +5 ?? padding problem? - String label = MainContentList.this.getLabel(index, - lines.getSize().getColumns() + 5); - graphics.putString(0, 0, label); - } - }); - - addComponent(lines, LinearLayout - .createLayoutData(LinearLayout.Alignment.Fill)); + lines.setListItemRenderer(new ListItemRenderer() { + /** + * 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 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)); } /** @@ -88,7 +123,40 @@ abstract public class MainContentList extends MainContent implements Runnable { public void addItem(String line) { 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 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} */ @@ -115,6 +183,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. @@ -128,17 +205,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 getLabel(int index, int width, boolean selected, + boolean focused) { + List parts = new LinkedList(); + + 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; } }