Hi, this patch adds a helper method which I need for the TextAction instances in DefaultEditorKit which I am currently rewriting (in other words the code is not used yet).
More important are the small fixes to getWordStart and getPreviousWord. I found
out that the RI gives other return values and accepts a bigger range of input
offsets. The patch fixes that. Still these method have problems when numbers are
embedded into the text. I post a PR about this later.
2006-05-13 Robert Schuster <[EMAIL PROTECTED]>
* javax/swing/text/Utilities.java:
(getNextWord): Fixed grammar in exception message.
(getPreviousWord): Changed expression in first if-clause, added sub-
expression to if-clause in while-loop.
(getWordStart): Changed expression in if-clause.
getNextVisualPositionFrom): Added package-private helper method.
cya
Robert
Index: javax/swing/text/Utilities.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/Utilities.java,v
retrieving revision 1.32
diff -u -r1.32 Utilities.java
--- javax/swing/text/Utilities.java 28 Apr 2006 09:44:08 -0000 1.32
+++ javax/swing/text/Utilities.java 13 May 2006 11:25:46 -0000
@@ -43,6 +43,9 @@
import java.awt.Point;
import java.text.BreakIterator;
+import javax.swing.SwingConstants;
+import javax.swing.text.Position.Bias;
+
/**
* A set of utilities to deal with text. This is used by several other classes
* inside this package.
@@ -337,7 +340,7 @@
// location or is not whitespace (meaning it is a number or
// punctuation). The first case means that 'last' denotes the
// beginning of a word while the second case means it is the start
- // of some else.
+ // of something else.
if (Character.isLetter(cp)
|| !Character.isWhitespace(cp))
return last;
@@ -346,7 +349,7 @@
current = wb.next();
}
- throw new BadLocationException("no more word", offs);
+ throw new BadLocationException("no more words", offs);
}
/**
@@ -363,24 +366,36 @@
public static final int getPreviousWord(JTextComponent c, int offs)
throws BadLocationException
{
- if (offs < 0 || offs > (c.getText().length() - 1))
- throw new BadLocationException("invalid offset specified", offs);
String text = c.getText();
+
+ if (offs <= 0 || offs > text.length())
+ throw new BadLocationException("invalid offset specified", offs);
+
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(text);
int last = wb.preceding(offs);
int current = wb.previous();
+ int cp;
while (current != BreakIterator.DONE)
{
for (int i = last; i < offs; i++)
{
- if (Character.isLetter(text.codePointAt(i)))
+ cp = text.codePointAt(i);
+
+ // Return the last found bound if there is a letter at the current
+ // location or is not whitespace (meaning it is a number or
+ // punctuation). The first case means that 'last' denotes the
+ // beginning of a word while the second case means it is the start
+ // of some else.
+ if (Character.isLetter(cp)
+ || !Character.isWhitespace(cp))
return last;
}
last = current;
current = wb.previous();
}
+
return 0;
}
@@ -394,14 +409,17 @@
public static final int getWordStart(JTextComponent c, int offs)
throws BadLocationException
{
- if (offs < 0 || offs >= c.getText().length())
+ String text = c.getText();
+
+ if (offs < 0 || offs > text.length())
throw new BadLocationException("invalid offset specified", offs);
- String text = c.getText();
BreakIterator wb = BreakIterator.getWordInstance();
wb.setText(text);
+
if (wb.isBoundary(offs))
return offs;
+
return wb.preceding(offs);
}
@@ -674,4 +692,38 @@
else
return offs+1;
}
+
+ /** This is an internal helper method which is used by the
+ * <code>javax.swing.text</code> package. It simply delegates the
+ * call to a method with the same name on the <code>NavigationFilter</code>
+ * of the provided <code>JTextComponent</code> (if it has one) or its UI.
+ *
+ * If the underlying method throws a <code>BadLocationException</code> it
+ * will be swallowed and the initial offset is returned.
+ */
+ static int getNextVisualPositionFrom(JTextComponent t, int offset, int direction)
+ {
+ NavigationFilter nf = t.getNavigationFilter();
+
+ try
+ {
+ return (nf != null)
+ ? nf.getNextVisualPositionFrom(t,
+ offset,
+ Bias.Forward,
+ direction,
+ null)
+ : t.getUI().getNextVisualPositionFrom(t,
+ offset,
+ Bias.Forward,
+ direction,
+ null);
+ }
+ catch (BadLocationException ble)
+ {
+ return offset;
+ }
+
+ }
+
}
signature.asc
Description: OpenPGP digital signature
