/[classpath]/classpath/javax/swing/text/Utilities.java
ViewVC logotype

Diff of /classpath/javax/swing/text/Utilities.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.11 by abalkiss, Thu Sep 22 20:17:08 2005 UTC revision 1.12 by abalkiss, Tue Sep 27 16:46:21 2005 UTC
# Line 394  public class Utilities Line 394  public class Utilities
394      wb.setText(text);      wb.setText(text);
395      return wb.following(offs);      return wb.following(offs);
396    }    }
397      
398      /**
399       * Get the model position of the end of the row that contains the
400       * specified model position.  Return null if the given JTextComponent
401       * does not have a size.
402       * @param c the JTextComponent
403       * @param offs the model position
404       * @return the model position of the end of the row containing the given
405       * offset
406       * @throws BadLocationException if the offset is invalid
407       */
408      public static final int getRowEnd(JTextComponent c, int offs)
409          throws BadLocationException
410      {
411        Element root = c.getDocument().getDefaultRootElement();
412        Element rowElement = root.getElement(root.getElementIndex(offs));
413        String text = c.getText(rowElement.getStartOffset(),
414                                rowElement.getEndOffset());
415        if (text == null)
416          return -1;
417    
418        // Do a binary search for the smallest position X > offs
419        // such that that character at positino X is not on the same
420        // line as the character at position offs
421        int high = offs + ((text.length() - 1 - offs) / 2);
422        int low = offs;
423        int oldHigh = text.length();
424        while (true)
425          {
426            if (c.modelToView(high).y != c.modelToView(offs).y)
427              {
428                oldHigh = high;
429                high = low + ((high + 1 - low) / 2);
430                if (oldHigh == high)
431                  return high - 1;
432              }
433            else
434              {
435                low = high;
436                high += ((oldHigh - high) / 2);
437                if (low == high)
438                  return low;
439              }
440          }
441      }
442          
443      /**
444       * Get the model position of the start of the row that contains the specified
445       * model position. Return null if the given JTextComponent does not have a
446       * size.
447       *
448       * @param c the JTextComponent
449       * @param offs the model position
450       * @return the model position of the start of the row containing the given
451       *         offset
452       * @throws BadLocationException if the offset is invalid
453       */
454      public static final int getRowStart(JTextComponent c, int offs)
455          throws BadLocationException
456      {
457        Element root = c.getDocument().getDefaultRootElement();
458        Element rowElement = root.getElement(root.getElementIndex(offs));
459        String text = c.getText(rowElement.getStartOffset(),
460                                rowElement.getEndOffset());
461        if (text == null)
462          return -1;
463    
464        // Do a binary search for the greatest position X < offs
465        // such that the character at position X is not on the same
466        // row as the character at position offs
467        int high = offs;
468        int low = 0;
469        int oldLow = 0;
470        while (true)
471          {
472            if (c.modelToView(low).y != c.modelToView(offs).y)
473              {
474                oldLow = low;
475                low = high - ((high + 1 - low) / 2);
476                if (oldLow == low)
477                  return low + 1;
478              }
479            else
480              {
481                high = low;
482                low -= ((low - oldLow) / 2);
483                if (low == high)
484                  return low;
485              }
486          }
487      }
488  }  }

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26