001/*
002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Julien Carsique
016 *
017 */
018
019package org.nuxeo.launcher.gui;
020
021import java.awt.Color;
022
023import javax.swing.JTextPane;
024import javax.swing.text.BadLocationException;
025import javax.swing.text.Document;
026import javax.swing.text.SimpleAttributeSet;
027import javax.swing.text.StyleConstants;
028import javax.swing.text.rtf.RTFEditorKit;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033/**
034 * Colored text pane. Allow to choose the style when appending some text.
035 *
036 * @author jcarsique
037 * @since 5.4.2
038 */
039public class ColoredTextPane extends JTextPane {
040
041    private static final long serialVersionUID = 1L;
042
043    private static final Log log = LogFactory.getLog(ColoredTextPane.class);
044
045    private SimpleAttributeSet style;
046
047    private Document doc;
048
049    private int maxSize = 0;
050
051    private boolean follow = true;
052
053    /**
054     * @since 5.5
055     * @return true if caret will follow additions
056     */
057    public boolean isFollow() {
058        return follow;
059    }
060
061    /**
062     * Whether to make the caret follow or not the additions (pin/unpin)
063     *
064     * @since 5.5
065     * @param follow true to make the caret follow additions
066     */
067    public void setFollow(boolean follow) {
068        this.follow = follow;
069    }
070
071    /**
072     * Limits the size of the text. 0 means no limit (default value).
073     *
074     * @since 5.5
075     * @param maxSize maximum number of character kept
076     */
077    public void setMaxSize(int maxSize) {
078        this.maxSize = maxSize;
079    }
080
081    public ColoredTextPane() {
082        style = new SimpleAttributeSet();
083        setContentType("text/rtf");
084        setEditorKit(new RTFEditorKit());
085        doc = getDocument();
086    }
087
088    /**
089     * Append text at the end of document, choosing foreground and background colors, and bold attribute.
090     *
091     * @param text Text to append
092     * @param color Foreground color
093     * @param bgColor Background color
094     * @param isBold Is the text bold ?
095     */
096    public void append(String text, Color color, Color bgColor, boolean isBold) {
097        StyleConstants.setForeground(style, color);
098        StyleConstants.setBackground(style, bgColor);
099        StyleConstants.setBold(style, isBold);
100        int len = doc.getLength();
101        try {
102            doc.insertString(len, text + "\n", style);
103            if (maxSize > 0 && len > maxSize) {
104                doc.remove(0, len - maxSize);
105            }
106        } catch (BadLocationException e) {
107            log.error(e);
108        }
109        if (follow) {
110            setCaretPosition(doc.getLength());
111        }
112    }
113
114    /**
115     * Calls {@link #append(String, Color)} with Color.WHITE foreground color.
116     *
117     * @see #append(String, Color) #append(String, Color, Color, boolean)
118     */
119    public void append(String text) {
120        append(text, Color.WHITE);
121    }
122
123    /**
124     * Calls {@link #append(String, Color, Color, boolean)} with foreground color given as parameter, background color
125     * equal to component background and isBold equal to false.
126     *
127     * @see #append(String, Color, Color, boolean)
128     */
129    public void append(String text, Color color) {
130        append(text, color, getBackground(), false);
131    }
132
133    /**
134     * Calls {@link #append(String, Color, Color, boolean)} with background color equal to component background.
135     *
136     * @see #append(String, Color, Color, boolean)
137     */
138    public void append(String text, Color color, boolean isBold) {
139        append(text, color, getBackground(), isBold);
140    }
141
142}