1 package org.joshy.html; 2 3 import org.joshy.html.box.*; 4 import java.awt.Point ; 5 import java.awt.Rectangle ; 6 import org.w3c.dom.Element ; 7 import org.w3c.dom.Node ; 8 import org.w3c.dom.NodeList ; 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 17 18 public Box layout(Context c, Element elem) { 19 Box box = createBox(c,elem); 21 return layoutChildren(c, box); 22 } 23 24 public Box createBox(Context c, Node node) { 25 Box box = new Box(); 26 box.node = node; 27 return box; 28 } 29 30 36 37 public Box layoutChildren(Context c, Box box) { 38 40 Element elem = (Element )box.node; 41 42 NodeList nl = elem.getChildNodes(); 44 for(int i=0; i<nl.getLength(); i++) { 45 Node child = nl.item(i); 46 47 48 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 child_elem = (Element )child; 58 c.parent_box = box; 61 c.placement_point = new Point (0,box.height); 62 child_box = layout.layout(c,child_elem); 63 } else { 65 child_box = ((AnonymousBoxLayout)layout).layout(c,elem,child); 73 74 Node last_node = ((AnonymousBlockBox)child_box).last_node; 77 if(child != last_node) { 80 while(true) { 81 i++; 82 Node ch = nl.item(i); 83 if(ch == last_node) { 85 break; 86 } 87 } 88 } 89 90 91 92 } 93 box.addChild(child_box); 94 child_box.x = 0; 96 child_box.y = box.height; 97 98 if(child_box.fixed) { 102 continue; 103 } 104 105 106 if(child_box.width > box.width) { 108 box.width = child_box.width; 109 } 110 111 box.height += child_box.height; 113 } 116 c.addMaxWidth(box.width); 117 return box; 118 } 119 120 121 122 123 public void paint(Context c, Box box) { 126 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 for(int i=0; i<box.getChildCount(); i++) { 145 Box child = (Box)box.getChild(i); 146 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 163 public Layout getLayout(Node node) { 164 return LayoutFactory.getLayout(node); 165 } 166 167 public static String getPosition(Context c, Box box) { 168 String 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 node, Context c) { 187 String 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 elem, Context c) { 195 NodeList children = elem.getChildNodes(); 196 for(int i=0; i<children.getLength(); i++) { 197 Node child = children.item(i); 198 if(isBlockNode(child,c)) { 199 return true; 201 } 202 } 203 return false; 204 } 205 206 public static boolean isBlockNode(Node child, Context c) { 207 if(child instanceof Element ) { 208 Element el = (Element )child; 209 String display = c.css.getStringProperty(el,"display",false); 210 if(display != null && display.equals("block")) { 212 if(!isFloated(el,c)) { 213 return true; 215 } else { 216 } 218 } 219 } 220 return false; 221 } 222 223 public static boolean isReplaced(Node 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 node, Context c) { 235 if(node.getNodeType() != node.ELEMENT_NODE) { 236 return false; 237 } 238 239 Element el = (Element )node; 240 String display = c.css.getStringProperty(el,"display",false); 241 if(display != null && display.equals("block")) { 243 if(isFloated(node,c)) { 244 return true; 246 } 247 } 248 return false; 249 } 250 251 } 252 253 | Popular Tags |