001package jmri.configurexml.swing;
002
003import java.awt.HeadlessException;
004
005import java.awt.Component;
006import java.awt.Toolkit;
007import java.awt.event.ActionEvent;
008
009import javax.swing.BorderFactory;
010import javax.swing.BoxLayout;
011import javax.swing.JButton;
012import javax.swing.JDialog;
013import javax.swing.JLabel;
014import javax.swing.JPanel;
015
016import jmri.configurexml.ShutdownPreferences;
017import jmri.configurexml.ShutdownPreferences.DialogDisplayOptions;
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 void showDialog() {
029        if (_preferences.getDisplayDialog().equals(ShutdownPreferences.DialogDisplayOptions.SkipDialog)) {
030            performStore();
031            return;
032        }
033
034        try {
035            // Provide option to invoke the store process before the shutdown.
036            final JDialog dialog = new JDialog();
037            dialog.setTitle(Bundle.getMessage("QuestionTitle"));     // NOI18N
038            dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
039            JPanel container = new JPanel();
040            container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
041            container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
042            JLabel question = new JLabel(Bundle.getMessage("StoreAndCompareRequest"));  // NOI18N
043            question.setAlignmentX(Component.CENTER_ALIGNMENT);
044            container.add(question);
045
046            JButton noButton = new JButton(Bundle.getMessage("ButtonNo"));    // NOI18N
047            JButton yesButton = new JButton(Bundle.getMessage("ButtonYes"));      // NOI18N
048            JPanel button = new JPanel();
049            button.setAlignmentX(Component.CENTER_ALIGNMENT);
050            button.add(noButton);
051            button.add(yesButton);
052            container.add(button);
053
054            noButton.addActionListener((ActionEvent e) -> {
055                dialog.dispose();
056            });
057
058            yesButton.addActionListener((ActionEvent e) -> {
059                dialog.setVisible(false);
060                performStore();
061                dialog.dispose();
062            });
063
064            container.setAlignmentX(Component.CENTER_ALIGNMENT);
065            container.setAlignmentY(Component.CENTER_ALIGNMENT);
066            dialog.getContentPane().add(container);
067            dialog.pack();
068            dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - dialog.getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - dialog.getHeight() / 2);
069            dialog.setModal(true);
070            dialog.setVisible(true);
071
072        } catch (HeadlessException ex) {
073            // silently do nothig - we can't display a dialog and shutdown continues without a store.
074        }
075    }
076
077    static private void performStore() {
078        new jmri.configurexml.StoreXmlUserAction("").actionPerformed(null);
079    }
080}