1 /* HtmlDemo.java -- HTML viewer demo
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.swing;
41 import java.awt.BorderLayout;
42 import java.awt.Dimension;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.io.IOException;
46 import java.io.OutputStreamWriter;
47 import java.io.PrintWriter;
48 import java.net.MalformedURLException;
50 import java.net.URLConnection;
51 import java.util.LinkedList;
53 import javax.swing.BoxLayout;
54 import javax.swing.Icon;
55 import javax.swing.JButton;
56 import javax.swing.JComponent;
57 import javax.swing.JEditorPane;
58 import javax.swing.JFrame;
59 import javax.swing.JPanel;
60 import javax.swing.JScrollPane;
61 import javax.swing.JTextField;
62 import javax.swing.JTextPane;
63 import javax.swing.JToolBar;
64 import javax.swing.SwingUtilities;
65 import javax.swing.event.HyperlinkEvent;
66 import javax.swing.event.HyperlinkListener;
67 import javax.swing.text.html.FormSubmitEvent;
70 * Parses and displays HTML content.
72 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
74 public class HtmlDemo extends JPanel
77 private class LoadActionListener
78 implements ActionListener
81 public void actionPerformed(ActionEvent event)
83 String urlStr = url.getText();
86 setPage(new URL(url.getText()));
88 catch (MalformedURLException ex)
90 // Do something more useful here.
97 * Setting this to true causes the parsed element structure to be dumped.
99 private static final boolean DEBUG = true;
102 * The URL entry field.
104 JTextField url = new JTextField();
106 JTextPane html = new JTextPane();
111 * The browsing history.
113 * Package private to avoid accessor method.
120 history = new LinkedList();
125 * Returns a panel with the demo content. The panel uses a BorderLayout(), and
126 * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
127 * the bottom of the panel if they want to (a close button is added if this
128 * demo is being run as a standalone demo).
130 private void createContent()
132 setLayout(new BorderLayout());
134 JEditorPane.registerEditorKitForContentType("text/html",
135 BrowserEditorKit.class.getName());
136 html.setEditable(false);
137 html.addHyperlinkListener(new HyperlinkListener()
140 public void hyperlinkUpdate(HyperlinkEvent event)
142 if (event instanceof FormSubmitEvent)
144 submitForm((FormSubmitEvent) event);
148 URL u = event.getURL();
158 JScrollPane scroller = new JScrollPane(html);
159 JPanel urlPanel = new JPanel();
160 urlPanel.setLayout(new BoxLayout(urlPanel, BoxLayout.X_AXIS));
161 url.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
162 LoadActionListener action = new LoadActionListener();
163 url.addActionListener(action);
165 JButton loadButton = new JButton("go");
166 urlPanel.add(loadButton);
167 loadButton.addActionListener(action);
169 // Setup control panel.
170 JToolBar controlPanel = createToolBar();
171 JPanel browserPanel = new JPanel();
172 browserPanel.setLayout(new BorderLayout());
173 browserPanel.add(urlPanel, BorderLayout.NORTH);
174 browserPanel.add(scroller, BorderLayout.CENTER);
175 add(controlPanel, BorderLayout.NORTH);
176 add(browserPanel, BorderLayout.CENTER);
181 URL startpage = getClass().getResource("welcome.html");
182 html.setPage(startpage);
183 url.setText(startpage.toString());
184 history.addLast(startpage);
188 System.err.println("couldn't load page: "/* + startpage*/);
189 ex.printStackTrace();
191 setPreferredSize(new Dimension(800, 600));
196 * Creates the toolbar with the control buttons.
198 * @return the toolbar with the control buttons
200 JToolBar createToolBar()
202 JToolBar tb = new JToolBar();
203 Icon backIcon = Demo.getIcon("/gnu/classpath/examples/icons/back.png",
205 JButton back = new JButton(backIcon);
206 back.addActionListener(new ActionListener()
208 public void actionPerformed(ActionEvent ev)
210 if (history.size() > 1)
212 URL last = (URL) history.removeLast();
213 last = (URL) history.getLast();
214 url.setText(last.toString());
219 catch (IOException ex)
221 // Do something more useful.
222 ex.printStackTrace();
228 Icon reloadIcon = Demo.getIcon("/gnu/classpath/examples/icons/reload.png",
230 JButton reload = new JButton(reloadIcon);
231 reload.addActionListener(new ActionListener()
233 public void actionPerformed(ActionEvent ev)
235 if (history.size() > 0)
237 URL last = (URL) history.getLast();
238 url.setText(last.toString());
243 catch (IOException ex)
245 // Do something more useful.
246 ex.printStackTrace();
256 * The executable method to display the editable table.
261 public static void main(String[] args)
263 SwingUtilities.invokeLater
268 HtmlDemo demo = new HtmlDemo();
269 JFrame frame = new JFrame();
270 frame.getContentPane().add(demo);
271 frame.setSize(new Dimension(750, 480));
272 frame.setVisible(true);
278 * Helper method to navigate to a new URL.
280 * @param u the new URL to navigate to
286 url.setText(u.toString());
290 catch (IOException ex)
292 // Do something more useful here.
293 ex.printStackTrace();
298 * Submits a form when a FormSubmitEvent is received. The HTML API
299 * provides automatic form submit but when this is enabled we don't
300 * receive any notification and can't update our location field.
302 * @param ev the form submit event
304 void submitForm(FormSubmitEvent ev)
306 URL url = ev.getURL();
307 String data = ev.getData();
308 FormSubmitEvent.MethodType method = ev.getMethod();
309 if (method == FormSubmitEvent.MethodType.POST)
313 URLConnection conn = url.openConnection();
314 postData(conn, data);
316 catch (IOException ex)
319 ex.printStackTrace();
326 url = new URL(url.toString() + "?" + data);
328 catch (MalformedURLException ex)
330 ex.printStackTrace();
337 * Posts the form data for forms with HTTP POST method.
339 * @param conn the connection
340 * @param data the form data
342 private void postData(URLConnection conn, String data)
344 conn.setDoOutput(true);
345 PrintWriter out = null;
348 out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
352 catch (IOException ex)
355 ex.printStackTrace();
365 * Returns a DemoFactory that creates a HtmlDemo.
367 * @return a DemoFactory that creates a HtmlDemo
369 public static DemoFactory createDemoFactory()
371 return new DemoFactory()
373 public JComponent createDemo()
375 return new HtmlDemo();