Example usage for javax.swing.text StyleConstants ComponentAttribute

List of usage examples for javax.swing.text StyleConstants ComponentAttribute

Introduction

In this page you can find the example usage for javax.swing.text StyleConstants ComponentAttribute.

Prototype

Object ComponentAttribute

To view the source code for javax.swing.text StyleConstants ComponentAttribute.

Click Source Link

Document

Name of the component attribute.

Usage

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java

/**
 * @param formatter//from w ww . ja v a2 s  .co m
 */
protected void setFormatterFromTextPane(final DataObjSwitchFormatter formatter) {
    // visit every character in the document text looking for fields
    // store characters not associated with components (jbutton) to make up the separator text
    DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
    String text = formatEditor.getText();
    int docLen = doc.getLength();
    int lastFieldPos = 0;

    Vector<DataObjDataField> fields = new Vector<DataObjDataField>();
    //int cnt = 0;
    for (int i = 0; i < docLen; ++i) {
        Element element = doc.getCharacterElement(i);
        AttributeSet attrs = element.getAttributes();
        Object obj = attrs.getAttribute(StyleConstants.ComponentAttribute);
        //System.out.print(String.format("i: %d, lastFieldPos: %d cnt: %d, F: %s", i, lastFieldPos, cnt, (obj instanceof FieldDefinitionComp ? "Y" : "N")));
        if (obj instanceof FieldDefinitionComp) {
            //System.out.println(cnt+"  "+(obj instanceof FieldDefinitionComp));
            // found button at the current position
            // create corresponding field
            String sepStr = (lastFieldPos <= i - 1) ? text.substring(lastFieldPos, i) : "";

            FieldDefinitionComp fieldDefBtn = (FieldDefinitionComp) obj;
            DataObjDataField fmtField = fieldDefBtn.getValue();
            fmtField.setSep(sepStr);
            fields.add(fmtField);

            //System.out.print(" Sep: ["+sepStr+"]");

            lastFieldPos = i + 1;
            //cnt++;
        }
    }

    // XXX: what do we do with the remaining of the text? right now we ignore it
    // That's because we can't create an empty formatter field just to use the separator... 

    DataObjDataField[] fieldsArray = new DataObjDataField[fields.size()];
    for (int i = 0; i < fields.size(); ++i) {
        fieldsArray[i] = fields.elementAt(i);
    }

    DataObjDataFieldFormat singleFormatter = fieldsArray.length == 0 ? null
            : new DataObjDataFieldFormat("", tableInfo.getClassObj(), false, "", "", fieldsArray);
    formatter.setSingle(singleFormatter);
}

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 w  w w .jav a  2  s  .  co 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);
        }
    }
}