Example usage for javax.swing.text BadLocationException BadLocationException

List of usage examples for javax.swing.text BadLocationException BadLocationException

Introduction

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

Prototype

public BadLocationException(String s, int offs) 

Source Link

Document

Creates a new BadLocationException object.

Usage

From source file:Main.java

protected static int getLineOfOffset(int offset, Document doc) throws BadLocationException {

    if (offset < 0 || doc == null) {
        throw new BadLocationException("", -1);
    } else if (offset > doc.getLength()) {
        throw new BadLocationException("", doc.getLength() + 1);
    } else {//  w  w w  .j  a v  a2  s.c  o  m
        Element map = doc.getDefaultRootElement();
        return map.getElementIndex(offset);
    }
}

From source file:Main.java

public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
    if ((getLength() + str.length()) <= maxSize) {
        super.insertString(offs, str, a);
    } else {/*from w ww.  j  a  v  a2s.  c o  m*/
        throw new BadLocationException("Insertion exceeds max size of document", offs);
    }
}

From source file:Main.java

public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str, AttributeSet attrs)
        throws BadLocationException {
    int newLength = fb.getDocument().getLength() - length + str.length();
    if (newLength <= maxSize) {
        fb.replace(offset, length, str, attrs);
    } else {/*from   w  ww . j  av a 2s. c  o  m*/
        throw new BadLocationException("New characters exceeds max size of document", offset);
    }
}

From source file:MainClass.java

private int checkInput(String proposedValue, int offset) throws BadLocationException {
    int newValue = 0;
    if (proposedValue.length() > 0) {
        try {//from  w ww .  j a  v  a2s  . com
            newValue = Integer.parseInt(proposedValue);
        } catch (NumberFormatException e) {
            throw new BadLocationException(proposedValue, offset);
        }
    }
    if ((minimum <= newValue) && (newValue <= maximum)) {
        return newValue;
    } else {
        throw new BadLocationException(proposedValue, offset);
    }
}

From source file:com.keevosh.springframework.boot.netbeans.SpringBootConfigurationCompletionProvider.java

static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException {
    Element lineElement = doc.getParagraphElement(offset);
    int start = lineElement.getStartOffset();
    while (start + 1 < lineElement.getEndOffset()) {
        try {//from ww w .  j ava 2  s. c  o m
            if (doc.getText(start, 1).charAt(0) != ' ') {
                break;
            }
        } catch (BadLocationException ex) {
            throw (BadLocationException) new BadLocationException(
                    "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(),
                    start).initCause(ex);
        }
        start++;
    }
    return start;
}

From source file:net.team2xh.crt.gui.editor.EditorTextPane.java

/**
 * Returns the first character offset of the given line number.
 *
 * @param line Line number.//from   ww w.j a v  a  2s  .  c om
 * @return First character offset of that line.
 * @throws BadLocationException
 */
public int getLineStartOffset(int line) throws BadLocationException {
    Element map = doc.getDefaultRootElement();
    if (line < 0) {
        throw new BadLocationException("Negative line", -1);
    } else if (line >= map.getElementCount()) {
        throw new BadLocationException("No such line", doc.getLength() + 1);
    } else {
        Element lineElem = map.getElement(line);
        return lineElem.getStartOffset();
    }
}

From source file:net.team2xh.crt.gui.editor.EditorTextPane.java

/**
 * Returns the last character offset of the given line number.
 *
 * @param line Line number.//from w  ww. j  a  va2s .  co m
 * @return Last character offset of that line.
 * @throws BadLocationException
 */
public int getLineEndOffset(int line) throws BadLocationException {
    Element map = doc.getDefaultRootElement();
    if (line < 0) {
        throw new BadLocationException("Negative line", -1);
    } else if (line >= map.getElementCount()) {
        throw new BadLocationException("No such line", doc.getLength() + 1);
    } else {
        Element lineElem = map.getElement(line);
        return lineElem.getEndOffset();
    }
}

From source file:net.team2xh.crt.gui.editor.EditorTextPane.java

/**
 * Returns the line number of the given character offset, for the current document.
 *
 * @param offset Character offset./*ww  w  . jav  a  2 s  . c o m*/
 * @return Line number.
 * @throws BadLocationException
 */
public int getLineOfOffset(int offset) throws BadLocationException {
    if (offset < 0) {
        throw new BadLocationException("Can't translate offset to line", -1);
    } else if (offset > doc.getLength()) {
        throw new BadLocationException("Can't translate offset to line", doc.getLength() + 1);
    } else {
        Element map = doc.getDefaultRootElement();
        return map.getElementIndex(offset);
    }
}

From source file:org.debux.webmotion.netbeans.Utils.java

public static int getRowFirstNonWhite(StyledDocument doc, int offset) throws BadLocationException {
    Element lineElement = doc.getParagraphElement(offset);
    int start = lineElement.getStartOffset();
    while (start + 1 < lineElement.getEndOffset()) {
        try {//from w w  w.j ava 2  s  .c  o  m
            char charAt = doc.getText(start, 1).charAt(0);
            if (charAt != ' ') {
                break;
            }
        } catch (BadLocationException ex) {
            throw (BadLocationException) new BadLocationException(
                    "calling getText(" + start + ", " + (start + 1) + ") on doc of length: " + doc.getLength(),
                    start).initCause(ex);
        }
        start++;
    }
    return start;
}

From source file:org.fit.cssbox.swingbox.SwingBoxEditorKit.java

private void readImpl(InputStream in, SwingBoxDocument doc, int pos) throws IOException, BadLocationException {

    if (component == null)
        throw new IllegalStateException(
                "Component is null, editor kit is probably deinstalled from a JEditorPane.");
    if (pos > doc.getLength() || pos < 0) {
        BadLocationException e = new BadLocationException("Invalid location", pos);
        readError(null, e);/*from  w  w w  . java  2  s  . c  o m*/
        throw e;
    }

    ContentReader rdr = new ContentReader();
    URL url = (URL) doc.getProperty(Document.StreamDescriptionProperty);
    CSSBoxAnalyzer analyzer = getCSSBoxAnalyzer();

    Container parent = component.getParent();
    Dimension dim;
    if (parent != null && parent instanceof JViewport) {
        dim = ((JViewport) parent).getExtentSize();
    } else {
        dim = component.getBounds().getSize();
    }

    if (dim.width <= 10) {
        // component might not be initialized, use screen size :)
        Dimension tmp = Toolkit.getDefaultToolkit().getScreenSize();
        dim.setSize(tmp.width / 2.5, tmp.height / 2.5);
    }

    // long time = System.currentTimeMillis();

    List<ElementSpec> elements;
    try {
        String ctype = null;
        Object ct = doc.getProperty("Content-Type");
        if (ct != null) {
            if (ct instanceof List)
                ctype = (String) ((List<?>) ct).get(0);
            else
                ctype = ct.toString();
        }

        DocumentSource docSource = new StreamDocumentSource(in, url, ctype);
        elements = rdr.read(docSource, analyzer, dim);
        String title = analyzer.getDocumentTitle();
        if (title == null)
            title = "No title";
        doc.putProperty(Document.TitleProperty, title);
    } catch (IOException e) {
        readError(url, e);
        throw e;
    }

    // System.out.println(System.currentTimeMillis() - time + " ms");

    ElementSpec elementsArray[] = elements.toArray(new ElementSpec[0]);
    doc.create(elementsArray);
    // component.revalidate();
    // component.repaint();

    // System.out.println(System.currentTimeMillis() - time + " ms");

    // Dictionary<Object, Object> dic = doc.getDocumentProperties();
    // Enumeration<Object> en = dic.keys();
    // while( en.hasMoreElements()) {
    // Object k = en.nextElement();
    // System.out.println(k + "  " + dic.get(k));
    // }

    readFinish(url);

}