40 |
|
|
41 |
import java.awt.FontMetrics; |
import java.awt.FontMetrics; |
42 |
import java.awt.Graphics; |
import java.awt.Graphics; |
43 |
|
import java.text.BreakIterator; |
44 |
|
|
45 |
/** |
/** |
46 |
* A set of utilities to deal with text. This is used by several other classes |
* A set of utilities to deal with text. This is used by several other classes |
281 |
{ |
{ |
282 |
return getTabbedTextOffset(s, fm, x0, x, te, p0, true); |
return getTabbedTextOffset(s, fm, x0, x, te, p0, true); |
283 |
} |
} |
284 |
|
|
285 |
|
/** |
286 |
|
* Finds the start of the next word for the given offset. |
287 |
|
* |
288 |
|
* @param c |
289 |
|
* the text component |
290 |
|
* @param offs |
291 |
|
* the offset in the document |
292 |
|
* @return the location in the model of the start of the next word. |
293 |
|
* @throws BadLocationException |
294 |
|
* if the offset is invalid. |
295 |
|
*/ |
296 |
|
public static final int getNextWord(JTextComponent c, int offs) |
297 |
|
throws BadLocationException |
298 |
|
{ |
299 |
|
if (offs < 0 || offs > (c.getText().length() - 1)) |
300 |
|
throw new BadLocationException("invalid offset specified", offs); |
301 |
|
String text = c.getText(); |
302 |
|
BreakIterator wb = BreakIterator.getWordInstance(); |
303 |
|
wb.setText(text); |
304 |
|
int last = wb.following(offs); |
305 |
|
int current = wb.next(); |
306 |
|
while (current != BreakIterator.DONE) |
307 |
|
{ |
308 |
|
for (int i = last; i < current; i++) |
309 |
|
{ |
310 |
|
// FIXME: Should use isLetter(int) and text.codePointAt(int) |
311 |
|
// instead, but isLetter(int) isn't implemented yet |
312 |
|
if (Character.isLetter(text.charAt(i))) |
313 |
|
return last; |
314 |
|
} |
315 |
|
last = current; |
316 |
|
current = wb.next(); |
317 |
|
} |
318 |
|
return BreakIterator.DONE; |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
|
* Finds the start of the previous word for the given offset. |
323 |
|
* |
324 |
|
* @param c |
325 |
|
* the text component |
326 |
|
* @param offs |
327 |
|
* the offset in the document |
328 |
|
* @return the location in the model of the start of the previous word. |
329 |
|
* @throws BadLocationException |
330 |
|
* if the offset is invalid. |
331 |
|
*/ |
332 |
|
public static final int getPreviousWord(JTextComponent c, int offs) |
333 |
|
throws BadLocationException |
334 |
|
{ |
335 |
|
if (offs < 0 || offs > (c.getText().length() - 1)) |
336 |
|
throw new BadLocationException("invalid offset specified", offs); |
337 |
|
String text = c.getText(); |
338 |
|
BreakIterator wb = BreakIterator.getWordInstance(); |
339 |
|
wb.setText(text); |
340 |
|
int last = wb.following(offs); |
341 |
|
int current = wb.previous(); |
342 |
|
while (current != BreakIterator.DONE) |
343 |
|
{ |
344 |
|
for (int i = last; i < current; i++) |
345 |
|
{ |
346 |
|
// FIXME: Should use isLetter(int) and text.codePointAt(int) |
347 |
|
// instead, but isLetter(int) isn't implemented yet |
348 |
|
if (Character.isLetter(text.charAt(i))) |
349 |
|
return last; |
350 |
|
} |
351 |
|
last = current; |
352 |
|
current = wb.next(); |
353 |
|
} |
354 |
|
return BreakIterator.DONE; |
355 |
|
} |
356 |
} |
} |