001: /*
002: * StyledLogPane.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Color;
025: import javax.swing.JTextPane;
026: import javax.swing.text.AttributeSet;
027: import javax.swing.text.BadLocationException;
028: import javax.swing.text.DefaultStyledDocument;
029: import javax.swing.text.MutableAttributeSet;
030: import javax.swing.text.SimpleAttributeSet;
031: import javax.swing.text.StyleConstants;
032:
033: /* ----------------------------------------------------------
034: * CVS NOTE: Changes to the CVS repository prior to the
035: * release of version 3.0.0beta1 has meant a
036: * resetting of CVS revision numbers.
037: * ----------------------------------------------------------
038: */
039:
040: /**
041: * Appending text pane with styled (coloured) text for process logging.
042: *
043: * @author Takis Diakoumis
044: * @version $Revision: 1.4 $
045: * @date $Date: 2006/05/14 06:56:07 $
046: */
047: public class StyledLogPane extends JTextPane {
048:
049: /** Indicates an action message (blue) */
050: public static final int ACTION_MESSAGE = 0;
051:
052: /** Indicates an error message (red) */
053: public static final int ERROR_MESSAGE = 1;
054:
055: /** Indicates a normal output message (black) */
056: public static final int PLAIN_MESSAGE = 2;
057:
058: /** Indicates a normal output message (dark orange) */
059: public static final int WARNING_MESSAGE = 3;
060:
061: /** Indicates an action message (blue) in fixed-width font */
062: public static final int ACTION_MESSAGE_PREFORMAT = 4;
063:
064: /** Indicates an error message (red) in fixed-width font */
065: public static final int ERROR_MESSAGE_PREFORMAT = 5;
066:
067: /** Indicates a normal output message (black) in fixed-width font */
068: public static final int PLAIN_MESSAGE_PREFORMAT = 6;
069:
070: /** Indicates a normal output message (dark orange) in fixed-width font */
071: public static final int WARNING_MESSAGE_PREFORMAT = 7;
072:
073: /** the styled document for this text pane */
074: private StyledOutputPaneDocument document;
075:
076: public StyledLogPane() {
077: document = new StyledOutputPaneDocument();
078: setDocument(document);
079: }
080:
081: public void append(String text) {
082: appendPlain(text);
083: }
084:
085: public void append(int type, String text) {
086: switch (type) {
087: case ACTION_MESSAGE:
088: appendAction(text);
089: break;
090: case ERROR_MESSAGE:
091: appendError(text);
092: break;
093: case WARNING_MESSAGE:
094: appendWarning(text);
095: break;
096: case PLAIN_MESSAGE:
097: appendPlain(text);
098: break;
099: case ACTION_MESSAGE_PREFORMAT:
100: appendActionFixedWidth(text);
101: break;
102: case ERROR_MESSAGE_PREFORMAT:
103: appendErrorFixedWidth(text);
104: break;
105: case WARNING_MESSAGE_PREFORMAT:
106: appendWarningFixedWidth(text);
107: break;
108: case PLAIN_MESSAGE_PREFORMAT:
109: appendPlainFixedWidth(text);
110: break;
111: default:
112: appendPlain(text);
113: break;
114: }
115: }
116:
117: public void appendError(String text) {
118: document.appendError(text);
119: }
120:
121: public void appendWarning(String text) {
122: document.appendWarning(text);
123: }
124:
125: public void appendPlain(String text) {
126: document.appendPlain(text);
127: }
128:
129: public void appendAction(String text) {
130: document.appendAction(text);
131: }
132:
133: public void appendErrorFixedWidth(String text) {
134: document.appendErrorFixedWidth(text);
135: }
136:
137: public void appendWarningFixedWidth(String text) {
138: document.appendWarningFixedWidth(text);
139: }
140:
141: public void appendPlainFixedWidth(String text) {
142: document.appendPlainFixedWidth(text);
143: }
144:
145: public void appendActionFixedWidth(String text) {
146: document.appendActionFixedWidth(text);
147: }
148:
149: public boolean isEditable() {
150: return false;
151: }
152:
153: class StyledOutputPaneDocument extends DefaultStyledDocument {
154:
155: private final char NEW_LINE_CHAR = '\n';
156:
157: /** temp text buffer */
158: private StringBuffer textBuffer;
159:
160: // normal font
161: protected MutableAttributeSet plain;
162: protected MutableAttributeSet error;
163: protected MutableAttributeSet warning;
164: protected MutableAttributeSet action;
165:
166: // fixed width font
167: protected MutableAttributeSet plainFixedWidth;
168: protected MutableAttributeSet errorFixedWidth;
169: protected MutableAttributeSet warningFixedWidth;
170: protected MutableAttributeSet actionFixedWidth;
171:
172: public StyledOutputPaneDocument() {
173: initStyles();
174: textBuffer = new StringBuffer();
175: }
176:
177: protected void initStyles() {
178: // normal font styles
179: plain = new SimpleAttributeSet();
180: StyleConstants.setForeground(plain, Color.BLACK);
181:
182: error = new SimpleAttributeSet();
183: StyleConstants.setForeground(error, Color.RED.darker());
184:
185: warning = new SimpleAttributeSet();
186: StyleConstants.setForeground(warning,
187: new Color(222, 136, 8));
188:
189: action = new SimpleAttributeSet();
190: StyleConstants.setForeground(action, Color.BLUE.darker());
191:
192: // fixed width font styles
193: String fixedWidthFontName = "monospaced";
194: plainFixedWidth = new SimpleAttributeSet(plain);
195: StyleConstants.setFontFamily(plainFixedWidth,
196: fixedWidthFontName);
197:
198: errorFixedWidth = new SimpleAttributeSet(error);
199: StyleConstants.setFontFamily(errorFixedWidth,
200: fixedWidthFontName);
201:
202: warningFixedWidth = new SimpleAttributeSet(warning);
203: StyleConstants.setFontFamily(warningFixedWidth,
204: fixedWidthFontName);
205:
206: actionFixedWidth = new SimpleAttributeSet(action);
207: StyleConstants.setFontFamily(actionFixedWidth,
208: fixedWidthFontName);
209:
210: }
211:
212: protected void appendErrorFixedWidth(String text) {
213: append(text, errorFixedWidth);
214: }
215:
216: protected void appendWarningFixedWidth(String text) {
217: append(text, warningFixedWidth);
218: }
219:
220: protected void appendPlainFixedWidth(String text) {
221: append(text, plainFixedWidth);
222: }
223:
224: protected void appendActionFixedWidth(String text) {
225: append(text, actionFixedWidth);
226: }
227:
228: protected void appendError(String text) {
229: append(text, error);
230: }
231:
232: protected void appendWarning(String text) {
233: append(text, warning);
234: }
235:
236: protected void appendPlain(String text) {
237: append(text, plain);
238: }
239:
240: protected void appendAction(String text) {
241: append(text, action);
242: }
243:
244: protected void append(String text, AttributeSet attrs) {
245: int length = getLength();
246: if (length > 0) {
247: textBuffer.append(NEW_LINE_CHAR);
248: }
249:
250: textBuffer.append(text);
251:
252: try {
253: insertString(length, textBuffer.toString(), attrs);
254: if (length > 0) {
255: setCaretPosition(getLength());
256: }
257: } catch (BadLocationException e) {
258: }
259: textBuffer.setLength(0);
260: }
261:
262: } // class OutputPaneDocument
263:
264: }
|