OSDN Git Service

2006-08-14 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / examples / gnu / classpath / examples / swing / HtmlDemo.java
1 /* HtmlDemo.java -- HTML viewer demo
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38
39 package gnu.classpath.examples.swing;
40
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;
46
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;
57
58 /**
59  * Parses and displays HTML content.
60  * 
61  * @author Audrius Meskauskas (audriusa@bioinformatics.org)
62  */
63 public class HtmlDemo extends JPanel
64
65   
66   JTextPane html = new JTextPane();
67
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>");  
75   
76   JPanel buttons;
77   
78   int n;
79
80   public HtmlDemo()
81   {
82     super();
83     html.setContentType("text/html"); // not now.
84     createContent();
85   }
86   
87   /**
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).
92    */
93   private void createContent()
94   {
95     setLayout(new BorderLayout());
96     
97     JPanel center = new JPanel();
98     GridLayout layout = new GridLayout();
99     layout.setRows(2);
100     center.setLayout(layout);
101     center.add(new JScrollPane(text));
102     center.add(new JScrollPane(html));
103     
104     buttons = new JPanel();
105     
106     JButton parse = new JButton("parse");
107     parse.addActionListener(new ActionListener()
108       {
109         public void actionPerformed(ActionEvent event)
110           {
111             String t = text.getText();
112             System.out.println("HtmlDemo.java.createContent:Parsing started");
113             html.setText(t);
114             System.out.println("HtmlDemo.java.createContent:Parsing completed");            
115           }
116       });
117     
118     buttons.add(parse);
119     
120     JButton insertBeforeEnd = new JButton("before end");
121     insertBeforeEnd.addActionListener(new ActionListener()
122       {
123         public void actionPerformed(ActionEvent event)
124           {
125             HTMLDocument doc = (HTMLDocument) html.getDocument();
126             Element el = doc.getElement("insertHere");
127             System.out.println("Element found:"+el);
128             try
129               {
130                 doc.insertBeforeEnd(el,"before end "+(n++));
131               }
132             catch (Exception e)
133               {
134                 e.printStackTrace();
135               }
136           }
137       });
138     
139     JButton insertBeforeStart = new JButton("before start");
140     insertBeforeStart.addActionListener(new ActionListener()
141       {
142         public void actionPerformed(ActionEvent event)
143           {
144             HTMLDocument doc = (HTMLDocument) html.getDocument();
145             Element el = doc.getElement("insertHere");
146             System.out.println("Element found:"+el);
147             try
148               {
149                 doc.insertBeforeStart(el,"before start "+(n++));
150               }
151             catch (Exception e)
152               {
153                 e.printStackTrace();
154               }
155           }
156       });
157     
158     JButton insertAfterEnd = new JButton("after end");
159     insertAfterEnd.addActionListener(new ActionListener()
160       {
161         public void actionPerformed(ActionEvent event)
162           {
163             HTMLDocument doc = (HTMLDocument) html.getDocument();
164             Element el = doc.getElement("insertHere");
165             System.out.println("Element found:"+el);
166             try
167               {
168                 doc.insertAfterEnd(el,"after end "+(n++));
169               }
170             catch (Exception e)
171               {
172                 e.printStackTrace();
173               }
174           }
175       });
176     
177     JButton insertAfterStart = new JButton("after start");
178     insertAfterStart.addActionListener(new ActionListener()
179       {
180         public void actionPerformed(ActionEvent event)
181           {
182             HTMLDocument doc = (HTMLDocument) html.getDocument();
183             Element el = doc.getElement("insertHere");
184             System.out.println("Element found:"+el);
185             try
186               {
187                 doc.insertAfterStart(el,"after start "+(n++));
188               }
189             catch (Exception e)
190               {
191                 e.printStackTrace();
192               }
193           }
194       });
195     
196
197     JButton setInner = new JButton("inner");
198     setInner.addActionListener(new ActionListener()
199       {
200         public void actionPerformed(ActionEvent event)
201           {
202             HTMLDocument doc = (HTMLDocument) html.getDocument();
203             Element el = doc.getElement("insertHere");
204             System.out.println("Element found:"+el);
205             try
206               {
207                 doc.setInnerHTML(el,"inner "+(n++));
208               }
209             catch (Exception e)
210               {
211                 e.printStackTrace();
212               }
213           }
214       });
215     
216     JButton setOuter = new JButton("outer");
217     setOuter.addActionListener(new ActionListener()
218       {
219         public void actionPerformed(ActionEvent event)
220           {
221             HTMLDocument doc = (HTMLDocument) html.getDocument();
222             Element el = doc.getElement("insertHere");
223             System.out.println("Element found:"+el);
224             try
225               {
226                 doc.setOuterHTML(el,"outer "+(n++));
227               }
228             catch (Exception e)
229               {
230                 e.printStackTrace();
231               }
232           }
233       });
234     
235
236     buttons.add(insertBeforeStart);
237     buttons.add(insertAfterStart);    
238     buttons.add(insertBeforeEnd);
239     buttons.add(insertAfterEnd);
240
241     buttons.add(setInner);
242     buttons.add(setOuter);
243     
244     add(center, BorderLayout.CENTER);
245     add(buttons, BorderLayout.SOUTH);
246   }
247  
248   /**
249    * The executable method to display the editable table.
250    * 
251    * @param args
252    *          unused.
253    */
254   public static void main(String[] args)
255   {
256     SwingUtilities.invokeLater
257     (new Runnable()
258      {
259        public void run()
260        {
261          HtmlDemo demo = new HtmlDemo();
262          
263          JButton exit = new JButton("exit");
264          exit.addActionListener(new ActionListener()
265            {
266              public void actionPerformed(ActionEvent event)
267                {
268                  System.exit(0);
269                }
270            });
271          
272          demo.buttons.add(exit);
273          
274          JFrame frame = new JFrame();
275          frame.getContentPane().add(demo);
276          frame.setSize(new Dimension(700, 480));
277          frame.setVisible(true);
278        }
279      });
280   }
281
282   /**
283    * Returns a DemoFactory that creates a HtmlDemo.
284    *
285    * @return a DemoFactory that creates a HtmlDemo
286    */
287   public static DemoFactory createDemoFactory()
288   {
289     return new DemoFactory()
290     {
291       public JComponent createDemo()
292       {
293         return new HtmlDemo();
294       }
295     };
296   }
297 }
298