Example usage for javax.swing.text Style addAttribute

List of usage examples for javax.swing.text Style addAttribute

Introduction

In this page you can find the example usage for javax.swing.text Style addAttribute.

Prototype

public void addAttribute(Object name, Object value);

Source Link

Document

Creates a new attribute set similar to this one except that it contains an attribute with the given name and value.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {//from  w w  w.  j a va2 s. c  o m
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    doc.insertString(0, text, null);

                    doc.setParagraphAttributes(0, 1, heading2Style, false);
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample5.java

public static void main(String[] args) {
    try {/* w  w w .j  av a  2  s . co m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 5");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Create and add the style
    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Finally, apply the style to the heading
                    doc.setParagraphAttributes(0, 1, heading2Style, false);

                    // Set the foreground and font
                    pane.setForeground(Color.blue);
                    pane.setFont(new Font("serif", Font.PLAIN, 12));
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

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.
 *//*w  w  w  .java  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);
        }
    }
}

From source file:tk.tomby.tedit.editorkits.HighlightPlainContext.java

/**
 * Creates a new HighlightContext object.
 *
 * @param syntax DOCUMENT ME!/*from  w ww .j a  va2 s .com*/
 */
public HighlightPlainContext(Syntax syntax) {
    super();

    tokenizer = new Tokenizer(syntax);

    Style style = addStyle(Syntax.UNKNOWN_NAME, getStyle(DEFAULT_STYLE));

    int red = PreferenceManager.getInt("general.editor.foreground.red", 0);
    int green = PreferenceManager.getInt("general.editor.foreground.green", 0);
    int blue = PreferenceManager.getInt("general.editor.foreground.blue", 0);
    style.addAttribute(StyleConstants.Foreground, new Color(red, green, blue));

    String font = PreferenceManager.getString("general.editor.font", "Monospaced");
    int size = PreferenceManager.getInt("general.editor.fontSize", 12);
    style.addAttribute(StyleConstants.FontFamily, new Font(font, Font.PLAIN, size));

    createStyle(Syntax.KEYWORD1_NAME, "highlight.keyword1");
    createStyle(Syntax.KEYWORD2_NAME, "highlight.keyword2");
    createStyle(Syntax.KEYWORD3_NAME, "highlight.keyword3");
    createStyle(Syntax.KEYWORD4_NAME, "highlight.keyword4");

    createStyle(Syntax.COMMENT1_NAME, "highlight.comment1");
    createStyle(Syntax.COMMENT2_NAME, "highlight.comment2");
    createStyle(Syntax.COMMENT3_NAME, "highlight.comment3");
    createStyle(Syntax.COMMENT4_NAME, "highlight.comment3");

    createStyle(Syntax.LITERAL1_NAME, "highlight.literal1");
    createStyle(Syntax.LITERAL2_NAME, "highlight.literal2");
    createStyle(Syntax.LITERAL3_NAME, "highlight.literal3");
    createStyle(Syntax.LITERAL4_NAME, "highlight.literal4");

    createStyle(Syntax.IDENTIFIER1_NAME, "highlight.identifier1");
    createStyle(Syntax.IDENTIFIER2_NAME, "highlight.identifier2");
    createStyle(Syntax.IDENTIFIER3_NAME, "highlight.identifier3");
    createStyle(Syntax.IDENTIFIER4_NAME, "highlight.identifier4");

    createStyle(Syntax.OPERATOR_NAME, "highlight.operator");
    createStyle(Syntax.SEPARATOR_NAME, "highlight.separator");
    createStyle(Syntax.LABEL_NAME, "highlight.label");
    createStyle(Syntax.MARKUP_NAME, "highlight.markup");
    createStyle(Syntax.DIGIT_NAME, "highlight.digit");
    createStyle(Syntax.SCRIPTLET_NAME, "highlight.scriptlet");

    MessageManager.addMessageListener(MessageManager.PREFERENCE_GROUP_NAME, this);
}

From source file:tk.tomby.tedit.editorkits.HighlightPlainContext.java

/**
 * DOCUMENT ME!/*  w  w w  .  j av  a2  s  .co m*/
 *
 * @param message DOCUMENT ME!
 */
public void receiveMessage(PreferenceMessage message) {
    if (message.getKey().startsWith("highlight")) {
        int index = message.getKey().indexOf('.');
        String styleName = message.getKey().substring(index + 1);

        log.debug("styleName=" + styleName);

        HighlightModel model = (HighlightModel) message.getNewValue();

        Style style = getStyle(styleName);
        style.addAttribute(StyleConstants.Foreground, model.getColor());
        style.addAttribute(StyleConstants.FontFamily, model.getFont());
    }
}

From source file:tk.tomby.tedit.editorkits.HighlightPlainContext.java

/**
 * DOCUMENT ME!//from w w  w .j a  va2  s. c om
 *
 * @param name DOCUMENT ME!
 * @param key DOCUMENT ME!
 */
private void createStyle(String name, String key) {
    HighlightModel model = new HighlightModel(key);

    Style style = addStyle(name, getStyle(DEFAULT_STYLE));
    style.addAttribute(StyleConstants.Foreground, model.getColor());
    style.addAttribute(StyleConstants.FontFamily, model.getFont());
}