jTracer  1.03
Stack trace visualization tool
MenuBar.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer;
8 
9 import java.util.Vector;
10 
11 import java.beans.PropertyChangeEvent;
12 import java.beans.PropertyChangeListener;
13 
14 import java.awt.event.InputEvent;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.Font;
18 import java.awt.Color;
19 import java.awt.Component;
20 
21 import javax.swing.KeyStroke;
22 import javax.swing.JMenuItem;
23 import javax.swing.JMenu;
24 import javax.swing.JMenuBar;
25 import javax.swing.JFrame;
26 import javax.swing.ImageIcon;
27 import javax.swing.BorderFactory;
28 import javax.swing.border.EtchedBorder;
29 
30 import static java.awt.event.KeyEvent.*;
31 
35 public class MenuBar extends JMenuBar implements
36  ActionListener,
37  PropertyChangeListener
38 {
40  private static final long serialVersionUID = 0x00;
41 
43  private boolean toggleStates[] = {true, true, false};
44 
46  private ActionListener handler = null;
47 
48 
54  MenuBar(ActionListener owner)
55  {
56  super();
57  handler = owner;
58  setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
59 
60  Registry conf = Registry.getCurrent();
61  Font fnt = (Font) conf.get("component", "font");
62  Color fg = (Color) conf.get("component", "fgcolor");
63 
64  /* Create and populate the 'Service' menu */
65  JMenu menu = new JMenu("Service");
66  menu.setFont(fnt);
67  menu.setForeground(fg);
68  menu.setMnemonic(VK_S);
69 
70  menu.add(createItem("Start", VK_S, VK_S));
71  menu.add(createItem("Stop", VK_T, VK_T));
72  menu.add(createItem("Restart", VK_R, VK_R));
73  menu.add(createItem("Clear log", VK_L, VK_L));
74  menu.addSeparator();
75 
76  menu.add(createItem("Quit", VK_Q, VK_Q));
77  add(menu);
78 
79  /* Create and populate the 'View' menu */
80  menu = new JMenu("View");
81  menu.setFont(fnt);
82  menu.setForeground(fg);
83  menu.setMnemonic(VK_V);
84 
85  menu.add(createToggle("Toolbar", 0, VK_T));
86  menu.add(createToggle("Statusbar", 1, VK_S));
87  menu.add(createToggle("Always on top", 2, VK_A));
88  menu.addSeparator();
89 
90  menu.add(createItem("Find...", VK_F, VK_F));
91  menu.add(createItem("Preferences...", VK_P, VK_P));
92  menu.addSeparator();
93 
94  menu.add(createItem("Full screen", VK_U, VK_F11));
95  add(menu);
96 
97  /* Create and populate the 'Session' menu */
98  menu = new JMenu("Session");
99  menu.setFont(fnt);
100  menu.setForeground(fg);
101  menu.setMnemonic(VK_I);
102 
103  menu.add(createItem("Select previous", VK_P, VK_COMMA));
104  menu.add(createItem("Select next", VK_N, VK_PERIOD));
105  menu.add(createItem("Close", VK_C, VK_W));
106  menu.addSeparator();
107 
108  menu.add(createItem("Cascade", VK_S, VK_SLASH));
109  menu.add(createItem("Minimize all", VK_M, VK_DOWN));
110  menu.add(createItem("Restore all", VK_R, VK_UP));
111  menu.add(createItem("Close all", VK_E, VK_E));
112  add(menu);
113 
114  /* Create and populate the 'Help' menu */
115  menu = new JMenu("Help");
116  menu.setFont(fnt);
117  menu.setForeground(fg);
118  menu.setMnemonic(VK_H);
119 
120  menu.add(createItem("Online documentation", VK_D, 0));
121  menu.add(createItem("Bug tracker", VK_B, 0));
122  menu.add(createItem("Submit feedback", VK_F, 0));
123  menu.add(createItem("Check for updates", VK_U, 0));
124  menu.addSeparator();
125 
126  String nm = (String) conf.get("generic", "name");
127  menu.add(createItem("About " + nm, VK_A, VK_F1));
128  add(menu);
129  }
130 
131 
141  public void setSelectedSession(int sel)
142  {
143  JMenu menu = getMenu(2);
144  Registry conf = Registry.getCurrent();
145  ImageIcon onIcon = conf.loadIcon("on16.png");
146  ImageIcon offIcon = conf.loadIcon("voidrect16.png");
147 
148  for (int i = menu.getMenuComponentCount() - 1; i >= 9; i--) {
149  JMenuItem item = (JMenuItem) menu.getMenuComponent(i);
150  item.setIcon((i - 9 == sel) ? onIcon : offIcon);
151  }
152  }
153 
154 
162  public void listSessions(Vector list, int sel)
163  {
164  JMenu menu = getMenu(2);
165  while (menu.getMenuComponentCount() > 8)
166  menu.remove(8);
167 
168  int cnt = list.size();
169  if (cnt == 0)
170  return;
171 
172  menu.addSeparator();
173  Registry conf = Registry.getCurrent();
174  Font fnt = (Font) conf.get("component", "font");
175  Color fg = (Color) conf.get("component", "fgcolor");
176  Integer delay = (Integer) conf.get("component", "click-delay");
177  ImageIcon onIcon = conf.loadIcon("on16.png");
178  ImageIcon offIcon = conf.loadIcon("voidrect16.png");
179 
180  for (int i = 0; i < cnt; i++) {
181  JFrame sess = (JFrame) list.get(i);
182 
183  JMenuItem item = new JMenuItem(sess.getTitle() + " ");
184  item.setFont(fnt);
185  item.setForeground(fg);
186  if (delay != null)
187  item.setMultiClickThreshhold(delay);
188 
189  if (i < 10) {
190  int accel = VK_0 + i;
191  KeyStroke ks = KeyStroke.getKeyStroke(accel, InputEvent.ALT_DOWN_MASK);
192  item.setAccelerator(ks);
193  }
194 
195  item.setIcon((i == sel) ? onIcon : offIcon);
196  item.setActionCommand("Select session " + i);
197  item.addActionListener(handler);
198  menu.add(item);
199  }
200  }
201 
202 
210  public boolean getToggleState(int index)
211  {
212  return toggleStates[index];
213  }
214 
215 
227  private JMenuItem createItem(String text, int mnemonic, int accel)
228  {
229  JMenuItem retval = new JMenuItem(text + " ");
230  retval.setMnemonic(mnemonic);
231 
232  Registry conf = Registry.getCurrent();
233  retval.setFont((Font) conf.get("component", "font"));
234  retval.setForeground((Color) conf.get("component", "fgcolor"));
235 
236  Integer delay = (Integer) conf.get("component", "click-delay");
237  if (delay != null)
238  retval.setMultiClickThreshhold(delay);
239 
240  if (accel != 0) {
241  int modifiers = 0;
242  if (accel < VK_F1 || accel > VK_F12)
243  modifiers |= InputEvent.CTRL_DOWN_MASK;
244 
245  KeyStroke ks = KeyStroke.getKeyStroke(accel, modifiers);
246  retval.setAccelerator(ks);
247  }
248 
249  /* The action command is the item caption with the trailing dots trimmed */
250  text = text.replace('.', ' ').trim();
251  retval.setActionCommand(text);
252  retval.addActionListener(handler);
253 
254  String nm = text.toLowerCase().replace(' ', '_') + "16.png";
255  retval.setIcon(conf.loadIcon(nm));
256  return retval;
257  }
258 
259 
271  private JMenuItem createToggle(String text, int index, int mnemonic)
272  {
273  JMenuItem retval = new JMenuItem(text);
274  retval.setMnemonic(mnemonic);
275 
276  Registry conf = Registry.getCurrent();
277  retval.setFont((Font) conf.get("component", "font"));
278  retval.setForeground((Color) conf.get("component", "fgcolor"));
279 
280  Integer delay = (Integer) conf.get("component", "click-delay");
281  if (delay != null)
282  retval.setMultiClickThreshhold(delay);
283 
284  retval.setActionCommand(text);
285  retval.addActionListener(handler);
286  retval.addActionListener(this);
287 
288  String nm = ((toggleStates[index]) ? "on" : "off") + "16.png";
289  retval.setIcon(conf.loadIcon(nm));
290  return retval;
291  }
292 
293 
307  private void renderItem(Component c, int menu, int i, String key, Object val)
308  {
309  Boolean how = (Boolean) val;
310 
311  if (key.equals("isServing")) {
312  if (menu != 0)
313  return;
314 
315  switch (i) {
316  case 0:
317  c.setEnabled(!how);
318  break;
319 
320  case 1:
321  case 2:
322  c.setEnabled(how);
323  }
324  }
325 
326  else if (key.equals("hasClients"))
327  switch (menu) {
328  case 1:
329  if (i == 4)
330  c.setEnabled(how);
331  break;
332 
333  case 2:
334  if (i < 8)
335  c.setEnabled(how);
336  }
337  }
338 
339 
345  public void actionPerformed(ActionEvent event)
346  {
347  try {
348  String cmd = event.getActionCommand();
349 
350  int index = 0;
351  if (cmd.equals("Statusbar"))
352  index++;
353 
354  else if (cmd.equals("Always on top"))
355  index += 2;
356 
357  /* Change the state and icon of the toggle item */
358  boolean state = !toggleStates[index];
359  toggleStates[index] = state;
360  String nm = ((state) ? "on" : "off") + "16.png";
361 
362  Registry conf = Registry.getCurrent();
363  getMenu(1).getItem(index).setIcon(conf.loadIcon(nm));
364  }
365 
366  catch (Throwable t) {
367  Registry.debug(t);
368  }
369  }
370 
371 
377  public void propertyChange(PropertyChangeEvent event)
378  {
379  try {
380  String key = event.getPropertyName();
381  Object val = event.getNewValue();
382 
383  for (int i = getMenuCount() - 1; i >= 0; i--) {
384  JMenu menu = getMenu(i);
385 
386  for (int j = menu.getMenuComponentCount() - 1; j >= 0; j--) {
387  Component c = menu.getMenuComponent(j);
388  if (c instanceof JMenuItem)
389  renderItem(c, i, j, key, val);
390  }
391  }
392  }
393 
394  catch (Throwable t) {
395  Registry.debug(t);
396  }
397  }
398 }
399 
Object get(String section, String key)
Get an entry.
Definition: Registry.java:151
Configuration registry.
Definition: Registry.java:41
boolean toggleStates[]
State of the toggle menu items.
Definition: MenuBar.java:43
ImageIcon loadIcon(String nm)
Load an icon from the current theme.
Definition: Registry.java:220
void renderItem(Component c, int menu, int i, String key, Object val)
Update the UI state of a single menu item.
Definition: MenuBar.java:307
void setSelectedSession(int sel)
Update the session list menu to show the currently selected session.
Definition: MenuBar.java:141
void actionPerformed(ActionEvent event)
Handler for toggle menu item action events.
Definition: MenuBar.java:345
JMenuItem createToggle(String text, int index, int mnemonic)
Create a toggle menu item.
Definition: MenuBar.java:271
Main application menubar equipped with a dynamic UI updating mechanism.
Definition: MenuBar.java:35
void listSessions(Vector list, int sel)
Update the session list menu.
Definition: MenuBar.java:162
static final long serialVersionUID
Class version.
Definition: MenuBar.java:40
ActionListener handler
Event handler.
Definition: MenuBar.java:46
boolean getToggleState(int index)
Get the state of a toggle menu item.
Definition: MenuBar.java:210
JMenuItem createItem(String text, int mnemonic, int accel)
Create a menu item.
Definition: MenuBar.java:227
void propertyChange(PropertyChangeEvent event)
Handler for events fired when UI properties change.
Definition: MenuBar.java:377