KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > ExtFinderFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.ext;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import org.netbeans.editor.Analyzer;
24 import org.netbeans.editor.FinderFactory;
25 import org.netbeans.editor.BaseDocument;
26 import org.netbeans.editor.Utilities;
27
28 /**
29 * Various finders are located here.
30 *
31 * @author Miloslav Metelka
32 * @version 1.00
33 */

34
35 public class ExtFinderFactory {
36
37     /** Finder that collects the whole lines and calls
38     * the <tt>lineFound()</tt> method that can do a local find.
39     * !!! Udelat to poradne i s vice bufferama
40     */

41     public static abstract class LineFwdFinder extends FinderFactory.AbstractFinder {
42
43         private int origStartPos;
44
45         private int origLimitPos;
46
47         public LineFwdFinder() {
48         }
49
50         public int adjustStartPos(BaseDocument doc, int startPos) {
51             origStartPos = startPos;
52             try {
53                 return Utilities.getRowStart(doc, startPos);
54             } catch (BadLocationException JavaDoc e) {
55                 return startPos;
56             }
57         }
58
59         public int adjustLimitPos(BaseDocument doc, int limitPos) {
60             origLimitPos = limitPos;
61             try {
62                 return Utilities.getRowEnd(doc, limitPos);
63             } catch (BadLocationException JavaDoc e) {
64                 return limitPos;
65             }
66         }
67
68         /** find function that must be defined by descendant */
69         public int find(int bufferStartPos, char buffer[],
70                         int offset1, int offset2, int reqPos, int limitPos) {
71             int offset = reqPos - bufferStartPos; // !!! Udelat poradne s moznosti vice bufferu
72
while (true) {
73                 int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset);
74                 boolean lfFound = (lfOffset >= 0);
75                 if (!lfFound) {
76                     lfOffset = offset2;
77                 }
78
79                 int lineOffset = lineFound(buffer, offset, lfOffset,
80                                            Math.max(origStartPos - bufferStartPos, offset),
81                                            Math.min(origLimitPos - bufferStartPos, lfOffset));
82                 if (lineOffset >= 0) {
83                     found = true;
84                     return bufferStartPos + offset + lineOffset;
85                 }
86
87                 if (lfFound) {
88                     offset = lfOffset + 1; // skip '\n'
89
} else {
90                     break;
91                 }
92             }
93             return bufferStartPos + offset2;
94         }
95
96         /** Line was found and is present in the given buffer. The given
97         * buffer is either the original buffer passed to the <tt>find()</tt>
98         * or constructed buffer if the line is at the border of the previous
99         * and next buffer.
100         * @return non-negative number means the target string was found and
101         * the returned number is offset on the line where the string was found.
102         * Negative number means the target string was not found on the line
103         * and the search will continue with the next line.
104         */

105         protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
106                                          int startOffset, int endOffset);
107
108     }
109
110     /** Finder that collects the whole lines and calls
111     * the <tt>lineFound()</tt> method that can do a local find.
112     * !!! Udelat to poradne i s vice bufferama
113     */

114     public static abstract class LineBwdFinder extends FinderFactory.AbstractFinder {
115
116         private int origStartPos;
117
118         private int origLimitPos;
119
120         public LineBwdFinder() {
121         }
122
123         public int adjustStartPos(BaseDocument doc, int startPos) {
124             origStartPos = startPos;
125             try {
126                 return Utilities.getRowEnd(doc, startPos);
127             } catch (BadLocationException JavaDoc e) {
128                 return startPos;
129             }
130         }
131
132         public int adjustLimitPos(BaseDocument doc, int limitPos) {
133             origLimitPos = limitPos;
134             try {
135                 return Utilities.getRowStart(doc, limitPos);
136             } catch (BadLocationException JavaDoc e) {
137                 return limitPos;
138             }
139         }
140
141         /** find function that must be defined by descendant */
142         public int find(int bufferStartPos, char buffer[],
143                         int offset1, int offset2, int reqPos, int limitPos) {
144             int offset = reqPos - bufferStartPos + 1; // !!! Udelat poradne s moznosti vice bufferu
145
while (true) {
146                 boolean lfFound = false;
147                 int lfOffsetP1 = offset;
148                 while (lfOffsetP1 > offset1) {
149                     if (buffer[--lfOffsetP1] == '\n') {
150                         lfFound = true;
151                         lfOffsetP1++; // past '\n'
152
break;
153                     }
154                 }
155                 if (!lfFound) {
156                     lfOffsetP1 = offset1;
157                 }
158
159                 int lineOffset = lineFound(buffer, lfOffsetP1, offset,
160                                            Math.max(origLimitPos - bufferStartPos, lfOffsetP1),
161                                            Math.min(origStartPos - bufferStartPos, offset));
162                 if (lineOffset >= 0) {
163                     found = true;
164                     return bufferStartPos + offset + lineOffset;
165                 }
166
167                 if (lfFound) {
168                     offset = lfOffsetP1 - 1; // skip '\n'
169
} else {
170                     break;
171                 }
172             }
173             return bufferStartPos + offset1 - 1;
174         }
175
176         /** Line was found and is present in the given buffer. The given
177         * buffer is either the original buffer passed to the <tt>find()</tt>
178         * or constructed buffer if the line is at the border of the previous
179         * and next buffer.
180         * @return non-negative number means the target string was found and
181         * the returned number is offset on the line where the string was found.
182         * Negative number means the target string was not found on the line
183         * and the search will continue with the next line.
184         */

185         protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
186                                          int startOffset, int endOffset);
187
188     }
189
190     /** Finder that collects the whole lines and calls
191     * the <tt>lineFound()</tt> method that can do a local find.
192     * !!! Udelat to poradne i s vice bufferama
193     */

194     public static abstract class LineBlocksFinder extends FinderFactory.AbstractBlocksFinder {
195
196         private int origStartPos;
197
198         private int origLimitPos;
199
200         public LineBlocksFinder() {
201         }
202
203         public int adjustStartPos(BaseDocument doc, int startPos) {
204             origStartPos = startPos;
205             try {
206                 return Utilities.getRowStart(doc, startPos);
207             } catch (BadLocationException JavaDoc e) {
208                 return startPos;
209             }
210         }
211
212         public int adjustLimitPos(BaseDocument doc, int limitPos) {
213             origLimitPos = limitPos;
214             try {
215                 return Utilities.getRowEnd(doc, limitPos);
216             } catch (BadLocationException JavaDoc e) {
217                 return limitPos;
218             }
219         }
220
221         /** find function that must be defined by descendant */
222         public int find(int bufferStartPos, char buffer[],
223                         int offset1, int offset2, int reqPos, int limitPos) {
224             int offset = reqPos - bufferStartPos; // !!! Udelat poradne s moznosti vice bufferu
225
while (true) {
226                 int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset);
227                 boolean lfFound = (lfOffset >= 0);
228                 if (!lfFound) {
229                     lfOffset = offset2;
230                 }
231
232                 int lineOffset = lineFound(buffer, offset, lfOffset,
233                                            Math.max(origStartPos - bufferStartPos, offset),
234                                            Math.min(origLimitPos - bufferStartPos, lfOffset));
235                 if (lineOffset >= 0) {
236                     found = true;
237                     return bufferStartPos + offset + lineOffset;
238                 }
239
240                 if (lfFound) {
241                     offset = lfOffset + 1; // skip '\n'
242
} else {
243                     break;
244                 }
245             }
246             return bufferStartPos + offset2;
247         }
248
249         /** Line was found and is present in the given buffer. The given
250         * buffer is either the original buffer passed to the <tt>find()</tt>
251         * or constructed buffer if the line is at the border of the previous
252         * and next buffer.
253         * @return non-negative number means the target string was found and
254         * the returned number is offset on the line where the string was found.
255         * Negative number means the target string was not found on the line
256         * and the search will continue with the next line.
257         */

258         protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset,
259                                          int startOffset, int endOffset);
260
261     }
262
263
264
265 }
266
Popular Tags