001: /*
002: * HyperSearchResult.java - HyperSearch result
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.search;
024:
025: //{{{ Imports
026: import javax.swing.text.Position;
027: import org.gjt.sp.jedit.io.VFSManager;
028: import org.gjt.sp.jedit.textarea.*;
029: import org.gjt.sp.jedit.*;
030:
031: //}}}
032:
033: /**
034: * A set of occurrences of the search string on a given line in a buffer.
035: * @author Slava Pestov
036: */
037: public class HyperSearchResult implements HyperSearchNode {
038: public String path;
039: public Buffer buffer;
040: public int line;
041: public String str; // cached for speed
042: public Occur occur;
043: public int occurCount;
044:
045: //{{{ getBuffer() method
046: public Buffer getBuffer(View view) {
047: if (buffer == null)
048: buffer = jEdit.openFile(view, path);
049: return buffer;
050: } //}}}
051:
052: //{{{ getSelection() method
053: /**
054: * Returns an array of selection objects pointing to the occurrences
055: * of the search term on the current line. The buffer must be opened
056: * first.
057: * @since jEdit 4.2pre5
058: */
059: public Selection[] getSelection() {
060: if (buffer == null)
061: return null;
062:
063: Selection[] returnValue = new Selection[occurCount];
064: Occur o = occur;
065: int i = 0;
066: while (o != null) {
067: Selection.Range s = new Selection.Range(o.startPos
068: .getOffset(), o.endPos.getOffset());
069: returnValue[i++] = s;
070: o = o.next;
071: }
072: return returnValue;
073: } //}}}
074:
075: //{{{ goTo() method
076: public void goTo(final EditPane editPane) {
077: final Buffer buffer = getBuffer(editPane.getView());
078: if (buffer == null)
079: return;
080: editPane.setBuffer(buffer);
081:
082: VFSManager.runInAWTThread(new Runnable() {
083: public void run() {
084: Selection[] s = getSelection();
085: if (s == null)
086: return;
087:
088: JEditTextArea textArea = editPane.getTextArea();
089: if (textArea.isMultipleSelectionEnabled())
090: textArea.addToSelection(s);
091: else
092: textArea.setSelection(s);
093:
094: textArea.moveCaretPosition(occur.endPos.getOffset());
095: }
096: });
097: } //}}}
098:
099: //{{{ toString() method
100: public String toString() {
101: return str;
102: } //}}}
103:
104: //{{{ Package-private members
105:
106: //{{{ HyperSearchResult constructor
107: HyperSearchResult(Buffer buffer, int line) {
108: path = buffer.getPath();
109:
110: if (!buffer.isTemporary())
111: bufferOpened(buffer);
112:
113: this .line = line;
114:
115: str = (line + 1) + ": "
116: + buffer.getLineText(line).replace('\t', ' ').trim();
117: } //}}}
118:
119: //{{{ bufferOpened() method
120: void bufferOpened(Buffer buffer) {
121: this .buffer = buffer;
122: Occur o = occur;
123: while (o != null) {
124: o.bufferOpened();
125: o = o.next;
126: }
127: } //}}}
128:
129: //{{{ bufferClosed() method
130: void bufferClosed() {
131: buffer = null;
132: Occur o = occur;
133: while (o != null) {
134: o.bufferClosed();
135: o = o.next;
136: }
137: } //}}}
138:
139: //{{{ addOccur() method
140: void addOccur(int start, int end) {
141: Occur o = new Occur(start, end);
142: o.next = occur;
143: occur = o;
144: occurCount++;
145: } //}}}
146:
147: //{{{ pathEquals() method
148: /**
149: * @param path A canonical path
150: */
151: boolean pathEquals(String path) {
152: return path.equals(MiscUtilities.resolveSymlinks(this .path));
153: } //}}}
154:
155: //{{{ equals() method
156: public boolean equals(Object compareObj) {
157: if (!(compareObj instanceof HyperSearchResult))
158: return false;
159: HyperSearchResult otherResult = (HyperSearchResult) compareObj;
160: return pathEquals(otherResult.path) && line == otherResult.line
161: && buffer.equals(otherResult.buffer);
162: }//}}}
163:
164: //}}}
165:
166: //{{{ Occur class
167: public class Occur {
168: public int start, end;
169: public Position startPos, endPos;
170: public Occur next;
171:
172: //{{{ Occur constructor
173: Occur(int start, int end) {
174: this .start = start;
175: this .end = end;
176:
177: if (buffer != null && !buffer.isTemporary())
178: bufferOpened();
179: } //}}}
180:
181: //{{{ bufferOpened() method
182: void bufferOpened() {
183: startPos = buffer.createPosition(Math.min(buffer
184: .getLength(), start));
185: endPos = buffer.createPosition(Math.min(buffer.getLength(),
186: end));
187: } //}}}
188:
189: //{{{ bufferClosed() method
190: void bufferClosed() {
191: start = startPos.getOffset();
192: end = endPos.getOffset();
193: startPos = endPos = null;
194: } //}}}
195: } //}}}
196: }
|