1 package org.joshy.html.app.browser; 2 3 import java.io.File ; 4 import java.net.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 import org.joshy.html.*; 9 import org.joshy.html.swing.*; 10 import org.joshy.u; 11 import org.joshy.x; 12 import java.util.logging.*; 13 import org.xml.sax.*; 14 import javax.xml.parsers.*; 15 import org.w3c.dom.*; 16 17 public class BrowserStartup { 18 public static Logger logger = Logger.getLogger("app.browser"); 19 BrowserPanel panel; 20 BrowserMenuBar menu; 21 JFrame frame; 22 protected HistoryManager history; 23 24 public BrowserStartup() { 25 logger.info("starting up"); 26 history = new HistoryManager(); 27 } 28 29 public void init() { 30 logger.info("creating UI"); 31 panel = new BrowserPanel(this); 32 panel.init(); 33 panel.createLayout(); 34 panel.createActions(); 35 36 menu = new BrowserMenuBar(this); 37 menu.init(); 38 menu.createLayout(); 39 menu.createActions(); 40 } 41 42 public static void main(String [] args) { 43 JFrame frame = new JFrame(); 44 BrowserStartup bs = new BrowserStartup(); 45 bs.frame = frame; 46 bs.init(); 47 frame.setJMenuBar(bs.menu); 48 frame.getContentPane().add(bs.panel); 49 frame.pack(); 50 frame.setSize(500,600); 51 frame.show(); 52 } 53 54 } 55 56 class BrowserMenuBar extends JMenuBar { 57 JMenu file; 58 JMenuItem quit; 59 JMenuItem open_file; 60 JMenu view; 61 JMenuItem view_source; 62 JMenu debug; 63 JMenu demos; 64 BrowserStartup root; 65 66 public BrowserMenuBar(BrowserStartup root) { 67 this.root = root; 68 } 69 70 public void init() { 71 file = new JMenu("File"); 72 open_file = new JMenuItem("Open File..."); 73 quit = new JMenuItem("Quit"); 74 debug = new JMenu("Debug"); 75 demos = new JMenu("Demos"); 76 view = new JMenu("View"); 77 view_source = new JMenuItem("Page Source"); 78 } 79 80 public void createLayout() { 81 file.add(open_file); 82 file.add(quit); 83 add(file); 84 85 view.add(view_source); 86 add(view); 87 88 demos.add(new LoadAction("Borders","demo:demos/border.xhtml")); 89 demos.add(new LoadAction("Backgrounds","demo:demos/background.xhtml")); 90 demos.add(new LoadAction("Paragraph","demo:demos/paragraph.xhtml")); 91 demos.add(new LoadAction("Line Breaking","demo:demos/breaking.xhtml")); 92 demos.add(new LoadAction("Forms","demo:demos/forms.xhtml")); 93 demos.add(new LoadAction("Headers","demo:demos/header.xhtml")); 94 demos.add(new LoadAction("Nested Divs","demo:demos/nested.xhtml")); 95 demos.add(new LoadAction("Selectors","demo:demos/selectors.xhtml")); 96 demos.add(new LoadAction("Images","demo:demos/image.xhtml")); 97 98 add(demos); 99 100 debug.add(new AbstractAction("draw boxes") { 101 public void actionPerformed(ActionEvent evt) { 102 root.panel.view.c.debug_draw_boxes = !root.panel.view.c.debug_draw_boxes; 103 root.panel.view.repaint(); 104 } 105 }); 106 107 debug.add(new AbstractAction("draw line boxes") { 108 public void actionPerformed(ActionEvent evt) { 109 root.panel.view.c.debug_draw_line_boxes = !root.panel.view.c.debug_draw_line_boxes; 110 root.panel.view.repaint(); 111 } 112 }); 113 114 debug.add(new AbstractAction("draw inline boxes") { 115 public void actionPerformed(ActionEvent evt) { 116 root.panel.view.c.debug_draw_inline_boxes = !root.panel.view.c.debug_draw_inline_boxes; 117 root.panel.view.repaint(); 118 } 119 }); 120 121 debug.add(new AbstractAction("DOM tree inspector") { 122 public void actionPerformed(ActionEvent evt) { 123 JFrame frame = new JFrame(); 124 frame.getContentPane().add(new DOMInspector(root.panel.view.doc)); 125 frame.pack(); 126 frame.setSize(250,500); 127 frame.show(); 128 } 129 }); 130 131 132 add(debug); 133 } 134 135 public void createActions() { 136 open_file.addActionListener(new ActionListener() { 137 public void actionPerformed(ActionEvent evt) { 138 try { 139 FileDialog fd = new FileDialog(root.frame,"Open a local file",FileDialog.LOAD); 140 fd.show(); 141 String file = fd.getDirectory() + fd.getFile(); 142 root.panel.loadPage(file); 143 } catch (Exception ex) { 144 logger.info("error:" + ex); 145 } 146 } 147 }); 148 quit.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent evt) { 150 System.exit(0); 151 } 152 }); 153 } 154 155 public static Logger logger = Logger.getLogger("app.browser"); 156 157 class LoadAction extends AbstractAction { 158 protected String url; 159 public LoadAction(String name, String url) { 160 super(name); 161 this.url = url; 162 } 163 164 public void actionPerformed(ActionEvent evt) { 165 try { 166 root.panel.loadPage(url); 167 } catch (Exception ex) { 168 u.p(ex); 169 } 170 } 171 172 } 173 174 } 175 176 177 class BrowserPanel extends JPanel { 178 public static Logger logger = Logger.getLogger("app.browser"); 179 JButton forward; 180 JButton backward; 181 JButton stop; 182 JButton reload; 183 JTextField url; 184 JLabel status; 185 HTMLPanel view; 186 JScrollPane scroll; 187 BrowserStartup root; 188 189 public BrowserPanel(BrowserStartup root) { 190 this.root = root; 191 } 192 193 194 public void init() { 195 forward = new JButton("Forward"); 196 backward = new JButton("Back"); 197 stop = new JButton("Stop"); 198 reload = new JButton("Reload"); 199 url = new JTextField(); 200 view = new HTMLPanel(); 201 status = new JLabel("Status"); 202 203 int text_width = 200; 204 view.setPreferredSize(new Dimension(text_width,text_width)); 205 scroll = new JScrollPane(view); 206 scroll.setVerticalScrollBarPolicy(scroll.VERTICAL_SCROLLBAR_ALWAYS); 207 scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_ALWAYS); 208 scroll.setPreferredSize(new Dimension(text_width,text_width)); 209 view.setViewportComponent(scroll); 210 view.setJScrollPane(scroll); 211 212 } 213 214 public void createLayout() { 215 GridBagLayout gbl = new GridBagLayout(); 216 GridBagConstraints c = new GridBagConstraints(); 217 setLayout(gbl); 218 219 c.gridx = 0; c.gridy = 0; 220 c.weightx = c.weighty = 0.0; 221 gbl.setConstraints(backward,c); 222 add(backward); 223 224 c.gridx++; 225 gbl.setConstraints(forward,c); 226 add(forward); 227 228 c.gridx++; 229 gbl.setConstraints(stop,c); 230 add(stop); 231 232 c.gridx++; 233 gbl.setConstraints(reload,c); 234 add(reload); 235 236 c.gridx++; 237 c.fill = c.HORIZONTAL; 238 c.weightx = 10.0; 239 gbl.setConstraints(url,c); 240 add(url); 241 242 c.gridx = 0; 243 c.gridy++; 244 c.fill = c.BOTH; 245 c.gridwidth = 5; 246 c.weightx = c.weighty = 10.0; 247 gbl.setConstraints(scroll,c); 248 add(scroll); 249 250 c.gridx = 0; 251 c.gridy++; 252 c.fill = c.HORIZONTAL; 253 c.weighty = 0.1; 254 gbl.setConstraints(status,c); 255 add(status); 256 257 } 258 259 String current_url = null; 260 261 262 263 public void goForward() { 264 root.history.goNext(); 265 view.setDocument(root.history.getCurrentDocument(),root.history.getCurrentURL()); 266 updateButtons(); 268 } 269 270 public void goBack() throws Exception { 271 root.history.goPrevious(); 272 view.setDocument(root.history.getCurrentDocument(),root.history.getCurrentURL()); 273 updateButtons(); 275 } 276 277 public void reloadPage() throws Exception { 278 logger.info("Reloading Page: "); 279 if(current_url != null) { 280 loadPage(current_url); 281 } 282 } 283 284 285 public void setStatus(String txt) { 286 status.setText(txt); 287 } 288 289 public void createActions() { 290 backward.addActionListener(new ActionListener() { 291 public void actionPerformed(ActionEvent evt) { 292 try { 293 goBack(); 294 view.repaint(); 295 } catch (Exception ex) { 296 u.p(ex); 297 } 298 } 299 }); 300 forward.addActionListener(new ActionListener() { 301 public void actionPerformed(ActionEvent evt) { 302 try { 303 goForward(); 304 view.repaint(); 305 } catch (Exception ex) { 306 u.p(ex); 307 } 308 } 309 }); 310 311 312 313 reload.addActionListener(new ActionListener() { 314 public void actionPerformed(ActionEvent evt) { 315 try { 316 reloadPage(); 317 view.repaint(); 318 } catch (Exception ex) { 319 u.p(ex); 320 } 321 } 322 }); 323 324 url.addActionListener(new ActionListener() { 325 public void actionPerformed(ActionEvent evt) { 326 try { 327 String url_text = url.getText(); 328 loadPage(url_text); 329 view.repaint(); 330 } catch (Exception ex) { 331 u.p(ex); 332 } 333 } 334 }); 335 336 updateButtons(); 337 } 338 339 protected void updateButtons() { 340 if(root.history.hasPrevious()) { 341 backward.setEnabled(true); 342 } else { 343 backward.setEnabled(false); 344 } 345 if(root.history.hasNext()) { 346 forward.setEnabled(true); 347 } else { 348 forward.setEnabled(false); 349 } 350 } 351 352 public void loadPage(Document doc, URL url) throws Exception { 353 view.setDocument(doc,url); 354 root.history.goNewDocument(doc); 355 updateButtons(); 356 } 357 public void loadPage(String url_text) throws Exception { 358 logger.info("Loading Page: " + url_text); 359 current_url = url_text; 360 361 362 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 363 DocumentBuilder builder = fact.newDocumentBuilder(); 364 builder.setErrorHandler(new ErrorHandler() { 365 public void warning(SAXParseException ex) throws SAXException { 366 logger.info("warning: " + ex); 367 } 368 public void error(SAXParseException ex) throws SAXException { 369 logger.info("error: " + ex); 370 logger.info("error on line: " + ex.getLineNumber() + " column: " + ex.getColumnNumber()); 371 setStatus("Error Loading Document"); 372 } 373 public void fatalError(SAXParseException ex) throws SAXException { 374 logger.info("fatal error: " + ex); 375 logger.info("error on line: " + ex.getLineNumber() + " column: " + ex.getColumnNumber()); 376 setStatus("Error Loading Document"); 377 } 378 }); 379 380 Document doc = null; 381 382 383 URL ref = null; 384 385 if(url_text.startsWith("demo:")) { 386 DemoMarker marker = new DemoMarker(); 387 u.p("marker = " + marker); 388 String short_url = url_text.substring(5); 389 u.p("sub = " + short_url); 390 if(!short_url.startsWith("/")) { 391 short_url = "/" + short_url; 392 } 393 doc = builder.parse(marker.getClass().getResourceAsStream(short_url)); 394 ref = marker.getClass().getResource(short_url); 395 } else if(url_text.startsWith("http")) { 396 doc = builder.parse(url_text); 397 ref = new File (url_text).toURL(); 398 } else { 399 doc = builder.parse(url_text); 400 ref = new File (url_text).toURL(); 401 } 402 loadPage(doc,ref); 403 404 setStatus("Successfully loaded: " + url_text); 405 } 406 } 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | Popular Tags |