KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > box > Box


1 package org.joshy.html.box;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Image JavaDoc;
5 import org.joshy.html.Border;
6 import org.w3c.dom.Node JavaDoc;
7 import org.w3c.dom.Element JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import org.joshy.u;
13
14 public class Box {
15     // dimensions stuff
16
public int x;
17     public int y;
18     public int width;
19     public int height;
20     
21     public int getHeight() {
22         return height;
23     }
24     
25     public int getWidth() {
26         return width;
27     }
28
29     /** Return true if the target coordinates are inside of this box. The
30     target coordinates are already translated to be relative to the origin
31     of this box. ie x=0 & y=0. Thus the point 100,100 in a box with coordinates
32     20,20 x 90x90 would have the target coordinates passed in as 80,80 and
33     the function would return true.
34     */

35      
36     public boolean contains(int x, int y) {
37         if((x >= 0) && (x<= 0 + this.width)) {
38             if((y>=0) && (y<=0 + this.height)) {
39                 return true;
40             }
41         }
42 /*
43         if((x >= this.x) && (x<= this.x + this.width)) {
44             if((y>=this.y) && (y<=this.y + this.height)) {
45                 return true;
46             }
47         }
48         */

49         return false;
50     }
51
52     // element stuff
53
public Node JavaDoc node;
54     private Box parent;
55     
56     public Box getParent() {
57         return parent;
58     }
59     public void setParent(Box box) {
60         this.parent = box;
61     }
62
63     // display stuff
64
public boolean display_none = false;
65
66     // position stuff
67
public boolean relative = false;
68     public boolean fixed = false;
69     public int top = 0;
70     public int right = 0;
71     public boolean right_set = false;
72     public int bottom = 0;
73     public boolean bottom_set = false;
74     public int left = 0;
75     public boolean floated = false;
76
77     // margins, borders, and padding stuff
78
public Color JavaDoc border_color;
79     public Border padding;
80     public Border border;
81     public Border margin;
82     public String JavaDoc border_style;
83     
84     public Box click_styles;
85
86     // foreground stuff
87
public Color JavaDoc color;
88     
89     // background stuff
90
public Color JavaDoc background_color;
91     public Image JavaDoc background_image;
92     public String JavaDoc repeat;
93     public String JavaDoc attachment;
94     public int background_position_vertical = 0;
95     public int background_position_horizontal = 0;
96     public boolean clicked = false;
97
98     // children stuff
99
private List JavaDoc boxes;
100     public void addChild(Box child) {
101         child.setParent(this);
102         boxes.add(child);
103         //u.p("added child: " + child + " to " + this);
104
}
105     
106     public int getChildCount() {
107         return boxes.size();
108     }
109     
110     public Box getChild(int i) {
111         return (Box) boxes.get(i);
112     }
113     
114     public Iterator JavaDoc getChildIterator() {
115         return boxes.iterator();
116     }
117     
118     
119     // element stuff
120
public Element JavaDoc getElement() {
121         return (Element JavaDoc)node;
122     }
123     public boolean isElement() {
124         if(node.getNodeType() == node.ELEMENT_NODE) {
125             return true;
126         }
127         return false;
128     }
129     public boolean isAnonymous() {
130         return false;
131     }
132
133     // printing stuff and constructor
134
public Box() {
135         this(true);
136     }
137     
138     public Box(boolean create_substyles) {
139         boxes = new ArrayList JavaDoc();
140         if(create_substyles) {
141             this.click_styles = new Box(false);
142         }
143     }
144     
145     public Box(int x, int y, int width, int height) {
146         this();
147         this.x = x;
148         this.y = y;
149         this.width = width;
150         this.height = height;
151     }
152
153     public Dimension JavaDoc getInternalDimension() {
154        int w = this.getWidth() - totalHorizontalPadding();
155        int h = this.getHeight() - totalVerticalPadding();
156        return new Dimension JavaDoc(w,h);
157     }
158     public int totalHorizontalPadding() {
159         int pd = 0;
160         if(this.margin != null) {
161             pd+= this.margin.left + this.margin.right;
162         }
163         if(this.padding != null) {
164             pd+= this.padding.left + this.padding.right;
165         }
166         if(this.border != null) {
167             pd+= this.border.left + this.border.right;
168         }
169         return pd;
170     }
171     public int totalVerticalPadding() {
172         int pd = 0;
173         if(this.margin != null) {
174             pd+= this.margin.top + this.margin.bottom;
175         }
176         if(this.padding != null) {
177             pd+= this.padding.top + this.padding.bottom;
178         }
179         if(this.border != null) {
180             pd+= this.border.top + this.border.bottom;
181         }
182         return pd;
183     }
184     
185     public int totalTopPadding() {
186         int pd = 0;
187         if(this.margin != null) {
188             pd+= this.margin.top;
189         }
190         if(this.padding != null) {
191             pd+= this.padding.top;
192         }
193         if(this.border != null) {
194             pd+= this.border.top;
195         }
196         return pd;
197     }
198     
199     public int totalLeftPadding() {
200         int pd = 0;
201         if(this.margin != null) { pd+= this.margin.left; }
202         if(this.padding != null) { pd+= this.padding.left; }
203         if(this.border != null) { pd+= this.border.left; }
204         return pd;
205     }
206     
207     public String JavaDoc toString() {
208         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
209         sb.append("Box: ");
210         if(node == null) {
211             sb.append(" null node, ");
212         } else {
213             sb.append(node.getNodeName());
214         }
215         sb.append(" (" + x + ","+y+")->("+width+" x "+height+")");
216         return sb.toString();
217     }
218
219 }
220
Popular Tags