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;
021    
022    import java.util.Iterator;
023    
024    /**
025     * Something that can be rendered within a context.
026     */
027    public abstract class Renderer {
028    
029      public static Renderer compose(Iterable<Renderer> renderers) {
030        Iterator<Renderer> i = renderers.iterator();
031        if (i.hasNext()) {
032          Renderer renderer = i.next();
033          if (i.hasNext()) {
034            return new Composite(renderers);
035          } else {
036            return renderer;
037          }
038        } else {
039          return new Renderer() {
040            @Override
041            public int getActualWidth() {
042              return 0;
043            }
044            @Override
045            public int getMinWidth() {
046              return 0;
047            }
048            @Override
049            public LineReader renderer(int width) {
050              return new LineReader() {
051                public boolean hasLine() {
052                  return false;
053                }
054                public void renderLine(RenderAppendable to) throws IllegalStateException {
055                  throw new IllegalStateException();
056                }
057              };
058            }
059          };
060        }
061      }
062    
063      /**
064       * Returns the element actual width.
065       *
066       * @return the actual width
067       */
068      public abstract int getActualWidth();
069    
070      /**
071       * Returns the element minimum width.
072       *
073       * @return the minimum width
074       */
075      public abstract int getMinWidth();
076    
077      /**
078       * Create a renderer for the specified width or return null if the element does not provide any output.
079       *
080       * @param width the width the width
081       * @return the renderer
082       */
083      public abstract LineReader renderer(int width);
084    
085      /**
086       * Renders this object to the provided output.
087       *
088       * @param out the output
089       */
090      public final void render(RenderAppendable out) {
091        LineReader renderer = renderer(out.getWidth());
092        if (renderer != null) {
093          while (renderer.hasLine()) {
094            renderer.renderLine(out);
095            out.append('\n');
096          }
097        }
098      }
099    
100      private static class Composite extends Renderer {
101    
102        /** . */
103        private final Iterable<? extends Renderer> renderers;
104    
105        /** . */
106        private final int actualWidth;
107    
108        /** . */
109        private final int minWidth;
110    
111        private Composite(Iterable<? extends Renderer> renderers) {
112    
113          int actualWidth = 0;
114          int minWidth = 0;
115          for (Renderer renderer : renderers) {
116            actualWidth = Math.max(actualWidth, renderer.getActualWidth());
117            minWidth = Math.max(minWidth, renderer.getMinWidth());
118          }
119    
120          this.actualWidth = actualWidth;
121          this.minWidth = minWidth;
122          this.renderers = renderers;
123        }
124    
125        @Override
126        public int getActualWidth() {
127          return actualWidth;
128        }
129    
130        @Override
131        public int getMinWidth() {
132          return minWidth;
133        }
134    
135        @Override
136        public LineReader renderer(final int width) {
137          return new LineReader() {
138    
139            /** . */
140            Iterator<? extends Renderer> i = renderers.iterator();
141    
142            /** . */
143            LineReader current = null;
144    
145            public boolean hasLine() {
146              while (true) {
147                if (current != null) {
148                  if (current.hasLine()) {
149                    break;
150                  } else {
151                    current = null;
152                  }
153                } else {
154                  if (i.hasNext()) {
155                    current = i.next().renderer(width);
156                  } else {
157                    break;
158                  }
159                }
160              }
161              return current != null;
162            }
163    
164            public void renderLine(RenderAppendable to) throws IllegalStateException {
165              if (!hasLine()) {
166                throw new IllegalStateException();
167              }
168              current.renderLine(to);
169            }
170          };
171        }
172      }
173    }