1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/*--
Copyright (C) 2000 Brett McLaughlin & Jason Hunter. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions, the disclaimer that follows these conditions,
and/or other materials provided with the distribution.
3. The names "JDOM" and "Java Document Object Model" must not be used to
endorse or promote products derived from this software without prior
written permission. For written permission, please contact
license@jdom.org.
4. Products derived from this software may not be called "JDOM", nor may
"JDOM" appear in their name, without prior written permission from the
JDOM Project Management (pm@jdom.org).
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
JDOM PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Java Document Object Model Project and was originally
created by Brett McLaughlin <brett@jdom.org> and
Jason Hunter <jhunter@jdom.org>. For more information on the JDOM
Project, please see <http://www.jdom.org/>.
*/
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.tree.*;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.contrib.output.JTreeOutputter;
/**
* <p><code>JTreeOutputterDemo</code> demonstrates how to
* build a JTree (in Swing) from a JDOM <code>{@link Document}</code>.
* </p>
*
* @author Jon Baer
* @author Brett McLaughlin
* @version 1.0
*/
@SuppressWarnings("javadoc")
public class JTreeOutputterDemo implements ActionListener {
public JFrame frame;
public Document doc;
public DefaultMutableTreeNode root;
public JTreeOutputter outputter;
public JTree tree;
public JScrollPane scrollPane;
public SAXBuilder saxBuilder;
public JMenuItem openFile, openURL, openSQL, exitMenu;
public JButton openButton, reloadButton, exitButton, aboutButton;
public static void main(String[] args) {
new JTreeOutputterDemo();
}
public JTreeOutputterDemo() {
frame = new JFrame(" JDOM Viewer 1.0");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
openFile = new JMenuItem("Open XML File");
openFile.addActionListener(this);
openURL = new JMenuItem("Open URL Stream");
openURL.addActionListener(this);
openSQL = new JMenuItem("Query Database");
openSQL.addActionListener(this);
exitMenu = new JMenuItem("Exit");
exitMenu.addActionListener(this);
menu.add(openFile);
menu.add(openURL);
menu.add(new JSeparator());
menu.add(openSQL);
menu.add(new JSeparator());
menu.add(exitMenu);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
openButton = new JButton("Open");
openButton.addActionListener(this);
reloadButton = new JButton("Reload");
reloadButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
aboutButton = new JButton("About");
aboutButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(reloadButton);
buttonPanel.add(exitButton);
buttonPanel.add(aboutButton);
root = new DefaultMutableTreeNode("JDOM");
outputter = new JTreeOutputter(true);
tree = new JTree(root);
saxBuilder = new SAXBuilder();
scrollPane = new JScrollPane();
scrollPane.getViewport().add(tree);
frame.setSize(400,400);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add("Center", scrollPane);
frame.getContentPane().add("South", buttonPanel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// Open File
if (e.getSource() == openButton || e.getSource() == openFile) {
doFile();
}
// Open URL
if (e.getSource() == openURL) {
doURL();
}
// Query Database
if (e.getSource() == openSQL) {
doSQL();
}
// Exit
if (e.getSource() == exitButton || e.getSource() == exitMenu) {
System.exit(0);
}
}
public void doFile() {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Select an XML File");
int returnVal = fc.showDialog(frame, "Load XML");
if (returnVal == 0) {
try {
doc = saxBuilder.build(fc.getSelectedFile());
} catch (Exception e) {e.printStackTrace();}
outputter.output(doc, root);
}
}
public void doURL() {
URLDialog urlDialog = new URLDialog(frame);
if (urlDialog.getURL() != null) {
try {
doc = saxBuilder.build(new URL(urlDialog.getURL()));
} catch (Exception e) {
e.printStackTrace();
}
outputter.output(doc, root);
}
}
public void doSQL() {
// do nothing
}
}
class URLDialog extends JDialog implements ActionListener {
/**
* Default.
*/
private static final long serialVersionUID = 1L;
public String url;
public JTextField urlField;
public JButton okButton, cancelButton;
public URLDialog(Frame frame) {
super(frame, "Enter A URL", true);
urlField = new JTextField("http://");
JPanel buttonPanel = new JPanel();
okButton = new JButton("OK");
okButton.addActionListener(this);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", urlField);
getContentPane().add("South", buttonPanel);
setSize(400, 150);
setVisible(true);
}
public String getURL() {
return this.url;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
this.url = urlField.getText(); setVisible(false);
}
if (e.getSource() == cancelButton) {
setVisible(false);
}
}
}
|