KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > util > TextPaneMessageLogger


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.gui.util;
32
33 import org.objectweb.proactive.ic2d.util.IC2DMessageLogger;
34
35 public class TextPaneMessageLogger implements IC2DMessageLogger {
36
37   private javax.swing.JTextPane JavaDoc messageArea;
38   
39   private javax.swing.text.Document JavaDoc document;
40   
41   private java.text.DateFormat JavaDoc dateFormat = new java.text.SimpleDateFormat JavaDoc("HH:mm:ss");
42   
43   private javax.swing.text.Style JavaDoc regularStyle;
44   private javax.swing.text.Style JavaDoc stackTraceStyle;
45   private javax.swing.text.Style JavaDoc errorStyle;
46   private javax.swing.text.Style JavaDoc threadNameStyle;
47   private javax.swing.text.Style JavaDoc timeStampStyle;
48   
49   //
50
// -- CONSTRUCTORS -----------------------------------------------
51
//
52

53   public TextPaneMessageLogger(javax.swing.JTextPane JavaDoc messageArea) {
54     this.messageArea = messageArea;
55     this.document = messageArea.getDocument();
56     messageArea.setEditable(false);
57     javax.swing.text.Style JavaDoc def = javax.swing.text.StyleContext.getDefaultStyleContext().getStyle(javax.swing.text.StyleContext.DEFAULT_STYLE);
58     // regular Style
59
regularStyle = messageArea.addStyle("regular", def);
60     javax.swing.text.StyleConstants.setFontFamily(regularStyle, "SansSerif");
61     javax.swing.text.StyleConstants.setFontSize(regularStyle, 10);
62     // error Style
63
errorStyle = messageArea.addStyle("error", regularStyle);
64     javax.swing.text.StyleConstants.setForeground(errorStyle, java.awt.Color.red);
65     // stacktrace Style
66
stackTraceStyle = messageArea.addStyle("stackTrace", regularStyle);
67     javax.swing.text.StyleConstants.setForeground(stackTraceStyle, java.awt.Color.lightGray);
68     // threadName Style
69
threadNameStyle = messageArea.addStyle("threadName", regularStyle);
70     javax.swing.text.StyleConstants.setForeground(threadNameStyle, java.awt.Color.darkGray);
71     javax.swing.text.StyleConstants.setItalic(threadNameStyle, true);
72     // timeStamp Style
73
timeStampStyle = messageArea.addStyle("timeStamp", regularStyle);
74     javax.swing.text.StyleConstants.setForeground(timeStampStyle, java.awt.Color.blue);
75     javax.swing.text.StyleConstants.setItalic(timeStampStyle, true);
76   }
77
78   //
79
// -- PUBLIC METHODS -----------------------------------------------
80
//
81

82   //
83
// -- implements IC2DMessageLogger -----------------------------------------------
84
//
85

86   public void warn(String JavaDoc message) {
87     logInternal(message, errorStyle);
88     invokeDialog(message);
89   }
90
91   public void log(String JavaDoc message) {
92     logInternal(message, regularStyle);
93   }
94
95   public void log(String JavaDoc message, Throwable JavaDoc e) {
96     logInternal(message, errorStyle);
97     java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
98     java.io.PrintWriter JavaDoc pw = new java.io.PrintWriter JavaDoc(baos, false);
99     e.printStackTrace(pw);
100     pw.flush();
101     logInternal(baos.toString(), stackTraceStyle);
102     invokeDialog(message);
103   }
104
105   public void log(Throwable JavaDoc e) {
106     log(e.getMessage(), e);
107   }
108
109
110
111   //
112
// -- PRIVATE METHODS -----------------------------------------------
113
//
114

115   // make sure the caller thread (especially if it is the gui thread does not get stuck
116
// with a deadlock.
117
private void invokeDialog(final String JavaDoc message) {
118     new Thread JavaDoc(new Runnable JavaDoc() {
119       public void run() {
120         DialogUtils.displayWarningDialog(messageArea, message);
121       }
122     }).start();
123   }
124
125   private void logInternal(final String JavaDoc message, final javax.swing.text.AttributeSet JavaDoc style) {
126     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
127       public void run() {
128         append(dateFormat.format(new java.util.Date JavaDoc()), timeStampStyle);
129         append(" (", threadNameStyle);
130         append(Thread.currentThread().getName(), threadNameStyle);
131         append(") => ", threadNameStyle);
132         append(message, style);
133         append("\n", style);
134         messageArea.setCaretPosition(document.getLength());
135       }
136     });
137   }
138   
139   private void append(String JavaDoc str, javax.swing.text.AttributeSet JavaDoc style) {
140     try {
141       document.insertString(document.getLength(), str, style);
142     } catch (javax.swing.text.BadLocationException JavaDoc e) {}
143   }
144   
145 }
146
Popular Tags