1 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.net.URL ; 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 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 [] 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 if (e.getSource() == openButton || e.getSource() == openFile) { 144 doFile(); 145 } 146 if (e.getSource() == openURL) { 148 doURL(); 149 } 150 if (e.getSource() == openSQL) { 152 doSQL(); 153 } 154 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 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 (urlDialog.getURL())); 177 } catch (Exception 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 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 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 |