Package jbrickbreaker.view

Source Code of jbrickbreaker.view.UnexpectedExceptionView

package jbrickbreaker.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.MessageFormat;
import java.util.Locale;

import javax.swing.*;

import jbrickbreaker.JBrickBreaker;
import jbrickbreaker.controller.LanguageListener;

/**
* This class represents a dialog that is displayed to the user when an
* unexpected exception occurs.
*
* Date: 12 Dec 2010
* Time: 15:57:10
*
* @author Thomas Michel
*/
public class UnexpectedExceptionView extends JDialog implements ILanguage {

    private JBrickBreaker bb;

    private Throwable throwable;

    private static UnexpectedExceptionView instance;

    private JLabel detailedStackLabel, text;

    private UnexpectedExceptionView(JBrickBreaker brick, Throwable e) {
        super();
        bb = brick;
        throwable = e;
        createView();
        bb.addLanguageListener(new LanguageListener(this));
    }

    public static void showUnexpectedExceptionView(JBrickBreaker brick,
            Throwable e) {
        if (instance == null) {
            instance = new UnexpectedExceptionView(brick, e);
        }
        instance.throwable = e;
        instance.setVisible(true);
    }

    private void createView() {
        setModal(true);
        setResizable(false);
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0.0d, 0.0d,
                GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
                new Insets(2, 2, 2, 2), 0, 0);
        Icon error = UIManager.getIcon("OptionPane.errorIcon"); //$NON-NLS-1$
        JLabel errorLabel = new JLabel(error);
        text = new JLabel();
        JTextArea stack = new JTextArea();
        stack.setEditable(false);
        stack.setBackground(UIManager.getColor("control")); //$NON-NLS-1$
        StringBuffer sb = new StringBuffer(100);
        sb.append(throwable.getClass().getCanonicalName()).append(": ") //$NON-NLS-1$
                .append(throwable.getLocalizedMessage());
        sb.append('\n');
        for (StackTraceElement ste : throwable.getStackTrace()) {
            sb.append(ste.toString()).append('\n');
        }
        detailedStackLabel = new JLabel();
        stack.setText(sb.toString());
        gbc.fill = GridBagConstraints.NONE;
        add(errorLabel, gbc);
        gbc.gridx++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0d;
        add(text, gbc);
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        add(detailedStackLabel, gbc);
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.weightx = 1.0d;
        gbc.weighty = 1.0d;
        gbc.fill = GridBagConstraints.BOTH;
        add(stack, gbc);
        pack();
        updateLanguage(bb.getLanguage());
    }

    @Override
    public void updateLanguage(Locale lang) {
        String k = throwable.getLocalizedMessage() == null ? "JBrickBreaker.39" : "JBrickBreaker.37"; //$NON-NLS-1$ //$NON-NLS-2$
        text.setText(MessageFormat.format(bb.getString(k),
                throwable.getLocalizedMessage()));
        setTitle(bb.getString("UnexpectedExceptionView.1")); //$NON-NLS-1$
        detailedStackLabel.setText(bb.getString("UnexpectedExceptionView.0")); //$NON-NLS-1$
        pack();
        repaint();
    }
}
TOP

Related Classes of jbrickbreaker.view.UnexpectedExceptionView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.