KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > forms > FormItemLayout


1 package org.joshy.html.forms;
2
3 import java.util.*;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Point JavaDoc;
6 import javax.swing.JComponent JavaDoc;
7 import org.joshy.u;
8 import org.joshy.html.box.Box;
9 import org.joshy.html.box.*;
10 import org.joshy.html.*;
11 import org.w3c.dom.*;
12
13 public abstract class FormItemLayout extends CustomBlockLayout {
14     
15     abstract public JComponent JavaDoc createComponent(Element elem);
16     
17     private JComponent JavaDoc comp;
18     
19     public Box createBox(Context c, Node node) {
20         Element elem = (Element)node;
21         comp = createComponent(elem);
22         c.canvas.add(comp);
23         comp.setLocation(100,100);
24         //u.p("added a component to the viewport: " + comp);
25
//u.p("pref size = " + comp.getPreferredSize());
26
InputBox box = new InputBox();
27         box.node = node;
28         box.component = comp;
29         return box;
30     }
31     
32     public Dimension JavaDoc getIntrinsicDimensions(Context c, Element elem) {
33         //comp.setLocation(50,50);
34
Dimension JavaDoc dim = comp.getPreferredSize();
35         //return new Dimension(10,10);
36
//u.p("get intrinsic = " + dim);
37
return dim;
38     }
39     
40     public void doInlinePaint(Context c, InlineBox block) {
41         //u.p("FormItemLayout.doInlinePaint() : " + block);
42
//u.p("sub = " + block.sub_block);
43

44         // get the border and padding
45
Border border = getBorder(c,block);
46         Border padding = getPadding(c,block);
47         Border margin = getMargin(c, block);
48
49         // calculate the insets
50
int top_inset = margin.top + border.top + padding.top;
51         int left_inset = margin.left + border.left + padding.left;
52
53         // shrink the bounds to be based on the contents
54
c.getExtents().width = block.width;
55         
56         // do all of the painting
57
paintBackground(c,block);
58         //u.p("insets = " + left_inset + " " + top_inset);
59
c.getGraphics().translate(left_inset,top_inset);
60         paintComponent(c,block.sub_block);
61         c.getGraphics().translate(-left_inset,-top_inset);
62         paintBorder(c,block);
63         
64         // move the origin down now that we are done painting (should move this later)
65
c.getExtents().y = c.getExtents().y + block.height;
66 /* */
67         //super.paint(c,block.sub_block);
68
}
69     
70     
71     public void paint(Context c, Box box) {
72         if(box instanceof InlineBox) {
73             InlineBox block = (InlineBox)box;
74             //u.p("FormItemLayout.paint() box = " + block);
75
//u.p("FormItemLayout.paint() sub = " + block.sub_block);
76
doInlinePaint(c,block);
77         } else {
78             super.paint(c,box);
79         }
80         /*
81         
82         // set the contents size
83         //Rectangle contents = layout(c,elem);
84         
85         // get the border and padding
86         Border border = getBorder(c,block);
87         Border padding = getPadding(c,block);
88         Border margin = getMargin(c, block);
89
90         // calculate the insets
91         int top_inset = margin.top + border.top + padding.top;
92         int left_inset = margin.left + border.left + padding.left;
93
94         // shrink the bounds to be based on the contents
95         c.getExtents().width = block.width;
96         
97         // do all of the painting
98         //paintBackground(c,block);
99         //u.p("insets = " + left_inset + " " + top_inset);
100         c.getGraphics().translate(left_inset,top_inset);
101         //c.getExtents().translate(left_inset,top_inset);
102         
103         paintComponent(c,block.sub_block);
104         
105         c.getGraphics().translate(-left_inset,-top_inset);
106         //c.getExtents().translate(-left_inset,-top_inset);
107         //paintBorder(c,block);
108         
109         // move the origin down now that we are done painting (should move this later)
110         c.getExtents().y = c.getExtents().y + block.height;
111         
112         */

113     }
114
115     public void paintComponent(Context c, Box box) {
116         //u.p("FormItemLayout.paintComponent() = " + box);
117
InputBox ib = (InputBox)box;
118         //u.p("left inset = " + box.totalLeftPadding());
119
//u.p("comp dim = " + ib.component.getSize());
120
//c.getGraphics().fillRect(box.x,box.y,box.width,box.height);
121

122         //int yoff = c.canvas.getLocation().y;
123
//u.p("yoff = " + yoff);
124

125         //u.p("current x = " + box.x + " y " + box.y);
126
Point JavaDoc coords = absCoords(box);
127         
128         // joshy: i don't know why we have to add the extra +5
129
// i think it's because of the fact that this is a box
130
// nested inside of an inline. when we redo the inline-block code
131
// this should be fixed
132

133         coords.x += box.totalLeftPadding()+box.getParent().totalLeftPadding();
134         coords.y += box.totalTopPadding()+box.getParent().totalTopPadding();
135         //u.p("abs coords = " + coords);
136
//u.p("comp coords = " + ib.component.getLocation());
137

138         Point JavaDoc loc = ib.component.getLocation();
139         if(loc.y != coords.y ||
140             loc.x != coords.x) {
141             //u.p("coords = " + coords);
142
//u.p("loc = " + loc);
143
loc.y = coords.y;
144             loc.x = coords.x;
145             ib.component.setLocation(coords);
146             ib.component.invalidate();
147             //u.p("moved : " + ib.component + " to " + coords);
148
}
149         
150         //Point pt = new Point(0,0);
151
//comp.setLocation(pt);
152
//comp.setSize(50,50);
153
//comp.setLocation(50,50);
154
//u.p("painting");
155
//comp.paint(c.getGraphics());
156

157
158     }
159
160     public Point JavaDoc absCoords(Box box) {
161         //u.p("box = " + box);
162
//u.p("x = " + box.x + " y = " + box.y);
163
//u.p("Parent = " + box.getParent());
164
Point JavaDoc pt = new Point JavaDoc(0,0);
165         pt.x += box.x;
166         pt.y += box.y;
167         
168         if(box.getParent() != null) {
169             Point JavaDoc pt_parent = absCoords(box.getParent());
170             pt.x += pt_parent.x;
171             pt.y += pt_parent.y;
172             //return box.x + absX(box.getParent());
173
}
174         return pt;
175     }
176
177     
178 }
179
Popular Tags