001package jmri.jmrit.symbolicprog;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ItemEvent;
005import java.util.ResourceBundle;
006import javax.annotation.CheckForNull;
007import javax.swing.Box;
008import javax.swing.BoxLayout;
009import javax.swing.JCheckBox;
010import javax.swing.JComboBox;
011import javax.swing.JComponent;
012import javax.swing.JLabel;
013import javax.swing.JPanel;
014import jmri.InstanceManager;
015import jmri.jmrit.symbolicprog.tabbedframe.PaneProgFrame;
016import jmri.profile.ProfileManager;
017import jmri.swing.PreferencesPanel;
018import jmri.util.swing.JComboBoxUtil;
019import org.openide.util.lookup.ServiceProvider;
020
021/**
022 * Provide GUI to configure symbolic programmer defaults.
023 *
024 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2017
025 */
026@ServiceProvider(service = PreferencesPanel.class)
027public class ProgrammerConfigPane extends JPanel implements PreferencesPanel {
028
029    private final ResourceBundle apb = ResourceBundle.getBundle("apps.AppsConfigBundle");
030
031    public ProgrammerConfigPane() {
032        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
033        JPanel p = new JPanel();
034        p.setLayout(new java.awt.FlowLayout());
035        p.add(new JLabel("Format:"));
036        p.add(programmerBox = new JComboBox<>(ProgDefault.findListOfProgFiles()));
037        JComboBoxUtil.setupComboBoxMaxRows(programmerBox);
038        programmerBox.setSelectedItem(ProgDefault.getDefaultProgFile());
039        programmerBox.addActionListener((ActionEvent e) -> {
040            InstanceManager.getDefault(ProgrammerConfigManager.class).setDefaultFile(programmerBox.getSelectedItem().toString());
041        });
042        add(p);
043
044        // also create the advanced panel
045        advancedPanel = new JPanel();
046        advancedPanel.setLayout(new BoxLayout(advancedPanel, BoxLayout.Y_AXIS));
047
048        advancedPanel.add(showEmptyTabs = new JCheckBox(this.apb.getString("ProgShowEmptyTabs")));
049        showEmptyTabs.setSelected(PaneProgFrame.getShowEmptyPanes());
050        showEmptyTabs.addItemListener((ItemEvent e) -> {
051            InstanceManager.getDefault(ProgrammerConfigManager.class).setShowEmptyPanes(showEmptyTabs.isSelected());
052        });
053
054        advancedPanel.add(showCvNums = new JCheckBox(this.apb.getString("ProgShowCVInTips")));
055        showCvNums.setSelected(PaneProgFrame.getShowCvNumbers());
056        showCvNums.addItemListener((ItemEvent e) -> {
057            InstanceManager.getDefault(ProgrammerConfigManager.class).setShowCvNumbers(showCvNums.isSelected());
058        });
059
060        advancedPanel.add(canCacheDefault = new JCheckBox(this.apb.getString("ProgCanCacheDefault")));
061        canCacheDefault.setSelected(PaneProgFrame.getCanCacheDefault());
062        canCacheDefault.addItemListener((ItemEvent e) -> {
063            InstanceManager.getDefault(ProgrammerConfigManager.class).setCanCacheDefault(canCacheDefault.isSelected());
064        });
065
066        advancedPanel.add(doConfirmRead = new JCheckBox(this.apb.getString("ProgDoConfirmRead")));
067        doConfirmRead.setSelected(PaneProgFrame.getDoConfirmRead());
068        doConfirmRead.addItemListener((ItemEvent e) -> {
069            InstanceManager.getDefault(ProgrammerConfigManager.class).setDoConfirmRead(doConfirmRead.isSelected());
070        });
071
072        this.add(advancedPanel);
073        this.add(Box.createVerticalGlue());
074    }
075    JComboBox<String> programmerBox;
076
077    @CheckForNull
078    public String getSelectedItem() {
079        return (String) programmerBox.getSelectedItem();
080    }
081
082    public JPanel getAdvancedPanel() {
083        return advancedPanel;
084    }
085
086    JPanel advancedPanel;
087    JCheckBox showEmptyTabs;
088    JCheckBox showCvNums;
089    JCheckBox canCacheDefault;
090    JCheckBox doConfirmRead;
091
092    public boolean getShowEmptyTabs() {
093        return showEmptyTabs.isSelected();
094    }
095
096    public boolean getShowCvNums() {
097        return showCvNums.isSelected();
098    }
099
100    public boolean getCanCacheDefault() {
101        return canCacheDefault.isSelected();
102    }
103
104    public boolean getDoConfirmRead() {
105        return doConfirmRead.isSelected();
106    }
107
108    @Override
109    public String getPreferencesItem() {
110        return "ROSTER"; // NOI18N
111    }
112
113    @Override
114    public String getPreferencesItemText() {
115        return this.apb.getString("MenuRoster"); // NOI18N
116    }
117
118    @Override
119    public String getTabbedPreferencesTitle() {
120        return this.apb.getString("TabbedLayoutProgrammer"); // NOI18N
121    }
122
123    @Override
124    public String getLabelKey() {
125        return this.apb.getString("LabelTabbedLayoutProgrammer"); // NOI18N
126    }
127
128    @Override
129    public JComponent getPreferencesComponent() {
130        return this;
131    }
132
133    @Override
134    public boolean isPersistant() {
135        return true;
136    }
137
138    @Override
139    public String getPreferencesTooltip() {
140        return null;
141    }
142
143    @Override
144    public void savePreferences() {
145        InstanceManager.getDefault(ProgrammerConfigManager.class).savePreferences(ProfileManager.getDefault().getActiveProfile());
146    }
147
148    @Override
149    public boolean isDirty() {
150        String programmer = this.getSelectedItem();
151        return (this.getShowEmptyTabs() != PaneProgFrame.getShowEmptyPanes()
152                || this.getShowCvNums() != PaneProgFrame.getShowCvNumbers()
153                || this.getCanCacheDefault() != PaneProgFrame.getCanCacheDefault()
154                || this.getDoConfirmRead() != PaneProgFrame.getDoConfirmRead()
155                || ((programmer != null)
156                        ? !programmer.equals(ProgDefault.getDefaultProgFile())
157                        : ProgDefault.getDefaultProgFile() != null));
158    }
159
160    @Override
161    public boolean isRestartRequired() {
162        return this.isDirty();
163    }
164
165    @Override
166    public boolean isPreferencesValid() {
167        return true; // no validity checking performed
168    }
169}