001package squidpony;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006
007import squidpony.panel.IColoredString;
008
009/**
010 * An helper class for code that deals with lists of {@link IColoredString}s. It
011 * does nothing smart, its only purpose is to save you some typing for frequent
012 * calls. It is particularly useful when feeding large pieces of text to classes
013 * like {@link TextPanel}.
014 * 
015 * @author smelC
016 */
017public class ColoredStringList<T> extends ArrayList<IColoredString<T>> {
018
019        private static final long serialVersionUID = -5111205714079762803L;
020
021        public ColoredStringList() {
022                super();
023        }
024
025        /**
026         * Appends {@code text} to {@code this}, without specifying its color.
027         * 
028         * @param text
029         */
030        public void addText(String text) {
031                addColoredText(text, null);
032        }
033
034        /**
035         * Appends {@code text} to {@code this}.
036         * 
037         * @param text
038         */
039        public void addText(IColoredString<T> text) {
040                final int sz = size();
041                if (sz == 0)
042                        add(text);
043                else {
044                        get(sz - 1).append(text);
045                }
046        }
047
048        /**
049         * Appends colored text to {@code this}.
050         * 
051         * @param text
052         */
053        public void addColoredText(String text, T c) {
054                if (isEmpty())
055                        addColoredTextOnNewLine(text, c);
056                else {
057                        final IColoredString<T> last = get(size() - 1);
058                        last.append(text, c);
059                }
060        }
061
062        /**
063         * Appends text to {@code this}, on a new line; without specifying its
064         * color.
065         * 
066         * @param text
067         */
068        public void addTextOnNewLine(String text) {
069                addColoredTextOnNewLine(text, null);
070        }
071
072        public void addTextOnNewLine(IColoredString<T> text) {
073                add(text);
074        }
075
076        /**
077         * Appends colored text to {@code this}.
078         * 
079         * @param text
080         */
081        public void addColoredTextOnNewLine(String text, /* @Nullable */ T color) {
082                this.add(IColoredString.Impl.<T> create(text, color));
083        }
084
085        /**
086         * Adds {@code texts} to {@code this}, starting a new line for the first
087         * one.
088         * 
089         * @param texts
090         */
091        public void addOnNewLine(Collection<? extends IColoredString<T>> texts) {
092                final Iterator<? extends IColoredString<T>> it = texts.iterator();
093                boolean first = true;
094                while (it.hasNext()) {
095                        if (first) {
096                                addTextOnNewLine(it.next());
097                                first = false;
098                        } else
099                                addText(it.next());
100                }
101        }
102
103        /**
104         * Contrary to {@link Collection#addAll(Collection)}, this method appends
105         * text to the current text, without inserting new lines.
106         * 
107         * @param texts
108         */
109        public void addAllText(Collection<? extends IColoredString<T>> texts) {
110                for (IColoredString<T> text : texts)
111                        addText(text);
112        }
113
114        /**
115         * Jumps a line.
116         */
117        public void addEmptyLine() {
118                addTextOnNewLine("");
119                addTextOnNewLine("");
120        }
121
122}