jTracer  1.03
Stack trace visualization tool
TracePane.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer;
8 
9 import java.util.Hashtable;
10 import java.util.Enumeration;
11 import java.util.regex.Pattern;
12 import java.util.regex.Matcher;
13 
14 import java.awt.Font;
15 import java.awt.Color;
16 
17 import javax.swing.JTextPane;
18 import javax.swing.text.Style;
19 import javax.swing.text.StyleContext;
20 import javax.swing.text.StyleConstants;
21 import javax.swing.text.Document;
22 
26 public class TracePane extends JTextPane
27 {
29  private static final long serialVersionUID = 0x0800;
30 
31  private Hashtable<String, String> details = null;
32 
33 
37  TracePane(Hashtable<String, String> rqst)
38  {
39  details = rqst;
40  Registry conf = Registry.getCurrent();
41  StyleContext sc = StyleContext.getDefaultStyleContext();
42  Style s = addStyle("plain", sc.getStyle(sc.DEFAULT_STYLE));
43 
44  /* Create paragraph style attributes */
45  Integer padding = (Integer) conf.get("text", "padding");
46  if (padding != null) {
47  StyleConstants.setLeftIndent(s, padding);
48  StyleConstants.setRightIndent(s, padding);
49  }
50 
51  Float spacing = (Float) conf.get("text", "line-height");
52  if (spacing != null)
53  StyleConstants.setLineSpacing(s, spacing);
54 
55  StyleConstants.setAlignment(s, StyleConstants.ALIGN_JUSTIFIED);
56  setParagraphAttributes(s, true);
57 
58  /* Setup the default (plain) style */
59  Font fnt = (Font) conf.get("text", "font-trace");
60  if (fnt != null) {
61  StyleConstants.setFontFamily(s, fnt.getFamily());
62  StyleConstants.setFontSize(s, fnt.getSize());
63  StyleConstants.setBold(s, fnt.isBold());
64  StyleConstants.setItalic(s, fnt.isItalic());
65  }
66 
67  Color fg = (Color) conf.get("text", "fgcolor-plain");
68  StyleConstants.setForeground(s, fg);
69 
70  /* Based on the default style create a style for each type of token */
71  fg = (Color) conf.get("text", "fgcolor-keyword");
72  s = addStyle("keyword", s);
73  StyleConstants.setForeground(s, fg);
74 
75  fg = (Color) conf.get("text", "fgcolor-type");
76  s = addStyle("type", s);
77  StyleConstants.setForeground(s, fg);
78 
79  fg = (Color) conf.get("text", "fgcolor-number");
80  s = addStyle("number", s);
81  StyleConstants.setForeground(s, fg);
82 
83  fg = (Color) conf.get("text", "fgcolor-file");
84  s = addStyle("file", s);
85  StyleConstants.setForeground(s, fg);
86 
87  fg = (Color) conf.get("text", "fgcolor-delimiter");
88  s = addStyle("delimiter", s);
89  StyleConstants.setForeground(s, fg);
90 
91  fg = (Color) conf.get("text", "fgcolor-scope");
92  s = addStyle("scope", s);
93  StyleConstants.setForeground(s, fg);
94 
95  fg = (Color) conf.get("text", "fgcolor-function");
96  s = addStyle("function", s);
97  StyleConstants.setForeground(s, fg);
98 
99  /* Set selection colors */
100  Color bg = (Color) conf.get("text", "bgcolor-selection");
101  fg = (Color) conf.get("text", "fgcolor-selection");
102  setSelectionColor(bg);
103  setSelectedTextColor(fg);
104 
105  setEditable(false);
106  setAutoscrolls(true);
107  }
108 
109 
110  public String getField(String key)
111  {
112  return details.get(key);
113  }
114 
115 
121  public Enumeration getFieldKeys()
122  {
123  return details.keys();
124  }
125 
126 
132  public void append(String trace)
133  {
134  /* Delimiters */
135  String expr = "[\\s\\{\\}\\(\\)\\*&,:<>]+";
136 
137  Pattern regexp = Pattern.compile(expr);
138  Matcher parser = regexp.matcher(trace);
139 
140  /* Parse the trace and append it word-by-word doing syntax highlighting */
141  int offset = 0;
142  String prev = null;
143  while (parser.find()) {
144  String grp = parser.group();
145  String token = trace.substring(offset, parser.start());
146  offset = parser.end();
147 
148  append(token, prev, grp);
149  append(grp, "delimiter");
150  prev = grp;
151  }
152 
153  if (offset < trace.length() - 1)
154  append(trace.substring(offset), prev, null);
155  }
156 
157 
167  public void append(String token, String prev, String next)
168  {
169  Highlighter h = new Highlighter();
170  String num = "(0x)?\\p{XDigit}+$";
171 
172  /* Highlight decimal and hex numbers */
173  if (token.matches(num))
174  append(token, "number");
175 
176  /* Highlight file names */
177  else if (h.lookup(token, "extension", true))
178  append(token, "file");
179 
180  /* Highlight C++ integral types */
181  else if (h.lookup(token, "type", false))
182  append(token, "type");
183 
184  /* Highlight C++ keywords (apart those for integral types) */
185  else if (h.lookup(token, "keyword", false))
186  append(token, "keyword");
187 
188  /* Highlight C++ namespaces and classes */
189  else if (next.equals("::"))
190  append(token, "scope");
191 
192  /* Highlight function names */
193  else if (next.equals("(") || next.equals("<") || next.startsWith("\n"))
194  append(token, "function");
195 
196  else
197  append(token, "plain");
198  }
199 
200 
208  public void append(String ln, String tag)
209  {
210  try {
211  Document doc = getDocument();
212  int len = doc.getLength();
213  doc.insertString(len, ln, getStyle(tag));
214  setCaretPosition(len + ln.length());
215  }
216 
217  catch (Throwable t) {
218  Registry.debug(t);
219  }
220  }
221 
222 
230  public void appendln(String ln, String tag)
231  {
232  append(ln + "\n", tag);
233  }
234 
235 
239  public void clear()
240  {
241  try {
242  Document doc = getDocument();
243  doc.remove(0, doc.getLength());
244  setCaretPosition(0);
245  }
246 
247  catch (Throwable t) {
248  Registry.debug(t);
249  }
250  }
251 }
252 
boolean lookup(String token, String nm, boolean regex)
Check if a word belongs in a dictionary.
void append(String ln, String tag)
Append a line of formatted text.
Definition: TracePane.java:208
Object get(String section, String key)
Get an entry.
Definition: Registry.java:151
Configuration registry.
Definition: Registry.java:41
String getField(String key)
Definition: TracePane.java:110
Enumeration getFieldKeys()
Get all the keys of the request fields.
Definition: TracePane.java:121
void append(String trace)
Append a trace using the syntax highlighter.
Definition: TracePane.java:132
static final long serialVersionUID
Object version.
Definition: TracePane.java:29
C++ stack trace syntax highlighter.
Hashtable< String, String > details
Definition: TracePane.java:31
void appendln(String ln, String tag)
Append a line of formatted text and a line break.
Definition: TracePane.java:230
void append(String token, String prev, String next)
Append a single trace token.
Definition: TracePane.java:167
Rich text pane for trace visualization with syntax highlighting.
Definition: TracePane.java:26
void clear()
Remove all contents.
Definition: TracePane.java:239