Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/yaftp/utils/SwingHtmlTest.java


1    /**
2    *
3    * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program 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
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  
21  package com.yaftp.utils;
22  
23  
24  import javax.swing.*;
25  import javax.swing.text.*;
26  import java.awt.event.*;
27  import java.awt.*;
28  
29  /**
30    TestBrowser.java
31    A test bed for the JEditorPane and a custom editor kit.
32    This extremely simple browser has a text field for typing in
33    new urls, a JEditorPane to display the HTML page, and a status
34    bar to display the contents of hyperlinks the mouse passes over.
35  
36    @Author Jean-Yves MENGANT
37    CopyRights Jean-Yves MENGANT 1998,1999,2000
38  
39  */
40  
41  public class SwingHtmlTest extends JFrame {
42  
43    public SwingHtmlTest(String startingUrl) {
44      // Ok, first just get a screen up and visible, with an appropriate
45      // handler in place for the kill window command
46      super("Test Pane");
47      setSize(400,300);
48      addWindowListener(new WindowAdapter() {
49        public void windowClosing(WindowEvent we) {
50    we.getWindow().setVisible(false);
51    System.exit(0);
52        }
53      });
54  
55      // Now set up our basic screen components, the editor pane, the
56      // text field for URLs, and the label for status and link information
57      JPanel urlPanel = new JPanel();
58      urlPanel.setLayout(new BorderLayout());
59      JTextField urlField = new JTextField(startingUrl);
60      urlPanel.add(new JLabel("Site: "), BorderLayout.WEST);
61      urlPanel.add(urlField, BorderLayout.CENTER);
62      final JLabel statusBar = new JLabel(" ");
63  
64      // Here's the editor pane configuration.  It's important to make
65      // the "setEditable(false)" call, otherwise our hyperlinks won't
66      // work.  (If the text is editable, then clicking on a hyperlink
67      // simply means that you want to change the text...not follow the
68      // link.)
69      final JEditorPane jep = new JEditorPane();
70      jep.setEditable(false);
71  
72      // Here's where we force the pane to use our new editor kit
73      jep.setEditorKitForContentType("text/html", new SwingHtmlEditorKit());
74      try {
75        jep.setPage(startingUrl);
76      }
77      catch(Exception e) {
78        statusBar.setText("Could not open starting page.  Using a blank.");
79      }
80      JScrollPane jsp = new JScrollPane(jep);
81  
82      // and get the GUI components onto our content pane
83      getContentPane().add(jsp, BorderLayout.CENTER);
84      getContentPane().add(urlPanel, BorderLayout.NORTH);
85      getContentPane().add(statusBar, BorderLayout.SOUTH);
86  
87      // and last but not least, hook up our event handlers
88      urlField.addActionListener(new ActionListener() {
89        public void actionPerformed(ActionEvent ae) {
90    try {
91      jep.setPage(ae.getActionCommand());
92    }
93    catch(Exception e) {
94      statusBar.setText("Could not open starting page.  Using a blank.");
95    }
96        }
97      });
98      jep.addHyperlinkListener(new SwingHtmlLinkListener(jep, urlField, statusBar));
99    }
100 
101   public static void main(String args[]) {
102     (new SwingHtmlTest(args.length > 0
103          ? args[0]
104          : "file:///c:/tmp/test.html")).setVisible(true);
105   }
106 }
107