Example usage for javax.swing.text Element getEndOffset

List of usage examples for javax.swing.text Element getEndOffset

Introduction

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

Prototype

public int getEndOffset();

Source Link

Document

Fetches the offset from the beginning of the document that this element ends at.

Usage

From source file:org.domainmath.gui.MainFrame.java

private void gotoItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gotoItemActionPerformed
    if (fileTab.getSelectedIndex() >= 0) {
        String s = JOptionPane.showInputDialog("Line Number:");
        RTextScrollPane t = (RTextScrollPane) fileTab.getComponentAt(fileTab.getSelectedIndex());
        RSyntaxTextArea selectedArea = (RSyntaxTextArea) t.getTextArea();
        try {/* ww w  . j ava 2  s  .c  o  m*/
            Element element = selectedArea.getDocument().getDefaultRootElement();
            int lineRequested = Integer.parseInt(s);
            int rowCount = element.getElementCount();
            if (lineRequested > rowCount || lineRequested < 0) {

                setVisible(false);
                return;
            }
            Element row = null;
            int firstCharacter = 0;
            int rowNumber = 0;
            for (int i = 0; i < lineRequested; ++i) {
                firstCharacter = getFirstCharacter(row);
                rowNumber = element.getElementIndex(firstCharacter);
                row = element.getElement(rowNumber);
            }
            int lastColumnInRow = row.getEndOffset();
            selectedArea.select(firstCharacter, lastColumnInRow - 1);

        } catch (Exception e) {

        }
    }
}

From source file:org.executequery.gui.editor.QueryEditorTextPane.java

public String getTextAtRow(int rowNumber) {

    Element line = getElementMap().getElement(rowNumber);

    int startOffset = line.getStartOffset();
    int endOffset = line.getEndOffset();
    try {/*from  ww  w. j  av a 2 s . c  o  m*/

        return getText(startOffset, (endOffset - startOffset));

    } catch (BadLocationException e) {

        e.printStackTrace();
        return null;
    }
}

From source file:org.fife.rsta.ac.js.JavaScriptParser.java

/**
 * Gathers the syntax errors found by Rhino in-process when parsing the
 * document.// w ww  .j a v a  2s.co  m
 *
 * @param errorHandler The errors found by Rhino.
 * @param root The root element of the document parsed.
 * @see #gatherParserErrorsJsHint(RSyntaxDocument)
 */
private void gatherParserErrorsRhino(ErrorCollector errorHandler, Element root) {

    List<ParseProblem> errors = errorHandler.getErrors();
    if (errors != null && errors.size() > 0) {

        for (ParseProblem problem : errors) {

            int offs = problem.getFileOffset() - MIRTH_SCRIPT_PREFIX.length();
            if (offs >= root.getEndOffset()) {
                offs = root.getEndOffset() - MIRTH_SCRIPT_SUFFIX.length();
            }
            int len = problem.getLength();
            int line = root.getElementIndex(offs);
            String desc = problem.getMessage();
            DefaultParserNotice notice = new DefaultParserNotice(this, desc, line, offs, len);
            if (problem.getType() == ParseProblem.Type.Warning) {
                notice.setLevel(ParserNotice.Level.WARNING);
            }
            result.addNotice(notice);

        }

    }

}

From source file:org.fife.rsta.ac.js.JavaScriptParser.java

/**
 * {@inheritDoc}//w w  w  .j  a v  a2  s.  co  m
 */
public ParseResult parse(RSyntaxDocument doc, String style) {

    astRoot = null;
    result.clearNotices();
    // Always spell check all lines, for now.
    Element root = doc.getDefaultRootElement();
    int lineCount = root.getElementCount();
    result.setParsedLines(0, lineCount - 1);

    DocumentReader r = new DocumentReader(doc);
    ErrorCollector errorHandler = new ErrorCollector();
    long start = System.currentTimeMillis();
    try {
        String script = IOUtils.toString(r);
        CompilerEnvirons env = createCompilerEnvironment(errorHandler, langSupport);
        Parser parser = new Parser(env);
        astRoot = parser.parse(new StringReader(MIRTH_SCRIPT_PREFIX + script + MIRTH_SCRIPT_SUFFIX), null, 0);
        env = createCompilerEnvironment(new ErrorCollector(), langSupport);
        astRoot = new Parser(env).parse(script, null, 0);
        long time = System.currentTimeMillis() - start;
        result.setParseTime(time);
    } catch (IOException ioe) { // Never happens
        result.setError(ioe);
        ioe.printStackTrace();
    } catch (RhinoException re) {
        // Shouldn't happen since we're passing an ErrorCollector in
        int line = re.lineNumber();
        // if (line>0) {
        Element elem = root.getElement(line);
        int offs = elem.getStartOffset();
        int len = elem.getEndOffset() - offs - 1;
        String msg = re.details();
        result.addNotice(new DefaultParserNotice(this, msg, line, offs, len));
        // }
    } catch (Exception e) {
        result.setError(e); // catch all
    }

    r.close();

    // Get any parser errors.
    switch (langSupport.getErrorParser()) {
    default:
    case RHINO:
        gatherParserErrorsRhino(errorHandler, root);
        break;
    case JSHINT:
        gatherParserErrorsJsHint(doc);
        break;
    }

    // addNotices(doc);
    support.firePropertyChange(PROPERTY_AST, null, astRoot);

    return result;

}

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

private void handleBackspace() {
    // TODO: This sucks, clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    StyledDocument htmlDoc = editor.getStyledDocument();

    try {/*from   w w w.j a v a 2  s  .  c om*/
        if (pos > 0) {
            if ((editor.getSelectedText()) != null) {
                ExtendedHTMLEditorKit.delete(editor);
                return;
            }

            int sOffset = htmlDoc.getParagraphElement(pos).getStartOffset();

            if (sOffset == editor.getSelectionStart()) {

                if (ExtendedHTMLEditorKit
                        .checkParentsTag(htmlDoc.getParagraphElement(editor.getCaretPosition()), HTML.Tag.LI)) {
                    elem = ExtendedHTMLEditorKit
                            .getListItemParent(htmlDoc.getCharacterElement(editor.getCaretPosition()));
                    boolean content = false;
                    int so = elem.getStartOffset();
                    int eo = elem.getEndOffset();

                    if ((so + 1) < eo) {
                        char[] temp = editor.getText(so, eo - so).toCharArray();
                        for (char aTemp : temp) {
                            if (!Character.isWhitespace(aTemp)) {
                                content = true;
                            }
                        }
                    }

                    if (!content) {
                        elem.getParentElement();
                        ExtendedHTMLEditorKit.removeTag(editor, elem);
                        editor.setCaretPosition(sOffset - 1);
                        return;
                    }
                    editor.setCaretPosition(editor.getCaretPosition() - 1);
                    editor.moveCaretPosition(editor.getCaretPosition() - 2);
                    editor.replaceSelection("");
                    return;
                }
            }

            editor.replaceSelection("");
        }
    } catch (BadLocationException ble) {
        Logging.errorPrint(ble.getMessage(), ble);
    }
}

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

private void handleEnter() {
    // TODO: this sucks.  clean it up
    Element elem;
    int pos = editor.getCaretPosition();
    ExtendedHTMLDocument htmlDoc = (ExtendedHTMLDocument) editor.getStyledDocument();

    try {//w  w  w .jav a 2  s  .  co  m
        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.querybuilder.SQLViewPanel.java

private void highlightErrorLine(Exception e) {
    removeHighlightErrorLine();//  w w  w.  j  av  a 2 s .  c  o m
    // determine error line number, if any
    String er = e.getMessage();
    // error may contain line number info like " line 12" or " line 12, column 3 " ...
    String regex = ".*\\sline\\s(\\d+)([^\\d].*)*";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(er);
    // first found is line number
    if (m.find()) {
        String lineNo = m.group(1);
        LOG.info("Error on line number : " + lineNo);
        Document ed = sqlEditor.getEditorPanel().getEditorPane().getDocument();
        Element elem = ed.getDefaultRootElement().getElement(Integer.parseInt(lineNo) - 1);
        sqlEditor.getEditorPanel().getEditorPane().setCaretPosition(elem.getStartOffset());
        try {
            errorTag = sqlEditor.getEditorPanel().getEditorPane().getHighlighter()
                    .addHighlight(elem.getStartOffset(), elem.getEndOffset(), errorPainter);
        } catch (BadLocationException e1) {
            LOG.error("Could not highlight sql error line number", e1);
        }
    }
}

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

/**
 * DOCUMENT ME!/*from  w  ww.  j  a v  a  2  s. c  om*/
 */
public void next() {
    for (int count = 0, i = buffer.getCurrentLine(); i < buffer.getLineCount();) {
        Element line = init(i);

        int offset = line.getStartOffset();

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

        if (matcher.find()) {
            int start = offset + matcher.start();
            int end = offset + matcher.end();

            buffer.select(start, end);

            break;
        }

        if (PreferenceManager.getBoolean("snr.warp", false) && (i == (buffer.getLineCount() - 1))) {
            if (count > 1) {
                Toolkit.getDefaultToolkit().beep();

                break;
            }

            count++;

            i = 0;
        } else {
            i++;
        }
    }
}

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

/**
 * DOCUMENT ME!/*from   ww w.ja v  a 2  s. c  om*/
 *
 * @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;
}