1 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 messageArea; 38 39 private javax.swing.text.Document document; 40 41 private java.text.DateFormat dateFormat = new java.text.SimpleDateFormat ("HH:mm:ss"); 42 43 private javax.swing.text.Style regularStyle; 44 private javax.swing.text.Style stackTraceStyle; 45 private javax.swing.text.Style errorStyle; 46 private javax.swing.text.Style threadNameStyle; 47 private javax.swing.text.Style timeStampStyle; 48 49 53 public TextPaneMessageLogger(javax.swing.JTextPane messageArea) { 54 this.messageArea = messageArea; 55 this.document = messageArea.getDocument(); 56 messageArea.setEditable(false); 57 javax.swing.text.Style def = javax.swing.text.StyleContext.getDefaultStyleContext().getStyle(javax.swing.text.StyleContext.DEFAULT_STYLE); 58 regularStyle = messageArea.addStyle("regular", def); 60 javax.swing.text.StyleConstants.setFontFamily(regularStyle, "SansSerif"); 61 javax.swing.text.StyleConstants.setFontSize(regularStyle, 10); 62 errorStyle = messageArea.addStyle("error", regularStyle); 64 javax.swing.text.StyleConstants.setForeground(errorStyle, java.awt.Color.red); 65 stackTraceStyle = messageArea.addStyle("stackTrace", regularStyle); 67 javax.swing.text.StyleConstants.setForeground(stackTraceStyle, java.awt.Color.lightGray); 68 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 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 82 86 public void warn(String message) { 87 logInternal(message, errorStyle); 88 invokeDialog(message); 89 } 90 91 public void log(String message) { 92 logInternal(message, regularStyle); 93 } 94 95 public void log(String message, Throwable e) { 96 logInternal(message, errorStyle); 97 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream (); 98 java.io.PrintWriter pw = new java.io.PrintWriter (baos, false); 99 e.printStackTrace(pw); 100 pw.flush(); 101 logInternal(baos.toString(), stackTraceStyle); 102 invokeDialog(message); 103 } 104 105 public void log(Throwable e) { 106 log(e.getMessage(), e); 107 } 108 109 110 111 115 private void invokeDialog(final String message) { 118 new Thread (new Runnable () { 119 public void run() { 120 DialogUtils.displayWarningDialog(messageArea, message); 121 } 122 }).start(); 123 } 124 125 private void logInternal(final String message, final javax.swing.text.AttributeSet style) { 126 javax.swing.SwingUtilities.invokeLater(new Runnable () { 127 public void run() { 128 append(dateFormat.format(new java.util.Date ()), 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 str, javax.swing.text.AttributeSet style) { 140 try { 141 document.insertString(document.getLength(), str, style); 142 } catch (javax.swing.text.BadLocationException e) {} 143 } 144 145 } 146 | Popular Tags |