1 19 20 package org.openidex.search; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.openide.filesystems.FileObject; 25 import org.openide.loaders.DataFolder; 26 import org.openide.loaders.DataObject; 27 import org.openide.loaders.DataObjectNotFoundException; 28 import org.openide.nodes.AbstractNode; 29 import org.openide.nodes.Children; 30 import org.openide.nodes.Node; 31 32 41 public class FileObjectSearchGroup extends SearchGroup { 42 43 49 protected void add(SearchType searchType) { 50 boolean ok = false; 51 Class [] classes = searchType.getSearchTypeClasses(); 52 for (int i = 0; i < classes.length; i++) { 53 if (classes[i] == FileObject.class) { 54 ok = true; 55 break; 56 } 57 } 58 if (ok) { 59 super.add(searchType); 60 } 61 } 62 63 66 public void doSearch() { 67 FileObject[] rootFolders = getFileFolders(); 68 69 if (rootFolders == null) { 70 return; 71 } 72 for(int i = 0; i < rootFolders.length; i++) { 73 if (!scanFolder(rootFolders[i])) { 74 return; 75 } 76 } 77 } 78 79 80 private FileObject[] getFileFolders() { 81 Node[] nodes = normalizeNodes((Node[])searchRoots.toArray(new Node[searchRoots.size()])); 82 83 List children = new ArrayList (nodes.length); 84 85 for (int i = 0; i < nodes.length; i++) { 86 DataFolder dataFolder = (DataFolder) nodes[i].getCookie(DataFolder.class); 87 if (dataFolder != null) { 88 children.add(dataFolder.getPrimaryFile()); 89 } 90 } 91 92 return (FileObject[])children.toArray(new FileObject[children.size()]); 93 } 94 95 98 private boolean scanFolder(FileObject folder) { 99 FileObject[] children = folder.getChildren(); 100 101 for (int i = 0; i < children.length; i++) { 102 if (stopped) { 104 stopped = true; 105 return false; 106 } 107 108 if (children[i].isFolder()) { 109 if (!scanFolder(children[i])) { 110 return false; 111 } 112 } else { 113 processSearchObject(children[i]); 114 } 115 } 116 117 return true; 118 } 119 120 121 124 public Node getNodeForFoundObject(final Object object) { 125 if (!(object instanceof FileObject)) { 126 return null; 127 } 128 try { 129 return DataObject.find((FileObject) object).getNodeDelegate(); 130 } catch (DataObjectNotFoundException dnfe) { 131 return new AbstractNode(Children.LEAF) { 132 public String getName() { 133 return ((FileObject) object).getName(); 134 } 135 }; 136 } 137 } 138 139 140 141 142 private static Node[] normalizeNodes(Node[] nodes) { 143 144 List ret = new ArrayList (); 145 146 for (int i = 0; i<nodes.length; i++) { 147 if (!hasParent(nodes[i], nodes)) { 148 ret.add(nodes[i]); 149 } 150 } 151 152 return (Node[]) ret.toArray(new Node[ret.size()]); 153 } 154 155 156 private static boolean hasParent(Node node, Node[] nodes) { 157 for (Node parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) { 158 for (int i = 0; i<nodes.length; i++) { 159 if (nodes[i].equals(parent)) { 160 return true; 161 } 162 } 163 } 164 return false; 165 } 166 167 } 168 | Popular Tags |