Implemented this method that was missing from javax.swing.text.Utilities
and is needed for the implementation of WrappedPlainView that I'm
working on.

2005-10-25  Anthony Balkissoon  <[EMAIL PROTECTED]>

        * javax/swing/text/Utilities.java:
        (getBreakLocation): New API method.

--Tony
Index: javax/swing/text/Utilities.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/Utilities.java,v
retrieving revision 1.14
diff -u -r1.14 Utilities.java
--- javax/swing/text/Utilities.java	6 Oct 2005 20:16:32 -0000	1.14
+++ javax/swing/text/Utilities.java	25 Oct 2005 17:34:22 -0000
@@ -486,4 +486,40 @@
           }
       }
   }
+  
+  /**
+   * Determine where to break the text in the given Segment, attempting to find
+   * a word boundary.
+   * @param s the Segment that holds the text
+   * @param metrics the font metrics used for calculating the break point
+   * @param x0 starting view location representing the start of the text
+   * @param x the target view location
+   * @param e the TabExpander used for expanding tabs (if this is null tabs
+   * are expanded to 1 space)
+   * @param startOffset the offset in the Document of the start of the text
+   * @return the offset at which we should break the text
+   */
+  public static final int getBreakLocation(Segment s, FontMetrics metrics,
+                                           int x0, int x, TabExpander e,
+                                           int startOffset)
+  {
+    int mark = Utilities.getTabbedTextOffset(s, metrics, x0, x, e, startOffset);
+    BreakIterator breaker = BreakIterator.getWordInstance();
+    breaker.setText(s.toString());
+    
+    // If mark is equal to the end of the string, just use that position
+    if (mark == s.count)
+      return mark;
+    
+    // Try to find a word boundary previous to the mark at which we 
+    // can break the text
+    int preceding = breaker.preceding(mark + 1);
+    
+    if (preceding != 0)
+      return preceding;
+    else
+      // If preceding is 0 we couldn't find a suitable word-boundary so
+      // just break it on the character boundary
+      return mark;
+  }
 }
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to