View Javadoc
1 package net.sourceforge.pmd.swingui; 2 3 import javax.swing.JButton; 4 import javax.swing.JDialog; 5 import javax.swing.JPanel; 6 import javax.swing.JTextArea; 7 import javax.swing.UIManager; 8 import javax.swing.border.CompoundBorder; 9 import javax.swing.border.EmptyBorder; 10 import javax.swing.border.EtchedBorder; 11 import java.awt.BorderLayout; 12 import java.awt.Color; 13 import java.awt.Dialog; 14 import java.awt.FlowLayout; 15 import java.awt.Frame; 16 import java.awt.Rectangle; 17 import java.awt.Window; 18 import java.awt.event.ActionEvent; 19 import java.awt.event.ActionListener; 20 import java.io.ByteArrayOutputStream; 21 import java.io.PrintStream; 22 23 /*** 24 * 25 * @author Donald A. Leckie 26 * @since August 17, 2002 27 * @version $Revision: 1.18 $, $Date: 2003/05/28 18:08:32 $ 28 */ 29 public class MessageDialog extends JDialog { 30 31 private JTextArea m_messageArea; 32 private Exception m_exception; 33 private boolean m_yesButtonWasPressed; 34 35 /*** 36 ******************************************************************************* 37 * 38 * @param parentWindow 39 * @param title 40 * @param job 41 */ 42 private MessageDialog(Frame parentWindow, String title, String message) { 43 super(parentWindow, title, true); 44 45 initialize(parentWindow, message); 46 } 47 48 /*** 49 ******************************************************************************* 50 * 51 * @param parentWindow 52 * @param title 53 * @param job 54 */ 55 private MessageDialog(Dialog parentWindow, String title, String message) { 56 super(parentWindow, title, true); 57 58 initialize(parentWindow, message); 59 } 60 61 /*** 62 ******************************************************************************* 63 * 64 * @param parentWindow 65 * @param message 66 */ 67 private void initialize(Window parentWindow, String message) { 68 int dialogWidth = 600; 69 int dialogHeight = 150; 70 Rectangle parentWindowBounds = parentWindow.getBounds(); 71 int x = parentWindowBounds.x + (parentWindowBounds.width - dialogWidth) / 2; 72 int y = parentWindowBounds.y + (parentWindowBounds.height - dialogHeight) / 2; 73 74 setBounds(x, y, dialogWidth, dialogHeight); 75 76 EtchedBorder etchedBorder; 77 EmptyBorder emptyBorder; 78 CompoundBorder compoundBorder; 79 JPanel basePanel; 80 81 basePanel = new JPanel(); 82 etchedBorder = new EtchedBorder(EtchedBorder.LOWERED); 83 emptyBorder = new EmptyBorder(15, 15, 15, 15); 84 compoundBorder = new CompoundBorder(etchedBorder, emptyBorder); 85 86 basePanel.setBorder(compoundBorder); 87 basePanel.setLayout(new BorderLayout()); 88 getContentPane().add(basePanel, BorderLayout.CENTER); 89 90 m_messageArea = ComponentFactory.createTextArea(message); 91 92 m_messageArea.setFont(UIManager.getFont("messageFont")); 93 m_messageArea.setEditable(false); 94 basePanel.add(m_messageArea, BorderLayout.CENTER); 95 } 96 97 /*** 98 ******************************************************************************* 99 * 100 */ 101 private void addCloseButton() { 102 JButton closeButton = ComponentFactory.createButton("Close"); 103 JPanel buttonPanel = new JPanel(new FlowLayout()); 104 105 closeButton = ComponentFactory.createButton("Close"); 106 closeButton.setForeground(Color.white); 107 closeButton.setBackground(Color.blue); 108 closeButton.addActionListener(new CloseButtonActionListener()); 109 110 buttonPanel.add(closeButton); 111 } 112 113 /*** 114 ******************************************************************************* 115 * 116 */ 117 private void addAnswerButtons() { 118 JButton yesButton; 119 JButton noButton; 120 JPanel buttonPanel; 121 122 buttonPanel = new JPanel(new FlowLayout()); 123 124 yesButton = ComponentFactory.createButton("Yes"); 125 yesButton.setForeground(Color.white); 126 yesButton.setBackground(UIManager.getColor("pmdGreen")); 127 yesButton.addActionListener(new YesButtonActionListener()); 128 buttonPanel.add(yesButton); 129 130 noButton = ComponentFactory.createButton("No"); 131 noButton.setForeground(Color.white); 132 noButton.setBackground(Color.red); 133 noButton.addActionListener(new NoButtonActionListener()); 134 buttonPanel.add(noButton); 135 136 getContentPane().add(buttonPanel, BorderLayout.SOUTH); 137 } 138 139 /*** 140 ******************************************************************************* 141 * 142 * @param parentWindow 143 * @param message 144 * @param exception 145 */ 146 public static void show(Window parentWindow, String message, Exception exception) { 147 if (exception == null) { 148 show(parentWindow, message); 149 } else { 150 ByteArrayOutputStream stream = new ByteArrayOutputStream(5000); 151 PrintStream printStream = new PrintStream(stream); 152 153 exception.printStackTrace(printStream); 154 155 if (message == null) { 156 message = stream.toString(); 157 } else { 158 message = message + "\n" + stream.toString(); 159 } 160 161 printStream.close(); 162 163 MessageDialog dialog; 164 165 if (parentWindow instanceof Frame) { 166 dialog = new MessageDialog((Frame) parentWindow, "Exception", message); 167 } else { 168 dialog = new MessageDialog((Dialog) parentWindow, "Exception", message); 169 } 170 171 dialog.addCloseButton(); 172 dialog.setVisible(true); 173 } 174 } 175 176 /*** 177 ******************************************************************************* 178 * 179 * @param parentWindow 180 * @param message 181 */ 182 protected static boolean answerIsYes(Window parentWindow, String message) { 183 MessageDialog dialog = setup(parentWindow, message); 184 185 dialog.addAnswerButtons(); 186 dialog.setVisible(true); 187 188 return dialog.m_yesButtonWasPressed; 189 } 190 191 /*** 192 ******************************************************************************* 193 * 194 * @param parentWindow 195 * @param message 196 */ 197 public static void show(Window parentWindow, String message) { 198 MessageDialog dialog; 199 200 dialog = setup(parentWindow, message); 201 dialog.addCloseButton(); 202 dialog.setVisible(true); 203 } 204 205 /*** 206 ******************************************************************************* 207 * 208 * @param parentWindow 209 * @param message 210 */ 211 private static MessageDialog setup(Window parentWindow, String message) { 212 if (message == null) { 213 message = "There is no message."; 214 } 215 216 MessageDialog dialog; 217 String title; 218 219 title = "Information"; 220 221 if (parentWindow instanceof Frame) { 222 dialog = new MessageDialog((Frame) parentWindow, title, message); 223 } else { 224 dialog = new MessageDialog((Dialog) parentWindow, title, message); 225 } 226 227 return dialog; 228 } 229 230 /*** 231 ******************************************************************************* 232 ******************************************************************************* 233 ******************************************************************************* 234 */ 235 private class CloseButtonActionListener implements ActionListener { 236 237 /*** 238 ************************************************************************ 239 * 240 * @param event 241 */ 242 public void actionPerformed(ActionEvent event) { 243 MessageDialog.this.setVisible(false); 244 } 245 } 246 247 /*** 248 ******************************************************************************* 249 ******************************************************************************* 250 ******************************************************************************* 251 */ 252 private class YesButtonActionListener implements ActionListener { 253 254 /*** 255 ************************************************************************ 256 * 257 * @param event 258 */ 259 public void actionPerformed(ActionEvent event) { 260 MessageDialog.this.m_yesButtonWasPressed = true; 261 MessageDialog.this.setVisible(false); 262 } 263 } 264 265 /*** 266 ******************************************************************************* 267 ******************************************************************************* 268 ******************************************************************************* 269 */ 270 private class NoButtonActionListener implements ActionListener { 271 272 /*** 273 ************************************************************************ 274 * 275 * @param event 276 */ 277 public void actionPerformed(ActionEvent event) { 278 MessageDialog.this.m_yesButtonWasPressed = false; 279 MessageDialog.this.setVisible(false); 280 } 281 } 282 }

This page was automatically generated by Maven