001    /*
002     * Copyright (C) 2010 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.shell.ui;
021    
022    import org.crsh.shell.io.ShellWriter;
023    import org.crsh.text.Style;
024    
025    import java.io.IOException;
026    import java.util.ArrayList;
027    import java.util.List;
028    
029    /**
030     * @author <a href="mailto:alain.defrance@exoplatform.com">Alain Defrance</a>
031     */
032    public class RowElement extends Element {
033    
034      /** . */
035      private List<Element> cols;
036    
037      /** . */
038      boolean header;
039    
040      public RowElement() {
041        this.cols = new ArrayList<Element>();
042      }
043    
044      public RowElement(boolean header) {
045        this();
046        this.header = header;
047      }
048    
049      @Override
050      public void print(UIWriterContext ctx, ShellWriter writer) throws IOException {
051        doPrint(ctx, writer);
052      }
053    
054      @Override
055      void doPrint(UIWriterContext ctx, ShellWriter writer) throws IOException {
056    
057        int i = 0;
058        TableElement table = (TableElement) getParent();
059        List<Integer> colsSize = table.getColsSize();
060    
061        // Request bottom header line
062        if (table.border && header) {
063          ctx.needLine = header;
064        }
065    
066        // Init line padding
067        if (table.border) {
068          ctx.leftLinePadding += "| ";
069          ctx.rightLinePadding += "|";
070        }
071    
072        for (Element e : cols) {
073    
074          //
075          int availableWidth = (ctx.getConsoleWidth() - ctx.leftLinePadding.length() - ctx.rightLinePadding.length());
076          if (availableWidth <= 0) {
077            break;
078          }
079    
080          //
081          ctx.pad(writer);
082          if (ctx.needLF) {
083    
084            //
085            if (table.border) {
086              writer.append("|");
087            }
088            writer.append("\n");
089    
090            //
091            ctx.parentUIContext.pad(writer);
092            if (table.border && ctx.needLine) {
093              ctx.printLine(table.width() - 2, writer);
094              ctx.parentUIContext.pad(writer);
095            }
096    
097          }
098          ctx.padStyle = null;
099    
100          //
101          if (table.border) {
102            writer.append("|");
103            ctx.stack.add(Pad.SPACE);
104            ctx.padStyle = Style.style(e.getDecoration(), e.getForeground(), e.getBackground());
105            ctx.pad(writer);
106          }
107    
108          //
109          e.print(ctx, writer);
110          ctx.stack.clear();
111    
112          //
113          ctx.padStyle = Style.style(e.getDecoration(), e.getForeground(), e.getBackground());
114          for (int j = 0; j < colsSize.get(i) - e.width(); ++j) {
115            ctx.stack.add(Pad.SPACE);
116          }
117    
118          //
119          for (int index = 0; index < table.getColsSize().get(i); ++index) {
120            ctx.leftLinePadding += " ";
121          }
122          if (table.border) {
123            ctx.leftLinePadding += "| ";
124          }
125    
126          //
127          ++i;
128          ctx.needLF = false;
129          ctx.needLine = false;
130    
131        }
132    
133        //
134        ctx.needLF = true;
135        ctx.needLine = header;
136        ctx.leftLinePadding = "";
137        ctx.rightLinePadding = "";
138    
139      }
140    
141      @Override
142      int width() {
143        return 0;
144      }
145    
146      public void addValue(Element element) {
147        this.cols.add(element);
148      }
149    
150      public List<Element> getValues() {
151        return cols;
152      }
153      
154    }