KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.joshy.html;
2
3 import java.io.*;
4 import java.util.Iterator JavaDoc;
5 import javax.swing.JScrollPane JavaDoc;
6 import java.awt.Color JavaDoc;
7 import org.joshy.html.box.*;
8 import org.joshy.html.css.DefaultCSSMarker;
9 import java.awt.Graphics2D JavaDoc;
10 import java.awt.RenderingHints JavaDoc;
11 import java.awt.event.*;
12 import java.io.InputStream JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.awt.Dimension JavaDoc;
15 import java.awt.Graphics JavaDoc;
16 import java.awt.Point JavaDoc;
17 import java.awt.Rectangle JavaDoc;
18 import javax.swing.JPanel JavaDoc;
19 import javax.swing.JComponent JavaDoc;
20 import javax.swing.Scrollable JavaDoc;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.Element JavaDoc;
23 import org.joshy.u;
24 import org.joshy.x;
25 import java.net.URL JavaDoc;
26 import java.io.File JavaDoc;
27
28 import org.joshy.html.forms.*;
29
30 public class HTMLPanel extends JPanel JavaDoc implements ComponentListener {
31     //private int html_height = -1;
32
//private int max_width = -1;
33
public Document JavaDoc doc;
34     public Context c;
35     public Box body_box = null;
36     private JScrollPane JavaDoc viewport;
37     BodyLayout layout;
38     private Dimension JavaDoc intrinsic_size;
39
40     public HTMLPanel() {
41         c = new Context();
42         layout = new BodyLayout();
43         setLayout(new AbsoluteLayoutManager());
44     }
45     
46     public void setDocumentRelative(String JavaDoc filename ) throws Exception JavaDoc {
47         if(c != null && (!filename.startsWith("http"))) {
48             URL JavaDoc base = new URL JavaDoc(c.getBaseURL(),filename);
49             u.p("loading url " + base);
50             Document JavaDoc dom = x.loadDocument(base);
51             //URL base = new File(filename).toURL();
52
setDocument(dom, base);
53             return;
54         }
55         setDocument(x.loadDocument(filename),new File JavaDoc(filename).toURL());
56     }
57     public void setDocument(String JavaDoc filename) throws Exception JavaDoc {
58         setDocument(x.loadDocument(filename),new File JavaDoc(filename).toURL());
59     }
60     
61     public void setDocument(URL JavaDoc url) throws Exception JavaDoc {
62         setDocument(x.loadDocument(url),url);
63     }
64     
65     public void setDocument(Document JavaDoc doc) {
66         setDocument(doc,null);
67     }
68     
69     public void setDocument(Document JavaDoc doc, URL JavaDoc url) {
70         this.doc = doc;
71         Element JavaDoc html = (Element JavaDoc)doc.getDocumentElement();
72         c.css = new CSSBank();
73         c.setBaseURL(url);
74         try {
75             //c.css.parse(new FileReader("second.css"));
76
Object JavaDoc marker = new DefaultCSSMarker();
77             //u.p("getting: " + marker.getClass().getResource("default.css"));
78
if(marker.getClass().getResourceAsStream("default.css") != null) {
79                 URL JavaDoc stream = marker.getClass().getResource("default.css");
80                 //u.p("loading the url: " + stream);
81
String JavaDoc str = u.inputstream_to_string(stream.openStream());
82                 //u.p("loaded: " + str);
83
c.css.parse(new StringReader(str));//BufferInputStream(str));
84
} else {
85                 u.p("can't load default css");
86             }
87             c.css.parseInlineStyles(html);
88         } catch (Exception JavaDoc ex) {
89             u.p("error parsing CSS: " + ex);
90             u.p(ex);
91         }
92         this.body_box = null;
93         calcLayout();
94         repaint();
95     }
96     
97     public void setViewportComponent(JScrollPane JavaDoc component) {
98         this.viewport = component;
99         this.viewport.addComponentListener(this);
100     }
101     JScrollPane JavaDoc pane;
102     public void setJScrollPane(JScrollPane JavaDoc pane) {
103         this.pane = pane;
104     }
105     
106     public void paintComponent(Graphics JavaDoc g) {
107         //g.setColor(Color.blue);
108
//g.drawLine(0,0,50,50);
109
//u.p("paint() size = " + this.getSize());
110
//u.p("viewport size = " + this.viewport.getSize());
111
//u.p("w/o scroll = " + this.viewport.getViewportBorderBounds());
112
((Graphics2D JavaDoc)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
113         doPaint(g);
114     }
115
116     //public static long timestamp;
117
public void doPaint(Graphics JavaDoc g) {
118         //u.p("paint");
119

120         //long start_time = new java.util.Date().getTime();
121

122         if(body_box == null) {
123             calcLayout(g);
124         }
125         if(doc == null) { return; }
126         newContext(g);
127         
128         //u.p("paint");
129
layout.paint(c,body_box);
130         //long end_time = new java.util.Date().getTime();
131
//u.p("dist = " + (end_time-start_time));
132

133     }
134     
135     public void calcLayout() {
136         calcLayout(this.getGraphics());
137         this.setOpaque(false);
138     }
139     
140     private void newContext(Graphics JavaDoc g) {
141         Point JavaDoc origin = new Point JavaDoc(0,0);
142         Point JavaDoc last = new Point JavaDoc(0,0);
143         c.canvas = this;
144         c.graphics = g;
145         // set up the dimensions of the html canvas
146
//Rectangle dimensions = new Rectangle(this.getWidth(),this.getHeight());//layout.bwidth, layout.bheight);
147
//c.canvas_graphics = g.create();
148
//c.setExtents(new Rectangle(0,0,this.getWidth(),this.getHeight()));
149
//u.p("viewport size = " + viewport.getSize());
150
if(viewport != null) {
151             c.setExtents(new Rectangle JavaDoc(viewport.getViewportBorderBounds()));
152         } else {
153             c.setExtents(new Rectangle JavaDoc(200,200));
154         }
155         //c.setExtents(new Rectangle(0,0,viewport.getWidth(),viewport.getHeight()));
156
c.viewport = this.viewport;
157         c.cursor = last;
158         c.setMaxWidth(0);
159     }
160     
161     public void calcLayout(Graphics JavaDoc g) {
162         //u.p("calcLayout()");
163
this.removeAll();
164         //u.p("this = ");
165
//u.dump_stack();
166
if(g == null) {
167             //u.p("bad first graphics");
168
return;
169         }
170         //u.p("layout");
171
//u.p("size = " + this.getSize());
172
//u.dump_stack();
173
if(doc == null) { return; }
174         
175         Element JavaDoc html = (Element JavaDoc)doc.getDocumentElement();
176         Element JavaDoc body = x.child(html,"body");
177         
178         newContext(g);
179         // set up CSS
180
// the last added is used first
181

182         // start painting
183
c.setMaxWidth(0);
184         long start_time = new java.util.Date JavaDoc().getTime();
185         //u.p("starting count");
186
body_box = layout.layout(c,body);
187         long end_time = new java.util.Date JavaDoc().getTime();
188         //u.p("ending count = " + (end_time-start_time) + " msec");
189

190
191         if(this.pane != null) {
192             if(this.body_box != null) {
193                 //u.p("bcolor = " + body_box.background_color);
194
//u.p("body box = " + body_box.hashCode());
195
this.pane.getViewport().setBackground(body_box.background_color);
196             }
197         }
198
199         //u.p("calced height = " + layout.contents_height);//this.html_height);
200
//u.p("max width = " + c.getMaxWidth());
201
intrinsic_size = new Dimension JavaDoc(c.getMaxWidth(),layout.contents_height);
202         //u.p("intrinsic size = " + intrinsic_size);
203
//u.p("real size = " + this.getSize());
204
if(!intrinsic_size.equals(this.getSize())) {
205             //u.dump_stack();
206
this.setPreferredSize(intrinsic_size);
207             //u.p("setting preferred to : " + this.getPreferredSize());
208
//this.setSize(intrinsic_size);
209
this.revalidate();
210             //this.repaint();
211
//this.setPreferredSize(intrinsic_size);
212
}
213         //this.html_height = layout.contents_height;
214
/*if(c.getMaxWidth() > this.getSize().getWidth()) {
215             this.max_width = c.getMaxWidth();
216         }
217         //u.p("html height = " + this.html_height);
218         if(c.getMaxWidth() > this.getSize().getWidth()) {
219             this.setPreferredSize(new Dimension((int)c.getMaxWidth(),this.html_height));
220             this.max_width = c.getMaxWidth();
221             this.setMinimumSize(this.getPreferredSize());
222         } else {
223             this.setPreferredSize(new Dimension((int)this.getSize().getWidth(),this.html_height));
224             this.max_width = (int)this.getSize().getWidth();
225         }
226         */

227         
228         /*
229         u.p("size = " + this.getSize());
230         u.p("pref size = " + this.getPreferredSize());
231         if(!this.getSize().equals(this.getPreferredSize())) {
232             u.p("need a repaint");
233             u.p("size = " + this.getSize());
234             u.p("pref size = " + this.getPreferredSize());
235             super.setSize(this.getPreferredSize());
236             repaint();
237         }
238         */

239         //c.getGraphics().setColor(Color.blue);
240
//c.getGraphics().drawLine(0,0,50,50);
241
}
242     
243     
244     public void setSize(Dimension JavaDoc d) {
245         //u.p("set size called");
246
super.setSize(d);
247         this.calcLayout();
248     }
249     
250     
251     
252     /* === scrollable implementation === */
253     /*
254     public Dimension getPreferredScrollableViewportSize() {
255         u.p("get pref scrll view called");
256         //u.dump_stack();
257         
258         //u.p("size of viewport = " + viewport.getSize());
259         //u.p("size of intrinsic = " + intrinsic_size);
260         u.dump_stack();
261         if(intrinsic_size == null) {
262             return new Dimension(400,400);
263         }
264         
265         if(intrinsic_size.getWidth() > viewport.getWidth()) {
266             //u.p("intrinsic = " + this.intrinsic_size);
267             return new Dimension((int)intrinsic_size.getWidth(),400);
268         }
269         return null;
270     }
271     
272     public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
273         return 20;
274     }
275     
276     public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
277         return 100;
278     }
279     
280     public boolean getScrollableTracksViewportWidth() {
281         return false;
282     }
283     
284     public boolean getScrollableTracksViewportHeight() {
285         return false;
286     }
287     
288     */

289     
290     
291     
292     
293     
294     
295     public Box findBox(int x, int y) {
296         return findBox(this.body_box,x,y);
297     }
298     
299     public Box findBox(Box box, int x, int y) {
300         //u.p("findBox(" + box + " at ("+x+","+y+")");
301
Iterator JavaDoc it = box.getChildIterator();
302         while(it.hasNext()) {
303             Box bx = (Box)it.next();
304             int tx = x;
305             int ty = y;
306             tx -= bx.x;
307             tx -= bx.totalLeftPadding();
308             ty -= bx.y;
309             ty -= bx.totalTopPadding();
310             
311             // test the contents
312
Box retbox = findBox(bx,tx,ty);
313             if(retbox != null) {
314                 return retbox;
315             }
316             // test the box itself
317
//u.p("bx test = " + bx + " " + x +","+y);
318
if(bx.contains(x-bx.x,y-bx.y)) {
319                 return bx;
320             }
321         }
322
323         
324         /*
325         if(box instanceof LineBox) {
326             Iterator it2 = ((LineBox)box).inlines.iterator();
327             while(it2.hasNext()) {
328                 Box bx = (Box)it2.next();
329                 Box retbox = findBox(bx,x+bx.x,y-bx.y);
330                 if(retbox != null) {
331                     return retbox;
332                 }
333             }
334         }
335         
336         u.p("testing: " + box + " at ("+x+","+y+")");
337         if(box.contains(x,y)) {
338             return box;
339         }
340         */

341         return null;
342     }
343     
344     
345     public void componentHidden(ComponentEvent e) { }
346     public void componentMoved(ComponentEvent e) { }
347     public void componentResized(ComponentEvent e) {
348         calcLayout();
349     }
350     public void componentShown(ComponentEvent e) { }
351
352     
353     public void printTree() {
354         printTree(this.body_box, "");
355     }
356     private void printTree(Box box, String JavaDoc tab) {
357         u.p(tab + "Box = " + box);
358         Iterator JavaDoc it = box.getChildIterator();
359         while(it.hasNext()) {
360             Box bx = (Box)it.next();
361             printTree(bx,tab+ " ");
362         }
363         if(box instanceof InlineBox) {
364             InlineBox ib = (InlineBox)box;
365             if(ib.sub_block != null) {
366                 printTree(ib.sub_block,tab + " ");
367             }
368         }
369     }
370 }
371
372
Popular Tags