Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* www.javagl.de - Common - UI
*
* Copyright (c) 2013-2015 Marco Hutter - http://www.javagl.de
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package de.javagl.common.ui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EventObject;
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Function;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import de.javagl.common.ui.tree.renderer.GenericTreeCellRenderer;
/**
* Utility methods related to trees
*/
public class JTrees
{
/**
* Expand all rows of the given tree.
*
* Note that for large trees, {@link #expandAllFixedHeight(JTree)} may
* be significantly faster.
*
* @param tree The tree
*/
public static void expandAll(JTree tree)
{
int r = 0;
while (r < tree.getRowCount())
{
tree.expandRow(r);
r++;
}
}
/**
* Expand all rows of the given tree, assuming a fixed height for
* the rows.
*
* @param tree The tree
*/
public static void expandAllFixedHeight(JTree tree)
{
// Determine a suitable row height for the tree, based on the
// size of the component that is used for rendering the root
TreeCellRenderer cellRenderer = tree.getCellRenderer();
Component treeCellRendererComponent =
cellRenderer.getTreeCellRendererComponent(
tree, tree.getModel().getRoot(), false, false, false, 1, false);
int rowHeight = treeCellRendererComponent.getPreferredSize().height + 2;
tree.setRowHeight(rowHeight);
// Temporarily remove all listeners that would otherwise
// be flooded with TreeExpansionEvents
List expansionListeners =
Arrays.asList(tree.getTreeExpansionListeners());
for (TreeExpansionListener expansionListener : expansionListeners)
{
tree.removeTreeExpansionListener(expansionListener);
}
// Recursively expand all nodes of the tree
TreePath rootPath = new TreePath(tree.getModel().getRoot());
expandAllRecursively(tree, rootPath);
// Restore the listeners that the tree originally had
for (TreeExpansionListener expansionListener : expansionListeners)
{
tree.addTreeExpansionListener(expansionListener);
}
// Trigger an update for the TreeExpansionListeners
tree.collapsePath(rootPath);
tree.expandPath(rootPath);
}
/**
* Recursively expand all paths in the given tree, starting with the
* given path
*
* @param tree The tree
* @param treePath The current tree path
*/
private static void expandAllRecursively(JTree tree, TreePath treePath)
{
TreeModel model = tree.getModel();
Object lastPathComponent = treePath.getLastPathComponent();
int childCount = model.getChildCount(lastPathComponent);
if (childCount == 0)
{
return;
}
tree.expandPath(treePath);
for (int i=0; i 0)
{
class LocalTreePath extends TreePath
{
private static final long serialVersionUID = 0;
public LocalTreePath(
TreePath parent, Object lastPathComponent)
{
super(parent, lastPathComponent);
}
}
TreePath nextTreePath = new LocalTreePath(treePath, child);
expandAllRecursively(tree, nextTreePath);
}
}
}
/**
* Collapse all rows of the given tree
*
* @param tree The tree
* @param omitRoot Whether the root node should not be collapsed
*/
public static void collapseAll(JTree tree, boolean omitRoot)
{
int rows = tree.getRowCount();
int limit = (omitRoot ? 1 : 0);
for (int i = rows - 1; i >= limit; i--)
{
tree.collapseRow(i);
}
}
/**
* Count the number of nodes in the given tree model
*
* @param treeModel The tree model
* @return The number of nodes
*/
public static int countNodes(TreeModel treeModel)
{
return countNodes(treeModel, treeModel.getRoot());
}
/**
* Recursively count the number of nodes in the given tree model,
* starting with the given node
*
* @param treeModel The tree model
* @param node The node
* @return The number of nodes
*/
private static int countNodes(TreeModel treeModel, Object node)
{
int sum = 1;
int n = treeModel.getChildCount(node);
for (int i=0; inull if no matching node is found.
*
* @param treeModel The tree model
* @param userObject The user object
* @return The node with the given user object, or null
*/
public static DefaultMutableTreeNode findNode(
TreeModel treeModel, Object userObject)
{
return findNode(treeModel, treeModel.getRoot(), userObject);
}
/**
* Returns the node with the given user object in the tree that is
* rooted at the given node in the given model. This assumes that
* the user object is stored in a DefaultMutableTreeNode.
* Returns null if no matching node is found.
*
* @param treeModel The tree model
* @param node The root node
* @param userObject The user object
* @return The node with the given user object, or null
*/
private static DefaultMutableTreeNode findNode(
TreeModel treeModel, Object node, Object userObject)
{
if (node instanceof DefaultMutableTreeNode)
{
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)node;
Object object = treeNode.getUserObject();
if ((object == null && userObject == null) ||
(object != null && object.equals(userObject)))
{
return treeNode;
}
}
int n = treeModel.getChildCount(node);
for (int i=0; i getChildren(TreeModel treeModel, Object node)
{
List