Java Code Examples for javax.swing.JEditorPane#getSelectionStart()

The following examples show how to use javax.swing.JEditorPane#getSelectionStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StyledEditorKit.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Applies the given attributes to character
 * content.  If there is a selection, the attributes
 * are applied to the selection range.  If there
 * is no selection, the attributes are applied to
 * the input attribute set which defines the attributes
 * for any new text that gets inserted.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, then replace the existing attributes first
 */
protected final void setCharacterAttributes(JEditorPane editor,
                                      AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    if (p0 != p1) {
        StyledDocument doc = getStyledDocument(editor);
        doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
    }
    StyledEditorKit k = getStyledEditorKit(editor);
    MutableAttributeSet inputAttributes = k.getInputAttributes();
    if (replace) {
        inputAttributes.removeAttributes(inputAttributes);
    }
    inputAttributes.addAttributes(attr);
}
 
Example 2
Source File: StyledEditorKit.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the given attributes to character
 * content.  If there is a selection, the attributes
 * are applied to the selection range.  If there
 * is no selection, the attributes are applied to
 * the input attribute set which defines the attributes
 * for any new text that gets inserted.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, then replace the existing attributes first
 */
protected final void setCharacterAttributes(JEditorPane editor,
                                      AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    if (p0 != p1) {
        StyledDocument doc = getStyledDocument(editor);
        doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
    }
    StyledEditorKit k = getStyledEditorKit(editor);
    MutableAttributeSet inputAttributes = k.getInputAttributes();
    if (replace) {
        inputAttributes.removeAttributes(inputAttributes);
    }
    inputAttributes.addAttributes(attr);
}
 
Example 3
Source File: StyledEditorKit.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the given attributes to character
 * content.  If there is a selection, the attributes
 * are applied to the selection range.  If there
 * is no selection, the attributes are applied to
 * the input attribute set which defines the attributes
 * for any new text that gets inserted.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, then replace the existing attributes first
 */
protected final void setCharacterAttributes(JEditorPane editor,
                                      AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    if (p0 != p1) {
        StyledDocument doc = getStyledDocument(editor);
        doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
    }
    StyledEditorKit k = getStyledEditorKit(editor);
    MutableAttributeSet inputAttributes = k.getInputAttributes();
    if (replace) {
        inputAttributes.removeAttributes(inputAttributes);
    }
    inputAttributes.addAttributes(attr);
}
 
Example 4
Source File: StyledEditorKit.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the given attributes to character
 * content.  If there is a selection, the attributes
 * are applied to the selection range.  If there
 * is no selection, the attributes are applied to
 * the input attribute set which defines the attributes
 * for any new text that gets inserted.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, then replace the existing attributes first
 */
protected final void setCharacterAttributes(JEditorPane editor,
                                      AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    if (p0 != p1) {
        StyledDocument doc = getStyledDocument(editor);
        doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
    }
    StyledEditorKit k = getStyledEditorKit(editor);
    MutableAttributeSet inputAttributes = k.getInputAttributes();
    if (replace) {
        inputAttributes.removeAttributes(inputAttributes);
    }
    inputAttributes.addAttributes(attr);
}
 
Example 5
Source File: XMLMinifyClipboard.java    From minifierbeans with Apache License 2.0 6 votes vote down vote up
protected final void xmlMinify(final Node[] activatedNodes) {
    final EditorCookie editorCookie
            = Utilities.actionsGlobalContext().lookup(EditorCookie.class);

    for (final JEditorPane pane : editorCookie.getOpenedPanes()) {
        if (pane.isShowing()
                && pane.getSelectionEnd() > pane.getSelectionStart()) {
            try {
                StringSelection content = new StringSelection(selectedSourceAsMinify(pane));
                Toolkit.getDefaultToolkit().getSystemClipboard().
                        setContents(content, content);
                return;
            } catch (final HeadlessException e) {
                org.openide.ErrorManager.getDefault().notify(e);
            }
        }
    }
}
 
Example 6
Source File: StyledEditorKit.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Applies the given attributes to character
 * content.  If there is a selection, the attributes
 * are applied to the selection range.  If there
 * is no selection, the attributes are applied to
 * the input attribute set which defines the attributes
 * for any new text that gets inserted.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, then replace the existing attributes first
 */
protected final void setCharacterAttributes(JEditorPane editor,
                                      AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    if (p0 != p1) {
        StyledDocument doc = getStyledDocument(editor);
        doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
    }
    StyledEditorKit k = getStyledEditorKit(editor);
    MutableAttributeSet inputAttributes = k.getInputAttributes();
    if (replace) {
        inputAttributes.removeAttributes(inputAttributes);
    }
    inputAttributes.addAttributes(attr);
}
 
Example 7
Source File: JSMinifyClipboard.java    From minifierbeans with Apache License 2.0 6 votes vote down vote up
protected final void jsMinify(final Node[] activatedNodes) {
    final EditorCookie editorCookie
            = Utilities.actionsGlobalContext().lookup(EditorCookie.class);

    for (final JEditorPane pane : editorCookie.getOpenedPanes()) {
        if (pane.isShowing()
                && pane.getSelectionEnd() > pane.getSelectionStart()) {
            try {
                StringSelection content = new StringSelection(selectedSourceAsMinify(pane));
                Toolkit.getDefaultToolkit().getSystemClipboard().
                        setContents(content, content);
                return;
            } catch (final HeadlessException e) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }
}
 
Example 8
Source File: StyledEditorKit.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Applies the given attributes to character
 * content.  If there is a selection, the attributes
 * are applied to the selection range.  If there
 * is no selection, the attributes are applied to
 * the input attribute set which defines the attributes
 * for any new text that gets inserted.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, then replace the existing attributes first
 */
protected final void setCharacterAttributes(JEditorPane editor,
                                      AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    if (p0 != p1) {
        StyledDocument doc = getStyledDocument(editor);
        doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
    }
    StyledEditorKit k = getStyledEditorKit(editor);
    MutableAttributeSet inputAttributes = k.getInputAttributes();
    if (replace) {
        inputAttributes.removeAttributes(inputAttributes);
    }
    inputAttributes.addAttributes(attr);
}
 
Example 9
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String getSelectedText(JEditorPane pane, int offset) {
    if (pane != null
            && pane.getCaret() != null
            && pane.getSelectionStart() <= offset
            && offset <= pane.getSelectionEnd()) {
        return pane.getSelectedText();
    }
    return null;
}
 
Example 10
Source File: StatusProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public UpToDateStatus getUpToDate() {
    if (model == null) {
        return UpToDateStatus.UP_TO_DATE_OK;
    }
    FileObject fo = NbEditorUtilities.getFileObject(document);
    boolean ok = false;
    try {
        if (fo.isValid()) {
            DataObject dobj = DataObject.find(fo);
            EditorCookie ed = dobj.getLookup().lookup(EditorCookie.class);
            if (ed != null) {
                JEditorPane[] panes = ed.getOpenedPanes();
                if (panes != null && panes.length > 0) {
                    //#214527
                    JEditorPane pane = panes[0];
                    if (panes.length > 1) {
                        for (JEditorPane p : panes) {
                            if (p.isFocusOwner()) {
                                pane = p;
                                break;
                            }
                        }
                    }
                    //TODO this code is called very often apparently.
                    //we should only run the checks if something changed..
                    //something means file + selection start + selection end.
                    final int selectionStart = pane.getSelectionStart();
                    final int selectionEnd = pane.getSelectionEnd();
                    final int caretPosition = pane.getCaretPosition();
                    RP.post(new Runnable() {
                        @Override
                        public void run() {
                            refreshLinkAnnotations(document, model, selectionStart, selectionEnd);
                        }
                    });
                    if (selectionStart != selectionEnd) { //maybe we want to remove the condition?
                        RP.post(new Runnable() {
                            @Override public void run() {
                                //this condition is important in order not to break any running hints
                                //the model sync+refresh renders any existing POMComponents people
                                // might be holding useless
                                if (!model.isIntransaction()) {
                                    HintsController.setErrors(document, LAYER_POM_SELECTION, findHints(model, project, selectionStart, selectionEnd, caretPosition));
                                } else {
                                    HintsController.setErrors(document, LAYER_POM_SELECTION, Collections.<ErrorDescription>emptyList());
                                }
                                
                            }
                        });
                        ok = true;
                        return UpToDateStatus.UP_TO_DATE_PROCESSING;
                    }
                }
            }
        }
    } catch (DataObjectNotFoundException ex) {
        //#166011 just a minor issue, just log, but don't show to user directly
        LOG.log(Level.INFO, "Touched somehow invalidated FileObject", ex);
    } finally {
        if (!ok) {
            HintsController.setErrors(document, LAYER_POM_SELECTION, Collections.<ErrorDescription>emptyList());
        }
    }
    return UpToDateStatus.UP_TO_DATE_OK; // XXX should use UP_TO_DATE_PROCESSING if checkHints task is currently running
}
 
Example 11
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String getIdentifier (
    StyledDocument doc, 
    JEditorPane ep, 
    int offset
) {
    String t = null;
    if ( (ep.getSelectionStart () <= offset) &&
         (offset <= ep.getSelectionEnd ())
    )   t = ep.getSelectedText ();
    if (t != null) return t;
    
    int line = NbDocument.findLineNumber (
        doc,
        offset
    );
    int col = NbDocument.findLineColumn (
        doc,
        offset
    );
    try {
        Element lineElem = 
            NbDocument.findLineRootElement (doc).
            getElement (line);

        if (lineElem == null) return null;
        int lineStartOffset = lineElem.getStartOffset ();
        int lineLen = lineElem.getEndOffset() - lineStartOffset;
        t = doc.getText (lineStartOffset, lineLen);
        lineLen = t.length ();
        int identStart = col;
        while ( (identStart > 0) && 
                (t.charAt (identStart - 1) != '"')
        ) {
            identStart--;
        }
        int identEnd = Math.max (col, 1);
        while ( (identEnd < lineLen) && 
                (t.charAt (identEnd - 1) != '"')
        ) {
            identEnd++;
        }

        if (identStart == identEnd) return null;
        return t.substring (identStart, identEnd - 1);
    } catch (BadLocationException e) {
        return null;
    }
}
 
Example 12
Source File: AbstractJSToolTipAnnotation.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String getIdentifier (
    StyledDocument doc,
    JEditorPane ep,
    int line, int column, int offset,
    boolean[] isFunctionPtr
) {
    // do always evaluation if the tooltip is invoked on a text selection
    String t = null;
    if ( (ep.getSelectionStart () <= offset) &&
         (offset <= ep.getSelectionEnd ())
    ) {
        t = ep.getSelectedText ();
    }
    if (t != null) {
        return t;
    }
    Element lineElem =
        NbDocument.findLineRootElement (doc).
        getElement (line);

    if (lineElem == null) {
        return null;
    }
    int lineStartOffset = lineElem.getStartOffset ();
    int lineLen = lineElem.getEndOffset() - lineStartOffset;
    try {
        t = doc.getText (lineStartOffset, lineLen);
    } catch (BadLocationException ble) {
        return null;
    }
    column = Math.min(column, t.length());
    int identStart = column;

    boolean wasDot = false;
    while (identStart > 0) {
        char c = t.charAt (identStart - 1);
        if (Character.isJavaIdentifierPart(c) ||
            (c == '.' && (wasDot = true)) ||        // Please note that '=' is intentional here.
            (wasDot && (c == ']' || c == '['))) {

            identStart--;
        } else {
            break;
        }
    }
    int identEnd = column;
    while (identEnd < lineLen &&
            Character.isJavaIdentifierPart(t.charAt(identEnd))) {
        identEnd++;
    }
    if (identStart == identEnd) {
        return null;
    }

    String ident = t.substring (identStart, identEnd).trim();
    if (JS_KEYWORDS.contains(ident)) {
        // Java keyword => Do not show anything
        return null;
    }

    while (identEnd < lineLen &&
           Character.isWhitespace(t.charAt(identEnd))
    ) {
        identEnd++;
    }
    if (identEnd < lineLen && t.charAt(identEnd) == '(') {
        // We're at a function call
        isFunctionPtr[0] = true;
    }
    return ident;
}
 
Example 13
Source File: StyledEditorKit.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 14
Source File: StyledEditorKit.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 15
Source File: StyledEditorKit.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 16
Source File: StyledEditorKit.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 17
Source File: StyledEditorKit.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 18
Source File: StyledEditorKit.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 19
Source File: StyledEditorKit.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
 
Example 20
Source File: StyledEditorKit.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Applies the given attributes to paragraphs.  If
 * there is a selection, the attributes are applied
 * to the paragraphs that intersect the selection.
 * if there is no selection, the attributes are applied
 * to the paragraph at the current caret position.
 *
 * @param editor the editor
 * @param attr the attributes
 * @param replace   if true, replace the existing attributes first
 */
protected final void setParagraphAttributes(JEditorPane editor,
                                   AttributeSet attr, boolean replace) {
    int p0 = editor.getSelectionStart();
    int p1 = editor.getSelectionEnd();
    StyledDocument doc = getStyledDocument(editor);
    doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}