Example usage for javax.swing.text.html StyleSheet addStyle

List of usage examples for javax.swing.text.html StyleSheet addStyle

Introduction

In this page you can find the example usage for javax.swing.text.html StyleSheet addStyle.

Prototype

public Style addStyle(String nm, Style parent) 

Source Link

Document

Adds a new style into the style hierarchy.

Usage

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatConversationPanel.java

/**
 * Adds a custom component at the end of the conversation.
 *
 * @param component the component to add at the end of the conversation.
 *//*from  ww w .  jav a 2  s  .c  o  m*/
public void addComponent(ChatConversationComponent component) {
    synchronized (scrollToBottomRunnable) {
        StyleSheet styleSheet = document.getStyleSheet();
        Style style = styleSheet.addStyle(StyleConstants.ComponentElementName, styleSheet.getStyle("body"));

        // The image must first be wrapped in a style
        style.addAttribute(AbstractDocument.ElementNameAttribute, StyleConstants.ComponentElementName);

        TransparentPanel wrapPanel = new TransparentPanel(new BorderLayout());

        wrapPanel.add(component, BorderLayout.NORTH);

        style.addAttribute(StyleConstants.ComponentAttribute, wrapPanel);
        style.addAttribute(Attribute.ID, ChatHtmlUtils.MESSAGE_TEXT_ID);
        SimpleDateFormat sdf = new SimpleDateFormat(HistoryService.DATE_FORMAT);
        style.addAttribute(ChatHtmlUtils.DATE_ATTRIBUTE, sdf.format(component.getDate()));

        scrollToBottomIsPending = true;

        // We need to reinitialize the last message ID, because we don't
        // want components to be taken into account.
        lastMessageUID = null;

        // Insert the component style at the end of the text
        try {
            document.insertString(document.getLength(), "ignored text", style);
        } catch (BadLocationException e) {
            logger.error("Insert in the HTMLDocument failed.", e);
        }
    }
}