SampleTree.java revision 2362
/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS 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 COPYRIGHT OWNER OR
* 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.
*/
/*
*/
/**
* A demo for illustrating how to do different things with JTree.
* The data that this displays is rather boring, that is each node will
* have 7 children that have random names based on the fonts. Each node
* is then drawn with that font and in a different color.
* While the data isn't interesting the example illustrates a number
* of things:
*
* For an example of dynamicaly loading children refer to DynamicTreeNode.
* classes of this class, AddAction, RemovAction, InsertAction and
* ReloadAction.
* For an example of creating your own cell renderer refer to
* SampleTreeCellRenderer.
* For an example of subclassing JTreeModel for editing refer to
* SampleTreeModel.
*
* @author Scott Violet
*/
public class SampleTree
{
/** Window for showing Tree. */
/** Tree used for the example. */
/** Tree model. */
protected DefaultTreeModel treeModel;
/**
* Constructs a new instance of SampleTree.
*/
public SampleTree() {
// Force SampleTree to come up in the Cross Platform L&F
try {
// If you want the System L&F instead, comment out the above line and
// uncomment the following:
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
/* Create the JTreeModel. */
/* Create the tree. */
/* Enable tool tips for the tree, without this tool tips will not
be picked up. */
/* Make the tree use an instance of SampleTreeCellRenderer for
drawing. */
/* Make tree ask for the height of each row. */
/* Put the Tree in a scroller. */
/* And show it. */
}
/** Constructs a JPanel containing check boxes for the different
* options that tree supports. */
private JPanel constructOptionsPanel() {
/* Create a set of radio buttons that dictate what selection should
be allowed in the tree. */
public boolean isEnabled() { return true; }
public void actionPerformed(ActionEvent e) {
}
});
public boolean isEnabled() { return true; }
public void actionPerformed(ActionEvent e) {
}
});
public boolean isEnabled() { return true; }
public void actionPerformed(ActionEvent e) {
}
});
button.setSelected(true);
// NOTE: This will be enabled in a future release.
// Create a label and combobox to determine how many clicks are
// needed to expand.
/*
JPanel clickPanel = new JPanel();
Object[] values = { "Never", new Integer(1),
new Integer(2), new Integer(3) };
final JComboBox clickCBox = new JComboBox(values);
clickPanel.setLayout(new FlowLayout());
clickPanel.add(new JLabel("Click count to expand:"));
clickCBox.setSelectedIndex(2);
clickCBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Object selItem = clickCBox.getSelectedItem();
if(selItem instanceof Integer)
tree.setToggleClickCount(((Integer)selItem).intValue());
else // Don't toggle
tree.setToggleClickCount(0);
}
});
clickPanel.add(clickCBox);
borderPane.add(clickPanel, BorderLayout.NORTH);
*/
return borderPane;
}
/** Construct a menu. */
private JMenuBar constructMenuBar() {
/* Good ol exit. */
public void actionPerformed(ActionEvent e) {
}});
/* Tree related stuff. */
return menuBar;
}
/**
* Returns the TreeNode instance that is selected in the tree.
* If nothing is selected, null is returned.
*/
protected DefaultMutableTreeNode getSelectedNode() {
return null;
}
/**
* Returns the selected TreePaths in the tree, may return null if
* nothing is selected.
*/
protected TreePath[] getSelectedPaths() {
return tree.getSelectionPaths();
}
}
/**
* AddAction is used to add a new item after the selected item.
*/
{
/** Number of nodes that have been added. */
public int addCount;
/**
* Messaged when the user clicks on the Add menu item.
* Determines the selection from the Tree and adds an item
* after that. If nothing is selected, an item is added to
* the root.
*/
public void actionPerformed(ActionEvent e) {
/* Determine where to create the new node. */
}
}
else
// new root
}
else {
int newIndex;
else
/* Let the treemodel know. */
}
}
} // End of SampleTree.AddAction
/**
* InsertAction is used to insert a new item before the selected item.
*/
{
/** Number of nodes that have been added. */
public int insertCount;
/**
* Messaged when the user clicks on the Insert menu item.
* Determines the selection from the Tree and inserts an item
* after that. If nothing is selected, an item is added to
* the root.
*/
public void actionPerformed(ActionEvent e) {
/* Determine where to create the new node. */
}
}
else
// new root
}
else {
int newIndex;
else
/* Let the treemodel know. */
}
}
} // End of SampleTree.InsertAction
/**
* ReloadAction is used to reload from the selected node. If nothing
* is selected, reload is not issued.
*/
{
/**
* Messaged when the user clicks on the Reload menu item.
* Determines the selection from the Tree and asks the treemodel
* to reload from that node.
*/
public void actionPerformed(ActionEvent e) {
}
} // End of SampleTree.ReloadAction
/**
* RemoveAction removes the selected node from the tree. If
* The root or nothing is selected nothing is removed.
*/
{
/**
* Removes the selected item as long as it isn't root.
*/
public void actionPerformed(ActionEvent e) {
// The remove process consists of the following steps:
// 1 - find the shallowest selected TreePath, the shallowest
// path is the path with the smallest number of path
// components.
// 2 - Find the siblings of this TreePath
// 3 - Remove from selected the TreePaths that are descendants
// of the paths that are going to be removed. They will
// be removed as a result of their ancestors being
// removed.
// 4 - continue until selected contains only null paths.
}
}
}
/**
* Removes the sibling TreePaths of <code>path</code>, that are
* located in <code>paths</code>.
*/
// Find the siblings
// Special case, set the root to null
}
}
else {
// Find the siblings of path.
// First pass, find paths with a parent TreePath of parent
}
}
// Second pass, remove any paths that are descendants of the
// paths that are going to be removed. These paths are
// implicitly removed as a result of removing the paths in
// toRemove
rCounter--) {
}
}
}
}
// Sort the siblings based on position in the model
if (rCount > 1) {
}
}
}
}
/**
* Returns the TreePath with the smallest path count in
* <code>paths</code>. Will return null if there is no non-null
* TreePath is <code>paths</code>.
*/
int shallowest = -1;
if (shallowest != -1) {
if (shallowest == 1) {
return shallowestPath;
}
}
}
else {
}
}
}
return shallowestPath;
}
/**
* An Comparator that bases the return value on the index of the
* passed in objects in the TreeModel.
* <p>
* This is actually rather expensive, it would be more efficient
* to extract the indices and then do the comparision.
*/
private class PositionComparator implements Comparator {
}
}
}
} // End of SampleTree.RemoveAction
/**
* ShowHandlesChangeListener implements the ChangeListener interface
* to toggle the state of showing the handles in the tree.
*/
{
public void stateChanged(ChangeEvent e) {
}
} // End of class SampleTree.ShowHandlesChangeListener
/**
* ShowRootChangeListener implements the ChangeListener interface
* to toggle the state of showing the root node in the tree.
*/
{
public void stateChanged(ChangeEvent e) {
}
} // End of class SampleTree.ShowRootChangeListener
/**
* TreeEditableChangeListener implements the ChangeListener interface
* to toggle between allowing editing and now allowing editing in
* the tree.
*/
{
public void stateChanged(ChangeEvent e) {
}
} // End of class SampleTree.TreeEditableChangeListener
new SampleTree();
}
}