summaryrefslogtreecommitdiff
path: root/javax/swing/text/Utilities.java
diff options
context:
space:
mode:
Diffstat (limited to 'javax/swing/text/Utilities.java')
-rw-r--r--javax/swing/text/Utilities.java66
1 files changed, 59 insertions, 7 deletions
diff --git a/javax/swing/text/Utilities.java b/javax/swing/text/Utilities.java
index f154e55aa..36361f497 100644
--- a/javax/swing/text/Utilities.java
+++ b/javax/swing/text/Utilities.java
@@ -43,6 +43,9 @@ import java.awt.Graphics;
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 @@ public class Utilities
// 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 @@ public class Utilities
current = wb.next();
}
- throw new BadLocationException("no more word", offs);
+ throw new BadLocationException("no more words", offs);
}
/**
@@ -363,24 +366,36 @@ public class Utilities
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 class Utilities
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 @@ public class Utilities
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;
+ }
+
+ }
+
}