jTracer  1.03
Stack trace visualization tool
StatusBar.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer;
8 
9 import java.util.Hashtable;
10 
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.Font;
14 import java.awt.Color;
15 import java.awt.Insets;
16 import java.awt.GridBagLayout;
17 import java.awt.GridBagConstraints;
18 import java.awt.EventQueue;
19 
20 import javax.swing.JPanel;
21 import javax.swing.JLabel;
22 import javax.swing.Timer;
23 import javax.swing.SwingUtilities;
24 import javax.swing.BorderFactory;
25 import javax.swing.border.EtchedBorder;
26 
32 public class StatusBar extends JPanel implements ActionListener
33 {
35  private static final long serialVersionUID = 0x00;
36 
38  private long uptime = 0;
39 
41  private Timer timer = null;
42 
44  private Hashtable<String, JLabel> fields = null;
45 
46 
52  StatusBar(String msg)
53  {
54  super();
55  fields = new Hashtable<String, JLabel>();
56  GridBagLayout lm = new GridBagLayout();
57  GridBagConstraints c = new GridBagConstraints();
58  setLayout(lm);
59 
60  JPanel field = createField("status", msg);
61  c.gridx = c.gridy = 0;
62  c.weightx = 1;
63  c.insets = new Insets(0, 0, 0, 0);
64  c.fill = GridBagConstraints.HORIZONTAL;
65  lm.setConstraints(field, c);
66  add(field);
67 
68  field = createField("uptime", "Uptime 00:00:00");
69  c.gridx++;
70  c.weightx = 0;
71  c.fill = GridBagConstraints.NONE;
72  lm.setConstraints(field, c);
73  add(field);
74 
75  field = createField("protocol", "TCP");
76  c.gridx++;
77  lm.setConstraints(field, c);
78  add(field);
79 
80  field = createField("sessionCount", "0 sessions");
81  c.gridx++;
82  lm.setConstraints(field, c);
83  add(field);
84 
85  field = createField("traceCount", "0 traces");
86  c.gridx++;
87  lm.setConstraints(field, c);
88  add(field);
89  }
90 
91 
103  public void setMessage(final String msg, final boolean normal)
104  {
105  /* If the rendering is done by the event dispatching thread */
106  if (EventQueue.isDispatchThread()) {
107  JLabel field = fields.get("status");
108  field.setText(msg);
109 
110  Registry conf = Registry.getCurrent();
111  if (!normal) {
112  field.setForeground((Color) conf.get("statusbar", "fgcolor-error"));
113  field.setIcon(conf.loadIcon("stat_err.png"));
114  }
115  else {
116  field.setForeground((Color) conf.get("statusbar", "fgcolor"));
117  field.setIcon(conf.loadIcon("stat_ok.png"));
118  }
119 
120  return;
121  }
122 
123  /*
124  * If the rendering is done from any other thread, create a Worker task and
125  * register it to be called by the event dispatching thread
126  */
127  class Worker implements Runnable {
128  public void run()
129  {
130  /* The task is to recursively call the same method */
131  setMessage(msg, normal);
132  }
133  }
134 
135  try {
136  SwingUtilities.invokeLater(new Worker());
137  }
138 
139  catch (Throwable t) {
140  Registry.debug(t);
141  }
142  }
143 
144 
152  public void setIndicator(String nm, int cnt)
153  {
154  JLabel field = fields.get(nm + "Count");
155 
156  String text = cnt + " " + nm;
157  if (cnt != 1)
158  text += "s";
159  field.setText(text);
160 
161  Registry conf = Registry.getCurrent();
162  Color bg = null;
163  if (cnt > 0)
164  bg = (Color) conf.get("statusbar", "bgcolor-counter-active");
165  else
166  bg = (Color) conf.get("statusbar", "bgcolor");
167 
168  field.getParent().setBackground(bg);
169  }
170 
171 
179  public void setIndicators(int sessions, int traces)
180  {
181  setIndicator("session", sessions);
182  setIndicator("trace", traces);
183  }
184 
185 
189  public void startUptimeTimer()
190  {
191  /* If the rendering is done by the event dispatching thread */
192  if (EventQueue.isDispatchThread()) {
193  Registry conf = Registry.getCurrent();
194  Color bg = (Color) conf.get("statusbar", "bgcolor-uptime-active");
195  fields.get("uptime").getParent().setBackground(bg);
196 
197  timer = new Timer(1000, this);
198  timer.setActionCommand("Uptime timer");
199  timer.setCoalesce(false);
200  timer.start();
201  return;
202  }
203 
204  /*
205  * If the rendering is done from any other thread, create a Worker task and
206  * register it to be called by the event dispatching thread
207  */
208  class Worker implements Runnable {
209  public void run()
210  {
211  /* The task is to recursively call the same method */
213  }
214  }
215 
216  try {
217  SwingUtilities.invokeLater(new Worker());
218  }
219 
220  catch (Throwable t) {
221  Registry.debug(t);
222  }
223  }
224 
225 
231  public void stopUptimeTimer()
232  {
233  stopUptimeTimer(true, true);
234  }
235 
236 
244  public void stopUptimeTimer(final boolean reset, final boolean normal)
245  {
246  /* If the rendering is done by the event dispatching thread */
247  if (EventQueue.isDispatchThread()) {
248  timer.stop();
249  timer = null;
250 
251  Registry conf = Registry.getCurrent();
252  JLabel field = fields.get("uptime");
253  field.getParent().setBackground((Color) conf.get("statusbar", "bgcolor"));
254 
255  if (!normal)
256  field.setForeground((Color) conf.get("statusbar", "fgcolor-error"));
257 
258  if (reset) {
259  uptime = 0;
260  renderUptime();
261  }
262 
263  return;
264  }
265 
266  /*
267  * If the rendering is done from any other thread, create a Worker task and
268  * register it to be called by the event dispatching thread
269  */
270  class Worker implements Runnable {
271  public void run()
272  {
273  /* The task is to recursively call the same method */
274  stopUptimeTimer(reset, normal);
275  }
276  }
277 
278  try {
279  SwingUtilities.invokeLater(new Worker());
280  }
281 
282  catch (Throwable t) {
283  Registry.debug(t);
284  }
285  }
286 
287 
297  private JPanel createField(String tag, String text)
298  {
299  GridBagLayout lm = new GridBagLayout();
300  GridBagConstraints c = new GridBagConstraints();
301  JPanel retval = new JPanel(lm);
302 
303  Registry conf = Registry.getCurrent();
304  retval.setBackground((Color) conf.get("statusbar", "bgcolor"));
305  retval.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
306 
307  JLabel field = new JLabel(text);
308  field.setFont((Font) conf.get("statusbar", "font"));
309  field.setForeground((Color) conf.get("statusbar", "fgcolor"));
310 
311  if (tag.equals("status")) {
312  field.setIcon(conf.loadIcon("stat_ok.png"));
313  field.setIconTextGap(6);
314  c.weightx = 1;
315  c.anchor = GridBagConstraints.EAST;
316  }
317 
318  c.insets = new Insets(1, 8, 1, 8);
319  lm.setConstraints(field, c);
320  retval.add(field);
321 
322  fields.put(tag, field);
323  return retval;
324  }
325 
326 
330  private void renderUptime()
331  {
332  long now = uptime;
333  long hours = now / 3600;
334 
335  now = now % 3600;
336  long minutes = now / 60;
337  long seconds = now % 60;
338 
339  String tm = "Uptime ";
340  tm += ((hours < 10) ? "0" : "") + hours + ":";
341  tm += ((minutes < 10) ? "0" : "") + minutes + ":";
342  tm += ((seconds < 10) ? "0" : "") + seconds;
343 
344  fields.get("uptime").setText(tm);
345  }
346 
347 
353  public void actionPerformed(ActionEvent event)
354  {
355  try {
356  uptime++;
357  renderUptime();
358  }
359 
360  catch (Throwable t) {
361  Registry.debug(t);
362  }
363  }
364 }
365 
void stopUptimeTimer()
Stop and reset the server uptime timer.
Definition: StatusBar.java:231
Object get(String section, String key)
Get an entry.
Definition: Registry.java:151
Timer timer
Uptime timer.
Definition: StatusBar.java:41
void setIndicators(int sessions, int traces)
Set both the counter indicators.
Definition: StatusBar.java:179
void stopUptimeTimer(final boolean reset, final boolean normal)
Stop and reset the server uptime timer.
Definition: StatusBar.java:244
Configuration registry.
Definition: Registry.java:41
Application statusbar with multiple indicators and configurable styles.
Definition: StatusBar.java:32
void startUptimeTimer()
Start the server uptime timer.
Definition: StatusBar.java:189
void setIndicator(String nm, int cnt)
Set one of the counter indicators.
Definition: StatusBar.java:152
long uptime
Server uptime (seconds)
Definition: StatusBar.java:38
static final long serialVersionUID
Class version.
Definition: StatusBar.java:35
Hashtable< String, JLabel > fields
Indicator list.
Definition: StatusBar.java:44
void setMessage(final String msg, final boolean normal)
Set the status message.
Definition: StatusBar.java:103
void renderUptime()
Update the server uptime field.
Definition: StatusBar.java:330
JPanel createField(String tag, String text)
Create one of the fields.
Definition: StatusBar.java:297
void actionPerformed(ActionEvent event)
Handler for events fired from the server uptime timer.
Definition: StatusBar.java:353