001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.text.ui;
021    
022    import org.crsh.text.LineRenderer;
023    import org.crsh.text.Style;
024    import org.crsh.util.Utils;
025    
026    import java.util.Iterator;
027    
028    public class TextElement extends Element {
029    
030      /** . */
031      final Iterable<Object> stream;
032    
033      /** . */
034      final int minWidth;
035    
036      /** . */
037      final int width;
038    
039      private static int width(int width, Iterator<Object> stream, CharSequence current, Integer from) {
040        while (current == null) {
041          if (stream.hasNext()) {
042            Object next = stream.next();
043            if (next instanceof CharSequence) {
044              current = (CharSequence)next;
045              from = 0;
046            }
047          } else {
048            break;
049          }
050        }
051        if (current == null) {
052          return width;
053        } else {
054          int pos = Utils.indexOf(current, from, '\n');
055          if (pos == -1) {
056            return width(width + current.length() - from, stream, current, from);
057          } else {
058            return Math.max(width + pos - from, width(0, stream, null, 0));
059          }
060        }
061      }
062    
063      public TextElement(Iterable<Object> stream, int minWidth) {
064        if (minWidth < 0) {
065          throw new IllegalArgumentException("No negative min size allowed");
066        }
067    
068        // Determine width
069        int width = width(0, stream.iterator(), null, null);
070    
071        //
072        this.minWidth = Math.min(width, minWidth);
073        this.stream = stream;
074        this.width = width;
075      }
076    
077      public TextElement(Iterable<Object> stream) {
078        this(stream, 1);
079      }
080    
081      public LineRenderer renderer() {
082        // return new LabelRenderer(this);
083        throw new UnsupportedOperationException();
084      }
085    
086      @Override
087      public String toString() {
088        return "TextElement[]";
089      }
090    
091      @Override
092      public TextElement style(Style.Composite style) {
093        return (TextElement)super.style(style);
094      }
095    }