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    
024    import java.io.IOException;
025    import java.util.ArrayList;
026    import java.util.Collections;
027    import java.util.List;
028    
029    /**
030     * @author <a href="mailto:alain.defrance@exoplatform.com">Alain Defrance</a>
031     */
032    public class TableElement extends Element {
033    
034      /** . */
035      public final int MARGIN = 5;
036    
037      /** . */
038      private List<RowElement> rows = new ArrayList<RowElement>();
039    
040      /** . */
041      private List<Integer> colsSize = new ArrayList<Integer>();
042    
043      public TableElement addRow(RowElement row) {
044        rows.add(row);
045        return this;
046      }
047      
048      @Override
049      void doPrint(UIWriterContext ctx, ShellWriter writer) throws IOException {
050    
051        ctx = new UIWriterContext(ctx);
052    
053        colsSize = computeColSize(ctx.getConsoleWidth());
054        ctx.parentUIContext.pad(writer);
055        for (RowElement e : rows) {
056          e.print(ctx, writer);
057        }
058    
059        ctx.pad(writer);
060        writer.append("\n");
061        
062      }
063    
064      public List<RowElement> getRows() {
065        return rows;
066      }
067    
068      public List<Integer> getColsSize() {
069        return Collections.unmodifiableList(colsSize);
070      }
071    
072      private List<Integer> computeColSize(int consoleWidth) {
073    
074        List<Integer> colsSize = new ArrayList<Integer>();
075    
076        int colSum = 0;
077        for (int i = 0; i < columnNumber(); ++i) {
078          int colSize = 0;
079          for (RowElement row : rows) {
080            colSize = Math.max(colSize, row.getValues().get(i).width() + MARGIN);
081            int missingSpace = (colSum + colSize) - consoleWidth;
082            if (missingSpace > 0) {
083              colSize -= missingSpace;
084            }
085          }
086          colsSize.add(colSize);
087          colSum += colSize;
088        }
089    
090        return colsSize;
091        
092      }
093    
094      private int columnNumber() {
095    
096        int n = 0;
097    
098        for (RowElement row : rows) {
099          n = Math.max(n, row.getValues().size());
100        }
101    
102        return n;
103    
104      }
105    
106      @Override
107      int width() {
108        return 0;
109      }
110    }