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 * Simple confirmation dialog.
029 * 
030 * @author stephane
031 */
032public class ConfirmDialog
033{
034    private static class Confirmer implements Runnable
035    {
036        private final String title;
037        private final String message;
038        private final int optionType;
039
040        int intResult;
041        boolean result;
042
043        /**
044         * @param title
045         * @param message
046         * @param optionType
047         */
048        public Confirmer(String title, String message, int optionType)
049        {
050            super();
051
052            this.title = title;
053            this.message = message;
054            this.optionType = optionType;
055        }
056
057        @Override
058        public void run()
059        {
060            // always confirm in headless mode
061            if (Icy.getMainInterface().isHeadLess())
062                result = true;
063            else
064            {
065                final JFrame parent = Icy.getMainInterface().getMainFrame();
066                intResult = JOptionPane.showConfirmDialog(parent, message, title, optionType,
067                        JOptionPane.QUESTION_MESSAGE);
068                result = getBooleanReturnValue(intResult);
069            }
070        }
071    }
072
073    public static final int DEFAULT_OPTION = JOptionPane.YES_NO_CANCEL_OPTION;
074    /** Type used for <code>showConfirmDialog</code>. */
075    public static final int YES_NO_OPTION = JOptionPane.YES_NO_OPTION;
076    /** Type used for <code>showConfirmDialog</code>. */
077    public static final int YES_NO_CANCEL_OPTION = JOptionPane.YES_NO_CANCEL_OPTION;
078    /** Type used for <code>showConfirmDialog</code>. */
079    public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
080
081    public static boolean getBooleanReturnValue(final int returnValue)
082    {
083        return (returnValue == JOptionPane.YES_OPTION) || (returnValue == JOptionPane.OK_OPTION);
084    }
085
086    public static int confirmEx(final String title, final String message, final int optionType)
087    {
088        final Confirmer confirmer = new Confirmer(title, message, optionType);
089
090        ThreadUtil.invokeNow(confirmer);
091
092        return confirmer.intResult;
093    }
094
095    public static boolean confirm(final String title, final String message, final int optionType)
096    {
097        final Confirmer confirmer = new Confirmer(title, message, optionType);
098
099        ThreadUtil.invokeNow(confirmer);
100
101        return confirmer.result;
102    }
103
104    public static boolean confirm(final String message)
105    {
106        return confirm("Confirmation", message, YES_NO_OPTION);
107    }
108
109    public static boolean confirm(final String title, final String message)
110    {
111        return confirm(title, message, YES_NO_OPTION);
112    }
113}