List of usage examples for javax.swing JEditorPane getSelectionStart
@Transient public int getSelectionStart()
From source file:com.rapidminer.gui.flow.processrendering.annotations.AnnotationDrawUtils.java
/** * Returns plain text from the editor./*from w ww. j a va 2 s.c o m*/ * * @param editor * the editor from which to take the text. * @param onlySelected * if {@code true} will only return the selected text * @return the text of the editor converted to plain text * @throws BadLocationException * @throws IOException */ public static String getPlaintextFromEditor(final JEditorPane editor, final boolean onlySelected) throws IOException, BadLocationException { if (editor == null) { throw new IllegalArgumentException("editor must not be null!"); } HTMLDocument document = (HTMLDocument) editor.getDocument(); StringWriter writer = new StringWriter(); int start = 0; int length = document.getLength(); if (onlySelected) { start = editor.getSelectionStart(); length = editor.getSelectionEnd() - start; } editor.getEditorKit().write(writer, document, start, length); String text = writer.toString(); text = AnnotationDrawUtils.removeStyleFromComment(text); // switch <br> and <br/> to actual newline (current system) text = text.replaceAll("<br.*?>", System.lineSeparator()); // kill all other html tags text = text.replaceAll("\\<.*?>", ""); text = StringEscapeUtils.unescapeHtml(text); return text; }
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 ww. j a v a2 s . co 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")); } }
From source file:ja.lingo.application.gui.main.describer.DescriberGui.java
private void find(String searchText, boolean fromStart, boolean forwardDirection, boolean caseSensetive, boolean wholeWordsOnly) { JEditorPane editorPane = articlePanel.getEditorPane(); String article;/*from ww w .j a v a 2 s.com*/ try { article = editorPane.getDocument().getText(0, editorPane.getDocument().getLength()); } catch (BadLocationException e) { throw States.shouldNeverReachHere(e); } if (fromStart) { editorPane.select(0, 0); } // try search twice: after caret, before caret (the order depends on search direction) int attempt1 = forwardDirection ? editorPane.getSelectionEnd() : editorPane.getSelectionStart() - 1; int attempt2 = forwardDirection ? 0 : article.length(); int index = TextSearcher.indexOf(forwardDirection, article, searchText, attempt1, caseSensetive, wholeWordsOnly); if (index == -1) { index = TextSearcher.indexOf(forwardDirection, article, searchText, attempt2, caseSensetive, wholeWordsOnly); if (index == -1) { // reset selction editorPane.select(0, 0); model.find_sendFeedback(false); return; } } // highlight on success editorPane.select(index, index + searchText.length()); editorPane.getCaret().setSelectionVisible(true); model.find_sendFeedback(true); }
From source file:com.hexidec.ekit.action.ListAutomationAction.java
public void actionPerformed(ActionEvent ae) { try {//from ww w . java 2s . co m 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\"> </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:org.docx4all.swing.text.WordMLEditorKit.java
private void refreshCaretElement(JEditorPane editor) { caretListener.caretElement = null;/*from w w w. j a v a 2s .co m*/ int start = editor.getSelectionStart(); int end = editor.getSelectionEnd(); WordMLDocument doc = (WordMLDocument) editor.getDocument(); try { doc.readLock(); caretListener.updateCaretElement(start, end, editor); } finally { doc.readUnlock(); } }