001/* 002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Julien Carsique 018 * 019 */ 020 021package org.nuxeo.launcher.gui; 022 023import java.awt.Color; 024 025import javax.swing.JTextPane; 026import javax.swing.text.BadLocationException; 027import javax.swing.text.Document; 028import javax.swing.text.SimpleAttributeSet; 029import javax.swing.text.StyleConstants; 030import javax.swing.text.rtf.RTFEditorKit; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034 035/** 036 * Colored text pane. Allow to choose the style when appending some text. 037 * 038 * @author jcarsique 039 * @since 5.4.2 040 */ 041public class ColoredTextPane extends JTextPane { 042 043 private static final long serialVersionUID = 1L; 044 045 private static final Log log = LogFactory.getLog(ColoredTextPane.class); 046 047 private SimpleAttributeSet style; 048 049 private Document doc; 050 051 private int maxSize = 0; 052 053 private boolean follow = true; 054 055 /** 056 * @since 5.5 057 * @return true if caret will follow additions 058 */ 059 public boolean isFollow() { 060 return follow; 061 } 062 063 /** 064 * Whether to make the caret follow or not the additions (pin/unpin) 065 * 066 * @since 5.5 067 * @param follow true to make the caret follow additions 068 */ 069 public void setFollow(boolean follow) { 070 this.follow = follow; 071 } 072 073 /** 074 * Limits the size of the text. 0 means no limit (default value). 075 * 076 * @since 5.5 077 * @param maxSize maximum number of character kept 078 */ 079 public void setMaxSize(int maxSize) { 080 this.maxSize = maxSize; 081 } 082 083 public ColoredTextPane() { 084 style = new SimpleAttributeSet(); 085 setContentType("text/rtf"); 086 setEditorKit(new RTFEditorKit()); 087 doc = getDocument(); 088 } 089 090 /** 091 * Append text at the end of document, choosing foreground and background colors, and bold attribute. 092 * 093 * @param text Text to append 094 * @param color Foreground color 095 * @param bgColor Background color 096 * @param isBold Is the text bold ? 097 */ 098 public void append(String text, Color color, Color bgColor, boolean isBold) { 099 StyleConstants.setForeground(style, color); 100 StyleConstants.setBackground(style, bgColor); 101 StyleConstants.setBold(style, isBold); 102 int len = doc.getLength(); 103 try { 104 doc.insertString(len, text + "\n", style); 105 if (maxSize > 0 && len > maxSize) { 106 doc.remove(0, len - maxSize); 107 } 108 } catch (BadLocationException e) { 109 log.error(e); 110 } 111 if (follow) { 112 setCaretPosition(doc.getLength()); 113 } 114 } 115 116 /** 117 * Calls {@link #append(String, Color)} with Color.WHITE foreground color. 118 * 119 * @see #append(String, Color) #append(String, Color, Color, boolean) 120 */ 121 public void append(String text) { 122 append(text, Color.WHITE); 123 } 124 125 /** 126 * Calls {@link #append(String, Color, Color, boolean)} with foreground color given as parameter, background color 127 * equal to component background and isBold equal to false. 128 * 129 * @see #append(String, Color, Color, boolean) 130 */ 131 public void append(String text, Color color) { 132 append(text, color, getBackground(), false); 133 } 134 135 /** 136 * Calls {@link #append(String, Color, Color, boolean)} with background color equal to component background. 137 * 138 * @see #append(String, Color, Color, boolean) 139 */ 140 public void append(String text, Color color, boolean isBold) { 141 append(text, color, getBackground(), isBold); 142 } 143 144}