KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > Layout


1 package org.joshy.html;
2
3 import org.joshy.html.box.*;
4 import java.awt.Point JavaDoc;
5 import java.awt.Rectangle JavaDoc;
6 import org.w3c.dom.Element JavaDoc;
7 import org.w3c.dom.Node JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9 import org.joshy.u;
10 import org.joshy.html.box.Box;
11
12 public class Layout {
13     public int contents_height;
14
15
16     /* ============= layout code =================== */
17
18     public Box layout(Context c, Element JavaDoc elem) {
19         //u.p("Layout.layout(): " + elem);
20
Box box = createBox(c,elem);
21         return layoutChildren(c, box);
22     }
23
24     public Box createBox(Context c, Node JavaDoc node) {
25         Box box = new Box();
26         box.node = node;
27         return box;
28     }
29
30     /*
31     // we need to pass the graphics incase we need to grab font info for sizing
32     public Rectangle layoutNode(Context c, Node node) {
33         return new Rectangle(0,0);
34     }
35     */

36
37     public Box layoutChildren(Context c, Box box) {
38         //u.p("Layout.layoutChildren: " + box);
39

40         Element JavaDoc elem = (Element JavaDoc)box.node;
41
42         // for each child
43
NodeList JavaDoc nl = elem.getChildNodes();
44         for(int i=0; i<nl.getLength(); i++) {
45             Node JavaDoc child = nl.item(i);
46
47
48             // get the layout for this child
49
Layout layout = getLayout(child);
50             if(layout == null) { continue; }
51             if(layout instanceof NullLayout) { continue; }
52             if(child.getNodeName().equals("br")) { continue; }
53             if(child.getNodeType() == child.COMMENT_NODE) { continue; }
54
55             Box child_box = null;
56             if(child.getNodeType() == child.ELEMENT_NODE) {
57                 Element JavaDoc child_elem = (Element JavaDoc)child;
58                 // execute the layout and get the return bounds
59
//u.p("doing element layout: " + layout);
60
c.parent_box = box;
61                 c.placement_point = new Point JavaDoc(0,box.height);
62                 child_box = layout.layout(c,child_elem);
63                 //u.p("child box = " + child_box);
64
} else {
65                 //u.p("we have to do an anonymous text block on this: " + child.getNodeValue());
66
// create anonymous block box
67
// prepare the node list of the text children
68
//child_box = new AnonymousBlockBox(child);
69
// call layout
70
//u.p("layout = " + layout);
71
//u.p("doing non element layout: " + layout);
72
child_box = ((AnonymousBoxLayout)layout).layout(c,elem,child);
73
74                 // skip text children if the prev_child == anonymous block box
75
// because that means they were sucked into this block
76
Node JavaDoc last_node = ((AnonymousBlockBox)child_box).last_node;
77                 // if anonymous box is only one node wide then skip this
78
// junk
79
if(child != last_node) {
80                     while(true) {
81                         i++;
82                         Node JavaDoc ch = nl.item(i);
83                         //u.p("trying to skip: " + ch);
84
if(ch == last_node) {
85                             break;
86                         }
87                     }
88                 }
89
90
91
92             }
93             box.addChild(child_box);
94             // set the child_box location
95
child_box.x = 0;
96             child_box.y = box.height;
97
98             //joshy fix the 'fixed' stuff later
99
// if fixed then don't modify the final layout bounds
100
// because fixed elements are removed from normal flow
101
if(child_box.fixed) {
102                 continue;
103             }
104
105
106             // increase the final layout width if the child was greater
107
if(child_box.width > box.width) {
108                 box.width = child_box.width;
109             }
110
111             // increase the final layout height by the height of the child
112
box.height += child_box.height;
113             //u.p("final extents = " + lt);
114
//u.p("final child box was: " + child_box);
115
}
116         c.addMaxWidth(box.width);
117         return box;
118     }
119
120
121
122     /* ========== painting code ============== */
123     // the core function that implements the recursive layout/paint loop
124
// perhaps we should call it something else?
125
public void paint(Context c, Box box) {
126         //u.p("Layout.paint() " + box);
127
//Point old_cursor = new Point(c.getCursor());
128
//Rectangle contents = layoutChildren(c,elem);
129
//c.cursor = old_cursor;
130
paintBackground(c,box);
131         paintComponent(c,box);
132         paintChildren(c,box);
133         paintBorder(c,box);
134         this.contents_height = box.height;
135     }
136
137     public void paintBackground(Context c, Box box) { }
138     public void paintComponent(Context c, Box box) { }
139     public void paintBorder(Context c, Box box) { }
140
141     public void paintChildren(Context c, Box box) {
142         //u.p("Layout.paintChildren(): " + box);
143
//u.p("child count = " + box.boxes.size());
144
for(int i=0; i<box.getChildCount(); i++) {
145             Box child = (Box)box.getChild(i);
146             //u.p("child = " + child);
147
Layout layout = null;
148             if(child.isAnonymous()) {
149                 layout = new InlineLayout();
150             } else {
151                 layout = getLayout(child.node);
152             }
153             paintChild(c,child,layout);
154         }
155     }
156
157     public void paintChild(Context c, Box box, Layout layout) {
158         layout.paint(c,box);
159     }
160
161
162     /* =========== utility code ============= */
163     public Layout getLayout(Node JavaDoc node) {
164         return LayoutFactory.getLayout(node);
165     }
166
167     public static String JavaDoc getPosition(Context c, Box box) {
168         String JavaDoc position = c.css.getStringProperty(box.node,"position",false);
169         if(position == null) {
170             position = "static";
171         }
172         return position;
173     }
174
175     public boolean isFixed(Context c, Box box) {
176         if(getPosition(c,box).equals("fixed")) {
177             return true;
178         }
179         return false;
180     }
181
182
183     public static boolean isFloated(Box inline, Context c) {
184         return isFloated(inline.node,c);
185     }
186     public static boolean isFloated(Node JavaDoc node, Context c) {
187         String JavaDoc float_val = c.css.getStringProperty(node,"float");
188         if(float_val == null) { return false; }
189         if(float_val.equals("left")) { return true; }
190         if(float_val.equals("right")) { return true; }
191         return false;
192     }
193
194     public boolean isBlockLayout(Element JavaDoc elem, Context c) {
195         NodeList JavaDoc children = elem.getChildNodes();
196         for(int i=0; i<children.getLength(); i++) {
197             Node JavaDoc child = children.item(i);
198             if(isBlockNode(child,c)) {
199                 //u.p("this layout is block");
200
return true;
201             }
202         }
203         return false;
204     }
205
206     public static boolean isBlockNode(Node JavaDoc child, Context c) {
207         if(child instanceof Element JavaDoc) {
208             Element JavaDoc el = (Element JavaDoc)child;
209             String JavaDoc display = c.css.getStringProperty(el,"display",false);
210             //u.p("display = " + display);
211
if(display != null && display.equals("block")) {
212                 if(!isFloated(el,c)) {
213                     //u.p(child.getNodeName() + " is a block");
214
return true;
215                 } else {
216                     //u.p("isBlockNode() found a floated block");
217
}
218             }
219         }
220         return false;
221     }
222
223     public static boolean isReplaced(Node JavaDoc node) {
224         if(node.getNodeName().equals("img")) {
225             return true;
226         }
227         if(node.getNodeName().equals("input")) {
228             return true;
229         }
230
231         return false;
232     }
233
234     public static boolean isFloatedBlock(Node JavaDoc node, Context c) {
235         if(node.getNodeType() != node.ELEMENT_NODE) {
236             return false;
237         }
238
239         Element JavaDoc el = (Element JavaDoc)node;
240         String JavaDoc display = c.css.getStringProperty(el,"display",false);
241         //u.p("display = " + display);
242
if(display != null && display.equals("block")) {
243             if(isFloated(node,c)) {
244                 //u.p("it's a floated block");
245
return true;
246             }
247         }
248         return false;
249     }
250
251 }
252
253
Popular Tags