2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.editor.ext;
44
import javax.swing.text.BadLocationException;
45
import org.netbeans.editor.Analyzer;
46
import org.netbeans.editor.FinderFactory;
47
import org.netbeans.editor.BaseDocument;
48
import org.netbeans.editor.Utilities;
51
* Various finders are located here.
53
* @author Miloslav Metelka
57
public class ExtFinderFactory {
59
/** Finder that collects the whole lines and calls
60
* the <tt>lineFound()</tt> method that can do a local find.
61
* !!! Udelat to poradne i s vice bufferama
63
public static abstract class LineFwdFinder extends FinderFactory.AbstractFinder {
65
private int origStartPos;
67
private int origLimitPos;
69
public LineFwdFinder() {
72
public int adjustStartPos(BaseDocument doc, int startPos) {
73
origStartPos = startPos;
75
return Utilities.getRowStart(doc, startPos);
76
} catch (BadLocationException e) {
81
public int adjustLimitPos(BaseDocument doc, int limitPos) {
82
origLimitPos = limitPos;
84
return Utilities.getRowEnd(doc, limitPos);
85
} catch (BadLocationException e) {
90
/** find function that must be defined by descendant */
91
public int find(int bufferStartPos, char buffer[],
92
int offset1, int offset2, int reqPos, int limitPos) {
93
int offset = reqPos - bufferStartPos; // !!! Udelat poradne s moznosti vice bufferu
95
int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset);
96
boolean lfFound = (lfOffset >= 0);
101
int lineOffset = lineFound(buffer, offset, lfOffset,
102
Math.max(origStartPos - bufferStartPos, offset),
103
Math.min(origLimitPos - bufferStartPos, lfOffset));
104
if (lineOffset >= 0) {
106
return bufferStartPos + offset + lineOffset;
110
offset = lfOffset + 1; // skip '\n'
115
return bufferStartPos + offset2;
118
/** Line was found and is present in the given buffer. The given
119
* buffer is either the original buffer passed to the <tt>find()</tt>
120
* or constructed buffer if the line is at the border of the previous
122
* @return non-negative number means the target string was found and
123
* the returned number is offset on the line where the string was found.
124
* Negative number means the target string was not found on the line
125
* and the search will continue with the next line.
127
protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
128
int startOffset, int endOffset);
132
/** Finder that collects the whole lines and calls
133
* the <tt>lineFound()</tt> method that can do a local find.
134
* !!! Udelat to poradne i s vice bufferama
136
public static abstract class LineBwdFinder extends FinderFactory.AbstractFinder {
138
private int origStartPos;
140
private int origLimitPos;
142
public LineBwdFinder() {
145
public int adjustStartPos(BaseDocument doc, int startPos) {
146
origStartPos = startPos;
148
return Utilities.getRowEnd(doc, startPos);
149
} catch (BadLocationException e) {
154
public int adjustLimitPos(BaseDocument doc, int limitPos) {
155
origLimitPos = limitPos;
157
return Utilities.getRowStart(doc, limitPos);
158
} catch (BadLocationException e) {
163
/** find function that must be defined by descendant */
164
public int find(int bufferStartPos, char buffer[],
165
int offset1, int offset2, int reqPos, int limitPos) {
166
int offset = reqPos - bufferStartPos + 1; // !!! Udelat poradne s moznosti vice bufferu
168
boolean lfFound = false;
169
int lfOffsetP1 = offset;
170
while (lfOffsetP1 > offset1) {
171
if (buffer[--lfOffsetP1] == '\n') {
173
lfOffsetP1++; // past '\n'
178
lfOffsetP1 = offset1;
181
int lineOffset = lineFound(buffer, lfOffsetP1, offset,
182
Math.max(origLimitPos - bufferStartPos, lfOffsetP1),
183
Math.min(origStartPos - bufferStartPos, offset));
184
if (lineOffset >= 0) {
186
return bufferStartPos + offset + lineOffset;
190
offset = lfOffsetP1 - 1; // skip '\n'
195
return bufferStartPos + offset1 - 1;
198
/** Line was found and is present in the given buffer. The given
199
* buffer is either the original buffer passed to the <tt>find()</tt>
200
* or constructed buffer if the line is at the border of the previous
202
* @return non-negative number means the target string was found and
203
* the returned number is offset on the line where the string was found.
204
* Negative number means the target string was not found on the line
205
* and the search will continue with the next line.
207
protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
208
int startOffset, int endOffset);
212
/** Finder that collects the whole lines and calls
213
* the <tt>lineFound()</tt> method that can do a local find.
214
* !!! Udelat to poradne i s vice bufferama
216
public static abstract class LineBlocksFinder extends FinderFactory.AbstractBlocksFinder {
218
private int origStartPos;
220
private int origLimitPos;
222
public LineBlocksFinder() {
225
public int adjustStartPos(BaseDocument doc, int startPos) {
226
origStartPos = startPos;
228
return Utilities.getRowStart(doc, startPos);
229
} catch (BadLocationException e) {
234
public int adjustLimitPos(BaseDocument doc, int limitPos) {
235
origLimitPos = limitPos;
237
return Utilities.getRowEnd(doc, limitPos);
238
} catch (BadLocationException e) {
243
/** find function that must be defined by descendant */
244
public int find(int bufferStartPos, char buffer[],
245
int offset1, int offset2, int reqPos, int limitPos) {
246
int offset = reqPos - bufferStartPos; // !!! Udelat poradne s moznosti vice bufferu
248
int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset);
249
boolean lfFound = (lfOffset >= 0);
254
int lineOffset = lineFound(buffer, offset, lfOffset,
255
Math.max(origStartPos - bufferStartPos, offset),
256
Math.min(origLimitPos - bufferStartPos, lfOffset));
257
if (lineOffset >= 0) {
259
return bufferStartPos + offset + lineOffset;
263
offset = lfOffset + 1; // skip '\n'
268
return bufferStartPos + offset2;
271
/** Line was found and is present in the given buffer. The given
272
* buffer is either the original buffer passed to the <tt>find()</tt>
273
* or constructed buffer if the line is at the border of the previous
275
* @return non-negative number means the target string was found and
276
* the returned number is offset on the line where the string was found.
277
* Negative number means the target string was not found on the line
278
* and the search will continue with the next line.
280
protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
281
int startOffset, int endOffset);