KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > util > InlineUtil


1 package org.joshy.html.util;
2
3 import org.joshy.u;
4 import java.util.List JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.awt.Point JavaDoc;
7 import org.joshy.html.*;
8 import org.w3c.dom.*;
9 import org.joshy.html.box.*;
10 public class InlineUtil {
11
12 public static int doTextIndent(Context c, Element elem, int width, LineBox first_line) {
13     if(c.css.hasProperty(elem,"text-indent")) {
14         float indent = c.css.getFloatProperty(elem,"text-indent",width);
15         width = width - (int)indent;
16         first_line.x = first_line.x + (int)indent;
17     }
18     return width;
19 }
20
21 public static void handleFloated(Context c, InlineBox inline, LineBox line,
22     int full_width, Element enclosing_block) {
23
24     if(inline.node == enclosing_block) {
25         return;
26     }
27     // we must make sure not to grab the float from the containing
28
// block incase it is floated.
29
if(inline.node.getNodeType() == inline.node.TEXT_NODE) {
30         if(inline.node.getParentNode() == enclosing_block) {
31             return;
32         }
33     }
34
35     String JavaDoc float_val = c.css.getStringProperty(inline.node,"float",false);
36
37     if(float_val == null) {
38         float_val = "none";
39     }
40     if(float_val.equals("none")) {
41         return;
42     }
43     if(float_val.equals("left")) {
44         // move the inline to the left
45
inline.x = 0-inline.width;
46         // adjust the left tab
47
c.getLeftTab().x = inline.width;
48         c.getLeftTab().y += inline.height;
49     }
50     if(float_val.equals("right")) {
51         // move the inline to the right
52
inline.x = full_width - inline.width;
53         // adjust the right tab
54
c.getRightTab().x = inline.width;
55         c.getRightTab().y += inline.height;
56     }
57     // shrink the line width
58
line.width = line.width - inline.width;
59     // mark as floated
60
inline.floated = true;
61 }
62
63 public static List JavaDoc getInlineNodeList(Node node, Element elem, Context c) {
64     return getInlineNodeList(node,elem,c,false);
65 }
66 public static List JavaDoc getInlineNodeList(Node node, Element elem, Context c, boolean stop_at_blocks) {
67     List JavaDoc list = new ArrayList JavaDoc();
68     if(node == null) { return list; }
69     if(elem == null) { return list; }
70     if(!elem.hasChildNodes()) {
71         //u.p("it's empty");
72
return list;
73     }
74     
75     //u.p("starting at: " + node);
76
Node curr = node;
77     while(true) {
78         //u.p("now list = " + list);
79

80         // skip the first time through
81
if(curr != node) {
82             if(curr.getNodeType() == curr.TEXT_NODE) {
83                 //u.p("adding: " + curr);
84
list.add(curr);
85                 node = curr;
86                 continue;
87                 //return curr;
88
}
89             if(InlineLayout.isReplaced(curr)) {
90                 //u.p("adding: " + curr);
91
list.add(curr);
92                 node = curr;
93                 continue;
94                 //return curr;
95
}
96             if(InlineLayout.isFloatedBlock(curr,c)) {
97                 //u.p("adding: " + curr);
98
list.add(curr);
99                 node = curr;
100                 continue;
101                 //return curr;
102
}
103             if(isBreak(curr)) {
104                 //u.p("adding: " + curr);
105
list.add(curr);
106                 node = curr;
107                 continue;
108                 //return curr;
109
}
110             if(stop_at_blocks) {
111                 if(InlineLayout.isBlockNode(curr,c)) {
112                     //u.p("at block boundary");
113
return list;
114                 }
115             }
116         }
117
118         if(curr.hasChildNodes()) {
119             //u.p("about to test: " + curr);
120
// if it's a floating block we don't want to recurse
121
if(!InlineLayout.isFloatedBlock(curr,c)) {
122                 curr = curr.getFirstChild();
123                 //u.p("going to first child " + curr);
124
continue;
125             }
126             // it's okay to recurse if it's the root that's the float,
127
// not the node being examined. this only matters when we
128
// start the loop at the root of a floated block
129
if(InlineLayout.isFloatedBlock(node,c)) {
130                 if(node == elem) {
131                     curr = curr.getFirstChild();
132                     continue;
133                 }
134             }
135         }
136
137         if(curr.getNextSibling() != null) {
138             curr = curr.getNextSibling();
139             //u.p("going to next sibling: " + curr);
140
continue;
141         }
142
143         // keep going up until we get another sibling
144
// or we are at elem.
145
while(true) {
146             curr = curr.getParentNode();
147             //u.p("going to parent: " + curr);
148
// if we are at the top then return null
149
if(curr == elem) {
150                 //u.p("at the top again. returning null");
151
//u.p("returning the list");
152
//u.p(list);
153
return list;
154                 //return null;
155
}
156             if(curr.getNextSibling() != null) {
157                 curr = curr.getNextSibling();
158                 //u.p("going to next sibling: " + curr);
159
break;
160             }
161         }
162     }
163
164 }
165
166
167 public static Node nextTextNode(List JavaDoc node_list) {
168     if(node_list.size() < 1) {
169         return null;
170     }
171     Node nd = (Node)node_list.get(0);
172     node_list.remove(nd);
173     return nd;
174 }
175
176
177 public static boolean isBreak(Node node) {
178     if(node instanceof Element) {
179         if(((Element)node).getNodeName().equals("br")) {
180             return true;
181         }
182     }
183     return false;
184 }
185
186
187 }
188
Popular Tags