jTracer  1.03
Stack trace visualization tool
Alert.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer;
8 
9 import java.awt.event.KeyEvent;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.awt.Font;
13 import java.awt.Color;
14 import java.awt.Insets;
15 import java.awt.BorderLayout;
16 import java.awt.GridBagLayout;
17 import java.awt.GridBagConstraints;
18 
19 import javax.swing.JDialog;
20 import javax.swing.JFrame;
21 import javax.swing.JLabel;
22 import javax.swing.JPanel;
23 import javax.swing.ImageIcon;
24 import javax.swing.WindowConstants;
25 
29 public class Alert extends JDialog implements ActionListener
30 {
32  private static final long serialVersionUID = 0x00;
33 
35  public static final short QUESTION_MSG = 0x01;
36 
38  public static final short ERROR_MSG = 0x02;
39 
41  public static final short INFORMATION_MSG = 0x03;
42 
44  private boolean reply = false;
45 
46 
56  Alert(JFrame owner, String msg, short desc)
57  {
58  super(owner, true);
59 
60  Button ok = new Button("Ok", this);
61  ok.setMnemonic(KeyEvent.VK_O);
62 
63  Button cancel = new Button("Cancel", this);
64  cancel.setMnemonic(KeyEvent.VK_C);
65 
66  GridBagLayout lm = new GridBagLayout();
67  JPanel center = new JPanel(lm);
68  JPanel south = new JPanel(lm);
69  ImageIcon icon = null;
70 
71  Registry conf = Registry.getCurrent();
72  GridBagConstraints c = new GridBagConstraints();
73  c.gridy = c.gridx = 0;
74  c.insets = new Insets(12, 4, 4, 4);
75 
76  /* Based on the type, set alert title, buttons and icon */
77  switch (desc) {
78  case QUESTION_MSG:
79  setTitle("Question");
80  icon = conf.loadIcon("prompt32.png");
81 
82  lm.setConstraints(ok, c);
83  south.add(ok);
84 
85  c.gridx++;
86  lm.setConstraints(cancel, c);
87  south.add(cancel);
88  break;
89 
90  case ERROR_MSG:
91  setTitle("Error");
92  icon = conf.loadIcon("error32.png");
93  lm.setConstraints(ok, c);
94  south.add(ok);
95  break;
96 
97  case INFORMATION_MSG:
98  setTitle("Information");
99  icon = conf.loadIcon("info32.png");
100  lm.setConstraints(ok, c);
101  south.add(ok);
102  }
103 
104  /* Break up the message into lines */
105  String lines[] = msg.split("\\n");
106 
107  JLabel l = new JLabel(icon);
108  c.gridx = 0;
109  c.insets = new Insets(12, 12, 0, 12);
110  c.anchor = GridBagConstraints.NORTH;
111  c.gridheight = lines.length;
112  lm.setConstraints(l, c);
113  center.add(l);
114 
115  c.gridx = c.gridheight = 1;
116  c.insets = new Insets(8, 0, 0, 8);
117  c.anchor = GridBagConstraints.WEST;
118 
119  Font fnt = (Font) conf.get("message", "font");
120  Color fg = (Color) conf.get("message", "fgcolor");
121  for (int i = 0, cnt = lines.length; i < cnt; i++) {
122  l = new JLabel(lines[i]);
123  l.setFont(fnt);
124  l.setForeground(fg);
125 
126  c.gridy = i;
127  lm.setConstraints(l, c);
128  center.add(l);
129  c.insets.top = 4;
130  }
131 
132  add(center, BorderLayout.CENTER);
133  add(south, BorderLayout.SOUTH);
134 
135  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
136  setResizable(false);
137  pack();
138  setLocationRelativeTo(owner);
139 
140  getToolkit().beep();
141  setVisible(true);
142  }
143 
144 
150  public boolean getReply()
151  {
152  return reply;
153  }
154 
155 
165  public static void error(JFrame owner, String msg, boolean fatal)
166  {
167  new Alert(owner, msg, ERROR_MSG);
168  if (fatal)
169  System.exit(1);
170  }
171 
172 
180  public static void info(JFrame owner, String msg)
181  {
182  new Alert(owner, msg, INFORMATION_MSG);
183  }
184 
185 
195  public static boolean prompt(JFrame owner, String msg)
196  {
197  Alert prompt = new Alert(owner, msg, QUESTION_MSG);
198  return prompt.reply;
199  }
200 
201 
207  public void actionPerformed(ActionEvent event)
208  {
209  try {
210  String cmd = event.getActionCommand();
211  if (cmd.equals("Ok"))
212  reply = true;
213 
214  dispose();
215  }
216 
217  catch (Throwable t) {
218  Registry.debug(t);
219  }
220  }
221 }
222 
Multitype button with configurable styles and behaviour.
Definition: Button.java:20
Object get(String section, String key)
Get an entry.
Definition: Registry.java:151
static boolean prompt(JFrame owner, String msg)
Show an alert prompting the user to make a choice.
Definition: Alert.java:195
Configuration registry.
Definition: Registry.java:41
static void info(JFrame owner, String msg)
Show a generic alert.
Definition: Alert.java:180
boolean getReply()
Get the user reply.
Definition: Alert.java:150
void actionPerformed(ActionEvent event)
Handler for action events fired by the alert buttons.
Definition: Alert.java:207
static void error(JFrame owner, String msg, boolean fatal)
Show an error alert and exit if the error is marked as fatal.
Definition: Alert.java:165
static final long serialVersionUID
Class version.
Definition: Alert.java:32
User alert or simple prompt dialog with configurable styles.
Definition: Alert.java:29
static final short ERROR_MSG
Alert type for error messages.
Definition: Alert.java:38
boolean reply
The user reply.
Definition: Alert.java:44
static final short QUESTION_MSG
Alert type for prompt messages.
Definition: Alert.java:35
static final short INFORMATION_MSG
Alert type for plain, information messages.
Definition: Alert.java:41