package contrib; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.KeyStroke; import javax.swing.UIManager; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.io.StringReader; import java.util.Iterator; import java.util.List; import org.jdom.Attribute; import org.jdom.Element; /** * @author Phill Perryman * * Tests the functionality of the tree node */ public class DocumentViewer extends JFrame implements TreeSelectionListener { private JPanel jContentPane = new JPanel(); // an extended sax builder SAXBuilderx esb; // the tree that displays the document JTree tree; // A scroll pane for the tree JScrollPane treeScroller; // A split pane to hold the main view components JSplitPane mainSplitPane; // A panel to hold the info data JPanel infoPane = new JPanel(); // the text field for the namespace JTextField namespace = new JTextField(); // A panel to holf the attribute table JPanel attributePane = new JPanel(new BorderLayout()); // A text area to hold the element content JTextArea content = new JTextArea(); // Menus JMenu menuFile = new JMenu("File"); JMenuItem menuFileOpen = new JMenuItem(); // a descriptor class to provide user frieldly tree descriptions private class Descriptor implements ElementDescriptor { public String getDescription(Element e) { org.jdom.Attribute a = e.getAttribute("id"); if (a == null) { return "<" + e.getName() + ">"; } else { return "<" + e.getName() + " id='" + a.getValue() + "'>"; } } } /** * Constructor * */ public DocumentViewer() { super(); initialize(); } /** * Create a frame and place a test JTree in it */ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); System.exit(0); } DocumentViewer app = new DocumentViewer(); app.setVisible(true); } /** * This method initializes this * * @return void */ private void initialize() { jContentPane.setLayout(new java.awt.BorderLayout()); setContentPane(jContentPane); infoPane.setLayout(new java.awt.BorderLayout()); attributePane.setLayout(new java.awt.BorderLayout()); // create a simple xml string String xml = "" + "" + "" + "" + ""; // create a string reader StringReader sr = new StringReader(xml); // create a factory that will return the extended classes JDOMFactoryx factory = new JDOMFactoryx(); // now create a builder and assign the factory to it esb = new SAXBuilderx(); esb.setFactory(factory); // parse in the test document Documentx doc = null; try { // parse in the test document doc = (Documentx) esb.build(sr); } catch (Exception e) { e.printStackTrace(); } // set the frames operation setSize(800, 600); setTitle("Document Viewer"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set up the info pane infoPane.add(namespace, "North"); infoPane.add(attributePane, "Center"); infoPane.add(content, "South"); // get the root element so we can use it to build a tree Elementx root = doc.getRootElementx(); // set the descriptor to show the id attribute root.setDescriptor(new Descriptor()); // build a JTree, make sure it is read only and put it // into the content pane. tree = new JTree(root.getTreeNode()); tree.setEditable(false); tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); //Listen for when the selection changes. tree.addTreeSelectionListener(this); treeScroller = new JScrollPane(tree); mainSplitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, true, treeScroller, infoPane); jContentPane.add(mainSplitPane, "Center"); //set up the menus JMenuBar menuBar = new JMenuBar(); menuFile.add(menuFileOpen); menuFileOpen.setText("open"); menuFileOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); menuFileOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menuFileOpen(); } }); setJMenuBar(menuBar); menuBar.add(menuFile); } public void menuFileOpen() { File file; JFileChooser fc = new JFileChooser(); fc.addChoosableFileFilter(new FileExtensionFilter()); // Show open dialog; this method does not return until the dialog is closed int result = fc.showOpenDialog(this); if (result != JFileChooser.APPROVE_OPTION) return; file = fc.getSelectedFile(); if (file.exists() == false) { JOptionPane.showMessageDialog( this, "Error File does not exist", "File Error", JOptionPane.ERROR_MESSAGE); return; }; Documentx doc = null; try { // parse in the test document doc = (Documentx) esb.build(file); } catch (Exception e) { e.printStackTrace(); } // get the root element so we can use it to build a tree Elementx root = doc.getRootElementx(); // set the descriptor to show the id attribute root.setDescriptor(new Descriptor()); // build a JTree, and put it into the scroll pane. tree = new JTree(root.getTreeNode()); treeScroller.setViewportView(tree); treeScroller.validate(); } /** * The user has selected a different node in the tree so the * displayed data can change * * @param e TreeSelectionEvent */ public void valueChanged(TreeSelectionEvent e) { // get the new node DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) return; // the user object is a SelectionItem which tells us about // the actual item we need to act upon Elementx element = (Elementx) node.getUserObject(); namespace.setText(element.getNamespace().toString()); attributePane.removeAll(); List list = element.getAttributes(); JList jl = new JList(list.toArray()); attributePane.add(jl,"Center"); namespace.validate(); attributePane.validate(); } }