001package jmri.configurexml.swing;
002
003import java.awt.HeadlessException;
004
005import java.awt.Component;
006import java.awt.Toolkit;
007import java.awt.event.ActionEvent;
008import java.util.concurrent.atomic.AtomicBoolean;
009
010import javax.swing.BorderFactory;
011import javax.swing.BoxLayout;
012import javax.swing.JButton;
013import javax.swing.JDialog;
014import javax.swing.JLabel;
015import javax.swing.JPanel;
016
017import jmri.configurexml.ShutdownPreferences;
018
019/**
020 * Swing dialog notify that there is un-stored PanelPro data changes.
021 *
022 * @author Dave Sand Copyright (c) 2022
023 */
024public class StoreAndCompareDialog {
025
026    private static ShutdownPreferences _preferences = jmri.InstanceManager.getDefault(ShutdownPreferences.class);
027
028    static public boolean showAbortShutdownDialogPermissionDenied() {
029        AtomicBoolean result = new AtomicBoolean(false);
030        try {
031            // Provide option to invoke the store process before the shutdown.
032            final JDialog dialog = new JDialog();
033            dialog.setTitle(Bundle.getMessage("QuestionTitle"));     // NOI18N
034            dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
035            JPanel container = new JPanel();
036            container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
037            container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
038            JLabel question = new JLabel(Bundle.getMessage("StoreAndComparePermissionDenied"));  // NOI18N
039            question.setAlignmentX(Component.CENTER_ALIGNMENT);
040            container.add(question);
041
042            JButton noButton = new JButton(Bundle.getMessage("ButtonNo"));    // NOI18N
043            JButton yesButton = new JButton(Bundle.getMessage("ButtonYes"));      // NOI18N
044            JPanel button = new JPanel();
045            button.setAlignmentX(Component.CENTER_ALIGNMENT);
046            button.add(noButton);
047            button.add(yesButton);
048            container.add(button);
049
050            noButton.addActionListener((ActionEvent e) -> {
051                dialog.dispose();
052                result.set(false);
053            });
054
055            yesButton.addActionListener((ActionEvent e) -> {
056                dialog.dispose();
057                result.set(true);
058            });
059
060            container.setAlignmentX(Component.CENTER_ALIGNMENT);
061            container.setAlignmentY(Component.CENTER_ALIGNMENT);
062            dialog.getContentPane().add(container);
063            dialog.pack();
064            dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - dialog.getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - dialog.getHeight() / 2);
065            dialog.setModal(true);
066            dialog.setVisible(true);
067
068        } catch (HeadlessException ex) {
069            // silently do nothig - we can't display a dialog and shutdown continues without a store.
070        }
071        return result.get();
072    }
073
074    static public void showDialog() {
075        if (_preferences.getDisplayDialog().equals(ShutdownPreferences.DialogDisplayOptions.SkipDialog)) {
076            performStore();
077            return;
078        }
079
080        try {
081            // Provide option to invoke the store process before the shutdown.
082            final JDialog dialog = new JDialog();
083            dialog.setTitle(Bundle.getMessage("QuestionTitle"));     // NOI18N
084            dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
085            JPanel container = new JPanel();
086            container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
087            container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
088            JLabel question = new JLabel(Bundle.getMessage("StoreAndCompareRequest"));  // NOI18N
089            question.setAlignmentX(Component.CENTER_ALIGNMENT);
090            container.add(question);
091
092            JButton noButton = new JButton(Bundle.getMessage("ButtonNo"));    // NOI18N
093            JButton yesButton = new JButton(Bundle.getMessage("ButtonYes"));      // NOI18N
094            JPanel button = new JPanel();
095            button.setAlignmentX(Component.CENTER_ALIGNMENT);
096            button.add(noButton);
097            button.add(yesButton);
098            container.add(button);
099
100            noButton.addActionListener((ActionEvent e) -> {
101                dialog.dispose();
102            });
103
104            yesButton.addActionListener((ActionEvent e) -> {
105                dialog.setVisible(false);
106                performStore();
107                dialog.dispose();
108            });
109
110            container.setAlignmentX(Component.CENTER_ALIGNMENT);
111            container.setAlignmentY(Component.CENTER_ALIGNMENT);
112            dialog.getContentPane().add(container);
113            dialog.pack();
114            dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - dialog.getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - dialog.getHeight() / 2);
115            dialog.setModal(true);
116            dialog.setVisible(true);
117
118        } catch (HeadlessException ex) {
119            // silently do nothig - we can't display a dialog and shutdown continues without a store.
120        }
121    }
122
123    static private void performStore() {
124        new jmri.configurexml.StoreXmlUserAction("").actionPerformed(null);
125    }
126}