3840N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * Redistribution and use in source and binary forms, with or without 0N/A * modification, are permitted provided that the following conditions 0N/A * - Redistributions of source code must retain the above copyright 0N/A * notice, this list of conditions and the following disclaimer. 0N/A * - Redistributions in binary form must reproduce the above copyright 0N/A * notice, this list of conditions and the following disclaimer in the 0N/A * documentation and/or other materials provided with the distribution. 2362N/A * - Neither the name of Oracle nor the names of its 0N/A * contributors may be used to endorse or promote products derived 0N/A * from this software without specific prior written permission. 0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 4378N/A * This source code is provided to illustrate the usage of a given feature 4378N/A * or technique and has been deliberately simplified. Additional steps 4378N/A * required for a production-quality application, such as security checks, 4378N/A * input validation and proper error handling, might not be present in 3840N/A * A demo for illustrating how to do different things with JTree. 3840N/A * The data that this displays is rather boring, that is each node will 3840N/A * have 7 children that have random names based on the fonts. Each node 3840N/A * is then drawn with that font and in a different color. 3840N/A * While the data isn't interesting the example illustrates a number 3840N/A * For an example of dynamicaly loading children refer to DynamicTreeNode. 3840N/A * classes of this class, AddAction, RemovAction, InsertAction and 3840N/A * For an example of creating your own cell renderer refer to 3840N/A * For an example of subclassing JTreeModel for editing refer to 0N/A /** Window for showing Tree. */ 0N/A /** Tree used for the example. */ 3840N/A * Constructs a new instance of SampleTree. 3840N/A // Trying to set Nimbus look and feel 0N/A /* Create the JTreeModel. */ 0N/A /* Create the tree. */ 0N/A /* Enable tool tips for the tree, without this tool tips will not 0N/A /* Make the tree use an instance of SampleTreeCellRenderer for 0N/A /* Make tree ask for the height of each row. */ 0N/A /* Put the Tree in a scroller. */ 0N/A /** Constructs a JPanel containing check boxes for the different 3840N/A * options that tree supports. */ 0N/A /* Create a set of radio buttons that dictate what selection should 0N/A // NOTE: This will be enabled in a future release. 0N/A // Create a label and combobox to determine how many clicks are 0N/A // needed to expand. 0N/A JPanel clickPanel = new JPanel(); 0N/A Object[] values = { "Never", new Integer(1), 3840N/A new Integer(2), new Integer(3) }; 0N/A final JComboBox clickCBox = new JComboBox(values); 0N/A clickPanel.setLayout(new FlowLayout()); 0N/A clickPanel.add(new JLabel("Click count to expand:")); 0N/A clickCBox.setSelectedIndex(2); 0N/A clickCBox.addActionListener(new ActionListener() { 3840N/A public void actionPerformed(ActionEvent ae) { 3840N/A Object selItem = clickCBox.getSelectedItem(); 3840N/A if(selItem instanceof Integer) 3840N/A tree.setToggleClickCount(((Integer)selItem).intValue()); 3840N/A tree.setToggleClickCount(0); 0N/A clickPanel.add(clickCBox); 0N/A borderPane.add(clickPanel, BorderLayout.NORTH); 0N/A /** Construct a menu. */ 0N/A /* Tree related stuff. */ 3840N/A * Returns the TreeNode instance that is selected in the tree. 3840N/A * If nothing is selected, null is returned. 0N/A * Returns the selected TreePaths in the tree, may return null if 0N/A * nothing is selected. 3840N/A * AddAction is used to add a new item after the selected item. 0N/A /** Number of nodes that have been added. */ 3840N/A * Messaged when the user clicks on the Add menu item. 3840N/A * Determines the selection from the Tree and adds an item 3840N/A * after that. If nothing is selected, an item is added to 0N/A /* Determine where to create the new node. */ 0N/A /* Let the treemodel know. */ 0N/A }
// End of SampleTree.AddAction 3840N/A * InsertAction is used to insert a new item before the selected item. 0N/A /** Number of nodes that have been added. */ 3840N/A * Messaged when the user clicks on the Insert menu item. 3840N/A * Determines the selection from the Tree and inserts an item 3840N/A * after that. If nothing is selected, an item is added to 0N/A /* Determine where to create the new node. */ 0N/A /* Let the treemodel know. */ 0N/A }
// End of SampleTree.InsertAction 3840N/A * ReloadAction is used to reload from the selected node. If nothing 3840N/A * is selected, reload is not issued. 3840N/A * Messaged when the user clicks on the Reload menu item. 3840N/A * Determines the selection from the Tree and asks the treemodel 3840N/A * to reload from that node. 0N/A }
// End of SampleTree.ReloadAction 3840N/A * RemoveAction removes the selected node from the tree. If 3840N/A * The root or nothing is selected nothing is removed. 3840N/A * Removes the selected item as long as it isn't root. 0N/A // The remove process consists of the following steps: 0N/A // 1 - find the shallowest selected TreePath, the shallowest 0N/A // path is the path with the smallest number of path 0N/A // 2 - Find the siblings of this TreePath 0N/A // 3 - Remove from selected the TreePaths that are descendants 0N/A // of the paths that are going to be removed. They will 0N/A // be removed as a result of their ancestors being 0N/A // 4 - continue until selected contains only null paths. 0N/A * Removes the sibling TreePaths of <code>path</code>, that are 0N/A * located in <code>paths</code>. 0N/A // Find the siblings 0N/A // Special case, set the root to null 0N/A // Find the siblings of path. 0N/A // First pass, find paths with a parent TreePath of parent 0N/A // Second pass, remove any paths that are descendants of the 0N/A // paths that are going to be removed. These paths are 0N/A // implicitly removed as a result of removing the paths in 0N/A // Sort the siblings based on position in the model 0N/A * Returns the TreePath with the smallest path count in 0N/A * <code>paths</code>. Will return null if there is no non-null 0N/A * TreePath is <code>paths</code>. 0N/A * An Comparator that bases the return value on the index of the 0N/A * passed in objects in the TreeModel. 0N/A * This is actually rather expensive, it would be more efficient 0N/A * to extract the indices and then do the comparision. 0N/A }
// End of SampleTree.RemoveAction 3840N/A * ShowHandlesChangeListener implements the ChangeListener interface 3840N/A * to toggle the state of showing the handles in the tree. 0N/A }
// End of class SampleTree.ShowHandlesChangeListener 3840N/A * ShowRootChangeListener implements the ChangeListener interface 3840N/A * to toggle the state of showing the root node in the tree. 0N/A }
// End of class SampleTree.ShowRootChangeListener 3840N/A * TreeEditableChangeListener implements the ChangeListener interface 3840N/A * to toggle between allowing editing and now allowing editing in 0N/A }
// End of class SampleTree.TreeEditableChangeListener