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;
017
018/**
019 * Swing dialog notify that there is un-stored PanelPro data changes.
020 *
021 * @author Dave Sand Copyright (c) 2022
022 */
023public class StoreAndCompareDialog {
024
025    private static ShutdownPreferences _preferences = jmri.InstanceManager.getDefault(ShutdownPreferences.class);
026
027    static public void showDialog() {
028        if (_preferences.getDisplayDialog().equals(ShutdownPreferences.DialogDisplayOptions.SkipDialog)) {
029            performStore();
030            return;
031        }
032
033        try {
034            // Provide option to invoke the store process before the shutdown.
035            final JDialog dialog = new JDialog();
036            dialog.setTitle(Bundle.getMessage("QuestionTitle"));     // NOI18N
037            dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
038            JPanel container = new JPanel();
039            container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
040            container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
041            JLabel question = new JLabel(Bundle.getMessage("StoreAndCompareRequest"));  // NOI18N
042            question.setAlignmentX(Component.CENTER_ALIGNMENT);
043            container.add(question);
044
045            JButton noButton = new JButton(Bundle.getMessage("ButtonNo"));    // NOI18N
046            JButton yesButton = new JButton(Bundle.getMessage("ButtonYes"));      // NOI18N
047            JPanel button = new JPanel();
048            button.setAlignmentX(Component.CENTER_ALIGNMENT);
049            button.add(noButton);
050            button.add(yesButton);
051            container.add(button);
052
053            noButton.addActionListener((ActionEvent e) -> {
054                dialog.dispose();
055            });
056
057            yesButton.addActionListener((ActionEvent e) -> {
058                dialog.setVisible(false);
059                performStore();
060                dialog.dispose();
061            });
062
063            container.setAlignmentX(Component.CENTER_ALIGNMENT);
064            container.setAlignmentY(Component.CENTER_ALIGNMENT);
065            dialog.getContentPane().add(container);
066            dialog.pack();
067            dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width) / 2 - dialog.getWidth() / 2, (Toolkit.getDefaultToolkit().getScreenSize().height) / 2 - dialog.getHeight() / 2);
068            dialog.setModal(true);
069            dialog.setVisible(true);
070
071        } catch (HeadlessException ex) {
072            // silently do nothig - we can't display a dialog and shutdown continues without a store.
073        }
074    }
075
076    static private void performStore() {
077        new jmri.configurexml.StoreXmlUserAction("").actionPerformed(null);
078    }
079}