Example usage for javax.swing.text Utilities getRowEnd

List of usage examples for javax.swing.text Utilities getRowEnd

Introduction

In this page you can find the example usage for javax.swing.text Utilities getRowEnd.

Prototype

@SuppressWarnings("deprecation")
public static final int getRowEnd(JTextComponent c, int offs) throws BadLocationException 

Source Link

Document

Determines the ending row model position of the row that contains the specified model position.

Usage

From source file:Main.java

/**
 * Get the string from the passed in textComponent representing the text displayed by the
 * component with all the soft new lines (added in from text wrapping) added in. 
 * // w  ww  . jav  a  2s . c  o  m
 * @param c
 * @return
 *    Returns null if comp does not have a size
 */
public static String getWrappedText(JTextComponent c) {
    int len = c.getDocument().getLength();
    int offset = 0;

    // Increase 10% for extra newlines
    StringBuffer buf = new StringBuffer((int) (len * 1.10));

    try {
        while (offset < len) {
            int end = Utilities.getRowEnd(c, offset);
            if (end < 0) {
                break;
            }

            // Include the last character on the line
            end = Math.min(end + 1, len);

            String s = c.getDocument().getText(offset, end - offset);
            buf.append(s);

            // Add a newline if s does not have one
            if (!s.endsWith("\n")) {
                buf.append('\n');
            }
            offset = end;
        }
    } catch (BadLocationException e) {
    }
    return buf.toString();
}

From source file:de.unidue.inf.is.ezdl.gframedl.components.checkboxlist.CheckBoxListCellRenderer.java

private void shortenDescription() {
    List<String> lines = new ArrayList<String>();

    int length = description.getDocument().getLength();
    int offset = 0;

    try {/*  ww  w .j av  a 2s.  c  o  m*/
        while (offset < length) {
            int end = Utilities.getRowEnd(description, offset);

            if (end < 0) {
                break;
            }

            // Include the last character on the line
            end = Math.min(end + 1, length);

            String line = description.getDocument().getText(offset, end - offset);

            // Remove the line break character
            if (line.endsWith("\n")) {
                line = line.substring(0, line.length() - 1);
            }

            lines.add(line);

            offset = end;
        }
    } catch (BadLocationException e) {
        logger.error(e.getMessage(), e);
    }

    makeDescriptionText(lines);

    makeTooltipText(lines);
}