KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JTreeOutputterDemo


1 /*--
2
3  Copyright (C) 2000 Brett McLaughlin & Jason Hunter. All rights reserved.
4
5  Redistribution and use in source and binary forms, with or without modifica-
6  tion, are permitted provided that the following conditions are met:
7
8  1. Redistributions of source code must retain the above copyright notice,
9     this list of conditions, and the following disclaimer.
10
11  2. Redistributions in binary form must reproduce the above copyright notice,
12     this list of conditions, the disclaimer that follows these conditions,
13     and/or other materials provided with the distribution.
14
15  3. The names "JDOM" and "Java Document Object Model" must not be used to
16     endorse or promote products derived from this software without prior
17     written permission. For written permission, please contact
18     license@jdom.org.
19
20  4. Products derived from this software may not be called "JDOM", nor may
21     "JDOM" appear in their name, without prior written permission from the
22     JDOM Project Management (pm@jdom.org).
23
24  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
25  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  JDOM PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35  This software consists of voluntary contributions made by many individuals
36  on behalf of the Java Document Object Model Project and was originally
37  created by Brett McLaughlin <brett@jdom.org> and
38  Jason Hunter <jhunter@jdom.org>. For more information on the JDOM
39  Project, please see <http://www.jdom.org/>.
40
41  */

42
43 import java.awt.*;
44 import java.awt.event.*;
45 import java.net.URL JavaDoc;
46 import javax.swing.*;
47 import javax.swing.tree.*;
48 import javax.swing.filechooser.*;
49
50 import org.jdom.Document;
51 import org.jdom.input.SAXBuilder;
52 import org.jdom.contrib.output.JTreeOutputter;
53
54 /**
55  * <p><code>JTreeOutputterDemo</code> demonstrates how to
56  * build a JTree (in Swing) from a JDOM <code>{@link Document}</code>.
57  * </p>
58  *
59  * @author Jon Baer
60  * @author Brett McLaughlin
61  * @version 1.0
62  */

63 public class JTreeOutputterDemo implements ActionListener {
64
65     public JFrame frame;
66     public Document doc;
67     public DefaultMutableTreeNode root;
68     public JTreeOutputter outputter;
69     public JTree tree;
70     public JScrollPane scrollPane;
71     public SAXBuilder saxBuilder;
72     public JMenuItem openFile, openURL, openSQL, exitMenu;
73     public JButton openButton, reloadButton, exitButton, aboutButton;
74
75     public static void main(String JavaDoc[] args) {
76         new JTreeOutputterDemo();
77     }
78
79     public JTreeOutputterDemo() {
80
81         frame = new JFrame(" JDOM Viewer 1.0");
82         JMenuBar menuBar = new JMenuBar();
83         JMenu menu = new JMenu("File");
84         openFile = new JMenuItem("Open XML File");
85         openFile.addActionListener(this);
86         openURL = new JMenuItem("Open URL Stream");
87         openURL.addActionListener(this);
88         openSQL = new JMenuItem("Query Database");
89         openSQL.addActionListener(this);
90         exitMenu = new JMenuItem("Exit");
91         exitMenu.addActionListener(this);
92         menu.add(openFile);
93         menu.add(openURL);
94         menu.add(new JSeparator());
95         menu.add(openSQL);
96         menu.add(new JSeparator());
97         menu.add(exitMenu);
98         menuBar.add(menu);
99         frame.setJMenuBar(menuBar);
100
101         openButton = new JButton("Open");
102         openButton.addActionListener(this);
103         reloadButton = new JButton("Reload");
104         reloadButton.addActionListener(this);
105         exitButton = new JButton("Exit");
106         exitButton.addActionListener(this);
107         aboutButton = new JButton("About");
108         aboutButton.addActionListener(this);
109         JPanel buttonPanel = new JPanel();
110         buttonPanel.add(openButton);
111         buttonPanel.add(reloadButton);
112         buttonPanel.add(exitButton);
113         buttonPanel.add(aboutButton);
114
115         root = new DefaultMutableTreeNode("JDOM");
116
117         outputter = new JTreeOutputter(true);
118
119         tree = new JTree(root);
120
121         saxBuilder = new SAXBuilder();
122
123         scrollPane = new JScrollPane();
124         scrollPane.getViewport().add(tree);
125
126         frame.setSize(400,400);
127         frame.getContentPane().setLayout(new BorderLayout());
128         frame.getContentPane().add("Center", scrollPane);
129         frame.getContentPane().add("South", buttonPanel);
130
131         frame.addWindowListener(new WindowAdapter() {
132             public void windowClosing(WindowEvent evt) {
133                 System.exit(0);
134             }
135         });
136
137         frame.setVisible(true);
138
139     }
140
141     public void actionPerformed(ActionEvent e) {
142         // Open File
143
if (e.getSource() == openButton || e.getSource() == openFile) {
144             doFile();
145         }
146         // Open URL
147
if (e.getSource() == openURL) {
148             doURL();
149         }
150         // Query Database
151
if (e.getSource() == openSQL) {
152             doSQL();
153         }
154         // Exit
155
if (e.getSource() == exitButton || e.getSource() == exitMenu) {
156             System.exit(0);
157         }
158     }
159
160     public void doFile() {
161                 JFileChooser fc = new JFileChooser();
162                 fc.setDialogTitle("Select an XML File");
163                 int returnVal = fc.showDialog(frame, "Load XML");
164                 if (returnVal == 0) {
165                         try {
166                                 doc = saxBuilder.build(fc.getSelectedFile());
167                         } catch (Exception JavaDoc e) {e.printStackTrace();}
168                         outputter.output(doc, root);
169                 }
170     }
171
172     public void doURL() {
173         URLDialog urlDialog = new URLDialog(frame);
174         if (urlDialog.getURL() != null) {
175             try {
176                 doc = saxBuilder.build(new URL JavaDoc(urlDialog.getURL()));
177             } catch (Exception JavaDoc e) {
178                 e.printStackTrace();
179             }
180             outputter.output(doc, root);
181         }
182     }
183
184     public void doSQL() {
185     }
186
187 }
188
189 class URLDialog extends JDialog implements ActionListener {
190
191     public String JavaDoc url;
192     public JTextField urlField;
193     public JButton okButton, cancelButton;
194
195     public URLDialog(Frame frame) {
196         super(frame, "Enter A URL", true);
197         urlField = new JTextField("http://");
198         JPanel buttonPanel = new JPanel();
199         okButton = new JButton("OK");
200         okButton.addActionListener(this);
201         cancelButton = new JButton("Cancel");
202         cancelButton.addActionListener(this);
203         buttonPanel.add(okButton);
204         buttonPanel.add(cancelButton);
205         getContentPane().setLayout(new BorderLayout());
206         getContentPane().add("North", urlField);
207         getContentPane().add("South", buttonPanel);
208         setSize(400, 150);
209         setVisible(true);
210     }
211
212     public String JavaDoc getURL() {
213         return this.url;
214     }
215
216     public void actionPerformed(ActionEvent e) {
217         if (e.getSource() == okButton) {
218             this.url = urlField.getText(); setVisible(false);
219         }
220         if (e.getSource() == cancelButton) {
221             setVisible(false);
222         }
223     }
224
225 }
226
Popular Tags