下面列出了javax.swing.tree.DefaultMutableTreeNode#isRoot ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (!node.isRoot()) {
try {
SelectCommand command = CommandProcessor.getInstance().getCommand(SelectCommand.class);
command.setElement((GElement) node.getUserObject());
CommandProcessor.getInstance().excuteCommand(command);
} catch (Exception ex) {
Logger.getLogger(ElementSelectionListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
final JTree temp = (JTree) e.getSource();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
temp.updateUI();
}
});
}
}
public void nodeSelected(DefaultMutableTreeNode node, TreePath path) {
Dasa dasa = (Dasa) node.getUserObject();
boolean isRoot = node.isRoot();
if (node.isLeaf()){
updateDasaTable(dasa.getParent(), isRoot);
}else{
updateDasaTable(dasa, isRoot);
}
if (!isRoot && node.isLeaf()){
setRowSelected(node.getParent(), node);
}
String title;
if (isRoot){
title = DisplayStrings.VIM_DASA_STR.toString();
}else{
title = path.getPathComponent(1).toString() + " " + DisplayStrings.DASA_STR.toString();
}
dasaTitle.setText(title);
}
public void valueChanged(TreeSelectionEvent e) {
JTree tree = (JTree) e.getSource();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node != null){
boolean isRoot = node.isRoot();
Dasa dasa = (Dasa) node.getUserObject();
//Add sub dasas only if not present already.
if (!isRoot && node.isLeaf()){
for(Dasa d : dasa.subDasas()){
node.add(new DefaultMutableTreeNode(d, true));
}
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
model.reload(node);
}
handler.nodeSelected(node, e.getNewLeadSelectionPath());
}
//tree.collapsePath(e.getOldLeadSelectionPath());
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
renderer.getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, hasFocus);
if (value != null) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode) value;
Dasa d = (Dasa)(n.getUserObject());
if (!n.isRoot()){
renderer.setToolTipText(d.getStartDate());
}else{
renderer.setToolTipText("");
}
if (d.isRunning() && !selected){
renderer.setForeground(Color.RED);
}
}
return renderer;
}
/**
* Get the list of selected nodes in depth-first traversal order.
* @param handler the selection handler.
* @param currentNode the node current being explored.
* @param treeNodes the selected tree nodes.
*/
private void exploreTreeSelection(final SelectionHandler handler, final DefaultMutableTreeNode currentNode, final List<DefaultMutableTreeNode> treeNodes) {
if (!currentNode.isRoot()) {
final AbstractComponent<?> data = (AbstractComponent<?>) currentNode.getUserObject();
if (handler.isSelected(data.getUuid())) treeNodes.add(currentNode);
}
for (int i=0; i<currentNode.getChildCount(); i++) exploreTreeSelection(handler, (DefaultMutableTreeNode) currentNode.getChildAt(i), treeNodes);
}
@Override
public boolean isEnableForComponent(Component invoker) {
if (invoker.getName() != null && "treeAlert".equals(invoker.getName())) {
JTree tree = (JTree) invoker;
if (tree.getLastSelectedPathComponent() != null) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
this.treeAlert = tree;
if (!node.isRoot()) {
return true;
}
}
}
return false;
}
public static void collapseFirstHierarchy(JTree tree) {
TreeModel model = tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
// // Java 9:
// Collections.list(root.breadthFirstEnumeration()).stream()
// .filter(DefaultMutableTreeNode.class::isInstance)
// .map(DefaultMutableTreeNode.class::cast)
// .takeWhile(node -> node.getLevel() <= 1)
// .dropWhile(DefaultMutableTreeNode::isRoot)
// .dropWhile(DefaultMutableTreeNode::isLeaf)
// .map(DefaultMutableTreeNode::getPath)
// .map(TreePath::new)
// .forEach(tree::collapsePath);
// Java 9: Enumeration<TreeNode> e = root.breadthFirstEnumeration();
Enumeration<?> e = root.breadthFirstEnumeration();
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
boolean isOverFirstLevel = node.getLevel() > 1;
if (isOverFirstLevel) { // Collapse only nodes in the first hierarchy
return;
} else if (node.isLeaf() || node.isRoot()) {
continue;
}
collapseNode(tree, node);
}
}
/**
*
* @param root
* @return boolean bad leaves were returned
*/
private boolean removeBadLeaves(DefaultMutableTreeNode root) {
// no bad leaves yet
boolean badLeaves = false;
// reference first leaf
DefaultMutableTreeNode leaf = root.getFirstLeaf();
// if leaf is root then its the only node
if (leaf.isRoot())
return false;
int leafCount = root.getLeafCount(); // this get method changes if in for loop so have to define outside of it
for (int i = 0; i < leafCount; i++) {
DefaultMutableTreeNode nextLeaf = leaf.getNextLeaf();
// if it does not start with the text then snip it off its parent
if (!leaf.getUserObject().toString().startsWith(textToMatch)) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();
if (parent != null)
parent.remove(leaf);
badLeaves = true;
}
leaf = nextLeaf;
}
return badLeaves;
}
/**
*
* @param root
* @return boolean bad leaves were returned
*/
private boolean removeBadLeaves(DefaultMutableTreeNode root) {
// no bad leaves yet
boolean badLeaves = false;
// reference first leaf
DefaultMutableTreeNode leaf = root.getFirstLeaf();
// if leaf is root then its the only node
if (leaf.isRoot())
return false;
int leafCount = root.getLeafCount(); // this get method changes if in for loop so have to define outside of it
for (int i = 0; i < leafCount; i++) {
DefaultMutableTreeNode nextLeaf = leaf.getNextLeaf();
// if it does not start with the text then snip it off its parent
if (!filter.accepts(filterText, leaf.getUserObject().toString())) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();
if (parent != null)
parent.remove(leaf);
badLeaves = true;
}
leaf = nextLeaf;
}
return badLeaves;
}
/**
* Recursively expand all non-leaf nodes in the specified table tree.
* @param tableTree the table tree that renders the nodes.
* @param node the root node to expand.
*/
public static void expand(final JPPFTableTree tableTree, final DefaultMutableTreeNode node) {
if (node.isLeaf()) return;
if (!node.isRoot()) tableTree.expand(node);
for (int i=0; i<node.getChildCount(); i++) expand(tableTree, (DefaultMutableTreeNode) node.getChildAt(i));
}