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

The following examples show how to use javax.swing.JEditorPane#getSelectionEnd() . 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 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 2
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 3
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 4
Source File: JSONMinifyClipboard.java    From minifierbeans with Apache License 2.0 6 votes vote down vote up
protected final void jsonMinify(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 5
Source File: StyledEditorKit.java    From openjdk-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 6
Source File: JSMinifyClipboard.java    From minifierbeans with Apache License 2.0 5 votes vote down vote up
private String selectedSourceAsMinify(final JEditorPane pane) {
    MinifyProperty minifyProperty = MinifyProperty.getInstance();
    String minifedString = "";
    String oldContent = "";

    FileObject fileObject = NbEditorUtilities.getFileObject(pane.getDocument());

    try {
        final TokenSequence ts = TokenHierarchy.get(pane.getDocument()).tokenSequence();
        final StringBuilder sb = new StringBuilder();
        ts.move(pane.getSelectionStart());

        while (ts.moveNext() && ts.offset() < pane.getSelectionEnd()) {
            sb.append(ts.token().text().toString());
        }

        MinifyUtil minifyUtil = new MinifyUtil();
        oldContent = sb.toString();
        minifedString = minifyUtil.compressSelectedJavaScript(fileObject.getNameExt(), sb.toString(), minifyProperty);

        NotificationDisplayer.getDefault().notify("Successful copied", NotificationDisplayer.Priority.NORMAL.getIcon(), "Copied as minified JS source.", null);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    if (minifedString.equals("")) {
        minifedString = oldContent;
    }

    return minifedString;
}
 
Example 7
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 8
Source File: JavaRefactoringActionDelegate.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Determine if the action should be enabled.  By default, this method
 * checks for the presence of an open editor;  if <code>requiresSelection</code>
 * was passed to the constructor, it also depends on a text selection in that
 * editor being present.
 * @param context A Lookup containing either an EditorCookie or one or more
 *                Nodes whose lookup contains an EditorCookie
 * @return true if the action should be enabled, false otherwise
 */
protected boolean isEnabled(Lookup context) {
    EditorCookie ck = JavaRefactoringGlobalAction.getEditorCookie(context);
    boolean result = false;
    if(ck != null) {
        JEditorPane pane = NbDocument.findRecentEditorPane(ck);
        result = pane != null;
        if (requiresSelection) {
            result = result && (pane.getSelectionStart() != pane.getSelectionEnd());
        }
    }
    return result;
}
 
Example 9
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String getIdentifier (
    JPDADebugger debugger,
    StyledDocument doc,
    JEditorPane ep,
    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;
    }
    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);
        int identStart = col;
        while (identStart > 0 &&
            (Character.isJavaIdentifierPart (
                t.charAt (identStart - 1)
            ) ||
            (t.charAt (identStart - 1) == '.'))) {
            identStart--;
        }
        int identEnd = col;
        while (identEnd < lineLen &&
               Character.isJavaIdentifierPart(t.charAt(identEnd))
        ) {
            identEnd++;
        }

        if (identStart == identEnd) {
            return null;
        }

        String ident = t.substring (identStart, identEnd);
        //if (JS_KEYWORDS.contains(ident)) {
            // JS 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;
    } catch (BadLocationException e) {
        return null;
    }
}
 
Example 10
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 11
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String getIdentifier (
    JPDADebugger debugger,
    StyledDocument doc,
    JEditorPane ep,
    int offset,
    boolean[] isMethodPtr,
    String[] fieldOfPtr
) {
    // 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;
    }
    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);
        int identStart = col;
        while (identStart > 0 &&
            (Character.isJavaIdentifierPart (
                t.charAt (identStart - 1)
            ) ||
            (t.charAt (identStart - 1) == '.'))) {
            identStart--;
        }
        int identEnd = col;
        while (identEnd < lineLen &&
               Character.isJavaIdentifierPart(t.charAt(identEnd))
        ) {
            identEnd++;
        }

        if (identStart == identEnd) {
            return null;
        }

        String ident = t.substring (identStart, identEnd);
        if (JAVA_KEYWORDS.contains(ident)) {
            // Java keyword => Do not show anything
            return null;
        }
        int newOffset = NbDocument.findLineOffset(doc, line) + identStart + 1;
        final boolean[] isFieldStatic = new boolean[] { false };
        if (!isValidTooltipLocation(debugger, doc, newOffset, ident, fieldOfPtr, isFieldStatic)) {
            return null;
        }

        while (identEnd < lineLen &&
               Character.isWhitespace(t.charAt(identEnd))
        ) {
            identEnd++;
        }
        if (identEnd < lineLen && t.charAt(identEnd) == '(') {
            // We're at a method call
            isMethodPtr[0] = true;
        }
        
        return ident;
    } catch (BadLocationException e) {
        return null;
    }
}
 
Example 12
Source File: EditorActions.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
	if (e.getSource() instanceof mxGraphComponent)
	{
		mxGraphComponent graphComponent = (mxGraphComponent) e
				.getSource();
		Component editorComponent = null;

		if (graphComponent.getCellEditor() instanceof mxCellEditor)
		{
			editorComponent = ((mxCellEditor) graphComponent
					.getCellEditor()).getEditor();
		}

		if (editorComponent instanceof JEditorPane)
		{
			JEditorPane editorPane = (JEditorPane) editorComponent;
			int start = editorPane.getSelectionStart();
			int ende = editorPane.getSelectionEnd();
			String text = editorPane.getSelectedText();

			if (text == null)
			{
				text = "";
			}

			try
			{
				HTMLEditorKit editorKit = new HTMLEditorKit();
				HTMLDocument document = (HTMLDocument) editorPane
						.getDocument();
				document.remove(start, (ende - start));
				editorKit.insertHTML(document, start, ((bold) ? "<b>"
						: "<i>") + text + ((bold) ? "</b>" : "</i>"),
						0, 0, (bold) ? HTML.Tag.B : HTML.Tag.I);
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}

			editorPane.requestFocus();
			editorPane.select(start, ende);
		}
		else
		{
			mxIGraphModel model = graphComponent.getGraph().getModel();
			model.beginUpdate();
			try
			{
				graphComponent.stopEditing(false);
				graphComponent.getGraph().toggleCellStyleFlags(
						mxConstants.STYLE_FONTSTYLE,
						(bold) ? mxConstants.FONT_BOLD
								: mxConstants.FONT_ITALIC);
			}
			finally
			{
				model.endUpdate();
			}
		}
	}
}
 
Example 13
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 14
Source File: StyledEditorKit.java    From openjdk-jdk9 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-jdk8u-backup 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 jdk8u60 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 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 18
Source File: StyledEditorKit.java    From TencentKona-8 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 19
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 20
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);
}