1 package org.joshy.html; 2 3 import java.awt.MediaTracker ; 4 import java.util.ArrayList ; 5 import java.awt.Color ; 6 import java.awt.Point ; 7 import java.awt.Rectangle ; 8 import javax.swing.ImageIcon ; 9 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 13 import org.joshy.u; 14 import org.joshy.x; 15 import org.joshy.html.box.BlockBox; 16 import org.joshy.html.box.Box; 17 import org.joshy.html.painter.BackgroundPainter; 18 import org.joshy.html.painter.BorderPainter; 19 import org.joshy.html.util.GraphicsUtil; 20 21 public class BoxLayout extends Layout { 22 23 public BoxLayout() { 24 } 25 26 public Box createBox(Context c, Node node) { 27 BlockBox block = new BlockBox(); 28 block.node = node; 29 return block; 30 } 31 32 public void prepareBox(Box box, Context c) { 33 Border border = getBorder(c,box); 34 Border padding = getPadding(c,box); 35 Border margin = getMargin(c,box); 36 } 37 38 public Box layout(Context c, Element elem) { 39 BlockBox block = (BlockBox)createBox(c,elem); 42 43 Rectangle oe = c.getExtents(); 44 c.setExtents(new Rectangle (oe)); 46 47 adjustWidth(c, block); 48 adjustHeight(c, block); 49 block.x = c.getExtents().x; 50 block.y = c.getExtents().y; 51 52 prepareBox(block,c); 53 Border border = getBorder(c, block); 54 Border padding = getPadding(c, block); 55 Border margin = getMargin(c, block); 56 getBackgroundColor(c,block); 57 61 62 layoutChildren(c, block); 65 66 block.width = margin.left + border.left + padding.left + block.width + 68 padding.right + border.right + margin.right; 69 block.height = margin.top + border.top + padding.top + block.height + 70 padding.bottom + border.bottom + margin.bottom; 71 73 78 79 c.setExtents(oe); 82 83 setupRelative(c, block); 85 setupFixed(c, block); 86 87 this.contents_height = block.height; 88 return block; 89 } 90 91 92 public void adjustWidth(Context c, BlockBox block) { 94 if(!block.isElement()) { return; } 97 Element elem = block.getElement(); 98 if (c.css.hasProperty(elem, "width", false)) { 99 float new_width = c.css.getFloatProperty(elem, "width", c.getExtents().width, false); 100 c.getExtents().width = (int) new_width; 101 block.width = (int) new_width; 102 block.auto_width = false; 103 } 105 } 106 107 public void adjustHeight(Context c, BlockBox block) { 109 if(!block.isElement()) { return; } 110 Element elem = block.getElement(); 111 if (c.css.hasProperty(elem, "height")) { 112 float new_height = c.css.getFloatProperty(elem, "height", c.getExtents().height); 113 c.getExtents().height = (int) new_height; 114 block.height = (int) new_height; 115 block.auto_height = false; 116 } 117 } 118 119 120 public void setupFixed(Context c, Box box) { 121 if (isFixed(c, box)) { 122 box.fixed = true; 123 if (c.css.hasProperty(box.node, "right", false)) { 124 box.right = (int) c.css.getFloatProperty(box.node, "right", 0, false); 125 box.right_set = true; 126 } 127 if (c.css.hasProperty(box.node, "bottom", false)) { 128 box.bottom = (int) c.css.getFloatProperty(box.node, "bottom", 0, false); 129 box.bottom_set = true; 130 } 131 } 132 } 133 134 135 136 public static void setupRelative(Context c, Box box) { 137 String position = getPosition(c, box); 138 if (position.equals("relative")) { 139 if (c.css.hasProperty(box.node, "right", false)) { 140 box.left = -(int) c.css.getFloatProperty(box.node, "right", 0, false); 141 } 142 if (c.css.hasProperty(box.node, "bottom", false)) { 143 box.top = -(int) c.css.getFloatProperty(box.node, "bottom", 0, false); 144 } 145 if (c.css.hasProperty(box.node, "top", false)) { 146 box.top = (int) c.css.getFloatProperty(box.node, "top", 0, false); 147 } 148 if (c.css.hasProperty(box.node, "left", false)) { 149 box.left = (int) c.css.getFloatProperty(box.node, "left", 0, false); 150 } 151 box.relative = true; 152 } 153 } 154 155 156 public Box layoutChildren(Context c, Box box) { 157 BlockBox block = (BlockBox)box; 159 c.shrinkExtents(block); 160 super.layoutChildren(c, block); 161 c.unshrinkExtents(block); 162 return block; 164 } 165 166 167 public void paint(Context c, Box box) { 168 BlockBox block = (BlockBox)box; 170 171 Rectangle oldBounds = new Rectangle (c.getExtents()); 173 174 175 176 if (block.relative) { 177 paintRelative(c,block); 178 } else if (block.fixed) { 179 paintFixed(c,block); 180 } else { 181 paintNormal(c,block); 182 } 183 184 185 oldBounds.y = oldBounds.y + block.height; 187 c.setExtents(oldBounds); 188 189 if(c.debugDrawBoxes()) { 190 GraphicsUtil.drawBox(c.getGraphics(),block,Color.red); 191 } 192 } 193 194 195 public void paintNormal(Context c, BlockBox block) { 196 paintBackground(c, block); 197 198 c.translateInsets(block); 199 paintComponent(c, block); 200 paintChildren(c, block); 201 c.untranslateInsets(block); 202 203 paintBorder(c, block); 204 } 205 206 public void paintRelative(Context ctx, BlockBox block) { 208 ctx.getGraphics().translate(block.left, block.top); 209 paintNormal(ctx,block); 210 ctx.getGraphics().translate(-block.left, -block.top); 211 } 212 213 public void paintFixed(Context c, BlockBox block) { 215 int xoff = 0; 216 int yoff = 0; 217 218 xoff = c.canvas.getWidth(); 219 yoff = c.canvas.getHeight(); 220 if (block.right_set) { 221 xoff = xoff - block.width; 222 } 223 224 if (block.bottom_set) { 225 230 yoff = c.viewport.getHeight(); 232 233 yoff = yoff - block.height; 235 237 yoff = yoff - c.getExtents().y; 239 241 yoff = yoff - c.canvas.getLocation().y; 243 } 244 245 c.translate(xoff, yoff); 246 247 paintNormal(c,block); 248 249 c.translate(-xoff, -yoff); 250 } 251 252 253 public void paintBackground(Context c, Box box) { 254 Box block = box; 255 getBackgroundColor(c, block); 257 258 String back_image = c.css.getStringProperty(block.getElement(), "background-image", false); 260 block.repeat = c.css.getStringProperty(block.getElement(), "background-repeat"); 261 block.attachment = c.css.getStringProperty(block.getElement(), "background-attachment"); 262 263 if(c.css.hasProperty(block.getElement(),"background-position",false)) { 266 Point pt = c.css.getFloatPairProperty(block.getElement(),"background-position",false); 267 block.background_position_horizontal = (int)pt.getX(); 268 block.background_position_vertical = (int)pt.getY(); 269 } 270 271 block.background_image = null; 273 if (back_image != null) { 274 try { 275 block.background_image = ImageUtil.loadImage(c,back_image); 276 } catch (Exception ex) { 277 u.p(ex); 278 } 279 285 } 286 287 BackgroundPainter.paint(c, block); 289 } 290 291 public void paintChildren(Context c, Box box) { 292 BlockBox block = (BlockBox)box; 293 c.getGraphics().translate(block.x,block.y); 294 super.paintChildren(c, block); 295 c.getGraphics().translate(-block.x,-block.y); 296 } 297 298 299 public void paintBorder(Context c, Box box) { 300 Box block = box; 301 303 BorderPainter bp = new BorderPainter(); 305 306 311 bp.paint(c, block); 312 } 313 314 315 317 public Border getBorder(Context c, Box box) { 318 if(box.isElement()) { 319 if(box.border == null) { 320 box.border = c.css.getBorderWidth(box.getElement()); 321 } 322 } 323 return box.border; 324 } 325 326 327 public Border getPadding(Context c, Box box) { 328 if(box.isElement()) { 329 if(box.padding == null) { 330 box.padding = c.css.getPaddingWidth(box.getElement()); 331 } 332 } 333 return box.padding; 334 } 335 336 337 public Border getMargin(Context c, Box box) { 338 if(box.isElement()) { 339 if(box.margin == null) { 340 box.margin = c.css.getMarginWidth(box.getElement()); 341 } 342 } 343 return box.margin; 344 } 345 346 private Color getBackgroundColor(Context c, Box box) { 347 if(box.background_color == null) { 348 box.background_color = c.css.getBackgroundColor(box.getElement()); 349 } 350 return box.background_color; 351 } 352 353 } 354 355 356 | Popular Tags |