summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Balkissoon <abalkiss@redhat.com>2005-09-27 16:46:20 +0000
committerAnthony Balkissoon <abalkiss@redhat.com>2005-09-27 16:46:20 +0000
commitae91f91aa000795cef7c83a2791907c9cc2a0a6b (patch)
tree6b8858b82933885adcef5da74082f650b4cea38d
parent68be2b800020fbb561b323b6b01dfb1288b446f2 (diff)
downloadclasspath-ae91f91aa000795cef7c83a2791907c9cc2a0a6b.tar.gz
2005-09-27 Anthony Balkissoon <abalkiss@redhat.com>
* javax/swing/text/Utilities.java: (getRowEnd): New method. (getRowStart): New method.
-rw-r--r--ChangeLog6
-rw-r--r--javax/swing/text/Utilities.java91
2 files changed, 97 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 55fd9385c..f7a483fbc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2005-09-27 Anthony Balkissoon <abalkiss@redhat.com>
+
+ * javax/swing/text/Utilities.java:
+ (getRowEnd): New method.
+ (getRowStart): New method.
+
2005-09-27 Tom Tromey <tromey@redhat.com>
* java/beans/beancontext/BeanContextServicesSupport.java
diff --git a/javax/swing/text/Utilities.java b/javax/swing/text/Utilities.java
index 486c8044c..cf1061374 100644
--- a/javax/swing/text/Utilities.java
+++ b/javax/swing/text/Utilities.java
@@ -394,4 +394,95 @@ public class Utilities
wb.setText(text);
return wb.following(offs);
}
+
+ /**
+ * Get the model position of the end of the row that contains the
+ * specified model position. Return null if the given JTextComponent
+ * does not have a size.
+ * @param c the JTextComponent
+ * @param offs the model position
+ * @return the model position of the end of the row containing the given
+ * offset
+ * @throws BadLocationException if the offset is invalid
+ */
+ public static final int getRowEnd(JTextComponent c, int offs)
+ throws BadLocationException
+ {
+ Element root = c.getDocument().getDefaultRootElement();
+ Element rowElement = root.getElement(root.getElementIndex(offs));
+ String text = c.getText(rowElement.getStartOffset(),
+ rowElement.getEndOffset());
+ if (text == null)
+ return -1;
+
+ // Do a binary search for the smallest position X > offs
+ // such that that character at positino X is not on the same
+ // line as the character at position offs
+ int high = offs + ((text.length() - 1 - offs) / 2);
+ int low = offs;
+ int oldHigh = text.length();
+ while (true)
+ {
+ if (c.modelToView(high).y != c.modelToView(offs).y)
+ {
+ oldHigh = high;
+ high = low + ((high + 1 - low) / 2);
+ if (oldHigh == high)
+ return high - 1;
+ }
+ else
+ {
+ low = high;
+ high += ((oldHigh - high) / 2);
+ if (low == high)
+ return low;
+ }
+ }
+ }
+
+ /**
+ * Get the model position of the start of the row that contains the specified
+ * model position. Return null if the given JTextComponent does not have a
+ * size.
+ *
+ * @param c the JTextComponent
+ * @param offs the model position
+ * @return the model position of the start of the row containing the given
+ * offset
+ * @throws BadLocationException if the offset is invalid
+ */
+ public static final int getRowStart(JTextComponent c, int offs)
+ throws BadLocationException
+ {
+ Element root = c.getDocument().getDefaultRootElement();
+ Element rowElement = root.getElement(root.getElementIndex(offs));
+ String text = c.getText(rowElement.getStartOffset(),
+ rowElement.getEndOffset());
+ if (text == null)
+ return -1;
+
+ // Do a binary search for the greatest position X < offs
+ // such that the character at position X is not on the same
+ // row as the character at position offs
+ int high = offs;
+ int low = 0;
+ int oldLow = 0;
+ while (true)
+ {
+ if (c.modelToView(low).y != c.modelToView(offs).y)
+ {
+ oldLow = low;
+ low = high - ((high + 1 - low) / 2);
+ if (oldLow == low)
+ return low + 1;
+ }
+ else
+ {
+ high = low;
+ low -= ((low - oldLow) / 2);
+ if (low == high)
+ return low;
+ }
+ }
+ }
}