Example usage for javax.swing.text.html HTMLDocument remove

List of usage examples for javax.swing.text.html HTMLDocument remove

Introduction

In this page you can find the example usage for javax.swing.text.html HTMLDocument remove.

Prototype

public void remove(int offs, int len) throws BadLocationException 

Source Link

Document

Removes some content from the document.

Usage

From source file:com.fedroot.dacs.swing.DacsNoticePresentationDialog.java

/**
 * Sets the HTML content to be displayed.
 *
 * @param content an HTML document string
 *///w w w  .  j a  v  a  2 s .  c om
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
 *///  w  w  w . j av 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 {//from  www.  j a v a2 s . c om
        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
 *//* ww w. ja  va  2s .  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);

    /*// w  ww  .  j av  a  2  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:com.hexidec.ekit.action.ListAutomationAction.java

public void actionPerformed(ActionEvent ae) {
    try {//from  w  w  w .  j av  a 2 s .  c  o  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\">&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) {
    }
}