// Copyright (c) 2016 Timothy D. Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package io.github.jonestimd.swing.dialog;

import java.awt.BorderLayout;
import java.awt.Window;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.google.common.base.MoreObjects;
import io.github.jonestimd.swing.ComponentFactory;
import io.github.jonestimd.swing.action.CancelAction;

import static io.github.jonestimd.swing.ComponentFactory.*;

/**
 * Display an exception with its stack trace.
 */
public class ExceptionDialog extends MessageDialog {
    public static final String DEFAULT_RESOURCE_PREFIX = "exceptionDialog.";

    /**
     * Construct an exception dialog using {@link ComponentFactory#DEFAULT_BUNDLE}.
     */
    public ExceptionDialog(Window owner, Throwable exception) {
        this(owner, DEFAULT_BUNDLE, DEFAULT_RESOURCE_PREFIX, exception);
    }

    /**
     * Construct an exception dialog using {@link ComponentFactory#DEFAULT_BUNDLE}.
     */
    public ExceptionDialog(Window owner, String label, Throwable exception) {
        this(owner, DEFAULT_BUNDLE, DEFAULT_RESOURCE_PREFIX, label, exception);
    }

    /**
     * Construct an exception dialog using the specified resource bundle with fallback to
     * {@link ComponentFactory#DEFAULT_BUNDLE} for missing values.
     */
    public ExceptionDialog(Window owner, ResourceBundle bundle, String resourcePrefix, Throwable exception) {
        this(owner, bundle, resourcePrefix, getString(bundle, resourcePrefix, "exception.label"), exception);
    }

    /**
     * Construct an exception dialog using the specified resource bundle with fallback to
     * {@link ComponentFactory#DEFAULT_BUNDLE} for missing values.
     */
    public ExceptionDialog(Window owner, ResourceBundle bundle, String resourcePrefix, String label, Throwable exception) {
        super(owner, getString(bundle, resourcePrefix, "title"), ModalityType.APPLICATION_MODAL);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JLabel(label), BorderLayout.NORTH);
        JTextArea detailArea = new JTextArea(getInt(bundle, resourcePrefix, "exception.rows"), getInt(bundle, resourcePrefix, "exception.columns"));
        detailArea.setEditable(false);
        detailArea.setTabSize(4);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        exception.printStackTrace(new PrintStream(buffer));
        try {
            detailArea.setText(buffer.toString("UTF-8"));
        }
        catch (UnsupportedEncodingException ex) {
            detailArea.setText(getString(bundle, resourcePrefix, "noStackTrace"));
        }
        getContentPane().add(new JScrollPane(detailArea), BorderLayout.CENTER);
        pack();
        CancelAction.install(this);
    }

    private static int getInt(ResourceBundle bundle, String resourcePrefix, String key) {
        try {
            return Integer.parseInt(bundle.getObject(MoreObjects.firstNonNull(resourcePrefix, "") + key).toString());
        } catch (Exception ex) {
            return (int) DEFAULT_BUNDLE.getObject(DEFAULT_RESOURCE_PREFIX + key);
        }
    }

    private static String getString(ResourceBundle bundle, String resourcePrefix, String key) {
        try {
            return bundle.getString(MoreObjects.firstNonNull(resourcePrefix, "") + key);
        } catch (Exception ex) {
            return DEFAULT_BUNDLE.getString(DEFAULT_RESOURCE_PREFIX + key);
        }
    }
}
@@ -1180,16 +1180,43 @@
 Layout;%0A
+import java.awt.Component;%0A
 import j
@@ -1231,16 +1231,16 @@
 Window;%0A
-
 import j
@@ -1560,16 +1560,69 @@
 actory;%0A
+import io.github.jonestimd.swing.ComponentTreeUtils;%0A
 import i
@@ -1836,16 +1836,16 @@
 ialog %7B%0A
-
     publ
@@ -1914,16 +1914,202 @@
 log.%22;%0A%0A
+    public static void show(Component owner, Throwable throwable) %7B%0A        new ExceptionDialog(ComponentTreeUtils.findAncestor(owner, Window.class), throwable).setVisible(true);%0A    %7D%0A%0A
     /**%0A

Add static method to show an ExceptionDialog