1 23 package com.sun.enterprise.diagnostics.report.html; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.Iterator ; 28 29 33 public class Table extends HTMLElement { 34 35 private int border; 36 private int cellSpacing; 37 private String alignment; 38 private float width; 39 private boolean absoluteWidth; 40 41 public static final String ALIGN_LEFT="LEFT"; 42 public static final String ALIGN_CENTER="CENTER"; 43 public static final String ALIGN_RIGHT="RIGHT"; 44 45 46 public Table() { 47 super("table"); 48 } 49 50 51 public Table( int border, int cellSpacing){ 52 this(); 53 addAttribute("border",String.valueOf(border)); 54 addAttribute("cellspacing",String.valueOf(cellSpacing)); 55 56 } 57 58 62 public void setBorder(int border){ 63 this.border = border; 64 setAttribute("border",String.valueOf(border)); 65 } 66 67 public void setCellSpacing(int cellSpacing){ 68 this.cellSpacing = cellSpacing; 69 setAttribute("cellspacing", String.valueOf(cellSpacing)); 70 } 71 72 public void setAlignment(String alignment){ 73 this.alignment = alignment; 74 setAttribute("alignment",alignment ); 75 } 76 77 public void setWidth(float width, boolean absolute){ 78 this.width = width; 79 this.absoluteWidth = absolute; 80 if(absolute){ 81 setAttribute("width",String.valueOf(width)); 82 } 83 else{ 84 setAttribute("width",String.valueOf(width)+"%" ); 85 } 86 } 87 88 private void setAttribute(String property, String value){ 89 if(!(property==null || value==null)){ 90 List <Attribute> attributes= getAttributes(property); 91 if(attributes.size()==0){ 92 addAttribute(property,value); 93 } 94 else{ 95 Attribute attribute = attributes.get(0); 96 attribute.setValue(value); 97 } 98 } 99 } 100 101 102 public int getBorder(){ 103 return border; 104 } 105 106 public int getCellSpacing(){ 107 return cellSpacing; 108 } 109 110 public String getAlignment(){ 111 return alignment; 112 } 113 114 public float getWidth(){ 115 return width; 116 } 117 118 public boolean isAbsoluteWidth(){ 119 return absoluteWidth; 120 } 121 122 public TR addRow(Iterator <String > values, boolean header, String cssClass) { 123 TR row = new TR(); 124 while(values.hasNext()) { 125 CSSElement tableData = null; 126 if(header) 127 tableData = new TH(); 128 else 129 tableData = new TD(); 130 131 tableData.addText(values.next()); 132 setCSSClass(tableData, cssClass); 133 row.add(tableData); 134 } 135 add(row); 136 return row; 137 } 138 139 public TR addRow(ArrayList elements, String cssClass) { 140 TR row = new TR(); 141 for(int i=0; i<elements.size();i++){ 142 TD tableData = new TD(); 143 tableData.add((Element)elements.get(i)); 144 setCSSClass(tableData, cssClass); 145 row.add(tableData); 146 } 147 add(row); 148 return row; 149 } 150 151 private void setCSSClass(CSSElement element, String cssClass) { 152 if(cssClass != null){ 153 element.setCSSClass(cssClass); 154 } 155 } 156 } 157 | Popular Tags |