Example usage for javax.swing.text BadLocationException getMessage

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

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:plugin.notes.gui.NotesView.java

private void handleEnter() {
    // TODO: this sucks.  clean it up
    Element elem;//  w ww.j  av a  2  s  . co m
    int pos = editor.getCaretPosition();
    ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();

    try {
        if (ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()),
                HTML.Tag.UL)
                || ExtendedHTMLEditorKit.checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()),
                        HTML.Tag.OL)) {
            elem = ExtendedHTMLEditorKit
                    .getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));

            int so = elem.getStartOffset();
            int eo = elem.getEndOffset();
            char[] temp = editor.getText(so, eo - so).toCharArray();
            boolean content = false;

            for (char aTemp : temp) {
                if (!Character.isWhitespace(aTemp)) {
                    content = true;
                }
            }

            int repos = -1;
            if (content) {
                int end = -1;
                int j = temp.length;

                do {
                    j--;

                    if (Character.isLetterOrDigit(temp[j])) {
                        end = j;
                    }
                } while ((end == -1) && (j >= 0));

                j = end;

                do {
                    j++;

                    if (!Character.isSpaceChar(temp[j])) {
                        repos = j - end - 1;
                    }
                } while ((repos == -1) && (j < temp.length));

                if (repos == -1) {
                    repos = 0;
                }
            }

            if ((elem.getStartOffset() == elem.getEndOffset()) || !content) {
                manageListElement(htmlDoc);
            } else {
                if ((editor.getCaretPosition() + 1) == elem.getEndOffset()) {
                    ExtendedHTMLEditorKit.insertListElement(editor, "");
                    editor.setCaretPosition(pos - repos);
                } else {
                    int caret = editor.getCaretPosition();
                    String tempString = editor.getText(caret, eo - caret);
                    editor.select(caret, eo - 1);
                    editor.replaceSelection("");
                    ExtendedHTMLEditorKit.insertListElement(editor, tempString);

                    Element newLi = ExtendedHTMLEditorKit
                            .getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    editor.setCaretPosition(newLi.getEndOffset());
                }
            }
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxDocument.java

/**
 * Parse the entire document and return list of tokens that do not already
 * exist in the tokens list. There may be overlaps, and replacements, 
 * which we will cleanup later.//  w  ww .  jav a 2  s  .  c o  m
 * 
 * @return list of tokens that do not exist in the tokens field 
 */
private void parse() {
    // if we have no lexer, then we must have no tokens...
    if (lexer == null) {
        tokens = null;
        return;
    }

    List<Token> tokens = new ArrayList<Token>(getLength() / 10);
    long time = System.nanoTime();
    int length = getLength();
    try {
        Segment segment = new Segment();
        getText(0, getLength(), segment);
        CharArrayReader reader = new CharArrayReader(segment.array, segment.offset, segment.count);
        lexer.yyreset(reader);
        Token token;
        while ((token = lexer.yylex()) != null) {
            tokens.add(token);
        }
    } catch (BadLocationException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        // This will not be thrown from the Lexer
        LOG.error(e.getMessage(), e);
    } finally {
        // Benchmarks:
        // Parsed 574038 chars in 81 ms, giving 74584 tokens
        //                System.out.printf("Parsed %d in %d ms, giving %d tokens",
        //                        len, (System.nanoTime() - ts) / 1000000, toks.size());
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Parsed %d in %d ms, giving %d tokens", length,
                    (System.nanoTime() - time) / 1000000, tokens.size()));
        }
        this.tokens = tokens;
    }
}

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxUtil.java

/**
 * Return the lines that span the selection (split as an array of Strings)
 * if there is no selection then current line is returned.
 * /*from w  w w . j  a va 2s .  c o m*/
 * Note that the strings returned will not contain the terminating line feeds.
 * 
 * The text component will then have the full lines set as selection.
 * 
 * @param target
 * @return String[] of lines spanning selection / or Dot
 */
public static String[] getSelectedLines(JTextComponent target) {
    String[] lines = null;
    try {
        PlainDocument document = (PlainDocument) target.getDocument();
        int start = document.getParagraphElement(target.getSelectionStart()).getStartOffset();
        int end;
        if (target.getSelectionStart() == target.getSelectionEnd()) {
            end = document.getParagraphElement(target.getSelectionEnd()).getEndOffset();
        } else {
            // if more than one line is selected, we need to subtract one from the end
            // so that we do not select the line with the caret and no selection in it
            end = document.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset();
        }
        target.select(start, end);
        lines = document.getText(start, end - start).split("\n");
        target.select(start, end);
    } catch (BadLocationException e) {
        LOG.error(e.getMessage(), e);
        lines = EMPTY_STRING_ARRAY;
    }

    return lines;
}

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxUtil.java

/**
 * Return the line of text at the given position. The returned value may
 * be null. It will not contain the trailing new-line character.
 * /*from w w  w .ja  v a2 s .  com*/
 * @param doc
 * @param pos
 * @return
 */
public static String getLineAt(PlainDocument doc, int pos) {
    String line = null;
    int start = doc.getParagraphElement(pos).getStartOffset();
    int end = doc.getParagraphElement(pos).getEndOffset();
    try {
        line = doc.getText(start, end - start);
        if (line != null && line.endsWith("\n")) {
            line = line.substring(0, line.length() - 1);
        }
    } catch (BadLocationException e) {
        LOG.error(e.getMessage(), e);
    }

    return line;
}

From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxView.java

@Override
protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1) {
    Font saveFont = graphics.getFont();
    Color saveColor = graphics.getColor();
    SyntaxDocument doc = (SyntaxDocument) getDocument();
    Segment segment = getLineBuffer();
    try {//from  w w  w  .  j  a  va2 s.co m
        // Colour the parts
        Iterator<Token> tokens = doc.getTokens(p0, p1);
        int start = p0;
        while (tokens.hasNext()) {
            Token t = tokens.next();
            // if there is a gap between the next token start and where we
            // should be starting (spaces not returned in tokens), then draw
            // it in the default type
            if (start < t.start) {
                SyntaxStyles.getInstance().setGraphicsStyle(graphics, TokenType.DEFAULT);
                doc.getText(start, t.start - start, segment);
                x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
            }
            // t and s are the actual start and length of what we should
            // put on the screen.  assume these are the whole token....
            int l = t.length;
            int s = t.start;
            // ... unless the token starts before p0:
            if (s < p0) {
                // token is before what is requested. adgust the length and s
                l -= (p0 - s);
                s = p0;
            }
            // if token end (s + l is still the token end pos) is greater 
            // than p1, then just put up to p1
            if (s + l > p1) {
                l = p1 - s;
            }
            doc.getText(s, l, segment);
            x = SyntaxStyles.getInstance().drawText(segment, x, y, graphics, this, t);
            start = t.start + t.length;
        }
        // now for any remaining text not tokenized:
        if (start < p1) {
            SyntaxStyles.getInstance().setGraphicsStyle(graphics, TokenType.DEFAULT);
            doc.getText(start, p1 - start, segment);
            x = Utilities.drawTabbedText(segment, x, y, graphics, this, start);
        }
    } catch (BadLocationException e) {
        System.err.println("Requested: " + e.offsetRequested());
        LOG.error(e.getMessage(), e);
    } finally {
        graphics.setFont(saveFont);
        graphics.setColor(saveColor);
    }

    return x;
}

From source file:tk.tomby.tedit.core.Buffer.java

public void insertString(int offset, String text) {
    try {/*from  www. j  av a2s .  c o m*/
        getDocument().insertString(offset, text, null);
    } catch (BadLocationException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:tk.tomby.tedit.core.Buffer.java

public void replaceString(int offset, int length, String text) {
    try {/*from  w w w .j  av a  2 s .c om*/
        getDocument().remove(offset, length);
        getDocument().insertString(offset, text, null);
    } catch (BadLocationException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:tk.tomby.tedit.core.Buffer.java

public void removeString(int offset, int length) {
    try {/*from   w w w .j  a v a2  s.com*/
        getDocument().remove(offset, length);
    } catch (BadLocationException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:tk.tomby.tedit.core.snr.FindReplaceWorker.java

/**
 * DOCUMENT ME!/*from w ww . j  a va2 s  .c o  m*/
 */
public void replace() {
    String replace = PreferenceManager.getString("snr.replace", "");

    if ((replace != null) && !replace.equals("")) {
        int start = buffer.getSelectionStart();
        int end = buffer.getSelectionEnd();

        if (start != end) {
            Document doc = buffer.getDocument();

            try {
                doc.remove(start, end - start);
                doc.insertString(start, replace, null);
            } catch (BadLocationException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:tk.tomby.tedit.core.snr.FindReplaceWorker.java

/**
 * DOCUMENT ME!/*from  w w w.  ja  v  a2s.com*/
 *
 * @param lineNumber DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
private Element init(int lineNumber) {
    Document doc = buffer.getDocument();
    Element line = doc.getDefaultRootElement().getElement(lineNumber);

    try {
        int options = Pattern.DOTALL;

        String find = PreferenceManager.getString("snr.find", "");

        if ((find != null) && !find.equals("")) {
            if (PreferenceManager.getBoolean("snr.case", false)) {
                find = find.toLowerCase();

                options |= Pattern.CASE_INSENSITIVE;
            }

            if (PreferenceManager.getBoolean("snr.whole", false)) {
                find = "\\b" + find + "\\b";
            }

            int offset = line.getStartOffset();
            int length = line.getEndOffset() - offset;

            if (PreferenceManager.getInt("snr.direction", FORWARD) == FORWARD) {
                if ((buffer.getSelectionEnd() > line.getStartOffset())
                        && (buffer.getSelectionEnd() <= line.getEndOffset())) {
                    offset = buffer.getSelectionEnd();
                    length = line.getEndOffset() - offset;
                }
            } else {
                if ((buffer.getSelectionStart() > line.getStartOffset())
                        && (buffer.getSelectionStart() <= line.getEndOffset())) {
                    length = buffer.getSelectionStart() - offset;
                }
            }

            String text = doc.getText(offset, length);

            Pattern pattern = Pattern.compile(find, options);

            this.matcher = pattern.matcher(text);
        }
    } catch (BadLocationException e) {
        log.error(e.getMessage(), e);
    }

    return line;
}