KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > test > TestFrame


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Oct 22, 2005
23  */

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 /**
38  * A Swing frame that can be used to test the
39  * Cobra HTML rendering engine.
40  */

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 JavaDoc 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 JavaDoc uri) {
117         try {
118             URL url;
119             try {
120                 url = new URL(uri);
121             } catch(java.net.MalformedURLException JavaDoc mfu) {
122                 int idx = uri.indexOf(':');
123                 if(idx == -1 || idx == 1) {
124                     // try file
125
url = new URL("file:" + uri);
126                 }
127                 else {
128                     throw mfu;
129                 }
130             }
131             // Call SimpleHtmlRendererContext.navigate()
132
// which implements incremental rendering.
133
this.rcontext.navigate(url, null);
134         } catch(Exception JavaDoc err) {
135             logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err);
136         }
137     }
138
139 // Old parse+render method commented out below:
140

141 // private void process(String uri) {
142
// try {
143
// URL url;
144
// try {
145
// url = new URL(uri);
146
// } catch(java.net.MalformedURLException mfu) {
147
// int idx = uri.indexOf(':');
148
// if(idx == -1 || idx == 1) {
149
// // try file
150
// url = new URL("file:" + uri);
151
// }
152
// else {
153
// throw mfu;
154
// }
155
// }
156
// logger.info("process(): Loading URI=[" + uri + "].");
157
// long time0 = System.currentTimeMillis();
158
// URLConnection connection = url.openConnection();
159
// connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+");
160
// connection.setRequestProperty("Cookie", "");
161
// if(connection instanceof HttpURLConnection) {
162
// HttpURLConnection hc = (HttpURLConnection) connection;
163
// hc.setInstanceFollowRedirects(true);
164
// int responseCode = hc.getResponseCode();
165
// logger.info("process(): HTTP response code: " + responseCode);
166
// }
167
// InputStream in = connection.getInputStream();
168
// byte[] content;
169
// try {
170
// content = IO.load(in, 8192);
171
// } finally {
172
// in.close();
173
// }
174
// String source = new String(content, "ISO-8859-1");
175
// this.textArea.setText(source);
176
// long time1 = System.currentTimeMillis();
177
// InputStream bin = new MyByteArrayInputStream(content);
178
// HtmlParserContext context = new SimpleHtmlParserContext();
179
// HtmlRendererContext rcontext = new SimpleHtmlRendererContext(this.htmlPanel, context);
180
// DocumentBuilderImpl builder = new DocumentBuilderImpl(context, rcontext);
181
// // Provide a proper URI, in case it was a file.
182
// String actualURI = url.toExternalForm();
183
// Document document = builder.parse(new InputSourceImpl(bin, actualURI, "ISO-8859-1"));
184
// long time2 = System.currentTimeMillis();
185
// logger.info("Parsed URI=[" + uri + "]: Parse elapsed: " + (time2 - time1) + " ms. Load elapsed: " + (time1 - time0) + " ms.");
186
// this.tree.setModel(new NodeTreeModel(document));
187
// this.htmlPanel.setDocument(document, rcontext, context);
188
// } catch(Exception err) {
189
// logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err);
190
// }
191
// }
192
}
193
Popular Tags