/*
 * @(#)JPTError.java    1.0   2 May 2006
 *
 * Copyright 2006
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.util;

import edu.neu.ccs.gui.*;
import edu.neu.ccs.console.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * <P>An error thrown in the JPT that indicates 
 * a programming problem rather than a user or system error, 
 * which should not need to be caught 
 * within an application or applet.  
 *
 * In the debug state, a <CODE>JPTError</CODE>
 * is handled automatically by the toolkit 
 * and is presented to the programmer 
 * to signal that a grave programming error
 * has occurred in the running program.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 * @see JPTUtilities#setDebugMode(boolean)
 * @see JPTUtilities#isDebugModeSet()
 */
public class JPTError 
    extends Error 
    implements ConsoleAware 
{
    /**
     * Constructor for an error 
     * with no detail message.
     *
     * @see #JPTError(String)
     */
    public JPTError() {
        this("(details not provided)");
    }

    /**
     * Constructor for an error 
     * with the given detail message.
     *
     * @param errorMessage the detail message for the error
     * @see #JPTError()
     */
    public JPTError(String errorMessage) {
        super(errorMessage);

        // in debug mode, notify the programmer of the error
        // and provide options to exit or continue.
        if (JPTUtilities.isDebugModeSet()) {

            // build the dialog
            final JDialog dialog = 
                new JDialog((Frame)null, "Error!", true);
            dialog.setDefaultCloseOperation(
                JDialog.DISPOSE_ON_CLOSE);

            // build the content pane for the dialog
            JPanel pane = new JPanel(new BorderLayout());
            pane.setBorder(new EmptyBorder(25, 25, 25, 25));

            // build the error message and its prompt
            JLabel prompt = new JLabel(
                "The following error was reported:");
            JLabel message = new JLabel(errorMessage);
            message.setBorder(new EmptyBorder(10, 0, 10, 0));
            
            // build the actions for the dialog
            ActionsPanel actions = new ActionsPanel();
            actions.addAction(new SimpleAction("Exit Program") {
                public void perform() {
                    System.exit(0);
                }
            });
            actions.addAction(new SimpleAction("Print stack trace") {
                public void perform() {
                    printStackTrace(console.out);
                }
            });
            actions.addAction(new SimpleAction("Ignore") {
                public void perform() {
                    dialog.setVisible(false);
                    dialog.dispose();
                }
            });

            // add components to the dialog
            pane.add(prompt, BorderLayout.NORTH);
            pane.add(message, BorderLayout.CENTER);
            pane.add(actions, BorderLayout.SOUTH);
            dialog.setContentPane(pane);
            
            // size appropriately and center on screen
            dialog.pack();
            Dimension screen = 
                Toolkit.getDefaultToolkit().getScreenSize();
            dialog.setLocation(
                (screen.width  - dialog.getWidth())  / 2,
                (screen.height - dialog.getHeight()) / 2);

            // show dialog
            dialog.setVisible(true);
        }
    }
}
