001package apps.gui3.tabbedpreferences;
002
003import java.awt.event.WindowEvent;
004import java.awt.event.WindowListener;
005
006import javax.swing.JDialog;
007import javax.swing.SwingUtilities;
008import javax.swing.WindowConstants;
009
010import jmri.*;
011import jmri.swing.PreferencesPanel;
012import jmri.util.swing.JmriJOptionPane;
013
014/**
015 * Provide a Connection preferences dialog.
016 * <p>
017 * References the status of an {@link EditConnectionPreferences} object that
018 * is created (via new()) as part of this constructor.
019 *
020 * @author Kevin Dickerson Copyright 2010
021 */
022public final class EditConnectionPreferencesDialog extends JDialog implements WindowListener {
023
024    final EditConnectionPreferences editConnectionPreferences;
025    boolean restartProgram = false;
026
027    @Override
028    public String getTitle() {
029        return editConnectionPreferences.getTitle();
030    }
031
032    public boolean isMultipleInstances() {
033        return true;
034    }
035
036    /**
037     * Displays a dialog for editing the connections.
038     *
039     * @return true if the program should restart, false if the program should quit.
040     */
041    public static boolean showDialog() {
042        if (! InstanceManager.getDefault(PermissionManager.class)
043                .ensureAtLeastPermission(PermissionsSystemAdmin.PERMISSION_EDIT_PREFERENCES,
044                        BooleanPermission.BooleanValue.TRUE)) {
045            return false;
046        }
047        EditConnectionPreferencesDialog dialog = new EditConnectionPreferencesDialog();
048        SwingUtilities.updateComponentTreeUI(dialog);  // hack because sometimes this was created before L&F was set?
049        dialog.pack();
050        dialog.setBounds(
051                (int) ( jmri.util.JmriJFrame.getScreenDimensions().get(0).getBounds().getSize().getWidth() * 0.10 ),
052                (int) ( jmri.util.JmriJFrame.getScreenDimensions().get(0).getBounds().getSize().getHeight() * 0.10 ),
053                (int) ( jmri.util.JmriJFrame.getScreenDimensions().get(0).getBounds().getSize().getWidth() * 0.80 ),
054                (int) (jmri.util.JmriJFrame.getScreenDimensions().get(0).getBounds().getSize().getHeight()* 0.80 ));
055        dialog.setVisible(true);
056        return dialog.restartProgram;
057    }
058
059    public EditConnectionPreferencesDialog() {
060        super();
061        setModal(true);
062        editConnectionPreferences = new EditConnectionPreferences(this);
063        editConnectionPreferences.init();
064        add(editConnectionPreferences);
065//        addHelpMenu("package.apps.TabbedPreferences", true); // NOI18N
066        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
067        addWindowListener(EditConnectionPreferencesDialog.this);
068    }
069
070    public void gotoPreferenceItem(String item, String sub) {
071        editConnectionPreferences.gotoPreferenceItem(item, sub);
072    }
073
074    @Override
075    public void windowClosing(WindowEvent e) {
076        ShutDownManager sdm = InstanceManager.getDefault(ShutDownManager.class);
077        if (!editConnectionPreferences.isPreferencesValid() && !sdm.isShuttingDown()) {
078            for (PreferencesPanel panel : editConnectionPreferences.getPreferencesPanels().values()) {
079                if (!panel.isPreferencesValid()) {
080                    switch (JmriJOptionPane.showConfirmDialog(this,
081                            Bundle.getMessage("InvalidPreferencesMessage", panel.getTabbedPreferencesTitle()),
082                            Bundle.getMessage("InvalidPreferencesTitle"),
083                            JmriJOptionPane.YES_NO_OPTION,
084                            JmriJOptionPane.ERROR_MESSAGE)) {
085                        case JmriJOptionPane.YES_OPTION:
086                            // abort window closing and return to broken preferences
087                            editConnectionPreferences.gotoPreferenceItem(panel.getPreferencesItem(), panel.getTabbedPreferencesTitle());
088                            return;
089                        default:
090                            // do nothing
091                            break;
092                    }
093                }
094            }
095        }
096        if (editConnectionPreferences.isDirty()) {
097            switch (JmriJOptionPane.showConfirmDialog(this,
098                    Bundle.getMessage("UnsavedChangesMessage", editConnectionPreferences.getTitle()), // NOI18N
099                    Bundle.getMessage("UnsavedChangesTitle"), // NOI18N
100                    JmriJOptionPane.YES_NO_CANCEL_OPTION,
101                    JmriJOptionPane.QUESTION_MESSAGE)) {
102                case JmriJOptionPane.YES_OPTION:
103                    // save preferences
104                    editConnectionPreferences.savePressed(editConnectionPreferences.invokeSaveOptions());
105                    break;
106                case JmriJOptionPane.NO_OPTION:
107                    // do nothing
108                    break;
109                case JmriJOptionPane.CANCEL_OPTION:
110                default:
111                    // abort window closing
112                    return;
113            }
114        }
115        this.setVisible(false);
116    }
117
118    @Override
119    public void windowOpened(WindowEvent e) {
120    }
121
122    @Override
123    public void windowClosed(WindowEvent e) {
124    }
125
126    @Override
127    public void windowIconified(WindowEvent e) {
128    }
129
130    @Override
131    public void windowDeiconified(WindowEvent e) {
132    }
133
134    @Override
135    public void windowActivated(WindowEvent e) {
136    }
137
138    @Override
139    public void windowDeactivated(WindowEvent e) {
140    }
141}