001package apps.gui3.tabbedpreferences;
002
003import java.awt.event.WindowEvent;
004import javax.swing.WindowConstants;
005
006import jmri.InstanceManager;
007import jmri.ShutDownManager;
008import jmri.swing.PreferencesPanel;
009import jmri.util.JmriJFrame;
010import jmri.util.swing.JmriJOptionPane;
011
012/**
013 * Support displaying {@link TabbedPreferences} in a window.
014 *<p>
015 * The {@link TabbedPreferences} object is requested from the InstanceManager, and
016 * if need-be created and initialized in the process of doing that.
017 *
018 * @author Kevin Dickerson Copyright 2010
019 * @author Bob Jacobsen Copyright 2019
020 */
021public class TabbedPreferencesFrame extends JmriJFrame {
022
023    @Override
024    public String getTitle() {
025        return getTabbedPreferences().getTitle();
026    }
027
028    public boolean isMultipleInstances() {
029        return true;
030    }
031
032    public TabbedPreferencesFrame() {
033        super();
034        add(getTabbedPreferences());
035        addHelpMenu("package.apps.TabbedPreferences", true); // NOI18N
036        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
037    }
038
039    public void gotoPreferenceItem(String item, String sub) {
040        getTabbedPreferences().gotoPreferenceItem(item, sub);
041    }
042
043    @Override
044    public void windowClosing(WindowEvent e) {
045        ShutDownManager sdm = InstanceManager.getDefault(ShutDownManager.class);
046        if (!getTabbedPreferences().isPreferencesValid() && !sdm.isShuttingDown()) {
047            for (PreferencesPanel panel : getTabbedPreferences().getPreferencesPanels().values()) {
048                if (!panel.isPreferencesValid()) {
049                    switch (JmriJOptionPane.showConfirmDialog(this,
050                            Bundle.getMessage("InvalidPreferencesMessage", panel.getTabbedPreferencesTitle()),
051                            Bundle.getMessage("InvalidPreferencesTitle"),
052                            JmriJOptionPane.YES_NO_OPTION,
053                            JmriJOptionPane.ERROR_MESSAGE)) {
054                        case JmriJOptionPane.YES_OPTION:
055                            // abort window closing and return to broken preferences
056                            getTabbedPreferences().gotoPreferenceItem(panel.getPreferencesItem(), panel.getTabbedPreferencesTitle());
057                            return;
058                        default:
059                            // do nothing
060                            break;
061                    }
062                }
063            }
064        }
065        if (getTabbedPreferences().isDirty()) {
066            var buttons = JmriJOptionPane.YES_NO_CANCEL_OPTION;
067            if (sdm.isShuttingDown()) {
068                buttons = JmriJOptionPane.YES_NO_OPTION;
069            }
070            switch (JmriJOptionPane.showConfirmDialog(this,
071                    Bundle.getMessage("UnsavedChangesMessage", getTabbedPreferences().getTitle()), // NOI18N
072                    Bundle.getMessage("UnsavedChangesTitle"), // NOI18N
073                    buttons,
074                    JmriJOptionPane.QUESTION_MESSAGE)) {
075                case JmriJOptionPane.YES_OPTION:
076                    // save preferences
077                    getTabbedPreferences().savePressed(getTabbedPreferences().invokeSaveOptions());
078                    break;
079                case JmriJOptionPane.NO_OPTION:
080                    // do nothing
081                    break;
082                case JmriJOptionPane.CANCEL_OPTION:
083                default:
084                    // abort window closing
085                    return;
086            }
087        }
088        this.setVisible(false);
089    }
090
091    /**
092     * Ensure a TabbedPreferences instance is always available.
093     *
094     * @return the default TabbedPreferences instance, creating it if needed
095     */
096    private TabbedPreferences getTabbedPreferences() {
097        return InstanceManager.getOptionalDefault(TabbedPreferences.class).orElseGet(() -> {
098            return InstanceManager.setDefault(TabbedPreferences.class, new TabbedPreferences());
099        });
100    }
101}