1 21 24 package org.lobobrowser.html.test; 25 26 import java.awt.*; 27 import java.awt.event.*; 28 import javax.swing.*; 29 import javax.swing.event.*; 30 31 import java.net.*; 32 33 import org.lobobrowser.html.gui.*; 34 35 import java.util.logging.*; 36 37 41 public class TestFrame extends JFrame { 42 private static final Logger logger = Logger.getLogger(TestFrame.class.getName()); 43 private final SimpleHtmlRendererContext rcontext; 44 private final JTree tree; 45 private final HtmlPanel htmlPanel; 46 private final JTextArea textArea; 47 48 public TestFrame() throws HeadlessException { 49 this(""); 50 } 51 52 public TestFrame(String title) throws HeadlessException { 53 super(title); 54 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 55 Container contentPane = this.getContentPane(); 56 contentPane.setLayout(new BorderLayout()); 57 JPanel topPanel = new JPanel(); 58 topPanel.setLayout(new BorderLayout()); 59 JPanel bottomPanel = new JPanel(); 60 bottomPanel.setLayout(new BorderLayout()); 61 final JTextField textField = new JTextField(); 62 JButton button = new JButton("Parse & Render"); 63 final JTabbedPane tabbedPane = new JTabbedPane(); 64 final JTree tree = new JTree(); 65 final JScrollPane scrollPane = new JScrollPane(tree); 66 67 this.tree = tree; 68 69 contentPane.add(topPanel, BorderLayout.NORTH); 70 contentPane.add(bottomPanel, BorderLayout.CENTER); 71 72 topPanel.add(new JLabel("URL: "), BorderLayout.WEST); 73 topPanel.add(textField, BorderLayout.CENTER); 74 topPanel.add(button, BorderLayout.EAST); 75 76 bottomPanel.add(tabbedPane, BorderLayout.CENTER); 77 78 final HtmlPanel panel = new HtmlPanel(); 79 panel.addSelectionChangeListener(new SelectionChangeListener() { 80 public void selectionChanged(SelectionChangeEvent event) { 81 if(logger.isLoggable(Level.INFO)) { 82 logger.info("selectionChanged(): selection node: " + panel.getSelectionNode()); 83 } 84 } 85 }); 86 this.htmlPanel = panel; 87 this.rcontext = new SimpleHtmlRendererContext(panel); 88 89 final JTextArea textArea = new JTextArea(); 90 this.textArea = textArea; 91 textArea.setEditable(false); 92 final JScrollPane textAreaSp = new JScrollPane(textArea); 93 94 tabbedPane.addTab("HTML", panel); 95 tabbedPane.addTab("Tree", scrollPane); 96 tabbedPane.addTab("Source", textAreaSp); 97 tabbedPane.addChangeListener(new ChangeListener() { 98 public void stateChanged(ChangeEvent e) { 99 Component component = tabbedPane.getSelectedComponent(); 100 if(component == scrollPane) { 101 tree.setModel(new NodeTreeModel(panel.getRootNode())); 102 } 103 else if(component == textAreaSp) { 104 textArea.setText(rcontext.getSourceCode()); 105 } 106 } 107 }); 108 109 button.addActionListener(new ActionListener() { 110 public void actionPerformed(ActionEvent event) { 111 process(textField.getText()); 112 } 113 }); 114 } 115 116 private void process(String uri) { 117 try { 118 URL url; 119 try { 120 url = new URL(uri); 121 } catch(java.net.MalformedURLException mfu) { 122 int idx = uri.indexOf(':'); 123 if(idx == -1 || idx == 1) { 124 url = new URL("file:" + uri); 126 } 127 else { 128 throw mfu; 129 } 130 } 131 this.rcontext.navigate(url, null); 134 } catch(Exception err) { 135 logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err); 136 } 137 } 138 139 141 } 193 | Popular Tags |