OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [swing/] [HtmlDemo.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* 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.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;
49
import java.net.URL;
50
import java.net.URLConnection;
51
import java.util.LinkedList;
52
 
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;
68
 
69
/**
70
 * Parses and displays HTML content.
71
 *
72
 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
73
 */
74
public class HtmlDemo extends JPanel
75
{
76
 
77
  private class LoadActionListener
78
    implements ActionListener
79
  {
80
 
81
    public void actionPerformed(ActionEvent event)
82
    {
83
      String urlStr = url.getText();
84
      try
85
        {
86
          setPage(new URL(url.getText()));
87
        }
88
      catch (MalformedURLException ex)
89
        {
90
          // Do something more useful here.
91
          ex.printStackTrace();
92
        }
93
    }
94
  }
95
 
96
  /**
97
   * Setting this to true causes the parsed element structure to be dumped.
98
   */
99
  private static final boolean DEBUG = true;
100
 
101
  /**
102
   * The URL entry field.
103
   */
104
  JTextField url = new JTextField();
105
 
106
  JTextPane html = new JTextPane();
107
 
108
  int n;
109
 
110
  /**
111
   * The browsing history.
112
   *
113
   * Package private to avoid accessor method.
114
   */
115
  LinkedList history;
116
 
117
  public HtmlDemo()
118
  {
119
    super();
120
    history = new LinkedList();
121
    createContent();
122
  }
123
 
124
  /**
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).
129
   */
130
  private void createContent()
131
  {
132
    setLayout(new BorderLayout());
133
 
134
    JEditorPane.registerEditorKitForContentType("text/html",
135
                                             BrowserEditorKit.class.getName());
136
    html.setEditable(false);
137
    html.addHyperlinkListener(new HyperlinkListener()
138
    {
139
 
140
      public void hyperlinkUpdate(HyperlinkEvent event)
141
      {
142
        if (event instanceof FormSubmitEvent)
143
          {
144
            submitForm((FormSubmitEvent) event);
145
          }
146
        else
147
          {
148
            URL u = event.getURL();
149
            if (u != null)
150
              {
151
                setPage(u);
152
              }
153
          }
154
      }
155
 
156
    });
157
 
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);
164
    urlPanel.add(url);
165
    JButton loadButton = new JButton("go");
166
    urlPanel.add(loadButton);
167
    loadButton.addActionListener(action);
168
 
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);
177
 
178
    // Load start page.
179
    try
180
      {
181
        URL startpage = getClass().getResource("welcome.html");
182
        html.setPage(startpage);
183
        url.setText(startpage.toString());
184
        history.addLast(startpage);
185
      }
186
    catch (Exception ex)
187
      {
188
        System.err.println("couldn't load page: "/* + startpage*/);
189
        ex.printStackTrace();
190
      }
191
    setPreferredSize(new Dimension(800, 600));
192
  }
193
 
194
 
195
  /**
196
   * Creates the toolbar with the control buttons.
197
   *
198
   * @return the toolbar with the control buttons
199
   */
200
  JToolBar createToolBar()
201
  {
202
    JToolBar tb = new JToolBar();
203
    Icon backIcon = Demo.getIcon("/gnu/classpath/examples/icons/back.png",
204
                                 "back");
205
    JButton back = new JButton(backIcon);
206
    back.addActionListener(new ActionListener()
207
    {
208
      public void actionPerformed(ActionEvent ev)
209
      {
210
        if (history.size() > 1)
211
          {
212
            URL last = (URL) history.removeLast();
213
            last = (URL) history.getLast();
214
            url.setText(last.toString());
215
            try
216
              {
217
                html.setPage(last);
218
              }
219
            catch (IOException ex)
220
              {
221
                // Do something more useful.
222
                ex.printStackTrace();
223
              }
224
          }
225
      }
226
    });
227
    tb.add(back);
228
    Icon reloadIcon = Demo.getIcon("/gnu/classpath/examples/icons/reload.png",
229
                                   "reload");
230
    JButton reload = new JButton(reloadIcon);
231
    reload.addActionListener(new ActionListener()
232
    {
233
      public void actionPerformed(ActionEvent ev)
234
      {
235
        if (history.size() > 0)
236
          {
237
            URL last = (URL) history.getLast();
238
            url.setText(last.toString());
239
            try
240
              {
241
                html.setPage(last);
242
              }
243
            catch (IOException ex)
244
              {
245
                // Do something more useful.
246
                ex.printStackTrace();
247
              }
248
          }
249
      }
250
    });
251
    tb.add(reload);
252
    return tb;
253
  }
254
 
255
  /**
256
   * The executable method to display the editable table.
257
   *
258
   * @param args
259
   *          unused.
260
   */
261
  public static void main(String[] args)
262
  {
263
    SwingUtilities.invokeLater
264
    (new Runnable()
265
     {
266
       public void run()
267
       {
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);
273
       }
274
     });
275
  }
276
 
277
  /**
278
   * Helper method to navigate to a new URL.
279
   *
280
   * @param u the new URL to navigate to
281
   */
282
  void setPage(URL u)
283
  {
284
    try
285
      {
286
        url.setText(u.toString());
287
        html.setPage(u);
288
        history.addLast(u);
289
      }
290
    catch (IOException ex)
291
      {
292
        // Do something more useful here.
293
        ex.printStackTrace();
294
      }
295
  }
296
 
297
  /**
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.
301
   *
302
   * @param ev the form submit event
303
   */
304
  void submitForm(FormSubmitEvent ev)
305
  {
306
    URL url = ev.getURL();
307
    String data = ev.getData();
308
    FormSubmitEvent.MethodType method = ev.getMethod();
309
    if (method == FormSubmitEvent.MethodType.POST)
310
      {
311
        try
312
          {
313
            URLConnection conn = url.openConnection();
314
            postData(conn, data);
315
          }
316
        catch (IOException ex)
317
          {
318
            // Deal with this.
319
            ex.printStackTrace();
320
          }
321
      }
322
    else
323
      {
324
        try
325
          {
326
            url = new URL(url.toString() + "?" + data);
327
          }
328
        catch (MalformedURLException ex)
329
          {
330
            ex.printStackTrace();
331
          }
332
      }
333
    setPage(url);
334
  }
335
 
336
  /**
337
   * Posts the form data for forms with HTTP POST method.
338
   *
339
   * @param conn the connection
340
   * @param data the form data
341
   */
342
  private void postData(URLConnection conn, String data)
343
  {
344
    conn.setDoOutput(true);
345
    PrintWriter out = null;
346
    try
347
      {
348
        out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
349
        out.print(data);
350
        out.flush();
351
      }
352
    catch (IOException ex)
353
      {
354
        // Deal with this!
355
        ex.printStackTrace();
356
      }
357
    finally
358
      {
359
        if (out != null)
360
          out.close();
361
      }
362
  }
363
 
364
  /**
365
   * Returns a DemoFactory that creates a HtmlDemo.
366
   *
367
   * @return a DemoFactory that creates a HtmlDemo
368
   */
369
  public static DemoFactory createDemoFactory()
370
  {
371
    return new DemoFactory()
372
    {
373
      public JComponent createDemo()
374
      {
375
        return new HtmlDemo();
376
      }
377
    };
378
  }
379
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.