jTracer  1.03
Stack trace visualization tool
Alert.java
Go to the documentation of this file.
1 
7 package org.libcsdbg.jtracer.installer;
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.JPanel;
22 import javax.swing.JLabel;
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 yes = new Button("Yes", this);
61  yes.setMnemonic(KeyEvent.VK_Y);
62 
63  Button no = new Button("No", this);
64  no.setMnemonic(KeyEvent.VK_N);
65 
66  Button ok = new Button("Ok", this);
67  ok.setMnemonic(KeyEvent.VK_O);
68 
69  GridBagLayout lm = new GridBagLayout();
70  JPanel center = new JPanel(lm);
71  JPanel south = new JPanel(lm);
72  ImageIcon icon = null;
73 
74  Registry conf = Registry.getCurrent();
75  GridBagConstraints c = new GridBagConstraints();
76  c.gridy = c.gridx = 0;
77  c.insets = new Insets(12, 4, 4, 4);
78 
79  /* Based on the type, set alert title, buttons and icon */
80  switch (desc) {
81  case QUESTION_MSG:
82  setTitle("Question");
83  icon = conf.loadIcon("prompt32.png");
84 
85  lm.setConstraints(yes, c);
86  south.add(yes);
87 
88  c.gridx++;
89  lm.setConstraints(no, c);
90  south.add(no);
91  break;
92 
93  case ERROR_MSG:
94  setTitle("Error");
95  icon = conf.loadIcon("error32.png");
96  lm.setConstraints(ok, c);
97  south.add(ok);
98  break;
99 
100  case INFORMATION_MSG:
101  setTitle("Information");
102  icon = conf.loadIcon("info32.png");
103  lm.setConstraints(ok, c);
104  south.add(ok);
105  }
106 
107  /* Break up the message into lines */
108  String lines[] = msg.split("\\n");
109 
110  JLabel l = new JLabel(icon);
111  c.gridx = 0;
112  c.insets = new Insets(12, 12, 0, 12);
113  c.anchor = GridBagConstraints.NORTH;
114  c.gridheight = lines.length;
115  lm.setConstraints(l, c);
116  center.add(l);
117 
118  c.gridx = c.gridheight = 1;
119  c.insets = new Insets(8, 0, 0, 8);
120  c.anchor = GridBagConstraints.WEST;
121 
122  Font fnt = (Font) conf.get("message", "font");
123  Color fg = (Color) conf.get("message", "fgcolor");
124  for (int i = 0, cnt = lines.length; i < cnt; i++) {
125  l = new JLabel(lines[i]);
126  l.setFont(fnt);
127  l.setForeground(fg);
128 
129  c.gridy = i;
130  lm.setConstraints(l, c);
131  center.add(l);
132  c.insets.top = 4;
133  }
134 
135  add(center, BorderLayout.CENTER);
136  add(south, BorderLayout.SOUTH);
137 
138  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
139  setResizable(false);
140  pack();
141  setLocationRelativeTo(owner);
142 
143  getToolkit().beep();
144  setVisible(true);
145  }
146 
147 
153  public boolean getReply()
154  {
155  return reply;
156  }
157 
158 
168  public static void error(JFrame owner, String msg, boolean fatal)
169  {
170  new Alert(owner, msg, ERROR_MSG);
171  if (fatal)
172  System.exit(1);
173  }
174 
175 
183  public static void info(JFrame owner, String msg)
184  {
185  new Alert(owner, msg, INFORMATION_MSG);
186  }
187 
188 
198  public static boolean prompt(JFrame owner, String msg)
199  {
200  Alert prompt = new Alert(owner, msg, QUESTION_MSG);
201  return prompt.reply;
202  }
203 
204 
210  public void actionPerformed(ActionEvent event)
211  {
212  try {
213  String cmd = event.getActionCommand();
214  if (cmd.equals("Ok") || cmd.equals("Yes"))
215  reply = true;
216 
217  dispose();
218  }
219 
220  catch (Throwable t) {
221  Registry.debug(t);
222  }
223  }
224 }
225 
static final long serialVersionUID
Class version.
Definition: Alert.java:32
static final short INFORMATION_MSG
Alert type for plain, information messages.
Definition: Alert.java:41
Object get(String section, String key)
Get an entry.
Definition: Registry.java:106
boolean reply
The user reply.
Definition: Alert.java:44
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:168
void actionPerformed(ActionEvent event)
Handler for action events fired by the alert buttons.
Definition: Alert.java:210
static void info(JFrame owner, String msg)
Show a generic alert.
Definition: Alert.java:183
static final short ERROR_MSG
Alert type for error messages.
Definition: Alert.java:38
Custom icon-text button with configurable styles.
Definition: Button.java:18
User alert or simple prompt dialog with configurable styles.
Definition: Alert.java:29
static final short QUESTION_MSG
Alert type for prompt messages.
Definition: Alert.java:35
static boolean prompt(JFrame owner, String msg)
Show an alert prompting the user to make a choice.
Definition: Alert.java:198
boolean getReply()
Get the user reply.
Definition: Alert.java:153