Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xmltree » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.samples.xmltree;
   17   
   18   import org.apache.xmlbeans.XmlObject;
   19   import org.apache.xmlbeans.XmlCursor;
   20   import org.apache.xmlbeans.XmlOptions;
   21   
   22   import javax.swing;
   23   import java.awt;
   24   import java.awt.event.MouseEvent;
   25   import java.awt.event.MouseListener;
   26   import java.io.File;
   27   
   28   /**
   29    * Provides a frame within which to display an instance of the {@link XmlTree},
   30    * along with a pane within which to display the XML corresponding to a node
   31    * selected in the tree. The tree's data is managed by the {@link XmlModel}and
   32    * {@link XmlEntry}classes, along with XmlTree. The XmlTreeFrame class is
   33    * merely a container to show the tree in use.
   34    */
   35   final class XmlTreeFrame extends JFrame
   36   {
   37       // Variables for UI components.
   38       private XmlTree treeXmlTree;
   39   
   40       private JButton btnRefresh;
   41   
   42       private JPanel pnlContent;
   43   
   44       private JPanel pnlSelectionPanel;
   45   
   46       private JPanel pnlTree;
   47   
   48       private JScrollPane scrContent;
   49   
   50       private JScrollPane scrTree;
   51   
   52       private JSplitPane splTreeContent;
   53   
   54       private JTextField txtFileName;
   55   
   56       private JTextPane txtpnlContent;
   57   
   58       /**
   59        * Constructs the frame with an XML file to use for the tree.
   60        * 
   61        * @param xmlFile The file containing XML that the tree should represent.
   62        */
   63       public XmlTreeFrame(File xmlFile)
   64       {
   65           initComponents(xmlFile);
   66       }
   67   
   68       /**
   69        * Initializes UI components, setting properties and adding event listeners.
   70        * 
   71        * @param xmlFile The XML file to be represented by the tree.
   72        */
   73       private void initComponents(File xmlFile)
   74       {
   75           // Set properties for this frame.
   76           getContentPane().setLayout(new GridBagLayout());
   77           setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   78           setTitle("XML Tree View");
   79           setName("frmXmlTreeView");
   80   
   81           // Create the top panel that will contain text box and refresh button.
   82           pnlSelectionPanel = new JPanel();
   83           pnlSelectionPanel.setLayout(new GridBagLayout());
   84   
   85           // Create the text box to display the XML file path.
   86           txtFileName = new JTextField();
   87           GridBagConstraints gridBagConstraints;
   88           gridBagConstraints = new GridBagConstraints();
   89           gridBagConstraints.gridx = 0;
   90           gridBagConstraints.gridy = 0;
   91           gridBagConstraints.anchor = GridBagConstraints.WEST;
   92           gridBagConstraints.insets = new Insets(4, 4, 0, 4);
   93           gridBagConstraints.weightx = 1;
   94           gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
   95           txtFileName.setText(xmlFile.getAbsolutePath());
   96           pnlSelectionPanel.add(txtFileName, gridBagConstraints);
   97   
   98           // Create the refresh button.
   99           btnRefresh = new JButton();
  100           btnRefresh.setText("Refresh");
  101           gridBagConstraints = new GridBagConstraints();
  102           gridBagConstraints.gridwidth = 1;
  103           gridBagConstraints.gridx = 1;
  104           gridBagConstraints.gridy = 0;
  105           gridBagConstraints.anchor = GridBagConstraints.EAST;
  106           pnlSelectionPanel.add(btnRefresh, gridBagConstraints);
  107   
  108           // Add the selection panel to this frame.
  109           gridBagConstraints = new GridBagConstraints();
  110           gridBagConstraints.gridx = 0;
  111           gridBagConstraints.gridy = 0;
  112           gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
  113           gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
  114           gridBagConstraints.weightx = 1;
  115           gridBagConstraints.weighty = 0;
  116           getContentPane().add(pnlSelectionPanel, gridBagConstraints);
  117   
  118           // Create the split plane that separates the tree and the content panes.
  119           splTreeContent = new JSplitPane();
  120   
  121           // Create the components for the left side of the split pane:
  122           // the panel, scrolling panel, and the XML tree it will contain.
  123           pnlTree = new JPanel();
  124           scrTree = new JScrollPane();
  125           treeXmlTree = new XmlTree(xmlFile);
  126           scrTree.setViewportView(treeXmlTree);
  127           pnlTree.setLayout(new GridBagLayout());
  128           gridBagConstraints = new GridBagConstraints();
  129           gridBagConstraints.fill = GridBagConstraints.BOTH;
  130           gridBagConstraints.anchor = GridBagConstraints.NORTH;
  131           gridBagConstraints.weightx = 1.0;
  132           gridBagConstraints.weighty = 1.0;
  133           pnlTree.add(scrTree, gridBagConstraints);
  134   
  135           // Put the tree panel in the left side of the split pane.
  136           splTreeContent.setLeftComponent(pnlTree);
  137   
  138           // Create the components for the left side of the split pane:
  139           // the panel, scrolling panel, and the XML tree it will contain.
  140           pnlContent = new JPanel();
  141           scrContent = new JScrollPane();
  142           txtpnlContent = new JTextPane();
  143           scrContent.setViewportView(txtpnlContent);
  144           pnlContent.setLayout(new GridBagLayout());
  145           gridBagConstraints = new GridBagConstraints();
  146           gridBagConstraints.fill = GridBagConstraints.BOTH;
  147           gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
  148           gridBagConstraints.weightx = 1;
  149           gridBagConstraints.weighty = 1;
  150           pnlContent.add(scrContent, gridBagConstraints);
  151   
  152           // Put the content panel in the right side of the split pane.
  153           splTreeContent.setRightComponent(pnlContent);
  154   
  155           // Set the rest of the split pane's properties,
  156           splTreeContent.setDividerLocation(170);
  157           gridBagConstraints = new GridBagConstraints();
  158           gridBagConstraints.gridx = 0;
  159           gridBagConstraints.gridy = 1;
  160           gridBagConstraints.weightx = 1;
  161           gridBagConstraints.weighty = 1;
  162           gridBagConstraints.fill = GridBagConstraints.BOTH;
  163           gridBagConstraints.gridheight = GridBagConstraints.REMAINDER;
  164           gridBagConstraints.anchor = GridBagConstraints.NORTH;
  165           getContentPane().add(splTreeContent, gridBagConstraints);
  166   
  167           // Add a listener to get mouse clicks on the tree nodes.
  168           treeXmlTree.addMouseListener(new MouseListener()
  169           {
  170               public void mouseClicked(MouseEvent event)
  171               {
  172                   if (event.getClickCount() == 1)
  173                   {
  174                       XmlEntry selection = (XmlEntry) treeXmlTree
  175                               .getLastSelectedPathComponent();
  176                       // selection might be null if the user clicked one of the
  177                       // expandy/collapsy things without selecting a node.
  178                       if (selection == null)
  179                       {
  180                           return;
  181                       }
  182                       // Get the pretty-printed XML text and put it in the
  183                       // window on the right.
  184                       XmlObject node = selection.getXml();
  185                       XmlCursor nodeCursor = node.newCursor();
  186                       XmlOptions options = new XmlOptions();
  187                       options.setSavePrettyPrint();
  188                       options.setSavePrettyPrintIndent(4);
  189                       String xmlString = nodeCursor.xmlText(options);
  190                       txtpnlContent.setText(xmlString);
  191                   }
  192               }
  193   
  194               // Don't respond to these events.
  195               public void mouseEntered(MouseEvent event)
  196               {}
  197   
  198               public void mouseExited(MouseEvent event)
  199               {}
  200   
  201               public void mousePressed(MouseEvent event)
  202               {}
  203   
  204               public void mouseReleased(MouseEvent event)
  205               {}
  206           });
  207   
  208           // Add a listener to get mouse clicks on the Refresh button.
  209           btnRefresh.addMouseListener(new MouseListener()
  210           {
  211               public void mouseClicked(MouseEvent event)
  212               {
  213                   // Get the text from the file path box and make a file from it.
  214                   String filePath = txtFileName.getText();
  215                   File xmlFile = new File(filePath);
  216   
  217                   // If the path points to a file, build the tree all over again.
  218                   if (xmlFile.exists())
  219                   {
  220                       treeXmlTree.setXmlFile(xmlFile);
  221                       txtpnlContent.setText("");
  222                   } else
  223                   {
  224                       JOptionPane.showMessageDialog(null,
  225                               "The path you gave appears "
  226                                       + "not to point to a file.",
  227                               "XmlTree Message", JOptionPane.ERROR_MESSAGE);
  228                   }
  229   
  230               }
  231   
  232               // Don't respond to these events.
  233               public void mouseEntered(MouseEvent event)
  234               {}
  235   
  236               public void mouseExited(MouseEvent event)
  237               {}
  238   
  239               public void mousePressed(MouseEvent event)
  240               {}
  241   
  242               public void mouseReleased(MouseEvent event)
  243               {}
  244           });
  245   
  246           // Size all the components to their preferred size.
  247           pack();
  248           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  249           this.setSize(600, 640);
  250           this.setVisible(true);
  251       }
  252   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » xmltree » [javadoc | source]