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.gui.util.GuiUtil;
022import icy.main.Icy;
023import icy.preferences.GeneralPreferences;
024import icy.system.thread.ThreadUtil;
025import icy.util.StringUtil;
026
027import java.awt.BorderLayout;
028
029import javax.swing.Box;
030import javax.swing.JCheckBox;
031import javax.swing.JDialog;
032import javax.swing.JFrame;
033import javax.swing.JOptionPane;
034
035/**
036 * Confirmation dialog with a "do not display again" bottom checkbox.<br>
037 * The id is used to store the "do not display again" state.<br>
038 * If you call confirm(...) with "do not display again" already set to true then confirm dialog is
039 * not displayed.
040 * 
041 * @author Stephane
042 */
043public class IdConfirmDialog
044{
045    /**
046     * Keep it public in case we want custom IdConfirmDialog :)
047     */
048    public static class Confirmer implements Runnable
049    {
050        private final String title;
051        private final String message;
052        private final int optionType;
053        private final String id;
054
055        boolean result;
056        JCheckBox doNotDisplayCheckbox;
057
058        /**
059         * @param title
060         * @param message
061         * @param optionType
062         */
063        public Confirmer(String title, String message, int optionType, String id)
064        {
065            super();
066
067            this.id = id;
068            this.title = title;
069            this.message = message;
070            this.optionType = optionType;
071        }
072
073        @Override
074        public void run()
075        {
076            if (!StringUtil.isEmpty(id))
077            {
078                // Confirm dialog should not be displayed ?
079                if (!GeneralPreferences.getPreferencesConfirms().getBoolean(id, true))
080                {
081                    // confirmed and exit
082                    result = true;
083                    return;
084                }
085
086                // display checkbox
087                doNotDisplayCheckbox = new JCheckBox("Do not show this message again", false);
088            }
089            else
090                doNotDisplayCheckbox = null;
091
092            final JFrame parent = Icy.getMainInterface().getMainFrame();
093            final JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE, optionType, null, null,
094                    null);
095
096            pane.setInitialValue(null);
097            if (parent != null)
098                pane.setComponentOrientation(parent.getComponentOrientation());
099
100            final JDialog dialog = pane.createDialog(parent, title);
101
102            pane.selectInitialValue();
103            if (doNotDisplayCheckbox != null)
104            {
105                dialog.getContentPane().add(
106                        GuiUtil.createLineBoxPanel(doNotDisplayCheckbox, Box.createHorizontalGlue()),
107                        BorderLayout.SOUTH);
108                dialog.pack();
109            }
110            dialog.setVisible(true);
111            dialog.dispose();
112
113            final Object selectedValue = pane.getValue();
114
115            // save checkbox state
116            if ((doNotDisplayCheckbox != null) && doNotDisplayCheckbox.isSelected())
117                GeneralPreferences.getPreferencesConfirms().putBoolean(id, false);
118
119            if (selectedValue == null)
120                result = false;
121            else
122            {
123                if (selectedValue instanceof Integer)
124                    result = getBooleanReturnValue(((Integer) selectedValue).intValue());
125                else
126                    result = false;
127            }
128        }
129
130        public boolean getResult()
131        {
132            return result;
133        }
134    }
135
136    public static final int DEFAULT_OPTION = JOptionPane.YES_NO_CANCEL_OPTION;
137    /** Type used for <code>showConfirmDialog</code>. */
138    public static final int YES_NO_OPTION = JOptionPane.YES_NO_OPTION;
139    /** Type used for <code>showConfirmDialog</code>. */
140    public static final int YES_NO_CANCEL_OPTION = JOptionPane.YES_NO_CANCEL_OPTION;
141    /** Type used for <code>showConfirmDialog</code>. */
142    public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
143
144    public static boolean getBooleanReturnValue(final int returnValue)
145    {
146        return (returnValue == JOptionPane.YES_OPTION) || (returnValue == JOptionPane.OK_OPTION);
147    }
148
149    public static boolean confirm(String title, String message, int optionType, String id)
150    {
151        final Confirmer confirmer = new Confirmer(title, message, optionType, id);
152
153        // always confirm in headless mode
154        if (Icy.getMainInterface().isHeadLess())
155            return true;
156
157        ThreadUtil.invokeNow(confirmer);
158
159        return confirmer.result;
160    }
161
162    public static boolean confirm(final String title, final String message, String id)
163    {
164        return confirm(title, message, OK_CANCEL_OPTION, id);
165    }
166
167    public static boolean confirm(final String message, String id)
168    {
169        return confirm("Confirmation", message, OK_CANCEL_OPTION, id);
170    }
171}