001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.dialog;
020
021import icy.main.Icy;
022import icy.system.thread.ThreadUtil;
023
024import javax.swing.JFrame;
025import javax.swing.JOptionPane;
026
027/**
028 * @author stephane
029 */
030public class MessageDialog
031{
032    public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
033    /** Used for information messages. */
034    public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
035    /** Used for warning messages. */
036    public static final int WARNING_MESSAGE = JOptionPane.WARNING_MESSAGE;
037    /** Used for questions. */
038    public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE;
039    /** No icon is used. */
040    public static final int PLAIN_MESSAGE = JOptionPane.PLAIN_MESSAGE;
041
042    public static void showDialog(final String message)
043    {
044        showDialog("Information", message, INFORMATION_MESSAGE);
045    }
046
047    public static void showDialog(final String message, final int messageType)
048    {
049        final String title;
050
051        switch (messageType)
052        {
053            case INFORMATION_MESSAGE:
054                title = "Information";
055                break;
056
057            case WARNING_MESSAGE:
058                title = "Warning";
059                break;
060
061            case ERROR_MESSAGE:
062                title = "Error";
063                break;
064
065            case QUESTION_MESSAGE:
066                title = "Confirmation";
067                break;
068
069            default:
070                title = "Message";
071                break;
072        }
073
074        showDialog(title, message, messageType);
075    }
076
077    public static void showDialog(final String title, final String message)
078    {
079        showDialog(title, message, JOptionPane.INFORMATION_MESSAGE);
080    }
081
082    public static void showDialog(final String title, final String message, final int messageType)
083    {
084        if (!Icy.getMainInterface().isHeadLess())
085        {
086            ThreadUtil.invokeLater(new Runnable()
087            {
088                @Override
089                public void run()
090                {
091                    final JFrame parent = Icy.getMainInterface().getMainFrame();
092
093                    JOptionPane.showMessageDialog(parent, message, title, messageType);
094                }
095            });
096        }
097        else
098        {
099            if (messageType == ERROR_MESSAGE)
100                System.err.println(title + ": " + message);
101            else
102                System.out.println(title + ": " + message);
103        }
104    }
105}