View Javadoc
1 /*** 2 * ArgvDialog.java 3 * 4 * Project: Dependency Tool 5 * 6 * WHEN WHO WHAT 7 * 06.06.2003 pko initial public release 8 * 04.10.2002 pko creation 9 * 10 * Copyright 2003 ELCA Informatique SA 11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland 12 * www.elca.ch 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 of 17 * the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 27 * USA 28 */ 29 30 package ch.elca.dependency; 31 32 import java.awt.AWTEvent; 33 import java.awt.Container; 34 import java.awt.Toolkit; 35 import java.awt.event.ActionEvent; 36 import java.awt.event.ActionListener; 37 import java.awt.event.WindowEvent; 38 import java.awt.event.WindowAdapter; 39 import javax.swing.BorderFactory; 40 import javax.swing.ImageIcon; 41 import javax.swing.JButton; 42 import javax.swing.JFrame; 43 import javax.swing.JLabel; 44 import javax.swing.JPanel; 45 import javax.swing.JScrollPane; 46 import javax.swing.JTextArea; 47 import javax.swing.WindowConstants; 48 49 import info.clearthought.layout.TableLayout; 50 51 /*** 52 * Class <code>DPToolErrorDialog</code> used by DPTool after failure 53 * of the MainFrame.execute() method to give the user the opportunity to 54 * restart an analysis. 55 * 56 * @author Pawel Kowalski 57 * @version 1.0-beta 58 * 59 * @tbd provide a superclass because the structure of the Jframes is mostly the same. 60 * @tbd implement a StringManager. 61 */ 62 public class DPToolErrorDialog extends JFrame { 63 64 /*** 65 * <code>NO_CHIOCE</code> to indicate that user pressed the NO-Button. 66 */ 67 public static final int NO_CHIOCE = -1; 68 69 /*** 70 * <code>YES_CHOICE</code> to indicate that user pressed the YES-Button. 71 */ 72 public static final int YES_CHOICE = 1; 73 74 //======================================================================/ 75 // constants, every DPToolErrorDialog will use. 76 //======================================================================/ 77 78 private static final int ERROR = 0; 79 private static final int MESSAGE = 1; 80 81 private static final int FRAME_WIDTH = 450; 82 private static final int FRAME_HEIGHT = 400; 83 private static final int HORIZONTAL_INSET = 10; 84 private static final int VERTICAL_INSET = 10; 85 private static final String DIALOG_NAME = "Dependency Tool"; 86 private static final String DPT_ENCOUNTERED_PROBLEM 87 = "Dependency Tool encountered a problem"; 88 private static final String RESTART_DPT 89 = "Do you wish to restart Dependency Tool?"; 90 private static final String EXCEPTION_IS = "The problem is:"; 91 private static final String UNKNOWN_CAUSE = "unknown cause"; 92 private static final String CONTINUE = "CONTINUE"; 93 private static final String YES = "YES"; 94 private static final String NO = "NO"; 95 96 //======================================================================/ 97 // some gui elements. 98 //======================================================================/ 99 100 private static DPToolErrorDialog s_frame = null; 101 private JButton m_yesButton = null; 102 private JButton m_noButton = null; 103 private JButton m_continueButton = null; 104 private JTextArea m_exceptionDisplay = null; 105 106 //======================================================================/ 107 // some vars associated with an instance of DPToolErrorDialog. 108 //======================================================================/ 109 110 private Throwable m_exception = null; 111 private String m_message = null; 112 private boolean m_done = false; 113 private int m_choice = NO_CHIOCE; 114 115 //======================================================================/ 116 // static utility method 117 //======================================================================/ 118 119 /*** 120 * Show continue dialog to the user. 121 * A public utility method for using this Class. A user of this 122 * class will usually call only this method. 123 * 124 * @param e an <code>Exception</code> value 125 * @return an <code>int</code> value 126 */ 127 public static int showErrorDialog(Exception e) { 128 129 s_frame = new DPToolErrorDialog(ERROR, e.getMessage(), e); 130 s_frame.setVisible(true); 131 132 synchronized (s_frame) { 133 try { 134 s_frame.wait(); 135 } catch (InterruptedException ie) { 136 while (!s_frame.m_done) { 137 // in case of an Exception wait for termination of this frame 138 } 139 } 140 } 141 142 s_frame.dispose(); 143 144 return s_frame.m_choice; 145 } 146 147 /*** 148 * Show continue dialog to the user. The user can confirm with a 149 * "Continue" button and the application will continue processing. 150 * A public utility method for using this Class. A user of this 151 * class will usually call only this method. 152 * 153 * @param message a <code>String</code> value to be displayed. 154 */ 155 public static void showContinueDialog(String message, Throwable exception) { 156 157 s_frame = new DPToolErrorDialog(MESSAGE, message, exception); 158 s_frame.setVisible(true); 159 160 synchronized (s_frame) { 161 try { 162 s_frame.wait(); 163 } catch (InterruptedException ie) { 164 while (!s_frame.m_done) { 165 // in case of an Exception wait for termination of this frame 166 } 167 } 168 } 169 170 s_frame.dispose(); 171 } 172 173 //======================================================================/ 174 // constructor 175 //======================================================================/ 176 177 /*** 178 * Creates a new <code>DPToolErrorDialog</code> instance. 179 * 180 * @param e an <code>Exception</code> value 181 */ 182 private DPToolErrorDialog(int type, String message, Throwable exception) { 183 m_message = message; 184 m_exception = exception; 185 186 initFrame(); 187 188 if (type == ERROR) { 189 setContentPane(makeErrorGUI()); 190 initErrorListeners(); 191 } else { 192 setContentPane(makeMessageGUI()); 193 initMessageListeners(); 194 } 195 196 initGUI(); 197 } 198 199 //======================================================================/ 200 // init methods 201 //======================================================================/ 202 203 /*** 204 * Initialize this JFrame. 205 * 206 * @tbd get the icon through a RessourceManager, not directly. 207 */ 208 private void initFrame() { 209 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 210 211 setResizable(false); 212 setSize(FRAME_WIDTH, FRAME_HEIGHT); 213 setTitle(DIALOG_NAME); 214 setIconImage(new ImageIcon(Thread.currentThread().getContextClassLoader() 215 .getResource("images/dpticon.png")).getImage()); 216 217 Toolkit tollkit = Toolkit.getDefaultToolkit(); 218 int screenWidth = tollkit.getScreenSize().width; 219 int screenHeight = tollkit.getScreenSize().height; 220 221 setLocation((screenWidth - FRAME_WIDTH) / 2, 222 (screenHeight - FRAME_HEIGHT) / 2); 223 } 224 225 /*** 226 * Make the Error GUI. 227 * 228 * @return a <code>Container</code> value 229 * 230 * @tbd get the icon through a RessourceManager, not directly. 231 */ 232 private Container makeErrorGUI() { 233 234 double size[][] = 235 {{-2.0, 80.0, -1.0}, // Columns 236 {-2.0, 30.0, 30.0, 30.0, 30.0, 30.0, -1.0}}; // Rows 237 238 JPanel contentPane = new JPanel(); 239 contentPane.setLayout(new TableLayout(size)); 240 contentPane.setBorder(BorderFactory.createEmptyBorder(HORIZONTAL_INSET, VERTICAL_INSET, 241 HORIZONTAL_INSET, VERTICAL_INSET)); 242 243 JLabel iconLabel = new JLabel(new ImageIcon(Thread.currentThread().getContextClassLoader() 244 .getResource("images/dpt.png"))); 245 contentPane.add(iconLabel, "1, 1, 1, 3"); 246 247 JLabel problemLabel = new JLabel(DPT_ENCOUNTERED_PROBLEM); 248 contentPane.add(problemLabel, "2, 1, C, C"); 249 250 JLabel restartLabel = new JLabel(RESTART_DPT); 251 contentPane.add(restartLabel, "2, 2, C, C"); 252 253 JPanel controlPanel = new JPanel(); 254 controlPanel.add(m_yesButton = new JButton(YES)); 255 controlPanel.add(m_noButton = new JButton(NO)); 256 contentPane.add(controlPanel, "2, 3, C, C"); 257 contentPane.add(new JLabel(), "1, 4, F, F"); 258 259 JLabel exceptionLabel = new JLabel(EXCEPTION_IS); 260 contentPane.add(exceptionLabel, "1, 5, 2, 5"); 261 262 m_exceptionDisplay = new JTextArea(); 263 m_exceptionDisplay.setEditable(false); 264 JScrollPane scrollPane = new JScrollPane(m_exceptionDisplay); 265 contentPane.add(scrollPane, "1, 6, 2, 6"); 266 267 return contentPane; 268 } 269 270 /*** 271 * Make the Message GUI. 272 * 273 * @return a <code>Container</code> value 274 * 275 * @tbd get the icon through a RessourceManager, not directly. 276 */ 277 private Container makeMessageGUI() { 278 279 double size[][] = 280 {{-2.0, 80.0, -1.0}, // Columns 281 {-2.0, 30.0, 30.0, 30.0, 30.0, 30.0, -1.0}}; // Rows 282 283 JPanel contentPane = new JPanel(); 284 contentPane.setLayout(new TableLayout(size)); 285 contentPane.setBorder(BorderFactory.createEmptyBorder(HORIZONTAL_INSET, VERTICAL_INSET, 286 HORIZONTAL_INSET, VERTICAL_INSET)); 287 288 JLabel iconLabel = new JLabel(new ImageIcon(Thread.currentThread().getContextClassLoader() 289 .getResource("images/dpt.png"))); 290 contentPane.add(iconLabel, "1, 1, 1, 3"); 291 292 JLabel problemLabel = new JLabel(DPT_ENCOUNTERED_PROBLEM); 293 contentPane.add(problemLabel, "2, 1, C, C"); 294 295 JPanel controlPanel = new JPanel(); 296 controlPanel.add(m_continueButton = new JButton(CONTINUE)); 297 contentPane.add(controlPanel, "2, 3, C, C"); 298 contentPane.add(new JLabel(), "1, 4, F, F"); 299 300 JLabel exceptionLabel = new JLabel(EXCEPTION_IS); 301 contentPane.add(exceptionLabel, "1, 5, 2, 5"); 302 303 m_exceptionDisplay = new JTextArea(); 304 m_exceptionDisplay.setEditable(false); 305 JScrollPane scrollPane = new JScrollPane(m_exceptionDisplay); 306 contentPane.add(scrollPane, "1, 6, 2, 6"); 307 308 return contentPane; 309 } 310 311 /*** 312 * Initialize the GUI. 313 */ 314 private void initGUI() { 315 316 if (m_message != null) { 317 m_exceptionDisplay.append(m_message); 318 m_exceptionDisplay.append("\n\n"); 319 } 320 321 m_exceptionDisplay.append(m_exception.getMessage()); 322 m_exceptionDisplay.append("\n\n"); 323 324 if (m_exception == null) { 325 m_exceptionDisplay.setText(UNKNOWN_CAUSE); 326 return; 327 } 328 329 StackTraceElement[] stackTraceElements = m_exception.getStackTrace(); 330 for (int i = 0; i < stackTraceElements.length; i++) { 331 m_exceptionDisplay.append(stackTraceElements[i].toString()); 332 m_exceptionDisplay.append("\n"); 333 } 334 } 335 336 /*** 337 * Initialize all Listeners for the Error Message Box. 338 */ 339 private void initErrorListeners() { 340 m_yesButton.addActionListener(new ActionListener() { 341 public void actionPerformed(ActionEvent e) { 342 handleYesButton(); 343 } 344 }); 345 m_noButton.addActionListener(new ActionListener() { 346 public void actionPerformed(ActionEvent e) { 347 handleNoButton(); 348 } 349 }); 350 addWindowListener(new WindowAdapter() { 351 public void windowClosing(WindowEvent e) { 352 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 353 System.exit(0); 354 } 355 } 356 }); 357 } 358 359 /*** 360 * Initialize all Listeners for the Continue Message Box. 361 */ 362 private void initMessageListeners() { 363 m_continueButton.addActionListener(new ActionListener() { 364 public void actionPerformed(ActionEvent e) { 365 handleContinueButton(); 366 } 367 }); 368 addWindowListener(new WindowAdapter() { 369 public void windowClosing(WindowEvent e) { 370 handleContinueButton(); 371 } 372 }); 373 } 374 375 //======================================================================/ 376 // handle actions methods 377 //======================================================================/ 378 379 /*** 380 * Handles YES-Button Events. 381 */ 382 private void handleYesButton() { 383 m_choice = DPToolErrorDialog.YES_CHOICE; 384 terminate(); 385 } 386 387 /*** 388 * Handles NO-Button Events. 389 */ 390 private void handleNoButton() { 391 m_choice = DPToolErrorDialog.NO_CHIOCE; 392 terminate(); 393 } 394 395 /*** 396 * Handles Continue-Button Events. 397 */ 398 private void handleContinueButton() { 399 terminate(); 400 } 401 402 /*** 403 * Terminate this frame. 404 */ 405 private synchronized void terminate() { 406 m_done = true; 407 notifyAll(); 408 } 409 410 //======================================================================/ 411 // overriden methods 412 //======================================================================/ 413 414 /*** 415 * Overriden <code>processWindowEvent</code> method. 416 * 417 * @param e a <code>WindowEvent</code> value 418 */ 419 // protected void processWindowEvent(WindowEvent e) { 420 // super.processWindowEvent(e); 421 // if (e.getID() == WindowEvent.WINDOW_CLOSING) { 422 // System.exit(0); 423 // } 424 // } 425 } 426

This page was automatically generated by Maven