jTracer  1.03
Stack trace visualization tool
LogPane.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer;
8 
9 import java.awt.Font;
10 import java.awt.Color;
11 import java.awt.Insets;
12 import java.awt.EventQueue;
13 
14 import javax.swing.JTextPane;
15 import javax.swing.SwingUtilities;
16 import javax.swing.text.Style;
17 import javax.swing.text.StyleContext;
18 import javax.swing.text.StyleConstants;
19 import javax.swing.text.Document;
20 
27 public class LogPane extends JTextPane
28 {
30  private static final long serialVersionUID = 0x00;
31 
32 
36  LogPane()
37  {
38  StyleContext sc = StyleContext.getDefaultStyleContext();
39  Style s = addStyle("debug", sc.getStyle(StyleContext.DEFAULT_STYLE));
40  Registry conf = Registry.getCurrent();
41 
42  /* Create paragraph style attributes */
43  Integer padding = (Integer) conf.get("text", "padding");
44  if (padding != null) {
45  StyleConstants.setLeftIndent(s, padding);
46  StyleConstants.setRightIndent(s, padding);
47  }
48 
49  Float spacing = (Float) conf.get("text", "line-height");
50  if (spacing != null)
51  StyleConstants.setLineSpacing(s, spacing);
52 
53  StyleConstants.setAlignment(s, StyleConstants.ALIGN_JUSTIFIED);
54  setParagraphAttributes(s, true);
55 
56  /* Setup the default (debug) style */
57  Font fnt = (Font) conf.get("text", "font");
58  if (fnt != null) {
59  StyleConstants.setFontFamily(s, fnt.getFamily());
60  StyleConstants.setFontSize(s, fnt.getSize());
61  StyleConstants.setBold(s, fnt.isBold());
62  StyleConstants.setItalic(s, fnt.isItalic());
63  }
64 
65  Color fg = (Color) conf.get("text", "fgcolor-debug");
66  StyleConstants.setForeground(s, fg);
67 
68  /* Based on the default style create a style for each type of message */
69  s = addStyle("status", s);
70  fg = (Color) conf.get("text", "fgcolor-status");
71  StyleConstants.setForeground(s, fg);
72 
73  s = addStyle("data", s);
74  fg = (Color) conf.get("text", "fgcolor-data");
75  StyleConstants.setForeground(s, fg);
76 
77  s = addStyle("alert", s);
78  fg = (Color) conf.get("text", "fgcolor-alert");
79  StyleConstants.setForeground(s, fg);
80 
81  s = addStyle("error", s);
82  fg = (Color) conf.get("text", "fgcolor-error");
83  StyleConstants.setForeground(s, fg);
84 
85  /* Set selection colors */
86  Color bg = (Color) conf.get("text", "bgcolor-selection");
87  fg = (Color) conf.get("text", "fgcolor-selection");
88  setSelectionColor(bg);
89  setSelectedTextColor(fg);
90 
91  setMargin((Insets) conf.get("text", "margin"));
92  setEditable(false);
93  setAutoscrolls(true);
94  }
95 
96 
108  public void append(final String ln, final String tag)
109  {
110  /* If the rendering is done by the event dispatching thread */
111  if (EventQueue.isDispatchThread()) {
112  try {
113  Document doc = getDocument();
114  int len = doc.getLength();
115  doc.insertString(len, ln, getStyle(tag));
116  setCaretPosition(len + ln.length());
117  }
118 
119  catch (Throwable t) {
120  Registry.debug(t);
121  }
122 
123  return;
124  }
125 
126  /*
127  * If the rendering is done from any other thread, create a Worker task and
128  * register it to be called by the event dispatching thread
129  */
130  class Worker implements Runnable {
131  public void run()
132  {
133  /* The task is to recursively call the same method */
134  append(ln, tag);
135  }
136  }
137 
138  try {
139  SwingUtilities.invokeLater(new Worker());
140  }
141 
142  catch (Throwable t) {
143  Registry.debug(t);
144  }
145  }
146 
147 
155  public void appendln(String ln, String tag)
156  {
157  append(ln + "\n", tag);
158  }
159 
160 
164  public void clear()
165  {
166  try {
167  Document doc = getDocument();
168  doc.remove(0, doc.getLength());
169  setCaretPosition(0);
170  }
171 
172  catch (Throwable t) {
173  Registry.debug(t);
174  }
175  }
176 }
177 
Object get(String section, String key)
Get an entry.
Definition: Registry.java:151
Configuration registry.
Definition: Registry.java:41
Main application logging pane.
Definition: LogPane.java:27
void append(final String ln, final String tag)
Append a line of formatted text.
Definition: LogPane.java:108
void appendln(String ln, String tag)
Append a line of formatted text and a line break.
Definition: LogPane.java:155
void clear()
Remove all contents.
Definition: LogPane.java:164
static final long serialVersionUID
Class version.
Definition: LogPane.java:30