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
020package org.crsh.text.ui;
021
022import org.crsh.text.LineRenderer;
023import org.crsh.text.Style;
024
025import java.util.ArrayList;
026import java.util.List;
027
028public class TableElement extends Element {
029
030  /** . */
031  ArrayList<RowElement> rows = new ArrayList<RowElement>();
032
033  /** . */
034  protected BorderStyle border;
035
036  /** . */
037  protected BorderStyle separator;
038
039  /** . */
040  private Overflow overflow;
041
042  /** The column layout. */
043  protected Layout columnLayout;
044
045  /** The optional row row layout. */
046  protected Layout rowLayout;
047
048  /** Cell padding left. */
049  private int leftCellPadding;
050
051  /** Cell padding right. */
052  private int rightCellPadding;
053
054  public TableElement() {
055    this(Layout.flow(), Layout.flow());
056  }
057
058  public TableElement(int ... columns) {
059    this(Layout.flow(), Layout.weighted(columns));
060  }
061
062  public TableElement(int[] rows, int[] columns) {
063    this(Layout.weighted(rows), Layout.weighted(columns));
064  }
065
066  private TableElement(Layout rowLayout, Layout columnLayout) {
067    this.rowLayout = rowLayout;
068    this.columnLayout = columnLayout;
069    this.border = null;
070    this.separator = null;
071    this.overflow = Overflow.WRAP;
072    this.leftCellPadding = 0;
073    this.rightCellPadding = 0;
074  }
075
076  public TableElement add(RowElement row) {
077    rows.add(row);
078    return this;
079  }
080
081  public TableElement add(RowElement... rows) {
082    for (RowElement row : rows) {
083      add(row);
084    }
085    return this;
086  }
087
088  public TableElement header(Element... cols) {
089    return row(true, cols);
090  }
091
092  public TableElement row(Element... cols) {
093    return row(false, cols);
094  }
095
096  public TableElement row(String... cols) {
097    return row(false, cols);
098  }
099
100  public TableElement row(boolean header, Element... cols) {
101    return add(new RowElement(header).add(cols));
102  }
103
104  public TableElement row(boolean header, String... cols) {
105    return add(new RowElement(header).add(cols));
106  }
107
108  public Layout getColumnLayout() {
109    return columnLayout;
110  }
111
112  public void setColumnLayout(Layout columnLayout) {
113    if (columnLayout == null) {
114      throw new NullPointerException("Column layout cannot be null");
115    }
116    this.columnLayout = columnLayout;
117  }
118
119  public Layout getRowLayout() {
120    return rowLayout;
121  }
122
123  public void setRowLayout(Layout rowLayout) {
124    if (rowLayout == null) {
125      throw new NullPointerException("Row layout cannot be null");
126    }
127    this.rowLayout = rowLayout;
128  }
129
130  public LineRenderer renderer() {
131    return new TableLineRenderer(this);
132  }
133
134  public TableElement withColumnLayout(Layout columnLayout) {
135    setColumnLayout(columnLayout);
136    return this;
137  }
138
139  public TableElement withRowLayout(Layout rowLayout) {
140    setRowLayout(rowLayout);
141    return this;
142  }
143
144  public List<RowElement> getRows() {
145    return rows;
146  }
147
148  public BorderStyle getBorder() {
149    return border;
150  }
151
152  public void setBorder(BorderStyle border) {
153    this.border = border;
154  }
155
156  public TableElement border(BorderStyle border) {
157    setBorder(border);
158    return this;
159  }
160
161  public BorderStyle getSeparator() {
162    return separator;
163  }
164
165  public void setSeparator(BorderStyle separator) {
166    this.separator = separator;
167  }
168
169  public TableElement collapse() {
170    setSeparator(null);
171    return this;
172  }
173
174  public TableElement separator(BorderStyle separator) {
175    setSeparator(separator);
176    return this;
177  }
178
179  public void setOverflow(Overflow overflow) {
180    this.overflow = overflow;
181  }
182
183  public final Overflow getOverflow() {
184    return overflow;
185  }
186
187  public TableElement overflow(Overflow overflow) {
188    setOverflow(overflow);
189    return this;
190  }
191
192  public int getLeftCellPadding() {
193    return leftCellPadding;
194  }
195
196  public void setLeftCellPadding(int leftCellPadding) {
197    if (leftCellPadding < 0) {
198      throw new IllegalArgumentException("No negative cell padding left accepted");
199    }
200    this.leftCellPadding = leftCellPadding;
201  }
202
203  public TableElement leftCellPadding(int leftCellPadding) {
204    setLeftCellPadding(leftCellPadding);
205    return this;
206  }
207
208  public int getRightCellPadding() {
209    return rightCellPadding;
210  }
211
212  public void setRightCellPadding(int rightCellPadding) {
213    if (rightCellPadding < 0) {
214      throw new IllegalArgumentException("No negative cell padding right accepted");
215    }
216    this.rightCellPadding = rightCellPadding;
217  }
218
219  public TableElement rightCellPadding(int rightCellPadding) {
220    setRightCellPadding(rightCellPadding);
221    return this;
222  }
223
224  @Override
225  public TableElement style(Style.Composite style) {
226    return (TableElement)super.style(style);
227  }
228}