1 22 23 package org.gjt.sp.jedit.search; 24 25 import javax.swing.text.Segment ; 27 import javax.swing.tree.*; 28 import javax.swing.SwingUtilities ; 29 import org.gjt.sp.jedit.textarea.Selection; 30 import org.gjt.sp.jedit.io.VFSManager; 31 import org.gjt.sp.jedit.Buffer; 32 import org.gjt.sp.jedit.GUIUtilities; 33 import org.gjt.sp.jedit.jEdit; 34 import org.gjt.sp.jedit.View; 35 import org.gjt.sp.util.*; 36 38 43 class HyperSearchRequest extends WorkRequest 44 { 45 HyperSearchRequest(View view, SearchMatcher matcher, 47 HyperSearchResults results, Selection[] selection) 48 { 49 this.view = view; 50 this.matcher = matcher; 51 52 this.results = results; 53 searchString = SearchAndReplace.getSearchString(); 54 rootSearchNode = new DefaultMutableTreeNode(new HyperSearchOperationNode(searchString)); 55 56 this.selection = selection; 57 } 59 public void run() 61 { 62 setStatus(jEdit.getProperty("hypersearch-status")); 63 64 SearchFileSet fileset = SearchAndReplace.getSearchFileSet(); 65 String [] files = fileset.getFiles(view); 66 if(files == null || files.length == 0) 67 { 68 SwingUtilities.invokeLater(new Runnable () 69 { 70 public void run() 71 { 72 GUIUtilities.error(view,"empty-fileset",null); 73 results.searchDone(rootSearchNode); 74 } 75 }); 76 return; 77 } 78 79 setMaximum(fileset.getFileCount(view)); 80 81 84 String searchingCaption = jEdit.getProperty("hypersearch-results.searching") + ' '; 86 try 87 { 88 if(selection != null) 89 { 90 Buffer buffer = view.getBuffer(); 91 92 searchInSelection(buffer); 93 } 94 else 95 { 96 int current = 0; 97 98 long lastStatusTime = 0; 99 loop: for(int i = 0; i < files.length; i++) 100 { 101 String file = files[i]; 102 current++; 103 104 long currentTime = System.currentTimeMillis(); 105 if(currentTime - lastStatusTime > 250) 106 { 107 setValue(current); 108 lastStatusTime = currentTime; 109 results.setSearchStatus(searchingCaption + file); 110 } 111 112 Buffer buffer = jEdit.openTemporary(null,null,file,false); 113 if(buffer == null) 114 continue loop; 115 116 doHyperSearch(buffer); 117 } 118 } 119 } 120 catch(final Exception e) 121 { 122 Log.log(Log.ERROR,this,e); 123 SwingUtilities.invokeLater(new Runnable () 124 { 125 public void run() 126 { 127 SearchAndReplace.handleError(view,e); 128 } 129 }); 130 } 131 catch(WorkThread.Abort a) 132 { 133 } 134 finally 135 { 136 VFSManager.runInAWTThread(new Runnable () 137 { 138 public void run() 139 { 140 results.searchDone(rootSearchNode); 141 } 142 }); 143 } 144 } 146 148 private View view; 150 private SearchMatcher matcher; 151 private HyperSearchResults results; 152 private DefaultMutableTreeNode rootSearchNode; 153 private Selection[] selection; 154 private String searchString; 155 157 private int searchInSelection(Buffer buffer) throws Exception 159 { 160 setAbortable(false); 161 162 int resultCount = 0; 163 164 try 165 { 166 buffer.readLock(); 167 168 for(int i = 0; i < selection.length; i++) 169 { 170 Selection s = selection[i]; 171 if(s instanceof Selection.Rect) 172 { 173 for(int j = s.getStartLine(); 174 j <= s.getEndLine(); j++) 175 { 176 resultCount += doHyperSearch(buffer, 177 s.getStart(buffer,j), 178 s.getEnd(buffer,j)); 179 } 180 } 181 else 182 { 183 resultCount += doHyperSearch(buffer, 184 s.getStart(),s.getEnd()); 185 } 186 } 187 } 188 finally 189 { 190 buffer.readUnlock(); 191 } 192 193 setAbortable(true); 194 195 return resultCount; 196 } 198 private int doHyperSearch(Buffer buffer) 200 throws Exception 201 { 202 return doHyperSearch(buffer, 0, buffer.getLength()); 203 } 205 private int doHyperSearch(Buffer buffer, int start, int end) 207 throws Exception 208 { 209 setAbortable(false); 210 211 HyperSearchFileNode hyperSearchFileNode = new HyperSearchFileNode(buffer.getPath()); 212 DefaultMutableTreeNode bufferNode = new DefaultMutableTreeNode(hyperSearchFileNode); 213 214 int resultCount = doHyperSearch(buffer,start,end,bufferNode); 215 hyperSearchFileNode.setCount(resultCount); 216 if(resultCount != 0) 217 rootSearchNode.insert(bufferNode,rootSearchNode.getChildCount()); 218 219 setAbortable(true); 220 221 return resultCount; 222 } 224 private int doHyperSearch(Buffer buffer, int start, int end, 226 DefaultMutableTreeNode bufferNode) 227 { 228 int resultCount = 0; 229 230 try 231 { 232 buffer.readLock(); 233 234 boolean endOfLine = buffer.getLineEndOffset( 235 buffer.getLineOfOffset(end)) - 1 == end; 236 237 Segment text = new Segment (); 238 int offset = start; 239 240 HyperSearchResult lastResult = null; 241 242 loop: for(int counter = 0; ; counter++) 243 { 244 boolean startOfLine = buffer.getLineStartOffset( 245 buffer.getLineOfOffset(offset)) == offset; 246 247 buffer.getText(offset,end - offset,text); 248 SearchMatcher.Match match = matcher.nextMatch( 249 new SegmentCharSequence(text,false), 250 startOfLine,endOfLine,counter == 0, 251 false); 252 if(match == null) 253 break loop; 254 255 int newLine = buffer.getLineOfOffset( 256 offset + match.start); 257 if(lastResult == null || lastResult.line != newLine) 258 { 259 lastResult = new HyperSearchResult( 260 buffer,newLine); 261 bufferNode.add( 262 new DefaultMutableTreeNode( 263 lastResult,false)); 264 } 265 266 lastResult.addOccur(offset + match.start, 267 offset + match.end); 268 269 offset += match.end; 270 if (matcher.isMatchingEOL()) 271 { 272 if (offset < buffer.getLength()) 273 offset += 1; 274 else 275 break loop; 276 } 277 278 resultCount++; 279 } 280 } 281 finally 282 { 283 buffer.readUnlock(); 284 } 285 286 return resultCount; 287 } 289 } 291 | Popular Tags |