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

The following examples show how to use javax.swing.JEditorPane#getSelectedText() . 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: EditorContextImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Returns identifier currently selected in editor or <code>null</code>.
 *
 * @return identifier currently selected in editor or <code>null</code>
 */
@Override
public String getSelectedIdentifier () {
    JEditorPane ep = contextDispatcher.getCurrentEditor ();
    if (ep == null) {
        return null;
    }
    Caret caret = ep.getCaret();
    if (caret == null) {
        // No caret => no selected text
        return null;
    }
    String s = ep.getSelectedText ();
    if (s == null) {
        return null;
    }
    if (Utilities.isJavaIdentifier (s)) {
        return s;
    }
    return null;
}
 
Example 2
Source File: EditorContextImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** throws IllegalComponentStateException when can not return the data in AWT. */
private String getCurrentElement(FileObject fo, JEditorPane ep,
                                 final ElementKind kind, final String[] elementSignaturePtr)
        throws java.awt.IllegalComponentStateException {

    if (fo == null) {
        return null;
    }
    final int currentOffset;
    final String selectedIdentifier;
    if (ep != null) {
        String s;
        Caret caret = ep.getCaret();
        if (caret == null) {
            s = null;
            currentOffset = 0;
        } else {
            s = ep.getSelectedText ();
            currentOffset = ep.getCaretPosition();
            if (ep.getSelectionStart() > currentOffset || ep.getSelectionEnd() < currentOffset) {
                s = null; // caret outside of the selection
            }
        }
        if (s != null && Utilities.isJavaIdentifier (s)) {
            selectedIdentifier = s;
        } else {
            selectedIdentifier = null;
        }
    } else {
        selectedIdentifier = null;
        currentOffset = 0;
    }
    return EditorContextSupport.getCurrentElement(fo, currentOffset, selectedIdentifier, kind, elementSignaturePtr);
}
 
Example 3
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String getIdentifier(final StyledDocument doc, final JEditorPane ep, final int offset) {
    String t = null;
    if (ep.getCaret() != null) { // #255228
        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);
    Element lineElem = NbDocument.findLineRootElement(doc).getElement(line);
    try {
        if (lineElem == null) {
            return null;
        }
        int lineStartOffset = lineElem.getStartOffset();
        int lineLen = lineElem.getEndOffset() - lineStartOffset;
        if (col + 1 >= lineLen) {
            // do not evaluate when mouse hover behind the end of line (112662)
            return null;
        }
        t = doc.getText(lineStartOffset, lineLen);
        return getExpressionToEvaluate(t, col);
    } catch (BadLocationException e) {
        return null;
    }
}
 
Example 4
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 5
Source File: CodeEvaluatorUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void openEvaluator() {
    String selectedText = null;
    JEditorPane editor = EditorContextDispatcher.getDefault().getMostRecentEditor();
    if (editor != null) {
        selectedText = editor.getSelectedText();
    }
    CodeEvaluatorUI evaluator = getInstance();
    evaluator.open ();
    if (selectedText != null) {
        evaluator.codePane.setText(selectedText);
        evaluator.codeText = selectedText;
    }
    evaluator.codePane.selectAll();
    evaluator.requestActive ();
}
 
Example 6
Source File: FileSearchAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private FileDescriptor[] getSelectedFiles() {
    FileDescriptor[] result = null;
    panel = new FileSearchPanel(this, findCurrentProject());
    dialog = createDialog(panel);

    Node[] arr = TopComponent.getRegistry ().getActivatedNodes();
    if (arr.length > 0) {
        EditorCookie ec = arr[0].getCookie (EditorCookie.class);
        if (ec != null) {
            JEditorPane recentPane = NbDocument.findRecentEditorPane(ec);
            if (recentPane != null) {
                String initSearchText = null;
                if (org.netbeans.editor.Utilities.isSelectionShowing(recentPane.getCaret())) {
                    initSearchText = recentPane.getSelectedText();
                }
                if (initSearchText != null) {
                    if (initSearchText.length() > 256) {
                        initSearchText = initSearchText.substring(0, 256);
                    }
                    panel.setInitialText(initSearchText);
                } else {
                    FileObject fo = arr[0].getLookup().lookup(FileObject.class);
                    if (fo != null) {
                        panel.setInitialText(fo.getNameExt());
                    }
                }
            }
        }
    }

    dialog.setVisible(true);
    result = panel.getSelectedFiles();
    return result;
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: GetJavaWord.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static String forPane(JEditorPane p) {
    if (p == null) return null;
 
    String selection = p.getSelectedText ();
 
    if ( selection != null && selection.length() > 0 ) {
        return selection;
    } else {
 
        // try to guess which word is underneath the caret's dot.
 
        Document doc = p.getDocument();
        Element lineRoot;
 
        if (doc instanceof StyledDocument) {
            lineRoot = NbDocument.findLineRootElement((StyledDocument)doc);
        } else {
            lineRoot = doc.getDefaultRootElement();
        }
        int dot = p.getCaret().getDot();
        Element line = lineRoot.getElement(lineRoot.getElementIndex(dot));
 
        if (line == null) return null;
 
        String text = null;
        try {
            text = doc.getText(line.getStartOffset(),
                line.getEndOffset() - line.getStartOffset());
        } catch (BadLocationException e) {
            return null;
        }
        
        if ( text == null )
            return null;
        int pos = dot - line.getStartOffset();

        if ( pos < 0 || pos >= text.length() )
            return null;

        int bix, eix;

        for( bix = Character.isJavaIdentifierPart( text.charAt( pos ) ) ? pos : pos - 1;
                bix >= 0 && Character.isJavaIdentifierPart( text.charAt( bix ) ); bix-- );
        for( eix = pos; eix < text.length() && Character.isJavaIdentifierPart( text.charAt( eix )); eix++ );

        return bix == eix ? null : text.substring( bix + 1, eix  );
    }
}
 
Example 12
Source File: Utils.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 {
        javax.swing.text.Element lineElem = 
            org.openide.text.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;
        return t.substring (identStart, identEnd);
    } catch (javax.swing.text.BadLocationException e) {
        return null;
    }
}
 
Example 13
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;
}