Example usage for javax.swing.text Element getElementIndex

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

Introduction

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

Prototype

public int getElementIndex(int offset);

Source Link

Document

Gets the child element index closest to the given offset.

Usage

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

/**
 * Gathers the syntax errors found by Rhino in-process when parsing the
 * document.//from   w  w w .  j  a  va 2s  .  com
 *
 * @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:processing.app.EditorTab.java

private SketchTextArea createTextArea(RSyntaxDocument document) throws IOException {
    final SketchTextArea textArea = new SketchTextArea(document, editor.base.getPdeKeywords());
    textArea.setName("editor");
    textArea.setFocusTraversalKeysEnabled(false);
    //textArea.requestFocusInWindow();
    textArea.setMarkOccurrences(PreferencesData.getBoolean("editor.advanced"));
    textArea.setMarginLineEnabled(false);
    textArea.setCodeFoldingEnabled(PreferencesData.getBoolean("editor.code_folding"));
    textArea.setAutoIndentEnabled(PreferencesData.getBoolean("editor.indent"));
    textArea.setCloseCurlyBraces(PreferencesData.getBoolean("editor.auto_close_braces", true));
    textArea.setAntiAliasingEnabled(PreferencesData.getBoolean("editor.antialias"));
    textArea.setTabsEmulated(PreferencesData.getBoolean("editor.tabs.expand"));
    textArea.setTabSize(PreferencesData.getInteger("editor.tabs.size"));
    textArea.addHyperlinkListener(evt -> {
        try {//from w  w  w .j  a va  2  s.c  om
            UpdatableBoardsLibsFakeURLsHandler boardLibHandler = new UpdatableBoardsLibsFakeURLsHandler(
                    editor.base);
            boardLibHandler.openBoardLibManager(evt.getURL());
        } catch (Exception e) {
            try {
                editor.platform.openURL(editor.getSketch().getFolder(), evt.getURL().toExternalForm());
            } catch (Exception f) {
                Base.showWarning(f.getMessage(), f.getMessage(), f);
            }
        }
    });
    textArea.addCaretListener(e -> {
        Element root = textArea.getDocument().getDefaultRootElement();
        int lineStart = root.getElementIndex(e.getMark());
        int lineEnd = root.getElementIndex(e.getDot());

        editor.lineStatus.set(lineStart, lineEnd);
    });
    ToolTipManager.sharedInstance().registerComponent(textArea);

    configurePopupMenu(textArea);
    return textArea;
}

From source file:ru.gelin.fictionbook.reader.models.FBSimpleElementTest.java

@Test
public void testGetElementIndexLess() {
    Node node = fb.getDocument().selectSingleNode("//fb:body[2]");
    Element body = document.getElement(node);
    //index of the first child
    assertEquals(0, body.getElementIndex(1));
}

From source file:ru.gelin.fictionbook.reader.models.FBSimpleElementTest.java

@Test
public void testGetElementIndexMore() {
    Node node = fb.getDocument().selectSingleNode("//fb:body[1]");
    int children = node.selectNodes("*[node()]").size();
    Element body = document.getElement(node);
    //index of last subelement of this <body>
    assertEquals(children - 1, body.getElementIndex(document.getLength()));
}

From source file:ru.gelin.fictionbook.reader.models.FBSimpleElementTest.java

@Test
public void testGetElementIndex() {
    Node node = fb.getDocument().selectSingleNode("//fb:section[@id='section1']");
    Element section = document.getElement(node);
    node = node.selectSingleNode("fb:p");
    Element p = document.getElement(node);
    //index of <p> after <title> in <section>
    assertEquals(1, section.getElementIndex(p.getStartOffset() + 1));
}