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 boolean showDialog() {
075        if (_preferences.getDisplayDialog().equals(ShutdownPreferences.DialogDisplayOptions.SkipDialog)) {
076            performStore();
077            return false;
078        }
079
080        AtomicBoolean cancelShutdown = new AtomicBoolean(false);
081        try {
082            // Provide option to invoke the store process before the shutdown.
083            final JDialog dialog = new JDialog();
084            dialog.setTitle(Bundle.getMessage("QuestionTitle"));     // NOI18N
085            dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
086            JPanel container = new JPanel();
087            container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
088            container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
089            JLabel question = new JLabel(Bundle.getMessage("StoreAndCompareRequest"));  // NOI18N
090            question.setAlignmentX(Component.CENTER_ALIGNMENT);
091            container.add(question);
092
093            JButton noButton = new JButton(Bundle.getMessage("ButtonNo"));    // NOI18N
094            JButton yesButton = new JButton(Bundle.getMessage("ButtonYes"));      // NOI18N
095            JButton canButton = new JButton(Bundle.getMessage("ButtonCancel"));      // NOI18N
096            JPanel button = new JPanel();
097            button.setAlignmentX(Component.CENTER_ALIGNMENT);
098            button.add(noButton);
099            button.add(yesButton);
100            button.add(canButton);
101            container.add(button);
102
103            noButton.addActionListener((ActionEvent e) -> {
104                dialog.dispose();
105            });
106
107            canButton.addActionListener((ActionEvent e) -> {
108                cancelShutdown.set(true);
109                dialog.dispose();
110            });
111
112            yesButton.addActionListener((ActionEvent e) -> {
113                dialog.setVisible(false);
114                performStore();
115                dialog.dispose();
116            });
117
118            container.setAlignmentX(Component.CENTER_ALIGNMENT);
119            container.setAlignmentY(Component.CENTER_ALIGNMENT);
120            dialog.getContentPane().add(container);
121            dialog.pack();
122            dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - dialog.getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - dialog.getHeight() / 2);
123            dialog.setModal(true);
124            dialog.setVisible(true);
125
126        } catch (HeadlessException ex) {
127            // silently do nothig - we can't display a dialog and shutdown continues without a store.
128        }
129        return cancelShutdown.get();
130    }
131
132    static private void performStore() {
133        new jmri.configurexml.StoreXmlUserAction("").actionPerformed(null);
134    }
135}