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.GridLayout;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
47 import javax.swing.JButton;
48 import javax.swing.JComponent;
49 import javax.swing.JFrame;
50 import javax.swing.JPanel;
51 import javax.swing.JScrollPane;
52 import javax.swing.JTextArea;
53 import javax.swing.JTextPane;
54 import javax.swing.SwingUtilities;
55 import javax.swing.text.Element;
56 import javax.swing.text.html.HTMLDocument;
59 * Parses and displays HTML content.
61 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
63 public class HtmlDemo extends JPanel
66 JTextPane html = new JTextPane();
68 JTextArea text = new JTextArea("<html><body>" +
69 "123456789HR!<hr>987654321"+
70 "123456789BR!<br>987654321"+
71 "<p id='insertHere'>Insertion target</p><p>"+
72 "<font color=red>ma</font>"+
73 "<sup>sup</sup>normal<sub>sub</sub>normal</p><p>Table:"+
74 "<table><tr>a<td>b<td>c<tr>x<td>y<td>z</table></body></html>");
83 html.setContentType("text/html"); // not now.
88 * Returns a panel with the demo content. The panel uses a BorderLayout(), and
89 * the BorderLayout.SOUTH area is empty, to allow callers to add controls to
90 * the bottom of the panel if they want to (a close button is added if this
91 * demo is being run as a standalone demo).
93 private void createContent()
95 setLayout(new BorderLayout());
97 JPanel center = new JPanel();
98 GridLayout layout = new GridLayout();
100 center.setLayout(layout);
101 center.add(new JScrollPane(text));
102 center.add(new JScrollPane(html));
104 buttons = new JPanel();
106 JButton parse = new JButton("parse");
107 parse.addActionListener(new ActionListener()
109 public void actionPerformed(ActionEvent event)
111 String t = text.getText();
112 System.out.println("HtmlDemo.java.createContent:Parsing started");
114 System.out.println("HtmlDemo.java.createContent:Parsing completed");
120 JButton insertBeforeEnd = new JButton("before end");
121 insertBeforeEnd.addActionListener(new ActionListener()
123 public void actionPerformed(ActionEvent event)
125 HTMLDocument doc = (HTMLDocument) html.getDocument();
126 Element el = doc.getElement("insertHere");
127 System.out.println("Element found:"+el);
130 doc.insertBeforeEnd(el,"before end "+(n++));
139 JButton insertBeforeStart = new JButton("before start");
140 insertBeforeStart.addActionListener(new ActionListener()
142 public void actionPerformed(ActionEvent event)
144 HTMLDocument doc = (HTMLDocument) html.getDocument();
145 Element el = doc.getElement("insertHere");
146 System.out.println("Element found:"+el);
149 doc.insertBeforeStart(el,"before start "+(n++));
158 JButton insertAfterEnd = new JButton("after end");
159 insertAfterEnd.addActionListener(new ActionListener()
161 public void actionPerformed(ActionEvent event)
163 HTMLDocument doc = (HTMLDocument) html.getDocument();
164 Element el = doc.getElement("insertHere");
165 System.out.println("Element found:"+el);
168 doc.insertAfterEnd(el,"after end "+(n++));
177 JButton insertAfterStart = new JButton("after start");
178 insertAfterStart.addActionListener(new ActionListener()
180 public void actionPerformed(ActionEvent event)
182 HTMLDocument doc = (HTMLDocument) html.getDocument();
183 Element el = doc.getElement("insertHere");
184 System.out.println("Element found:"+el);
187 doc.insertAfterStart(el,"after start "+(n++));
197 JButton setInner = new JButton("inner");
198 setInner.addActionListener(new ActionListener()
200 public void actionPerformed(ActionEvent event)
202 HTMLDocument doc = (HTMLDocument) html.getDocument();
203 Element el = doc.getElement("insertHere");
204 System.out.println("Element found:"+el);
207 doc.setInnerHTML(el,"inner "+(n++));
216 JButton setOuter = new JButton("outer");
217 setOuter.addActionListener(new ActionListener()
219 public void actionPerformed(ActionEvent event)
221 HTMLDocument doc = (HTMLDocument) html.getDocument();
222 Element el = doc.getElement("insertHere");
223 System.out.println("Element found:"+el);
226 doc.setOuterHTML(el,"outer "+(n++));
236 buttons.add(insertBeforeStart);
237 buttons.add(insertAfterStart);
238 buttons.add(insertBeforeEnd);
239 buttons.add(insertAfterEnd);
241 buttons.add(setInner);
242 buttons.add(setOuter);
244 add(center, BorderLayout.CENTER);
245 add(buttons, BorderLayout.SOUTH);
249 * The executable method to display the editable table.
254 public static void main(String[] args)
256 SwingUtilities.invokeLater
261 HtmlDemo demo = new HtmlDemo();
263 JButton exit = new JButton("exit");
264 exit.addActionListener(new ActionListener()
266 public void actionPerformed(ActionEvent event)
272 demo.buttons.add(exit);
274 JFrame frame = new JFrame();
275 frame.getContentPane().add(demo);
276 frame.setSize(new Dimension(700, 480));
277 frame.setVisible(true);
283 * Returns a DemoFactory that creates a HtmlDemo.
285 * @return a DemoFactory that creates a HtmlDemo
287 public static DemoFactory createDemoFactory()
289 return new DemoFactory()
291 public JComponent createDemo()
293 return new HtmlDemo();