Example usage for javax.swing.text StyledDocument remove

List of usage examples for javax.swing.text StyledDocument remove

Introduction

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

Prototype

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

Source Link

Document

Removes a portion of the content of the document.

Usage

From source file:org.openmicroscopy.shoola.agents.fsimporter.view.ImporterUI.java

/** 
 * Adds the text to the debug pane.//w  w  w . j av  a2s  .  c  om
 * 
 * @param text The text to display.
 */
void appendDebugText(String text) {
    if (debugTextPane == null)
        return;
    StyledDocument doc = (StyledDocument) debugTextPane.getDocument();
    try {
        doc.insertString(doc.getLength(), text, doc.getStyle(STYLE));
        if (doc.getLength() > MAX_CHAR)
            doc.remove(0, doc.getLength() - MAX_CHAR);
    } catch (Exception e) {
        //ignore
    }
}

From source file:pl.otros.logview.gui.MessageDetailListener.java

public void updateInfo() {
    Collection<MessageFormatter> formatters = formattersContainer.getElements();
    Collection<MessageColorizer> colorizers = colorizersContainer.getElements();
    int row = table.getSelectedRow();
    if (row >= 0 && row < table.getRowCount()) {
        logDetailTextArea.setText("");
        int rowConverted = table.convertRowIndexToModel(row);
        LogData ld = dataTableModel.getLogData(rowConverted);
        StyledDocument document = logDetailTextArea.getStyledDocument();
        synchronized (document) {
            try {
                document.remove(0, document.getLength());
                String s1 = "Date:    " + dateFormat.format(ld.getDate()) + "\n";
                document.insertString(0, s1, mainStyle);
                s1 = "Class:   " + ld.getClazz() + "\n";
                document.insertString(document.getLength(), s1, classMethodStyle);
                s1 = "Method:  " + ld.getMethod() + "\n";
                document.insertString(document.getLength(), s1, classMethodStyle);
                s1 = "Level:   ";
                document.insertString(document.getLength(), s1, classMethodStyle);
                Icon levelIcon = LevelRenderer.getIconByLevel(ld.getLevel());
                if (levelIcon != null) {
                    logDetailTextArea.insertIcon(levelIcon);
                }/*from ww  w  .  ja va  2  s. c om*/
                s1 = " " + ld.getLevel().getName() + "\n";
                document.insertString(document.getLength(), s1, classMethodStyle);
                s1 = "Message: ";
                document.insertString(document.getLength(), s1, mainStyle);
                int beforeMessage = document.getLength();
                s1 = ld.getMessage();
                if (s1.length() > maximumMessageSize) {
                    int removedCharsSize = s1.length() - maximumMessageSize;
                    s1 = StringUtils.left(s1, maximumMessageSize)
                            + String.format("%n...%n...(+%,d chars)", removedCharsSize);
                }

                for (MessageFormatter messageFormatter : formatters) {
                    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                    try {
                        Thread.currentThread()
                                .setContextClassLoader(messageFormatter.getClass().getClassLoader());
                        if (messageFormatter.formattingNeeded(s1)) {
                            s1 = messageFormatter.format(s1);
                        }
                    } catch (Throwable e) {
                        LOGGER.severe(String.format("Error occured when using message formatter %s: %s",
                                messageFormatter.getName(), e.getMessage()));
                        LOGGER.fine(String.format(
                                "Error occured when using message formatter %s with message\"%s\"",
                                messageFormatter.getName(), s1));
                    } finally {
                        Thread.currentThread().setContextClassLoader(contextClassLoader);
                    }

                }
                document.insertString(document.getLength(), s1, mainStyle);
                searchResultMessageColorizer = null;
                for (MessageColorizer messageColorizer : colorizers) {
                    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                    try {
                        Thread.currentThread()
                                .setContextClassLoader(messageColorizer.getClass().getClassLoader());
                        if (messageColorizer.colorizingNeeded(s1)) {
                            messageColorizer.colorize(document, beforeMessage,
                                    document.getLength() - beforeMessage);
                        }
                    } catch (Throwable e) {
                        LOGGER.severe(String.format("Error occured when using message colorizer %s: %s",
                                messageColorizer.getName(), e.getMessage()));
                        LOGGER.fine(String.format(
                                "Error occured when using message colorizer %s with message\"%s\"",
                                messageColorizer.getName(), s1));
                    } finally {
                        Thread.currentThread().setContextClassLoader(contextClassLoader);
                    }

                    if (messageColorizer.getPluginableId().equals(SearchResultColorizer.class.getName())) {
                        searchResultMessageColorizer = messageColorizer;
                    }

                }
                if (searchResultMessageColorizer != null && searchResultMessageColorizer.colorizingNeeded(s1)) {
                    searchResultMessageColorizer.colorize(document, beforeMessage,
                            document.getLength() - beforeMessage);
                }
                document.insertString(document.getLength(), "\n", mainStyle);
                if (ld.getProperties() != null && ld.getProperties().size() > 0) {
                    document.insertString(document.getLength(), "\nProperties:\n", noteStyle);
                    String prop = Joiner.on("\n").withKeyValueSeparator("=").join(ld.getProperties());
                    document.insertString(document.getLength(), prop, noteStyle);
                    document.insertString(document.getLength(), "\n", noteStyle);
                }
                Note note = dataTableModel.getNote(rowConverted);
                if (note != null && note.getNote() != null && note.getNote().length() > 0) {
                    s1 = "\nNote: " + note.getNote();
                    document.insertString(document.getLength(), s1, noteStyle);
                }
            } catch (BadLocationException e) {
                LOGGER.warning("Cant set message details: " + e.getMessage());
            }
        }
    } else {
        StyledDocument document = logDetailTextArea.getStyledDocument();
        synchronized (document) {
            try {
                document.remove(0, document.getLength());
                document.insertString(0, "No event selected", mainStyle);
            } catch (BadLocationException e) {
                LOGGER.warning("Cant set message details: " + e.getMessage());
            }
        }
    }
    logDetailTextArea.setCaretPosition(0);
}