KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.joshy.html;
2
3 import org.joshy.html.box.BlockBox;
4 import org.joshy.html.box.Box;
5 import org.joshy.html.box.InlineBox;
6 import javax.swing.ImageIcon JavaDoc;
7 import java.awt.Image JavaDoc;
8 import java.awt.Rectangle JavaDoc;
9 import org.w3c.dom.Element JavaDoc;
10 import org.w3c.dom.Node JavaDoc;
11 import java.net.URL JavaDoc;
12 import org.joshy.u;
13
14 public class ImageLayout extends BoxLayout {
15     //Image img;
16
public Box createBox(Context c, Node JavaDoc node) {
17         BlockBox box = new BlockBox();
18         box.node = node;
19         return box;
20     }
21     
22     private Image JavaDoc getImage(Context c, Node JavaDoc node) {
23         if(node.getNodeType() != node.ELEMENT_NODE) {
24             return null;
25         }
26         String JavaDoc src = ((Element JavaDoc)node).getAttribute("src");
27         Image JavaDoc img = null;
28         try {
29             img = ImageUtil.loadImage(c,src);
30         } catch (Exception JavaDoc ex) {
31             u.p(ex);
32         }
33         return img;
34     }
35     public Box layout(Context c, Element JavaDoc elem) {
36         BlockBox block = (BlockBox)createBox(c,elem);
37         // load the image
38

39         Border border = getBorder(c, block);
40         Border padding = getPadding(c, block);
41         Border margin = getMargin(c, block);
42
43         
44         Image JavaDoc img = getImage(c,elem);
45         // calculate new contents
46
if(img != null) {
47             block.width = img.getWidth(null);
48             block.height = img.getHeight(null);
49         } else {
50             block.width = 50;
51             block.height = 50;
52         }
53         //Rectangle contents = new Rectangle(0,0,img.getWidth(null),img.getHeight(null));
54
/*
55         block.width = this.getMargin(c,elem).left + this.getBorder(c,elem).left + this.getPadding(c,elem).left +
56             block.width +
57             this.getMargin(c,elem).right + this.getBorder(c,elem).right + this.getPadding(c,elem).right;
58         block.height = this.getMargin(c,elem).top + this.getBorder(c,elem).top + this.getPadding(c,elem).top +
59             block.height +
60             this.getMargin(c,elem).bottom + this.getBorder(c,elem).bottom + this.getPadding(c,elem).bottom;
61         */

62         // calculate the inner width
63
block.width = margin.left + border.left + padding.left + block.width +
64                 padding.right + border.right + margin.right;
65         block.height = margin.top + border.top + padding.top + block.height +
66                 padding.bottom + border.bottom + margin.bottom;
67         // create the new block box
68
//block = new BlockBox();
69
block.x = c.getExtents().x;
70         block.y = c.getExtents().y;
71         //block.width = contents.width;
72
//block.height = contents.height;
73
return block;
74     }
75     
76
77         /*
78     public void paint(Context c, InlineBox box) {
79         // save the old extents
80         Rectangle oldExtents = new Rectangle(c.getExtents());
81         
82         Element elem = (Element)box.node;
83
84         // set the contents size
85         Rectangle contents = layout(c,elem);
86         
87         // get the border and padding
88         Border border = getBorder(c,elem);
89         Border padding = getPadding(c,elem);
90         Border margin = c.css.getMarginWidth(elem);
91
92         // calculate the insets
93         int top_inset = margin.top + border.top + padding.top;
94         int left_inset = margin.left + border.left + padding.left;
95
96         // shrink the bounds to be based on the contents
97         c.getExtents().width = contents.width;
98         
99         // do all of the painting
100         // set the origin to the origin of our box
101         c.getExtents().y = box.y;
102         c.getExtents().x = box.x;
103         
104         
105         paintBackground(c,elem,contents);
106         
107         // move the contents in to account for the insets
108         c.getExtents().translate(left_inset,top_inset);
109         paintComponent(c,elem,contents);
110         c.getExtents().translate(-left_inset,-top_inset);
111         
112         paintBorder(c,elem,contents);
113         
114         // restore the old extents
115         c.setExtents(oldExtents);
116     }
117     */

118     
119     public void paint(Context c, Box box) {
120         InlineBox block = (InlineBox)box;
121         // set the contents size
122
//Rectangle contents = layout(c,elem);
123

124         // get the border and padding
125
Border border = getBorder(c,block);
126         Border padding = getPadding(c,block);
127         Border margin = getMargin(c, block);
128
129         // calculate the insets
130
int top_inset = margin.top + border.top + padding.top;
131         int left_inset = margin.left + border.left + padding.left;
132
133         // shrink the bounds to be based on the contents
134
c.getExtents().width = block.width;
135         
136         // do all of the painting
137
paintBackground(c,block);
138         //u.p("insets = " + left_inset + " " + top_inset);
139
c.getGraphics().translate(left_inset,top_inset);
140         //c.getExtents().translate(left_inset,top_inset);
141
paintComponent(c,block);
142         c.getGraphics().translate(-left_inset,-top_inset);
143         //c.getExtents().translate(-left_inset,-top_inset);
144
paintBorder(c,block);
145         
146         // move the origin down now that we are done painting (should move this later)
147
c.getExtents().y = c.getExtents().y + block.height;
148     }
149     
150     public void paintComponent(Context c, Box box) {
151         Image JavaDoc img = getImage(c,box.node);
152         if(img != null) {
153             c.getGraphics().drawImage(img,box.x,box.y,null);
154         }
155     }
156
157     /*
158     public void paintComponent(Context c, Element elem, InlineBox box) {
159         c.getGraphics().drawImage(img,box.x,box.y,null);
160     }
161     */

162
163
164 }
165
Popular Tags