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.Renderer;
023    import org.crsh.text.Style;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    public class TableElement extends Element {
029    
030      /** . */
031      ArrayList<RowElement> rows = new ArrayList<RowElement>();
032    
033      /** . */
034      protected Border border;
035    
036      /** The column layout. */
037      protected ColumnLayout layout;
038    
039      /** The table height, null means no limit. */
040      protected Integer height;
041    
042      public TableElement() {
043        this.layout = ColumnLayout.rightToLeft();
044      }
045    
046      public TableElement(int ... weights) {
047        this.layout = ColumnLayout.weighted(weights);
048      }
049    
050      public TableElement add(RowElement row) {
051        if (row.parent != null) {
052          throw new IllegalArgumentException("Row has already a parent");
053        }
054        rows.add(row);
055        row.parent = this;
056        return this;
057      }
058    
059      public Integer getHeight() {
060        return height;
061      }
062    
063      public void setHeight(Integer height) throws IllegalArgumentException {
064        if (height != null && height < 0) {
065          throw new IllegalArgumentException("No negative table height accepted");
066        }
067        this.height = height;
068      }
069    
070      public ColumnLayout getLayout() {
071        return layout;
072      }
073    
074      public Border getBorder() {
075        return border;
076      }
077    
078      public Renderer renderer() {
079        return new TableRenderer(this);
080      }
081    
082      public TableElement layout(ColumnLayout layout) {
083        this.layout = layout;
084        return this;
085      }
086    
087      public List<RowElement> getRows() {
088        return rows;
089      }
090    
091      public TableElement border(Border border) {
092        setBorder(border);
093        return this;
094      }
095    
096      public void setBorder(Border border) {
097        this.border = border;
098      }
099    
100      @Override
101      public TableElement style(Style.Composite style) {
102        return (TableElement)super.style(style);
103      }
104    }