KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > search > HyperSearchResult


1 /*
2  * HyperSearchResult.java - HyperSearch result
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 2003 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.search;
24
25 //{{{ Imports
26
import javax.swing.text.Position JavaDoc;
27 import org.gjt.sp.jedit.io.VFSManager;
28 import org.gjt.sp.jedit.textarea.*;
29 import org.gjt.sp.jedit.*;
30 //}}}
31

32 /**
33  * A set of occurrences of the search string on a given line in a buffer.
34  */

35 public class HyperSearchResult implements HyperSearchNode
36 {
37     public String JavaDoc path;
38     public Buffer buffer;
39     public int line;
40     public String JavaDoc str; // cached for speed
41
public Occur occur;
42     public int occurCount;
43
44     //{{{ getBuffer() method
45
public Buffer getBuffer()
46     {
47         if(buffer == null)
48             buffer = jEdit.openFile(null,path);
49         return buffer;
50     } //}}}
51

52     //{{{ getSelection() method
53
/**
54      * Returns an array of selection objects pointing to the occurrences
55      * of the search term on the current line. The buffer must be opened
56      * first.
57      * @since jEdit 4.2pre5
58      */

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     } //}}}
78

79     //{{{ goTo() method
80
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 JavaDoc()
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     } //}}}
105

106     //{{{ toString() method
107
public String JavaDoc toString()
108     {
109         return str;
110     } //}}}
111

112     //{{{ Package-private members
113

114     //{{{ HyperSearchResult constructor
115
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     } //}}}
127

128     //{{{ bufferOpened() method
129
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     } //}}}
139

140     //{{{ bufferClosed() method
141
void bufferClosed()
142     {
143         buffer = null;
144         Occur o = occur;
145         while(o != null)
146         {
147             o.bufferClosed();
148             o = o.next;
149         }
150     } //}}}
151

152     //{{{ addOccur() method
153
void addOccur(int start, int end)
154     {
155         Occur o = new Occur(start,end);
156         o.next = occur;
157         occur = o;
158         occurCount++;
159     } //}}}
160

161     //{{{ pathEquals() method
162
/**
163      * @param path A canonical path
164      */

165     boolean pathEquals(String JavaDoc path)
166     {
167         return path.equals(MiscUtilities.resolveSymlinks(this.path));
168     } //}}}
169

170     //{{{ equals() method
171
public boolean equals(Object JavaDoc 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     }//}}}
179

180     //}}}
181

182     //{{{ Occur class
183
public class Occur
184     {
185         public int start, end;
186         public Position JavaDoc startPos, endPos;
187         public Occur next;
188
189         //{{{ Occur constructor
190
Occur(int start, int end)
191         {
192             this.start = start;
193             this.end = end;
194
195             if(buffer != null && !buffer.isTemporary())
196                 bufferOpened();
197         } //}}}
198

199         //{{{ bufferOpened() method
200
void bufferOpened()
201         {
202             startPos = buffer.createPosition(Math.min(
203                 buffer.getLength(),start));
204             endPos = buffer.createPosition(Math.min(
205                 buffer.getLength(),end));
206         } //}}}
207

208         //{{{ bufferClosed() method
209
void bufferClosed()
210         {
211             start = startPos.getOffset();
212             end = endPos.getOffset();
213             startPos = endPos = null;
214         } //}}}
215
} //}}}
216
}
217
Popular Tags