001package icy.gui.frame.error;
002
003import icy.gui.frame.IcyFrame;
004import icy.gui.frame.TitledFrame;
005import icy.gui.frame.progress.ProgressFrame;
006import icy.system.IcyExceptionHandler;
007
008import java.awt.BorderLayout;
009import java.awt.Dimension;
010import java.awt.event.ActionEvent;
011import java.awt.event.ActionListener;
012
013import javax.swing.Icon;
014import javax.swing.text.BadLocationException;
015
016public class ErrorReportFrame extends TitledFrame implements ActionListener
017{
018    /**
019     * This function test if we already have an active error report frame opened.
020     */
021    public static boolean hasErrorFrameOpened()
022    {
023        return !IcyFrame.getAllFrames(ErrorReportFrame.class).isEmpty();
024    }
025
026    // GUI
027    protected ErrorReportPanel panel;
028
029    // internals
030    protected ActionListener reportAction;
031
032    /**
033     * Create the frame.
034     */
035    public ErrorReportFrame(Icon icon, String title, String message)
036    {
037        super("Bug report", true, true, true, true);
038
039        panel = new ErrorReportPanel(icon, title, message);
040
041        panel.reportButton.addActionListener(this);
042        panel.closeButton.addActionListener(this);
043
044        // default report action
045        reportAction = new ActionListener()
046        {
047            @Override
048            public void actionPerformed(ActionEvent e)
049            {
050                final ProgressFrame progressFrame = new ProgressFrame("Sending report...");
051
052                try
053                {
054                    IcyExceptionHandler.report(panel.getReportMessage());
055                }
056                catch (BadLocationException ex)
057                {
058                    System.err.println("Error while reporting error :");
059                    IcyExceptionHandler.showErrorMessage(ex, true);
060                }
061                finally
062                {
063                    progressFrame.close();
064                }
065            }
066        };
067
068        mainPanel.add(panel, BorderLayout.CENTER);
069
070        addToDesktopPane();
071        setSize(new Dimension(640, 450));
072        setVisible(true);
073        requestFocus();
074        center();
075    }
076
077    /**
078     * Returns formatted report message (ready to send to web site).
079     * 
080     * @throws BadLocationException
081     */
082    public String getReportMessage() throws BadLocationException
083    {
084        return panel.getReportMessage();
085    }
086
087    /**
088     * Set a specific action on the report button
089     */
090    public void setReportAction(ActionListener action)
091    {
092        reportAction = action;
093    }
094
095    @Override
096    public void actionPerformed(ActionEvent e)
097    {
098        if ((e.getSource() == panel.reportButton) && (reportAction != null))
099            reportAction.actionPerformed(e);
100
101        close();
102    }
103}