001package com.hfg.html; 002 003import java.awt.Color; 004import java.util.List; 005 006import com.hfg.html.attribute.HTMLColor; 007import com.hfg.graphics.ColorUtil; 008import com.hfg.util.Recursion; 009import com.hfg.util.collection.CollectionUtil; 010import com.hfg.xml.XMLNode; 011 012//------------------------------------------------------------------------------ 013/** 014 * Represents a table (<table>) tag. 015 * 016 * @author J. Alex Taylor, hairyfatguy.com 017 */ 018//------------------------------------------------------------------------------ 019// com.hfg XML/HTML Coding Library 020// 021// This library is free software; you can redistribute it and/or 022// modify it under the terms of the GNU Lesser General Public 023// License as published by the Free Software Foundation; either 024// version 2.1 of the License, or (at your option) any later version. 025// 026// This library is distributed in the hope that it will be useful, 027// but WITHOUT ANY WARRANTY; without even the implied warranty of 028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 029// Lesser General Public License for more details. 030// 031// You should have received a copy of the GNU Lesser General Public 032// License along with this library; if not, write to the Free Software 033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 034// 035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 036// jataylor@hairyfatguy.com 037//------------------------------------------------------------------------------ 038 039public class Table extends HTMLTagWithCoreEvents 040{ 041 042 //########################################################################## 043 // PRIVATE FIELDS 044 //########################################################################## 045 046 047 //########################################################################## 048 // CONSTRUCTORS 049 //########################################################################## 050 051 //-------------------------------------------------------------------------- 052 public Table() 053 { 054 super(HTML.TABLE); 055 } 056 057 //-------------------------------------------------------------------------- 058 public Table(XMLNode inXMLNode) 059 { 060 this(); 061 initFromXMLNode(inXMLNode); 062 } 063 064 //########################################################################## 065 // PUBLIC METHODS 066 //########################################################################## 067 068 069 //-------------------------------------------------------------------------- 070 public THead addTHead() 071 { 072 THead thead = new THead(); 073 addSubtag(thead); 074 return thead; 075 } 076 077 //-------------------------------------------------------------------------- 078 public TBody addTBody() 079 { 080 TBody tbody = new TBody(); 081 addSubtag(tbody); 082 return tbody; 083 } 084 085 //-------------------------------------------------------------------------- 086 public TFoot addTFoot() 087 { 088 TFoot tfoot = new TFoot(); 089 addSubtag(tfoot); 090 return tfoot; 091 } 092 093 094 //-------------------------------------------------------------------------- 095 public Tr addRow() 096 { 097 Tr tr = new Tr(); 098 addSubtag(tr); 099 return tr; 100 } 101 102 //-------------------------------------------------------------------------- 103 public void addRow(Tr inRow) 104 { 105 addSubtag(inRow); 106 } 107 108 //-------------------------------------------------------------------------- 109 public List<Tr> getRows() 110 { 111 List<Tr> rows = getSubtagsByClass(Tr.class, Recursion.OFF); 112 if (!CollectionUtil.hasValues(rows)) 113 { 114 // The rows might be within a TBody 115 HTMLTag tbody = getOptionalSubtagByName(HTML.TBODY); 116 if (tbody != null) 117 { 118 rows = tbody.getSubtagsByClass(Tr.class, Recursion.OFF); 119 } 120 } 121 122 return rows; 123 } 124 125 126 //-------------------------------------------------------------------------- 127 public Colgroup addColgroup() 128 { 129 Colgroup colgroup = new Colgroup(); 130 addSubtag(colgroup); 131 132 return colgroup; 133 } 134 135 136 //-------------------------------------------------------------------------- 137 public Col addCol() 138 { 139 Col col = new Col(); 140 addSubtag(col); 141 142 return col; 143 } 144 145 //-------------------------------------------------------------------------- 146 public List<Col> getCols() 147 { 148 return getSubtagsByClass(Col.class); 149 } 150 151 //-------------------------------------------------------------------------- 152 public Table setCellPadding(int inValue) 153 { 154 setAttribute(HTML.CELLPADDING, inValue); 155 156 return this; 157 } 158 159 //-------------------------------------------------------------------------- 160 public Table setCellSpacing(int inValue) 161 { 162 setAttribute(HTML.CELLSPACING, inValue); 163 164 return this; 165 } 166 167 //-------------------------------------------------------------------------- 168 public Table setBorder(int inValue) 169 { 170 setAttribute(HTML.BORDER, inValue); 171 172 return this; 173 } 174 175 //--------------------------------------------------------------------------- 176 /** 177 Sets the background color for the table. 178 */ 179 public Table setBackgroundColor(HTMLColor inValue) 180 { 181 setAttribute(HTML.BGCOLOR, inValue); 182 return this; 183 } 184 185 //-------------------------------------------------------------------------- 186 /** 187 Sets the background color for the table. 188 */ 189 public Table setBackgroundColor(Color inValue) 190 { 191 setAttribute(HTML.BGCOLOR, "#" + ColorUtil.colorToHex(inValue)); 192 return this; 193 } 194 195 //-------------------------------------------------------------------------- 196 /** 197 Sets the background color for the table. 198 */ 199 public Table setBackgroundColor(String inValue) 200 { 201 setAttribute(HTML.BGCOLOR, inValue); 202 return this; 203 } 204 205 //-------------------------------------------------------------------------- 206 public Table setBackground(String inValue) 207 { 208 setAttribute(HTML.BACKGROUND, inValue); 209 return this; 210 } 211 212 //-------------------------------------------------------------------------- 213 public Table setWidth(String inValue) 214 { 215 setAttribute(HTML.WIDTH, inValue); 216 return this; 217 } 218 219 //-------------------------------------------------------------------------- 220 public Table setWidth(int inValue) 221 { 222 setAttribute(HTML.WIDTH, inValue); 223 return this; 224 } 225 226 227 // Overrides for HTMLTag setters to allow method chaining. 228 229 //-------------------------------------------------------------------------- 230 @Override 231 public Table addClass(String inValue) 232 { 233 return (Table) super.addClass(inValue); 234 } 235 236 //-------------------------------------------------------------------------- 237 @Override 238 public Table setClass(String inValue) 239 { 240 return (Table) super.setClass(inValue); 241 } 242 243 //-------------------------------------------------------------------------- 244 @Override 245 public Table setId(String inValue) 246 { 247 return (Table) super.setId(inValue); 248 } 249 250 //-------------------------------------------------------------------------- 251 @Override 252 public Table setStyle(CharSequence inValue) 253 { 254 return (Table) super.setStyle(inValue); 255 } 256 257 //-------------------------------------------------------------------------- 258 @Override 259 public Table addStyle(String inValue) 260 { 261 return (Table) super.addStyle(inValue); 262 } 263 264 // Overrides for HTMLTagWithCoreEvents setters to allow method chaining. 265 266 //-------------------------------------------------------------------------- 267 @Override 268 public Table setOnClick(String inValue) 269 { 270 return (Table) super.setOnClick(inValue); 271 } 272 273 //-------------------------------------------------------------------------- 274 @Override 275 public Table setOnDblClick(String inValue) 276 { 277 return (Table) super.setOnDblClick(inValue); 278 } 279 280 //-------------------------------------------------------------------------- 281 @Override 282 public Table setOnMouseDown(String inValue) 283 { 284 return (Table) super.setOnMouseDown(inValue); 285 } 286 287 //-------------------------------------------------------------------------- 288 @Override 289 public Table setOnMouseMove(String inValue) 290 { 291 return (Table) super.setOnMouseMove(inValue); 292 } 293 294 //-------------------------------------------------------------------------- 295 @Override 296 public Table appendToOnMouseOut(String inValue) 297 { 298 return (Table) super.appendToOnMouseOut(inValue); 299 } 300 301 //-------------------------------------------------------------------------- 302 @Override 303 public Table setOnMouseOut(String inValue) 304 { 305 return (Table) super.setOnMouseOut(inValue); 306 } 307 308 //-------------------------------------------------------------------------- 309 @Override 310 public Table appendToOnMouseOver(String inValue) 311 { 312 return (Table) super.appendToOnMouseOver(inValue); 313 } 314 315 //-------------------------------------------------------------------------- 316 @Override 317 public Table setOnMouseOver(String inValue) 318 { 319 return (Table) super.setOnMouseOver(inValue); 320 } 321 322 //-------------------------------------------------------------------------- 323 @Override 324 public Table setOnMouseUp(String inValue) 325 { 326 return (Table) super.setOnMouseUp(inValue); 327 } 328 329 //-------------------------------------------------------------------------- 330 @Override 331 public Table setOnKeyDown(String inValue) 332 { 333 return (Table) super.setOnKeyDown(inValue); 334 } 335 336 //-------------------------------------------------------------------------- 337 @Override 338 public Table setOnKeyPress(String inValue) 339 { 340 return (Table) super.setOnKeyPress(inValue); 341 } 342 343 //-------------------------------------------------------------------------- 344 @Override 345 public Table setOnKeyUp(String inValue) 346 { 347 return (Table) super.setOnKeyUp(inValue); 348 } 349}