001package squidpony;
002
003import squidpony.panel.IColoredString;
004
005import java.util.ArrayList;
006import java.util.Collection;
007import java.util.Iterator;
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 TextPanel in the display module.
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        public ColoredStringList(int expectedSize) {
026                super(expectedSize);
027        }
028
029        /**
030         * @return A fresh empty instance.
031         */
032        public static <T> ColoredStringList<T> create() {
033                return new ColoredStringList<T>();
034        }
035
036        /**
037         * @param expectedSize
038         * @return A fresh empty instance.
039         */
040        public static <T> ColoredStringList<T> create(int expectedSize) {
041                return new ColoredStringList<T>(expectedSize);
042        }
043
044        /**
045         * Appends {@code text} to {@code this}, without specifying its color.
046         * 
047         * @param text the text to append
048         */
049        public void addText(String text) {
050                addColoredText(text, null);
051        }
052
053        /**
054         * Appends {@code text} to {@code this}.
055         * 
056         * @param text the text to append
057         */
058        public void addText(IColoredString<T> text) {
059                final int sz = size();
060                if (sz == 0)
061                        add(text);
062                else {
063                        get(sz - 1).append(text);
064                }
065        }
066
067        /**
068         * Appends colored text to {@code this}.
069         * 
070         * @param text the text to append
071         */
072        public void addColoredText(String text, T c) {
073                if (isEmpty())
074                        addColoredTextOnNewLine(text, c);
075                else {
076                        final IColoredString<T> last = get(size() - 1);
077                        last.append(text, c);
078                }
079        }
080
081        /**
082         * Appends text to {@code this}, on a new line; without specifying its
083         * color.
084         * 
085         * @param text the text to append
086         */
087        public void addTextOnNewLine(String text) {
088                addColoredTextOnNewLine(text, null);
089        }
090
091        public void addTextOnNewLine(IColoredString<T> text) {
092                add(text);
093        }
094
095        /**
096         * Appends colored text to {@code this}.
097         * 
098         * @param text the text to append
099         */
100        public void addColoredTextOnNewLine(String text, /* @Nullable */ T color) {
101                this.add(IColoredString.Impl.create(text, color));
102        }
103
104        /**
105         * Adds {@code texts} to {@code this}, starting a new line for the first
106         * one.
107         * 
108         * @param texts the Collection of objects extending IColoredString to append
109         */
110        public void addOnNewLine(Collection<? extends IColoredString<T>> texts) {
111                final Iterator<? extends IColoredString<T>> it = texts.iterator();
112                boolean first = true;
113                while (it.hasNext()) {
114                        if (first) {
115                                addTextOnNewLine(it.next());
116                                first = false;
117                        } else
118                                addText(it.next());
119                }
120        }
121
122        /**
123         * Contrary to {@link Collection#addAll(Collection)}, this method appends
124         * text to the current text, without inserting new lines.
125         *
126         * @param texts the Collection of objects extending IColoredString to append
127         */
128        public void addAllText(Collection<? extends IColoredString<T>> texts) {
129                for (IColoredString<T> text : texts)
130                        addText(text);
131        }
132
133        /**
134         * Jumps a line.
135         */
136        public void addEmptyLine() {
137                addTextOnNewLine("");
138                addTextOnNewLine("");
139        }
140
141        /**
142         * Changes a color in members of {@code this}.
143         * 
144         * @param old The color to replace. Can be {@code null}.
145         */
146        public void replaceColor(T old, T new_) {
147                final int sz = size();
148                for (int i = 0; i < sz; i++)
149                        get(i).replaceColor(old, new_);
150        }
151
152}