Coverage Report - org.argouml.ui.ExceptionDialog
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionDialog
68%
44/64
10%
1/10
1.25
ExceptionDialog$1
33%
1/3
N/A
1.25
ExceptionDialog$2
100%
3/3
N/A
1.25
ExceptionDialog$3
33%
1/3
N/A
1.25
 
 1  
 /* $Id: ExceptionDialog.java 17841 2010-01-12 19:17:52Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 package org.argouml.ui;
 39  
 
 40  
 import java.awt.BorderLayout;
 41  
 import java.awt.Dimension;
 42  
 import java.awt.Frame;
 43  
 import java.awt.Toolkit;
 44  
 import java.awt.event.ActionEvent;
 45  
 import java.awt.event.ActionListener;
 46  
 import java.awt.event.WindowAdapter;
 47  
 import java.awt.event.WindowEvent;
 48  
 import java.io.PrintWriter;
 49  
 import java.io.StringWriter;
 50  
 import java.util.Date;
 51  
 
 52  
 import javax.swing.BorderFactory;
 53  
 import javax.swing.JButton;
 54  
 import javax.swing.JDialog;
 55  
 import javax.swing.JEditorPane;
 56  
 import javax.swing.JLabel;
 57  
 import javax.swing.JPanel;
 58  
 import javax.swing.JScrollPane;
 59  
 import javax.swing.event.HyperlinkEvent;
 60  
 import javax.swing.event.HyperlinkListener;
 61  
 
 62  
 import org.argouml.i18n.Translator;
 63  
 import org.argouml.util.osdep.StartBrowser;
 64  
 
 65  
 /**
 66  
  * A window that displays an exception to the user if we can't handle it
 67  
  * in any other way.
 68  
  * 
 69  
  * TODO: This has been partly converted to be a generic error dialog 
 70  
  * rather than something specific to exceptions.  This should be renamed
 71  
  * when that process is complete.
 72  
  */
 73  28
 public class ExceptionDialog extends JDialog implements ActionListener {
 74  
 
 75  
     private JButton closeButton;
 76  
     private JButton copyButton;
 77  
     private JLabel northLabel;
 78  
     private JEditorPane textArea;
 79  
 
 80  
     /**
 81  
      * Construct an exception dialog for the given frame and throwable.
 82  
      *
 83  
      * @param f the <code>Frame</code> from which the dialog is displayed
 84  
      * @param e the exception
 85  
      */
 86  
     public ExceptionDialog(Frame f, Throwable e) {
 87  0
         this(f, Translator.localize("dialog.exception.unknown.error"), e);
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Construct an exception dialog with the given parameters.
 92  
      *
 93  
      * @param f the <code>Frame</code> from which the dialog is displayed
 94  
      * @param message the message
 95  
      * @param e the exception
 96  
      */
 97  
     public ExceptionDialog(Frame f, String message, Throwable e) {
 98  0
         this(f, message, e, false);
 99  0
     }
 100  
 
 101  
     /**
 102  
      * Construct an exception dialog with the given parameters.
 103  
      *
 104  
      * @param f   the <code>Frame</code> from which the dialog is displayed
 105  
      * @param message
 106  
      *            the message
 107  
      * @param e   the exception
 108  
      * @param highlightCause
 109  
      *            give priority to Throwable.cause in display. Use this if the
 110  
      *            main exception is mostly boilerplate and the really useful
 111  
      *            information is in the enclosed cause.
 112  
      */
 113  
     public ExceptionDialog(Frame f, String message, Throwable e,
 114  
             boolean highlightCause) {
 115  0
         this(f, Translator.localize("dialog.exception.title"), 
 116  
                 Translator.localize("dialog.exception.message"), 
 117  
                 formatException(message, e, highlightCause));
 118  0
     }
 119  
 
 120  
     public static String formatException(String message, Throwable e,
 121  
             boolean highlightCause) {
 122  0
         StringWriter sw = new StringWriter();
 123  0
         PrintWriter pw = new PrintWriter(sw);
 124  
 
 125  0
         if (highlightCause && e.getCause() != null) {
 126  
 
 127  
             // This text is for the developers.
 128  
             // It doesn't need to be localized.
 129  0
             pw.print(message );
 130  0
             pw.print("<hr>System Info:<p>" + SystemInfoDialog.getInfo());
 131  0
             pw.print("<p><hr>Error occurred at : " + new Date());
 132  0
             pw.print("<p>  Cause : ");
 133  0
             e.getCause().printStackTrace(pw);
 134  0
             pw.print("-------<p>Full exception : ");
 135  
         }
 136  0
         e.printStackTrace(pw);
 137  0
         return sw.toString();
 138  
     }
 139  
 
 140  
     
 141  
     /**
 142  
      * Construct an exception dialog with given title, introduction, and detail
 143  
      * message.
 144  
      * 
 145  
      * @param f
 146  
      *            the <code>Frame</code> from which the dialog is displayed
 147  
      * @param title
 148  
      *            string to use as title of dialog box
 149  
      * @param intro
 150  
      *            introductory text (summary of error)
 151  
      * @param message
 152  
      *            the message
 153  
      */
 154  
     public ExceptionDialog(Frame f, String title, String intro, 
 155  
             String message) {
 156  26
         super(f);
 157  26
         setResizable(true);
 158  26
         setModal(false);
 159  26
         setTitle(title);
 160  
 
 161  26
         Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
 162  26
         getContentPane().setLayout(new BorderLayout(0, 0));
 163  
 
 164  
         // the introducing label
 165  26
         northLabel =
 166  
             new JLabel(intro);
 167  26
         northLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 168  26
         getContentPane().add(northLabel, BorderLayout.NORTH);
 169  
 
 170  
         // the text box containing the problem messages
 171  
         // TODO: This should be hidden by default, but accessible on 
 172  
         // via a "details" button or tab to provide more info to the user.
 173  26
         textArea = new JEditorPane();
 174  26
         textArea.setContentType("text/html");
 175  26
         textArea.setEditable(false);
 176  26
         textArea.addHyperlinkListener(new HyperlinkListener() {
 177  
             public void hyperlinkUpdate(HyperlinkEvent hle) {
 178  0
                 linkEvent(hle);
 179  0
             }
 180  
         });
 181  
 
 182  
         // These shouldn't really be <br> instead of <p> elements, but
 183  
         // the lines all get run together when pasted into a browser window.
 184  26
         textArea.setText(message.replaceAll("\n", "<p>"));
 185  26
         textArea.setCaretPosition(0);
 186  
 
 187  26
         JPanel centerPanel = new JPanel(new BorderLayout());
 188  26
         centerPanel.add(new JScrollPane(textArea));
 189  26
         centerPanel.setPreferredSize(new Dimension(500, 200));
 190  26
         getContentPane().add(centerPanel);
 191  
 
 192  26
         copyButton =
 193  
             new JButton(Translator.localize("button.copy-to-clipboard"));
 194  26
         copyButton.addActionListener(new ActionListener() {
 195  
             public void actionPerformed(ActionEvent evt) {
 196  2
                 copyActionPerformed(evt);
 197  2
             }
 198  
         });
 199  
 
 200  26
         closeButton = new JButton(Translator.localize("button.close"));
 201  26
         closeButton.addActionListener(this);
 202  26
         JPanel bottomPanel = new JPanel();
 203  
 
 204  26
         bottomPanel.add(copyButton);
 205  26
         bottomPanel.add(closeButton);
 206  26
         getContentPane().add(bottomPanel, BorderLayout.SOUTH);
 207  
 
 208  26
         addWindowListener(new WindowAdapter() {
 209  
             public void windowClosing(WindowEvent evt) {
 210  0
                 disposeDialog();
 211  0
             }
 212  
         });
 213  
 
 214  26
         pack();
 215  26
         Dimension contentPaneSize = getContentPane().getSize();
 216  26
         setLocation(scrSize.width / 2 - contentPaneSize.width / 2,
 217  
                 scrSize.height / 2 - contentPaneSize.height / 2);
 218  26
     }
 219  
 
 220  
     /*
 221  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 222  
      */
 223  
     public void actionPerformed(ActionEvent e) {
 224  22
         disposeDialog();
 225  22
     }
 226  
 
 227  
     /**
 228  
      * Copy the textpane's contents to the clipboard.
 229  
      *
 230  
      * @param e the actionEvent
 231  
      */
 232  
     private void copyActionPerformed(ActionEvent e) {
 233  2
         assert e.getSource() == copyButton;
 234  2
         textArea.setSelectionStart(0);
 235  2
         textArea.setSelectionEnd(textArea.getText().length());
 236  2
         textArea.copy();
 237  2
         textArea.setSelectionEnd(0);
 238  2
     }
 239  
 
 240  
     /**
 241  
      * Dispose this dialog.
 242  
      */
 243  
     private void disposeDialog() {
 244  22
         setVisible(false);
 245  22
         dispose();
 246  22
     }
 247  
 
 248  
     /**
 249  
      * Handle link activation for our hyperlink.
 250  
      *
 251  
      * @param e the event
 252  
      */
 253  
     private void linkEvent(HyperlinkEvent e) {
 254  0
         if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
 255  0
             StartBrowser.openUrl(e.getURL());
 256  
         }
 257  0
     }
 258  
 
 259  
      /**
 260  
      * The UID.
 261  
      */
 262  
     private static final long serialVersionUID = -2773182347529547418L;
 263  
 
 264  
 }