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 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 Layout getColumnLayout() { 082 return columnLayout; 083 } 084 085 public void setColumnLayout(Layout columnLayout) { 086 if (columnLayout == null) { 087 throw new NullPointerException("Column layout cannot be null"); 088 } 089 this.columnLayout = columnLayout; 090 } 091 092 public Layout getRowLayout() { 093 return rowLayout; 094 } 095 096 public void setRowLayout(Layout rowLayout) { 097 if (rowLayout == null) { 098 throw new NullPointerException("Row layout cannot be null"); 099 } 100 this.rowLayout = rowLayout; 101 } 102 103 public Renderer renderer() { 104 return new TableRenderer(this); 105 } 106 107 public TableElement withColumnLayout(Layout columnLayout) { 108 setColumnLayout(columnLayout); 109 return this; 110 } 111 112 public TableElement withRowLayout(Layout rowLayout) { 113 setRowLayout(rowLayout); 114 return this; 115 } 116 117 public List<RowElement> getRows() { 118 return rows; 119 } 120 121 public BorderStyle getBorder() { 122 return border; 123 } 124 125 public void setBorder(BorderStyle border) { 126 this.border = border; 127 } 128 129 public TableElement border(BorderStyle border) { 130 setBorder(border); 131 return this; 132 } 133 134 public BorderStyle getSeparator() { 135 return separator; 136 } 137 138 public void setSeparator(BorderStyle separator) { 139 this.separator = separator; 140 } 141 142 public TableElement collapse() { 143 setSeparator(null); 144 return this; 145 } 146 147 public TableElement separator(BorderStyle separator) { 148 setSeparator(separator); 149 return this; 150 } 151 152 public void setOverflow(Overflow overflow) { 153 this.overflow = overflow; 154 } 155 156 public final Overflow getOverflow() { 157 return overflow; 158 } 159 160 public TableElement overflow(Overflow overflow) { 161 setOverflow(overflow); 162 return this; 163 } 164 165 public int getLeftCellPadding() { 166 return leftCellPadding; 167 } 168 169 public void setLeftCellPadding(int leftCellPadding) { 170 if (leftCellPadding < 0) { 171 throw new IllegalArgumentException("No negative cell padding left accepted"); 172 } 173 this.leftCellPadding = leftCellPadding; 174 } 175 176 public TableElement leftCellPadding(int leftCellPadding) { 177 setLeftCellPadding(leftCellPadding); 178 return this; 179 } 180 181 public int getRightCellPadding() { 182 return rightCellPadding; 183 } 184 185 public void setRightCellPadding(int rightCellPadding) { 186 if (rightCellPadding < 0) { 187 throw new IllegalArgumentException("No negative cell padding right accepted"); 188 } 189 this.rightCellPadding = rightCellPadding; 190 } 191 192 public TableElement rightCellPadding(int rightCellPadding) { 193 setRightCellPadding(rightCellPadding); 194 return this; 195 } 196 197 @Override 198 public TableElement style(Style.Composite style) { 199 return (TableElement)super.style(style); 200 } 201 }