KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.joshy.html;
2
3 import java.awt.MediaTracker JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.awt.Color JavaDoc;
6 import java.awt.Point JavaDoc;
7 import java.awt.Rectangle JavaDoc;
8 import javax.swing.ImageIcon JavaDoc;
9
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.Node JavaDoc;
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 JavaDoc 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 JavaDoc elem) {
39         //u.p("BoxLayout.layout() : " + elem);
40
//u.p("box layout for: " + elem.getNodeName());
41
BlockBox block = (BlockBox)createBox(c,elem);
42
43         Rectangle JavaDoc oe = c.getExtents();
44         //u.p("old extents = " + c.getExtents());
45
c.setExtents(new Rectangle JavaDoc(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         //u.p("margin on box block = " + block.margin);
58
//u.p("border on box block = " + block.border);
59
//u.p("padding on box block = " + block.padding);
60

61
62         // do children's layout
63
//u.p("avail space = " + block.width);
64
layoutChildren(c, block);
65
66         // calculate the inner width
67
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         //u.p("final width = " + block.width);
72

73         // if this is a fixed height, then set it explicitly
74
/*if (!block.auto_height) {
75             contents.height = block.height;
76         }
77         */

78
79         //u.p("old extents = " + c.getExtents());
80
//restore the extents
81
c.setExtents(oe);
82
83         // account for special positioning
84
setupRelative(c, block);
85         setupFixed(c, block);
86
87         this.contents_height = block.height;
88         return block;
89     }
90
91     
92     // calculate the width based on css and available space
93
public void adjustWidth(Context c, BlockBox block) {
94         //c.getExtents().width = 150;
95
//u.p("current width = " + c.getExtents().width);
96
if(!block.isElement()) { return; }
97         Element JavaDoc 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             //u.p("setting width: " + block.width);
104
}
105     }
106     
107     // calculate the height based on css and available space
108
public void adjustHeight(Context c, BlockBox block) {
109         if(!block.isElement()) { return; }
110         Element JavaDoc 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 JavaDoc 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         //u.p("BoxLayout.layoutChildren("+box+")");
158
BlockBox block = (BlockBox)box;
159         c.shrinkExtents(block);
160         super.layoutChildren(c, block);
161         c.unshrinkExtents(block);
162         //u.p("BoxLayout.layoutChildren() returning children layout of: " + rt);
163
return block;
164     }
165     
166
167     public void paint(Context c, Box box) {
168         //u.p("BoxLayout.paint " + box);//+box.getElement().getNodeName()+") " + block);
169
BlockBox block = (BlockBox)box;
170
171         // copy the bounds to we don't mess it up
172
Rectangle JavaDoc oldBounds = new Rectangle JavaDoc(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         // move the origin down to account for the contents plus the margin, borders, and padding
186
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     // adjustments for relative painting
207
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     // adjustments for fixed painting
214
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             //joshy: this should really be block.height instead of bnds.y
226
// need to fix the setting of block.height
227
//joshy: need to do horizontal calcs too, inc scrolling
228
//joshy: need to make the body paint the whole canvas.
229

230             // start at the bottom of the viewport
231
yoff = c.viewport.getHeight();
232
233             // account for the width of the box
234
yoff = yoff - block.height;
235             // - bnds.y
236

237             // account for the current y offset of the box
238
yoff = yoff - c.getExtents().y;
239             //orig.y;
240

241             // account for the scrolling of the viewport
242
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         // cache the background color
256
getBackgroundColor(c, block);
257
258         // get the css properties
259
String JavaDoc 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         // handle image positioning issues
264
// need to update this to support vert and horz, not just vert
265
if(c.css.hasProperty(block.getElement(),"background-position",false)) {
266             Point JavaDoc 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         // load the background image
272
block.background_image = null;
273         if (back_image != null) {
274             try {
275                 block.background_image = ImageUtil.loadImage(c,back_image);
276             } catch (Exception JavaDoc ex) {
277                 u.p(ex);
278             }
279             /*
280             ImageIcon icon = new ImageIcon(back_image);
281             if(icon.getImageLoadStatus() == MediaTracker.COMPLETE) {
282                 block.background_image = icon.getImage();
283             }
284             */

285         }
286
287         // actually paint the background
288
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         // get the border parts
302

303         // paint the border
304
BorderPainter bp = new BorderPainter();
305
306         // adjust to a fixed height, if necessary
307
//if (!block.auto_height) {
308
//bnds.y = block.height - block.margin.top - block.margin.bottom;
309
//}
310

311         bp.paint(c, block);
312     }
313
314     
315     // === caching accessors =========
316

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 JavaDoc 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