Example usage for javax.swing JEditorPane getSelectedText

List of usage examples for javax.swing JEditorPane getSelectedText

Introduction

In this page you can find the example usage for javax.swing JEditorPane getSelectedText.

Prototype

@BeanProperty(bound = false)
public String getSelectedText() 

Source Link

Document

Returns the selected text contained in this TextComponent.

Usage

From source file:com.hexidec.ekit.action.ListAutomationAction.java

public void actionPerformed(ActionEvent ae) {
    try {/*w  w w. j  ava2  s  .  c  om*/
        JEditorPane jepEditor = (JEditorPane) (parentEkit.getTextPane());
        String selTextBase = jepEditor.getSelectedText();
        int textLength = -1;
        if (selTextBase != null) {
            textLength = selTextBase.length();
        }
        if (selTextBase == null || textLength < 1) {
            int pos = parentEkit.getCaretPosition();
            parentEkit.setCaretPosition(pos);
            if (ae.getActionCommand() != "newListPoint") {
                if (htmlUtilities.checkParentsTag(HTML.Tag.OL) || htmlUtilities.checkParentsTag(HTML.Tag.UL)) {
                    revertList(htmlUtilities.getListItemContainer());
                    return;
                }
            }
            String sListType = (baseTag == HTML.Tag.OL ? "ol" : "ul");
            StringBuffer sbNew = new StringBuffer();
            if (htmlUtilities.checkParentsTag(baseTag)) {
                sbNew.append("<li></li>");
                insertHTML(parentEkit.getTextPane(), parentEkit.getExtendedHtmlDoc(),
                        parentEkit.getTextPane().getCaretPosition(), sbNew.toString(), 0, 0, HTML.Tag.LI);
            } else {
                boolean isLast = false;
                int caretPos = parentEkit.getCaretPosition();
                if (caretPos == parentEkit.getExtendedHtmlDoc().getLength()) {
                    isLast = true;
                }
                sbNew.append("<" + sListType + "><li></li></" + sListType + ">"
                        + (isLast ? "<p style=\"margin-top: 0\">&nbsp;</p>" : ""));
                insertHTML(parentEkit.getTextPane(), parentEkit.getExtendedHtmlDoc(),
                        parentEkit.getTextPane().getCaretPosition(), sbNew.toString(), 0, 0,
                        (sListType.equals("ol") ? HTML.Tag.OL : HTML.Tag.UL));
                if (true) {
                    parentEkit.setCaretPosition(caretPos + 1);
                }
            }
            parentEkit.refreshOnUpdate();
        } else {
            if (htmlUtilities.checkParentsTag(HTML.Tag.OL) || htmlUtilities.checkParentsTag(HTML.Tag.UL)) {
                revertList(htmlUtilities.getListItemContainer());
                return;
            } else {
                String sListType = (baseTag == HTML.Tag.OL ? "ol" : "ul");
                HTMLDocument htmlDoc = (HTMLDocument) (jepEditor.getDocument());
                int iStart = jepEditor.getSelectionStart();
                int iEnd = jepEditor.getSelectionEnd();
                String selText = htmlDoc.getText(iStart, iEnd - iStart);
                /*
                for(int ch = 0; ch < selText.length(); ch++)
                {
                   Element elem = htmlDoc.getCharacterElement(iStart + ch);
                   log.debug("elem " + ch + ": " + elem.getName());
                   log.debug("char " + ch + ": " + selText.charAt(ch) + " [" + Character.getNumericValue(selText.charAt(ch)) + "]");
                   if(Character.getNumericValue(selText.charAt(ch)) < 0)
                   {
                      log.debug("  is space?    " + ((selText.charAt(ch) == '\u0020') ? "YES" : "---"));
                      log.debug("  is return?   " + ((selText.charAt(ch) == '\r') ? "YES" : "---"));
                      log.debug("  is newline?  " + ((selText.charAt(ch) == '\n') ? "YES" : "---"));
                      log.debug("  is nextline? " + ((selText.charAt(ch) == '\u0085') ? "YES" : "---"));
                      log.debug("  is linesep?  " + ((selText.charAt(ch) == '\u2028') ? "YES" : "---"));
                      log.debug("  is parasep?  " + ((selText.charAt(ch) == '\u2029') ? "YES" : "---"));
                      log.debug("  is verttab?  " + ((selText.charAt(ch) == '\u000B') ? "YES" : "---"));
                      log.debug("  is formfeed? " + ((selText.charAt(ch) == '\u000C') ? "YES" : "---"));
                   }
                }
                */
                StringBuffer sbNew = new StringBuffer();
                sbNew.append("<" + sListType + ">");
                // tokenize on known linebreaks if present, otherwise manually parse on <br> break tags
                if ((selText.indexOf("\r") > -1) || (selText.indexOf("\n") > -1)) {
                    String sToken = ((selText.indexOf("\r") > -1) ? "\r" : "\n");
                    StringTokenizer stTokenizer = new StringTokenizer(selText, sToken);
                    while (stTokenizer.hasMoreTokens()) {
                        sbNew.append("<li>");
                        sbNew.append(stTokenizer.nextToken());
                        sbNew.append("</li>");
                    }
                } else {
                    StringBuffer sbItem = new StringBuffer();
                    for (int ch = 0; ch < selText.length(); ch++) {
                        String elem = (htmlDoc.getCharacterElement(iStart + ch) != null
                                ? htmlDoc.getCharacterElement(iStart + ch).getName().toLowerCase()
                                : "[null]");
                        if (elem.equals("br") && sbItem.length() > 0) {
                            sbNew.append("<li>");
                            sbNew.append(sbItem.toString());
                            sbNew.append("</li>");
                            sbItem.delete(0, sbItem.length());
                        } else {
                            sbItem.append(selText.charAt(ch));
                        }
                    }
                }
                sbNew.append("</" + sListType + ">");
                htmlDoc.remove(iStart, iEnd - iStart);
                insertHTML(jepEditor, htmlDoc, iStart, sbNew.toString(), 1, 1, null);
            }
        }
    } catch (BadLocationException ble) {
    }
}

From source file:net.sf.jabref.gui.fieldeditors.PreviewPanelTransferHandler.java

@Override
protected Transferable createTransferable(JComponent component) {
    if (component instanceof JEditorPane) {
        // this method should be called from the preview panel only

        // the default TransferHandler implementation is aware of HTML
        // and returns an appropriate Transferable
        // as textTransferHandler.createTransferable() is not available and
        // I don't know any other method, I do the HTML conversion by hand

        // First, get the HTML of the selected text
        JEditorPane editorPane = (JEditorPane) component;
        StringWriter stringWriter = new StringWriter();
        try {// w  w w .  j  a  v a  2 s  .  c  o  m
            editorPane.getEditorKit().write(stringWriter, editorPane.getDocument(),
                    editorPane.getSelectionStart(), editorPane.getSelectionEnd());
        } catch (BadLocationException | IOException e) {
            LOGGER.warn("Cannot write preview", e);
        }

        // Second, return the HTML (and text as fallback)
        return new HtmlTransferable(stringWriter.toString(), editorPane.getSelectedText());
    } else {
        // if not called from the preview panel, return an error string
        return new StringSelection(Localization.lang("Operation not supported"));
    }
}