1 22 23 package org.gjt.sp.jedit.search; 24 25 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.swing.JTree ; 34 import javax.swing.tree.DefaultMutableTreeNode ; 35 import javax.swing.tree.DefaultTreeModel ; 36 import javax.swing.tree.MutableTreeNode ; 37 import javax.swing.tree.TreePath ; 38 40 44 public class HyperSearchOperationNode 45 { 46 private boolean treeViewDisplayed; 47 private final String searchString; 48 private List <DefaultMutableTreeNode > resultNodes; 49 50 public HyperSearchOperationNode(String searchString) 52 { 53 this.searchString = searchString; 54 } 56 public String toString() 58 { 59 return searchString; 60 } 62 public boolean isTreeViewDisplayed() 64 { 65 return treeViewDisplayed; 66 } 68 public void setTreeViewDisplayed(boolean treeViewDisplayed) 70 { 71 this.treeViewDisplayed = treeViewDisplayed; 72 } 74 public void restoreFlatNodes(JTree resultTree, DefaultMutableTreeNode operNode) 76 { 77 for (int i = 0; i < resultNodes.size(); i++) 78 { 79 DefaultMutableTreeNode element = resultNodes.get(i); 80 if (element.getUserObject() instanceof HyperSearchFileNode) 81 ((HyperSearchFileNode)element.getUserObject()).showFullPath = true; 82 83 operNode.insert(element, operNode.getChildCount()); 84 } 85 86 ((DefaultTreeModel )resultTree.getModel()).nodeStructureChanged(operNode); 87 88 for (Enumeration e = operNode.children(); e.hasMoreElements();) 89 { 90 DefaultMutableTreeNode node = (DefaultMutableTreeNode )e.nextElement(); 91 resultTree.expandPath(new TreePath (node.getPath())); 92 } 93 resultTree.scrollPathToVisible( 94 new TreePath (operNode.getPath())); 95 } 97 public void cacheResultNodes(DefaultMutableTreeNode operNode) 99 { 100 resultNodes = new ArrayList <DefaultMutableTreeNode >(operNode.getChildCount()); 101 for (Enumeration e = operNode.children(); e.hasMoreElements();) 102 resultNodes.add((DefaultMutableTreeNode ) e.nextElement()); 103 } 105 public static void removeNodeFromCache(MutableTreeNode mnode) 107 { 108 DefaultMutableTreeNode node = (DefaultMutableTreeNode )mnode; 109 if (node.getUserObject() instanceof HyperSearchOperationNode) 110 return; 111 112 DefaultMutableTreeNode tmpNode = node; 113 while ((tmpNode = (DefaultMutableTreeNode ) tmpNode.getParent()) != null) 114 { 115 if (!(tmpNode.getUserObject() instanceof HyperSearchOperationNode)) 116 continue; 117 HyperSearchOperationNode operNode = (HyperSearchOperationNode) tmpNode.getUserObject(); 118 if (operNode.resultNodes != null) 119 { 120 operNode.resultNodes.remove(node); 122 } 123 break; 124 } 125 126 } 128 public void insertTreeNodes(JTree resultTree, DefaultMutableTreeNode operNode) 130 { 131 String fileSep = System.getProperty("file.separator"); 132 String fileSepRegex = System.getProperty("file.separator"); 133 if (fileSep.equals("\\")) 134 fileSepRegex = "\\\\"; 135 136 String [] topPathTmp = null; 138 int topPathNdx = -1; 139 140 for (int i = 0;i < resultNodes.size();i++) 141 { 142 DefaultMutableTreeNode fileTreeNode = resultNodes.get(i); 143 Object obj = fileTreeNode.getUserObject(); 144 if (!(obj instanceof HyperSearchFileNode)) 145 continue; 146 HyperSearchFileNode fileNode = (HyperSearchFileNode)obj; 147 148 int pos = fileNode.path.lastIndexOf(fileSep); 149 String pathName = fileNode.path.substring(0, pos); 150 String [] paths = pathName.split(fileSepRegex); 151 if (topPathNdx == -1) 152 { 153 topPathNdx = paths.length; 154 topPathTmp = paths; 155 } 156 else if (paths.length < topPathNdx) 157 { 158 topPathNdx = paths.length; 159 topPathTmp = paths; 160 } 161 else 162 { 163 for (int ndx =0 ; ndx < topPathNdx; ndx++) 164 { 165 if (!paths[ndx].equals(topPathTmp[ndx])) 166 { 167 topPathNdx = ndx; 168 break; 169 } 170 } 171 } 172 } 173 String [] topPath = new String [topPathNdx]; 174 String topPathPath = ""; 175 for (int ndx = 0 ; ndx < topPathNdx; ndx++) 176 { 177 topPath[ndx] = topPathTmp[ndx]; 178 topPathPath = topPathPath.concat(topPath[ndx] + fileSep); 179 } 180 Map <String , DefaultMutableTreeNode > treeNodes = new HashMap <String , DefaultMutableTreeNode >(); 181 HyperSearchFolderNode folderNode = 182 new HyperSearchFolderNode(new File (topPathPath), true); 183 DefaultMutableTreeNode folderTreeNode = new DefaultMutableTreeNode (folderNode); 184 operNode.insert(folderTreeNode, operNode.getChildCount()); 185 treeNodes.put(topPathPath, folderTreeNode); 186 187 for (int i = 0;i < resultNodes.size();i++) 188 { 189 DefaultMutableTreeNode fileTreeNode = resultNodes.get(i); 190 Object obj = fileTreeNode.getUserObject(); 191 if (!(obj instanceof HyperSearchFileNode)) 192 continue; 193 HyperSearchFileNode fileNode = (HyperSearchFileNode)obj; 194 195 fileNode.showFullPath = false; 196 int pos = fileNode.path.lastIndexOf(fileSep); 197 String pathName = fileNode.path.substring(0, pos); 198 String [] paths = pathName.split(fileSepRegex); 199 200 DefaultMutableTreeNode insNode = folderTreeNode; 201 String partialPath = topPathPath; 202 for (int ndx = topPathNdx; ndx < paths.length; ndx++) 203 { 204 partialPath = partialPath.concat(paths[ndx] + fileSep); 205 DefaultMutableTreeNode tmpNode = treeNodes.get(partialPath); 206 if (tmpNode == null) 207 { 208 HyperSearchFolderNode tmpFolderNode = 209 new HyperSearchFolderNode(new File (partialPath), false); 210 tmpNode = new DefaultMutableTreeNode (tmpFolderNode); 211 insNode.insert(tmpNode, insNode.getChildCount()); 212 treeNodes.put(partialPath, tmpNode); 213 } 214 insNode = tmpNode; 215 } 216 insNode.insert(fileTreeNode, insNode.getChildCount()); 217 treeNodes.put(fileNode.path, insNode); 218 } 219 220 }} 222 | Popular Tags |