Package java.text

Examples of java.text.BreakIterator


        // assume completely blank line signifies end of sentence
        text = text.replaceFirst("(?ms)\\n\\s*\\n.*", "").trim();
        // assume @tag signifies end of sentence
        text = text.replaceFirst("(?ms)\\n\\s*@(see|param|throws|return|author|since|exception|version|deprecated|todo)\\s.*", "").trim();
        // Comment Summary using first sentence (Locale sensitive)
        BreakIterator boundary = BreakIterator.getSentenceInstance(Locale.getDefault()); // todo - allow locale to be passed in
        boundary.setText(text);
        int start = boundary.first();
        int end = boundary.next();
        if (start > -1 && end > -1) {
            // need to abbreviate this comment for the summary
            text = text.substring(start, end);
        }
        return text;
View Full Code Here


  public static TextBlock createTextBlock(final String text, final Font font,
      final Color paint, final float maxWidth, final int maxLines,
      final TextMeasurer measurer) {

    final TextBlock result = new TextBlock();
    final BreakIterator iterator = BreakIterator.getLineInstance();
    iterator.setText(text);
    int current = 0;
    int lines = 0;
    final int length = text.length();
    while ((current < length) && (lines < maxLines)) {
      final int next = nextLineBreak(text, current, maxWidth, iterator,
View Full Code Here

    if (string.length() == 0) {
      return 0;
    }

    FontMetrics metrics = getFontMetrics(font);
    BreakIterator breakItr = BreakIterator.getLineInstance();
    breakItr.setText(string);
    int MIN, min, max;
    if (avg == 0.0) {
      avg = metrics.getAverageCharWidth();
    }

    int firstBreak = breakItr.next();

    int winNL = string.indexOf("\r\n"); //$NON-NLS-1$
    int macNL = string.indexOf('\r');
    int unixNL = string.indexOf('\n');

    MIN = min = (wrapping == CSSTextLayout.WORD_WRAP_HARD) ? firstBreak : 1;
    if (macNL == winNL) {
      macNL = -1; // If the Mac newline is just the prefix to the win NL,
      // ignore it
    }

    max = string.length() + 1;

    if (winNL != -1) {
      max = Math.min(max, winNL);
      min = Math.min(min, winNL);
    }
    if (unixNL != -1) {
      max = Math.min(max, unixNL);
      min = Math.min(min, unixNL);
    }
    if (macNL != -1) {
      max = Math.min(max, macNL);
      min = Math.min(min, macNL);
    }

    int origMax = max;
    // The size of the current guess
    int guess = 0, guessSize = 0;

    while ((max - min) > 1) {
      // Pick a new guess size
      // New guess is the last guess plus the missing width in pixels
      // divided by the average character size in pixels
      guess = guess + (int) ((availableWidth - guessSize) / avg);

      if (guess >= max) {
        guess = max - 1;
      }
      if (guess <= min) {
        guess = min + 1;
      }

      // Measure the current guess
      guessSize = getStringExtents2(string.substring(0, guess), font).width;

      if (guessSize <= availableWidth) {
        // We did not use the available width
        min = guess;
      } else {
        // We exceeded the available width
        max = guess;
      }
    }

    int result = string.length();
    switch (wrapping) {
    case CSSTextLayout.WORD_WRAP_HARD:
      if (min == string.length() || min == winNL || min == unixNL
          || min == macNL) {
        result = min;
      } else if (max == origMax
          && getStringExtents2(string.substring(0, max), font).width <= availableWidth) {
        result = max;
      } else {
        result = Math.max(MIN, breakItr.preceding(Math.min(max, string
            .length() - 1)));
      }
      break;

    case CSSTextLayout.WORD_WRAP_SOFT:
      if (min == string.length() || min == winNL || min == unixNL
          || min == macNL) {
        result = min;
      } else if (max == origMax
          && getStringExtents2(string.substring(0, max), font).width <= availableWidth) {
        result = max;
      } else if (breakItr.isBoundary(min)) {
        result = min;
      } else if (breakItr.isBoundary(Math.min(max, string.length() - 1))) {
        result = max;
      } else {
        result = breakItr.preceding(Math.min(max, string.length() - 1));
      }
      if (result <= 0) {
        result = min;
      }
      break;
View Full Code Here

        if ( msg.length() <= offset )
        {
            return msg;
        }

        BreakIterator bIter = BreakIterator.getWordInstance();
        StringBuffer buf = new StringBuffer();
        String pad = " ";
        int currentPos = 0;
        bIter.setText( msg );

        while ( offset < bIter.getText().getEndIndex() )
        {
            if ( Character.isWhitespace( bIter.getText().first() ) )
            {
                // remove leading whitespace and continue
                msg = msg.substring( 1 );
                bIter.setText( msg );
                continue;
            }

            // get the last boundary before the specified offset
            currentPos = bIter.preceding( offset );
            // append from the start to currentPos
            buf.append( msg.substring( 0, currentPos ) );

            // start next line
            buf.append( LS );

            //pad with spaces to create indent
            for ( int i = 0; i != wrapIndent && i < lineWidth; i++ )
            {
                buf.append( pad );
            }

            // set the text of the break iterator to be the rest
            // of the string not already appended
            msg = msg.substring( currentPos );

            //reset the text for another go
            bIter.setText( msg );
        }

        // remove leading whitespace and continue
        while ( Character.isWhitespace( msg.charAt( 0 ) ) )
        {
View Full Code Here

  private void wrapAndSetLabelText(JLabel label, String text) {
    FontMetrics fm = label.getFontMetrics(label.getFont());
    Container container = label.getParent();
    int containerWidth = container.getWidth();

    BreakIterator boundary = BreakIterator.getWordInstance();
    boundary.setText(text);

    StringBuffer trial = new StringBuffer();
    StringBuffer real = new StringBuffer("<html><center>");

    int start = boundary.first();
    for (int end=boundary.next(); end!=BreakIterator.DONE; start=end, end=boundary.next()) {
      String word = text.substring(start, end);
      trial.append(word);
      int trialWidth = SwingUtilities.computeStringWidth(fm, trial.toString());
      if (trialWidth > containerWidth) {
        trial = new StringBuffer(word);
View Full Code Here

    int getBreakLocation(final Segment s, final FontMetrics fm, final int start, final int end,
            final TabExpander t, final int pos) {
        int offset = s.offset;
        int index = Utilities.getTabbedTextOffset(s, fm, start, end, t, pos, false);
        int fullIndex = offset + index;
        BreakIterator bi = BreakIterator.getWordInstance();
        bi.setText(s);
        if (bi.last() <= fullIndex) {
            return bi.last() - offset;
        }
        if (bi.isBoundary(fullIndex)) {
            return Character.isWhitespace(s.array[fullIndex]) ? index + 1 : index;
        }
        int prev = bi.preceding(fullIndex);
        if (prev == bi.first()) {
            return index;
        }
        return prev - offset;
    }
View Full Code Here

        assertEquals(-1, Utilities.getPositionBelow(jta, 1, 0));
    }

    void getWordStartTest(final JTextComponent c) {
        AbstractDocument ad = (AbstractDocument) c.getDocument();
        BreakIterator bi = BreakIterator.getWordInstance();
        int length = ad.getLength();
        try {
            bi.setText(ad.getText(0, ad.getLength()));
        } catch (BadLocationException e) {
        }
        int iteratorWordStart = 0;
        bi.first();
        for (int i = 0; i < length; i++) {
            int utilitiesWordStart = 0;
            if (i < length - 1) {
                iteratorWordStart = bi.preceding(i + 1);
            } else {
                bi.last();
                iteratorWordStart = bi.previous();
            }
            try {
                utilitiesWordStart = Utilities.getWordStart(c, i);
            } catch (BadLocationException e) {
            }
View Full Code Here

        getWordStartTest(jtf);
    }

    void getWordEndTest(final JTextComponent c) {
        AbstractDocument ad = (AbstractDocument) c.getDocument();
        BreakIterator bi = BreakIterator.getWordInstance();
        int length = ad.getLength();
        try {
            bi.setText(ad.getText(0, length));
        } catch (BadLocationException e) {
        }
        bi.first();
        for (int i = 0; i < length; i++) {
            int utilitiesWordEnd = 0;
            int iteratorWordEnd = bi.following(i);
            try {
                utilitiesWordEnd = Utilities.getWordEnd(c, i);
            } catch (BadLocationException e) {
            }
            assertEquals(iteratorWordEnd, utilitiesWordEnd);
View Full Code Here

        getRowStartEndTest(jtf);
    }

    void getPreviousWordTest(final JTextComponent c) {
        AbstractDocument ad = (AbstractDocument) c.getDocument();
        BreakIterator bi = BreakIterator.getWordInstance();
        int length = ad.getLength();
        String content = null;
        try {
            content = ad.getText(0, ad.getLength());
            bi.setText(content);
        } catch (BadLocationException e) {
        }
        assertNotNull(content);
        bi.first();
        for (int i = 0; i < length; i++) {
            int utilitiesPrevWord = 0;
            int iteratorPrevWord = bi.preceding(i);
            while (iteratorPrevWord > 0
                    && ((content.charAt(iteratorPrevWord) == ' ' || content
                            .charAt(iteratorPrevWord) == '\n') || content
                            .charAt(iteratorPrevWord) == '\t')) {
                iteratorPrevWord = bi.preceding(iteratorPrevWord);
            }
            try {
                utilitiesPrevWord = Utilities.getPreviousWord(c, i);
            } catch (BadLocationException e) {
            }
View Full Code Here

        getPreviousWordTest(jtf);
    }

    void getNextWordTest(final JTextComponent c) {
        AbstractDocument ad = (AbstractDocument) c.getDocument();
        BreakIterator bi = BreakIterator.getWordInstance();
        int length = ad.getLength();
        String content = null;
        try {
            content = ad.getText(0, ad.getLength());
            bi.setText(content);
        } catch (BadLocationException e) {
        }
        assertNotNull(content);
        bi.first();
        for (int i = 0; i < length; i++) {
            int utilitiesNextWord = 0;
            int iteratorNextWord = bi.following(i);
            while (iteratorNextWord < length
                    && ((content.charAt(iteratorNextWord) == ' ' || content
                            .charAt(iteratorNextWord) == '\n') || content
                            .charAt(iteratorNextWord) == '\t')) {
                iteratorNextWord = bi.following(iteratorNextWord);
            }
            try {
                utilitiesNextWord = Utilities.getNextWord(c, i);
            } catch (BadLocationException e) {
            }
View Full Code Here

TOP

Related Classes of java.text.BreakIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.