001: package org.acm.seguin.pmd.swingui;
002:
003: import javax.swing.JButton;
004: import javax.swing.JDialog;
005: import javax.swing.JPanel;
006: import javax.swing.JTextArea;
007: import javax.swing.UIManager;
008: import javax.swing.border.CompoundBorder;
009: import javax.swing.border.EmptyBorder;
010: import javax.swing.border.EtchedBorder;
011: import java.awt.BorderLayout;
012: import java.awt.Color;
013: import java.awt.Dialog;
014: import java.awt.FlowLayout;
015: import java.awt.Frame;
016: import java.awt.Rectangle;
017: import java.awt.Window;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.io.ByteArrayOutputStream;
021: import java.io.PrintStream;
022:
023: /**
024: *
025: * @author Donald A. Leckie
026: * @since August 17, 2002
027: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
028: */
029: public class MessageDialog extends JDialog {
030:
031: private JTextArea m_messageArea;
032: private Exception m_exception;
033: private boolean m_yesButtonWasPressed;
034:
035: /**
036: *******************************************************************************
037: *
038: * @param parentWindow
039: * @param title
040: * @param job
041: */
042: private MessageDialog(Frame parentWindow, String title,
043: String message) {
044: super (parentWindow, title, true);
045:
046: initialize(parentWindow, message);
047: }
048:
049: /**
050: *******************************************************************************
051: *
052: * @param parentWindow
053: * @param title
054: * @param job
055: */
056: private MessageDialog(Dialog parentWindow, String title,
057: String message) {
058: super (parentWindow, title, true);
059:
060: initialize(parentWindow, message);
061: }
062:
063: /**
064: *******************************************************************************
065: *
066: * @param parentWindow
067: * @param message
068: */
069: private void initialize(Window parentWindow, String message) {
070: int dialogWidth = 600;
071: int dialogHeight = 150;
072: Rectangle parentWindowBounds = parentWindow.getBounds();
073: int x = parentWindowBounds.x
074: + (parentWindowBounds.width - dialogWidth) / 2;
075: int y = parentWindowBounds.y
076: + (parentWindowBounds.height - dialogHeight) / 2;
077:
078: setBounds(x, y, dialogWidth, dialogHeight);
079:
080: EtchedBorder etchedBorder;
081: EmptyBorder emptyBorder;
082: CompoundBorder compoundBorder;
083: JPanel basePanel;
084:
085: basePanel = new JPanel();
086: etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);
087: emptyBorder = new EmptyBorder(15, 15, 15, 15);
088: compoundBorder = new CompoundBorder(etchedBorder, emptyBorder);
089:
090: basePanel.setBorder(compoundBorder);
091: basePanel.setLayout(new BorderLayout());
092: getContentPane().add(basePanel, BorderLayout.CENTER);
093:
094: m_messageArea = ComponentFactory.createTextArea(message);
095:
096: m_messageArea.setFont(UIManager.getFont("messageFont"));
097: m_messageArea.setEditable(false);
098: basePanel.add(m_messageArea, BorderLayout.CENTER);
099: }
100:
101: /**
102: *******************************************************************************
103: *
104: */
105: private void addCloseButton() {
106: JButton closeButton = ComponentFactory.createButton("Close");
107: JPanel buttonPanel = new JPanel(new FlowLayout());
108:
109: closeButton = ComponentFactory.createButton("Close");
110: closeButton.setForeground(Color.white);
111: closeButton.setBackground(Color.blue);
112: closeButton.addActionListener(new CloseButtonActionListener());
113:
114: buttonPanel.add(closeButton);
115: }
116:
117: /**
118: *******************************************************************************
119: *
120: */
121: private void addAnswerButtons() {
122: JButton yesButton;
123: JButton noButton;
124: JPanel buttonPanel;
125:
126: buttonPanel = new JPanel(new FlowLayout());
127:
128: yesButton = ComponentFactory.createButton("Yes");
129: yesButton.setForeground(Color.white);
130: yesButton.setBackground(UIManager.getColor("pmdGreen"));
131: yesButton.addActionListener(new YesButtonActionListener());
132: buttonPanel.add(yesButton);
133:
134: noButton = ComponentFactory.createButton("No");
135: noButton.setForeground(Color.white);
136: noButton.setBackground(Color.red);
137: noButton.addActionListener(new NoButtonActionListener());
138: buttonPanel.add(noButton);
139:
140: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
141: }
142:
143: /**
144: *******************************************************************************
145: *
146: * @param parentWindow
147: * @param message
148: * @param exception
149: */
150: public static void show(Window parentWindow, String message,
151: Exception exception) {
152: if (exception == null) {
153: show(parentWindow, message);
154: } else {
155: ByteArrayOutputStream stream = new ByteArrayOutputStream(
156: 5000);
157: PrintStream printStream = new PrintStream(stream);
158:
159: exception.printStackTrace(printStream);
160:
161: if (message == null) {
162: message = stream.toString();
163: } else {
164: message = message + "\n" + stream.toString();
165: }
166:
167: printStream.close();
168:
169: MessageDialog dialog;
170:
171: if (parentWindow instanceof Frame) {
172: dialog = new MessageDialog((Frame) parentWindow,
173: "Exception", message);
174: } else {
175: dialog = new MessageDialog((Dialog) parentWindow,
176: "Exception", message);
177: }
178:
179: dialog.addCloseButton();
180: dialog.setVisible(true);
181: }
182: }
183:
184: /**
185: *******************************************************************************
186: *
187: * @param parentWindow
188: * @param message
189: */
190: protected static boolean answerIsYes(Window parentWindow,
191: String message) {
192: MessageDialog dialog = setup(parentWindow, message);
193:
194: dialog.addAnswerButtons();
195: dialog.setVisible(true);
196:
197: return dialog.m_yesButtonWasPressed;
198: }
199:
200: /**
201: *******************************************************************************
202: *
203: * @param parentWindow
204: * @param message
205: */
206: public static void show(Window parentWindow, String message) {
207: MessageDialog dialog;
208:
209: dialog = setup(parentWindow, message);
210: dialog.addCloseButton();
211: dialog.setVisible(true);
212: }
213:
214: /**
215: *******************************************************************************
216: *
217: * @param parentWindow
218: * @param message
219: */
220: private static MessageDialog setup(Window parentWindow,
221: String message) {
222: if (message == null) {
223: message = "There is no message.";
224: }
225:
226: MessageDialog dialog;
227: String title;
228:
229: title = "Information";
230:
231: if (parentWindow instanceof Frame) {
232: dialog = new MessageDialog((Frame) parentWindow, title,
233: message);
234: } else {
235: dialog = new MessageDialog((Dialog) parentWindow, title,
236: message);
237: }
238:
239: return dialog;
240: }
241:
242: /**
243: *******************************************************************************
244: *******************************************************************************
245: *******************************************************************************
246: */
247: private class CloseButtonActionListener implements ActionListener {
248:
249: /**
250: ************************************************************************
251: *
252: * @param event
253: */
254: public void actionPerformed(ActionEvent event) {
255: MessageDialog.this .setVisible(false);
256: }
257: }
258:
259: /**
260: *******************************************************************************
261: *******************************************************************************
262: *******************************************************************************
263: */
264: private class YesButtonActionListener implements ActionListener {
265:
266: /**
267: ************************************************************************
268: *
269: * @param event
270: */
271: public void actionPerformed(ActionEvent event) {
272: MessageDialog.this .m_yesButtonWasPressed = true;
273: MessageDialog.this .setVisible(false);
274: }
275: }
276:
277: /**
278: *******************************************************************************
279: *******************************************************************************
280: *******************************************************************************
281: */
282: private class NoButtonActionListener implements ActionListener {
283:
284: /**
285: ************************************************************************
286: *
287: * @param event
288: */
289: public void actionPerformed(ActionEvent event) {
290: MessageDialog.this .m_yesButtonWasPressed = false;
291: MessageDialog.this .setVisible(false);
292: }
293: }
294: }
|