KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > report > html > Table


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.report.html;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  *
31  * @author Jagadish
32  */

33 public class Table extends HTMLElement {
34     
35     private int border;
36     private int cellSpacing;
37     private String JavaDoc alignment;
38     private float width;
39     private boolean absoluteWidth;
40
41     public static final String JavaDoc ALIGN_LEFT="LEFT";
42     public static final String JavaDoc ALIGN_CENTER="CENTER";
43     public static final String JavaDoc ALIGN_RIGHT="RIGHT";
44     
45     /** Creates a new instance of Table */
46     public Table() {
47         super("table");
48     }
49     
50     /** Creates a new instance of Table */
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     /**
59      *Sets the border attribute for the table
60      *@param border in pixels
61      */

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 JavaDoc 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 JavaDoc property, String JavaDoc value){
89         if(!(property==null || value==null)){
90             List JavaDoc<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 JavaDoc 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 JavaDoc<String JavaDoc> values, boolean header, String JavaDoc 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 JavaDoc elements, String JavaDoc 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 JavaDoc cssClass) {
152         if(cssClass != null){
153             element.setCSSClass(cssClass);
154         }
155     }
156 }
157
Popular Tags