1 package org.joshy.html; 2 3 import java.io.*; 4 import java.util.Iterator ; 5 import javax.swing.JScrollPane ; 6 import java.awt.Color ; 7 import org.joshy.html.box.*; 8 import org.joshy.html.css.DefaultCSSMarker; 9 import java.awt.Graphics2D ; 10 import java.awt.RenderingHints ; 11 import java.awt.event.*; 12 import java.io.InputStream ; 13 import java.io.InputStreamReader ; 14 import java.awt.Dimension ; 15 import java.awt.Graphics ; 16 import java.awt.Point ; 17 import java.awt.Rectangle ; 18 import javax.swing.JPanel ; 19 import javax.swing.JComponent ; 20 import javax.swing.Scrollable ; 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 import org.joshy.u; 24 import org.joshy.x; 25 import java.net.URL ; 26 import java.io.File ; 27 28 import org.joshy.html.forms.*; 29 30 public class HTMLPanel extends JPanel implements ComponentListener { 31 public Document doc; 34 public Context c; 35 public Box body_box = null; 36 private JScrollPane viewport; 37 BodyLayout layout; 38 private Dimension 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 filename ) throws Exception { 47 if(c != null && (!filename.startsWith("http"))) { 48 URL base = new URL (c.getBaseURL(),filename); 49 u.p("loading url " + base); 50 Document dom = x.loadDocument(base); 51 setDocument(dom, base); 53 return; 54 } 55 setDocument(x.loadDocument(filename),new File (filename).toURL()); 56 } 57 public void setDocument(String filename) throws Exception { 58 setDocument(x.loadDocument(filename),new File (filename).toURL()); 59 } 60 61 public void setDocument(URL url) throws Exception { 62 setDocument(x.loadDocument(url),url); 63 } 64 65 public void setDocument(Document doc) { 66 setDocument(doc,null); 67 } 68 69 public void setDocument(Document doc, URL url) { 70 this.doc = doc; 71 Element html = (Element )doc.getDocumentElement(); 72 c.css = new CSSBank(); 73 c.setBaseURL(url); 74 try { 75 Object marker = new DefaultCSSMarker(); 77 if(marker.getClass().getResourceAsStream("default.css") != null) { 79 URL stream = marker.getClass().getResource("default.css"); 80 String str = u.inputstream_to_string(stream.openStream()); 82 c.css.parse(new StringReader(str)); } else { 85 u.p("can't load default css"); 86 } 87 c.css.parseInlineStyles(html); 88 } catch (Exception 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 component) { 98 this.viewport = component; 99 this.viewport.addComponentListener(this); 100 } 101 JScrollPane pane; 102 public void setJScrollPane(JScrollPane pane) { 103 this.pane = pane; 104 } 105 106 public void paintComponent(Graphics g) { 107 ((Graphics2D )g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 113 doPaint(g); 114 } 115 116 public void doPaint(Graphics g) { 118 120 122 if(body_box == null) { 123 calcLayout(g); 124 } 125 if(doc == null) { return; } 126 newContext(g); 127 128 layout.paint(c,body_box); 130 133 } 134 135 public void calcLayout() { 136 calcLayout(this.getGraphics()); 137 this.setOpaque(false); 138 } 139 140 private void newContext(Graphics g) { 141 Point origin = new Point (0,0); 142 Point last = new Point (0,0); 143 c.canvas = this; 144 c.graphics = g; 145 if(viewport != null) { 151 c.setExtents(new Rectangle (viewport.getViewportBorderBounds())); 152 } else { 153 c.setExtents(new Rectangle (200,200)); 154 } 155 c.viewport = this.viewport; 157 c.cursor = last; 158 c.setMaxWidth(0); 159 } 160 161 public void calcLayout(Graphics g) { 162 this.removeAll(); 164 if(g == null) { 167 return; 169 } 170 if(doc == null) { return; } 174 175 Element html = (Element )doc.getDocumentElement(); 176 Element body = x.child(html,"body"); 177 178 newContext(g); 179 182 c.setMaxWidth(0); 184 long start_time = new java.util.Date ().getTime(); 185 body_box = layout.layout(c,body); 187 long end_time = new java.util.Date ().getTime(); 188 190 191 if(this.pane != null) { 192 if(this.body_box != null) { 193 this.pane.getViewport().setBackground(body_box.background_color); 196 } 197 } 198 199 intrinsic_size = new Dimension (c.getMaxWidth(),layout.contents_height); 202 if(!intrinsic_size.equals(this.getSize())) { 205 this.setPreferredSize(intrinsic_size); 207 this.revalidate(); 210 } 213 227 228 239 } 242 243 244 public void setSize(Dimension d) { 245 super.setSize(d); 247 this.calcLayout(); 248 } 249 250 251 252 253 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 Iterator 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 Box retbox = findBox(bx,tx,ty); 313 if(retbox != null) { 314 return retbox; 315 } 316 if(bx.contains(x-bx.x,y-bx.y)) { 319 return bx; 320 } 321 } 322 323 324 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 tab) { 357 u.p(tab + "Box = " + box); 358 Iterator 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 |