1 13 package org.jahia.utils; 14 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.swing.JTree ; 20 import javax.swing.tree.DefaultMutableTreeNode ; 21 import javax.swing.tree.TreeNode ; 22 import javax.swing.tree.TreePath ; 23 24 25 35 public class GUITreeTools { 36 37 46 public static ArrayList getFlatTree(JTree tree, DefaultMutableTreeNode node){ 47 ArrayList values = new ArrayList (); 48 if ( node != null ){ 49 values.add(node); 50 if ( tree.isExpanded(new TreePath (node.getPath())) ){ 51 Enumeration childrens = node.children(); 52 while( childrens.hasMoreElements() ){ 53 DefaultMutableTreeNode childNode = 54 (DefaultMutableTreeNode )childrens.nextElement(); 55 ArrayList childDescendants = getFlatTree(tree,childNode); 56 values.addAll(childDescendants); 57 } 58 } 59 } 60 return values; 61 } 62 63 69 public static ArrayList getLevelsWithVerticalLine(DefaultMutableTreeNode node){ 70 ArrayList values = new ArrayList (); 71 TreeNode [] treeNodes = node.getPath(); 72 for( int i=0; i<treeNodes.length; i++ ){ 73 DefaultMutableTreeNode n = (DefaultMutableTreeNode )treeNodes[i]; 74 DefaultMutableTreeNode parentNode = 75 (DefaultMutableTreeNode )n.getParent(); 76 if ( parentNode!=null && !n.equals(parentNode.getLastChild()) ){ 77 values.add(new Integer (i)); 78 } 79 } 80 81 return values; 82 } 83 84 96 public static void updateGUITree(JTree tree, 97 HttpServletRequest request){ 98 99 String treeOperation = request.getParameter("guitree"); 100 String nodeIndex = request.getParameter("nodeindex"); 101 102 if ( tree != null && treeOperation != null && nodeIndex != null ){ 103 104 DefaultMutableTreeNode rootNode = 105 (DefaultMutableTreeNode )tree.getModel().getRoot(); 106 if ( rootNode != null ){ 107 ArrayList nodeList = 108 GUITreeTools.getFlatTree(tree,rootNode); 109 DefaultMutableTreeNode node 110 = (DefaultMutableTreeNode ) 111 nodeList.get(Integer.parseInt(nodeIndex)); 112 if ( treeOperation.equals("expand") ){ 113 tree.expandPath(new TreePath (node.getPath())); 114 } else if ( treeOperation.equals("expandall") ){ 115 expandAllPath(tree,node); 116 } else if ( treeOperation.equals("collapse") ){ 117 tree.collapsePath(new TreePath (node.getPath())); 118 } 119 } 120 } 121 } 122 123 128 public static void expandAllPath(JTree tree, DefaultMutableTreeNode node){ 129 if ( !node.isLeaf() || node.children().hasMoreElements() ){ 130 tree.expandPath(new TreePath (node.getPath())); 131 } 132 Enumeration children = node.children(); 133 DefaultMutableTreeNode childNode = null; 134 while( children.hasMoreElements() ){ 135 childNode = (DefaultMutableTreeNode )children.nextElement(); 136 expandAllPath(tree,childNode); 137 } 138 } 139 } 140 141 | Popular Tags |