List of usage examples for javax.swing.text.html HTMLDocument getLength
public int getLength()
From source file:com.rapidminer.gui.flow.processrendering.annotations.AnnotationDrawUtils.java
/** * Returns plain text from the editor.//from w ww. j a v a 2 s . c om * * @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:com.fedroot.dacs.swing.DacsNoticePresentationDialog.java
/** * Sets the HTML content to be displayed. * * @param content an HTML document string *///from w w w . j a v a 2s . com private void setDocumentContent(String contenttype, String content) { HTMLDocument doc = new HTMLDocument(); try { doc.remove(0, doc.getLength()); } catch (BadLocationException e) { e.printStackTrace(); } // doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE); try { htmlpane.setContentType(contenttype); htmlpane.read(new ByteArrayInputStream(content.getBytes()), doc); htmlpane.setDocument(doc); htmlpane.setCaretPosition(0); } catch (IOException e) { e.printStackTrace(); } }
From source file:fedroot.dacs.swingdemo.DacsSwingDemo.java
/** * Sets the HTML content to be displayed. * * @param content an HTML document string *///from w w w. jav a 2 s .c o m private void setDocumentContent(String contenttype, String content) { HTMLDocument doc = new HTMLDocument(); try { doc.remove(0, doc.getLength()); } catch (BadLocationException ex) { logger.log(Level.WARNING, ex.getMessage()); } doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE); try { htmlPane.setContentType(contenttype); htmlPane.read(new ByteArrayInputStream(content.getBytes()), doc); htmlPane.setDocument(doc); htmlPane.setCaretPosition(0); } catch (IOException ex) { logger.log(Level.WARNING, ex.getMessage()); } responseTextArea.setText(content); responseTextArea.setCaretPosition(0); responseTextArea.requestFocus(); }
From source file:com.mindcognition.mindraider.ui.swing.concept.annotation.renderer.AbstractTextAnnotationRenderer.java
protected void setViewerText(String text) { try {// ww w . j ava2s . co m text = editorToViewerTransfomer.toHtml(text); if (text != null) { HTMLDocument document = (HTMLDocument) viewer.getStyledDocument(); document.remove(0, document.getLength()); kit.insertHTML(document, 0, text, 0, 0, HTML.Tag.HTML); } } catch (Exception e) { logger.error("Unable to set viewer text: " + text, e); // {{debug}} } // scroll to upper left corner viewer.setCaretPosition(0); }
From source file:fedroot.dacs.swingdemo.DacsClientFrame.java
/** * Sets the HTML content to be displayed. * * @param content an HTML document string *///from www .jav a 2 s . c o m private void setDocumentContent(String contenttype, String content) { HTMLDocument doc = new HTMLDocument(); try { doc.remove(0, doc.getLength()); } catch (BadLocationException e) { e.printStackTrace(); } doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE); try { htmlPane.setContentType(contenttype); htmlPane.read(new ByteArrayInputStream(content.getBytes()), doc); htmlPane.setDocument(doc); htmlPane.setCaretPosition(0); } catch (IOException e) { e.printStackTrace(); } responseTextArea.setText(content); responseTextArea.setCaretPosition(0); responseTextArea.requestFocus(); }
From source file:EditorPaneExample16.java
public HTMLDocument loadDocument(HTMLDocument doc, URL url, String charSet) throws IOException { doc.putProperty(Document.StreamDescriptionProperty, url); /*/*from ww w . ja v a2 s . c o m*/ * This loop allows the document read to be retried if the character * encoding changes during processing. */ InputStream in = null; boolean ignoreCharSet = false; for (;;) { try { // Remove any document content doc.remove(0, doc.getLength()); URLConnection urlc = url.openConnection(); in = urlc.getInputStream(); Reader reader = (charSet == null) ? new InputStreamReader(in) : new InputStreamReader(in, charSet); HTMLEditorKit.Parser parser = getParser(); HTMLEditorKit.ParserCallback htmlReader = getParserCallback(doc); parser.parse(reader, htmlReader, ignoreCharSet); htmlReader.flush(); // All done break; } catch (BadLocationException ex) { // Should not happen - throw an IOException throw new IOException(ex.getMessage()); } catch (ChangedCharSetException e) { // The character set has changed - restart charSet = getNewCharSet(e); // Prevent recursion by suppressing further exceptions ignoreCharSet = true; // Close original input stream in.close(); // Continue the loop to read with the correct encoding } } return doc; }
From source file:net.java.sip.communicator.impl.gui.main.chat.ChatWritePanel.java
/** * Appends the given text to the end of the contained HTML document. This * method is used to insert smileys when user selects a smiley from the * menu.//from www.j a v a 2s . c o m * * @param text the text to append. */ public void appendText(String text) { HTMLDocument doc = (HTMLDocument) editorPane.getDocument(); Element currentElement = doc.getCharacterElement(editorPane.getCaretPosition()); try { doc.insertAfterEnd(currentElement, text); } catch (BadLocationException e) { logger.error("Insert in the HTMLDocument failed.", e); } catch (IOException e) { logger.error("Insert in the HTMLDocument failed.", e); } this.editorPane.setCaretPosition(doc.getLength()); }
From source file:com.hexidec.ekit.EkitCore.java
/** * Method for saving text as a complete HTML document *///from w ww. j a v a 2 s. c o m public void writeOut(HTMLDocument doc, File whatFile) throws IOException, BadLocationException { if (whatFile == null) { whatFile = getFileFromChooser(".", JFileChooser.SAVE_DIALOG, extsHTML, Translatrix.getTranslationString("FiletypeHTML")); } if (whatFile != null) { FileWriter fw = new FileWriter(whatFile); htmlKit.write(fw, doc, 0, doc.getLength()); fw.flush(); fw.close(); currentFile = whatFile; updateTitle(); } refreshOnUpdate(); }