1 22 23 package org.gjt.sp.jedit.search; 24 25 import javax.swing.text.Position ; 27 import org.gjt.sp.jedit.io.VFSManager; 28 import org.gjt.sp.jedit.textarea.*; 29 import org.gjt.sp.jedit.*; 30 32 35 public class HyperSearchResult implements HyperSearchNode 36 { 37 public String path; 38 public Buffer buffer; 39 public int line; 40 public String str; public Occur occur; 42 public int occurCount; 43 44 public Buffer getBuffer() 46 { 47 if(buffer == null) 48 buffer = jEdit.openFile(null,path); 49 return buffer; 50 } 52 59 public Selection[] getSelection() 60 { 61 if(buffer == null) 62 return null; 63 64 Selection[] returnValue = new Selection[occurCount]; 65 Occur o = occur; 66 int i = 0; 67 while(o != null) 68 { 69 Selection.Range s = new Selection.Range( 70 o.startPos.getOffset(), 71 o.endPos.getOffset() 72 ); 73 returnValue[i++] = s; 74 o = o.next; 75 } 76 return returnValue; 77 } 79 public void goTo(final EditPane editPane) 81 { 82 final Buffer buffer = getBuffer(); 83 if(buffer == null) 84 return; 85 editPane.setBuffer(buffer); 86 87 VFSManager.runInAWTThread(new Runnable () 88 { 89 public void run() 90 { 91 Selection[] s = getSelection(); 92 if(s == null) 93 return; 94 95 JEditTextArea textArea = editPane.getTextArea(); 96 if(textArea.isMultipleSelectionEnabled()) 97 textArea.addToSelection(s); 98 else 99 textArea.setSelection(s); 100 101 textArea.moveCaretPosition(occur.endPos.getOffset()); 102 } 103 }); 104 } 106 public String toString() 108 { 109 return str; 110 } 112 114 HyperSearchResult(Buffer buffer, int line) 116 { 117 path = buffer.getPath(); 118 119 if(!buffer.isTemporary()) 120 bufferOpened(buffer); 121 122 this.line = line; 123 124 str = (line + 1) + ": " + buffer.getLineText(line) 125 .replace('\t',' ').trim(); 126 } 128 void bufferOpened(Buffer buffer) 130 { 131 this.buffer = buffer; 132 Occur o = occur; 133 while(o != null) 134 { 135 o.bufferOpened(); 136 o = o.next; 137 } 138 } 140 void bufferClosed() 142 { 143 buffer = null; 144 Occur o = occur; 145 while(o != null) 146 { 147 o.bufferClosed(); 148 o = o.next; 149 } 150 } 152 void addOccur(int start, int end) 154 { 155 Occur o = new Occur(start,end); 156 o.next = occur; 157 occur = o; 158 occurCount++; 159 } 161 165 boolean pathEquals(String path) 166 { 167 return path.equals(MiscUtilities.resolveSymlinks(this.path)); 168 } 170 public boolean equals(Object compareObj) 172 { 173 if (!(compareObj instanceof HyperSearchResult)) 174 return false; 175 HyperSearchResult otherResult = (HyperSearchResult)compareObj; 176 return pathEquals(otherResult.path) && line == otherResult.line 177 && buffer.equals(otherResult.buffer); 178 } 180 182 public class Occur 184 { 185 public int start, end; 186 public Position startPos, endPos; 187 public Occur next; 188 189 Occur(int start, int end) 191 { 192 this.start = start; 193 this.end = end; 194 195 if(buffer != null && !buffer.isTemporary()) 196 bufferOpened(); 197 } 199 void bufferOpened() 201 { 202 startPos = buffer.createPosition(Math.min( 203 buffer.getLength(),start)); 204 endPos = buffer.createPosition(Math.min( 205 buffer.getLength(),end)); 206 } 208 void bufferClosed() 210 { 211 start = startPos.getOffset(); 212 end = endPos.getOffset(); 213 startPos = endPos = null; 214 } } } 217 | Popular Tags |