KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > style > TableCellRenderState


1 package org.lobobrowser.html.style;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Insets JavaDoc;
5
6 import org.lobobrowser.html.domimpl.*;
7 import org.lobobrowser.util.gui.ColorFactory;
8 import org.w3c.dom.css.CSS2Properties;
9 import org.w3c.dom.html2.*;
10
11 public class TableCellRenderState extends DisplayRenderState {
12     public TableCellRenderState(RenderState prevRenderState, HTMLElementImpl element) {
13         super(prevRenderState, element, RenderState.DISPLAY_TABLE_CELL);
14     }
15
16     private int alignXPercent = -1;
17     private int alignYPercent = -1;
18     private BackgroundInfo backgroundInfo = INVALID_BACKGROUND_INFO;
19     
20     public void invalidate() {
21         super.invalidate();
22         this.alignXPercent = -1;
23         this.alignYPercent = -1;
24         this.backgroundInfo = INVALID_BACKGROUND_INFO;
25         this.paddingInsets = INVALID_INSETS;
26     }
27     
28     public int getAlignXPercent() {
29         int axp = this.alignXPercent;
30         if(axp != -1) {
31             return axp;
32         }
33         CSS2Properties props = this.getCssProperties();
34         if(props != null) {
35             String JavaDoc textAlign = props.getTextAlign();
36             if(textAlign != null && textAlign.length() != 0) {
37                 return super.getAlignXPercent();
38             }
39         }
40         // Parent already knows about "align" attribute, but override because of TH.
41
String JavaDoc align = this.element.getAttribute("align");
42         HTMLElement element = this.element;
43         HTMLElement rowElement = null;
44         Object JavaDoc parent = element.getParentNode();
45         if(parent instanceof HTMLElement) {
46             rowElement = (HTMLElement) parent;
47         }
48         if(align == null || align.length() == 0) {
49             if(rowElement != null) {
50                 align = rowElement.getAttribute("align");
51                 if(align != null && align.length() == 0) {
52                     align = null;
53                 }
54             }
55             else {
56                 align = null;
57             }
58         }
59         if(align == null) {
60             if("TH".equalsIgnoreCase(element.getNodeName())) {
61                 axp = 50;
62             }
63             else {
64                 axp = 0;
65             }
66         }
67         else if("center".equalsIgnoreCase(align) || "middle".equalsIgnoreCase(align)) {
68             axp = 50;
69         }
70         else if("left".equalsIgnoreCase(align)) {
71             axp = 0;
72         }
73         else if("right".equalsIgnoreCase(align)) {
74             axp = 100;
75         }
76         else {
77             // TODO: justify, etc.
78
axp = 0;
79         }
80         this.alignXPercent = axp;
81         return axp;
82     }
83
84     public int getAlignYPercent() {
85         int ayp = this.alignYPercent;
86         if(ayp != -1) {
87             return ayp;
88         }
89         CSS2Properties props = this.getCssProperties();
90         if(props != null) {
91             String JavaDoc textAlign = props.getVerticalAlign();
92             if(textAlign != null && textAlign.length() != 0) {
93                 return super.getAlignYPercent();
94             }
95         }
96         String JavaDoc valign = this.element.getAttribute("valign");
97         HTMLElement element = this.element;
98         HTMLElement rowElement = null;
99         Object JavaDoc parent = element.getParentNode();
100         if(parent instanceof HTMLElement) {
101             rowElement = (HTMLElement) parent;
102         }
103         if(valign == null || valign.length() == 0) {
104             if(rowElement != null) {
105                 valign = rowElement.getAttribute("valign");
106                 if(valign != null && valign.length() == 0) {
107                     valign = null;
108                 }
109             }
110             else {
111                 valign = null;
112             }
113         }
114         if(valign == null) {
115             ayp = 50;
116         }
117         else if("top".equalsIgnoreCase(valign)) {
118             ayp = 0;
119         }
120         else if("middle".equalsIgnoreCase(valign) || "center".equalsIgnoreCase(valign)) {
121             ayp = 50;
122         }
123         else if("bottom".equalsIgnoreCase(valign)) {
124             ayp = 100;
125         }
126         else {
127             //TODO: baseline, etc.
128
ayp = 50;
129         }
130         this.alignYPercent = ayp;
131         return ayp;
132     }
133     
134     public BackgroundInfo getBackgroundInfo() {
135         BackgroundInfo binfo = this.backgroundInfo;
136         if(binfo != INVALID_BACKGROUND_INFO) {
137             return binfo;
138         }
139         // Apply style based on deprecated attributes.
140
binfo = super.getBackgroundInfo();
141         HTMLTableCellElementImpl element = (HTMLTableCellElementImpl) this.element;
142         HTMLTableRowElementImpl rowElement = null;
143         Object JavaDoc parentNode = element.getParentNode();
144         if(parentNode instanceof HTMLTableRowElementImpl) {
145              rowElement = (HTMLTableRowElementImpl) parentNode;
146         }
147         if(binfo == null || binfo.backgroundColor == null) {
148             String JavaDoc bgColor = element.getBgColor();
149             if(bgColor == null || "".equals(bgColor)) {
150                 if(rowElement != null) {
151                     bgColor = rowElement.getBgColor();
152                 }
153             }
154             if(bgColor != null && !"".equals(bgColor)) {
155                 Color JavaDoc bgc = ColorFactory.getInstance().getColor(bgColor);
156                 if(binfo == null) {
157                     binfo = new BackgroundInfo();
158                 }
159                 binfo.backgroundColor = bgc;
160             }
161         }
162         if(binfo == null || binfo.backgroundImage == null) {
163             String JavaDoc background = element.getAttribute("background");
164             if(background != null && !"".equals(background)) {
165                 if(binfo == null) {
166                     binfo = new BackgroundInfo();
167                 }
168                 binfo.backgroundImage = background;
169             }
170         }
171         this.backgroundInfo = binfo;
172         return binfo;
173     }
174
175     private HTMLTableElement getTableElement() {
176         org.w3c.dom.Node JavaDoc ancestor = this.element.getParentNode();
177         while(ancestor != null && !(ancestor instanceof HTMLTableElement)) {
178             ancestor = ancestor.getParentNode();
179         }
180         return (HTMLTableElement) ancestor;
181     }
182     
183     private Insets JavaDoc paddingInsets = INVALID_INSETS;
184     
185     public Insets JavaDoc getPaddingInsets() {
186         Insets JavaDoc insets = this.paddingInsets;
187         if(insets != INVALID_INSETS) {
188             return insets;
189         }
190         insets = super.getPaddingInsets();
191         if(insets == null) {
192             HTMLTableElement tableElement = this.getTableElement();
193             if(tableElement == null) {
194                 // Return without caching
195
return null;
196             }
197             String JavaDoc cellPaddingText = tableElement.getAttribute("cellpadding");
198             if(cellPaddingText != null && cellPaddingText.length() != 0) {
199                 //TODO: Cellpadding as percent not implemented properly.
200
int cellPadding = HtmlValues.getOldSyntaxPixelSize(cellPaddingText, 600, 0);
201                 insets = new Insets JavaDoc(cellPadding, cellPadding, cellPadding, cellPadding);
202             }
203         }
204         this.paddingInsets = insets;
205         return insets;
206     }
207     
208 }
209
Popular Tags