001package jmri.jmrit.operations.setup.gui;
002
003import java.awt.GridBagLayout;
004import java.awt.event.ActionEvent;
005
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.setup.*;
010import jmri.util.swing.JmriJOptionPane;
011
012/**
013 * Frame for user edit of the build report options
014 *
015 * @author Dan Boudreau Copyright (C) 2008, 2010, 2011, 2012, 2013
016 * 
017 */
018public class BuildReportOptionPanel extends OperationsPreferencesPanel {
019
020    // major buttons
021    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
022
023    // radio buttons
024    JRadioButton buildReportMin = new JRadioButton(Bundle.getMessage("Minimal"));
025    JRadioButton buildReportNor = new JRadioButton(Bundle.getMessage("Normal"));
026    JRadioButton buildReportMax = new JRadioButton(Bundle.getMessage("Detailed"));
027    JRadioButton buildReportVD = new JRadioButton(Bundle.getMessage("VeryDetailed"));
028
029    JRadioButton buildReportRouterNor = new JRadioButton(Bundle.getMessage("Normal"));
030    JRadioButton buildReportRouterMax = new JRadioButton(Bundle.getMessage("Detailed"));
031    JRadioButton buildReportRouterVD = new JRadioButton(Bundle.getMessage("VeryDetailed"));
032
033    // check boxes
034    JCheckBox buildReportCheckBox = new JCheckBox(Bundle.getMessage("BuildReportEdit"));
035    JCheckBox buildReportIndentCheckBox = new JCheckBox(Bundle.getMessage("BuildReportIndent"));
036    JCheckBox buildReportAlwaysPreviewCheckBox = new JCheckBox(Bundle.getMessage("BuildReportAlwaysPreview"));
037
038    // combo boxes
039    JComboBox<Integer> fontSizeComboBox = new JComboBox<>();
040
041    public BuildReportOptionPanel() {
042
043        // the following code sets the frame's initial state
044        // add tool tips
045        saveButton.setToolTipText(Bundle.getMessage("SaveToolTip"));
046        buildReportCheckBox.setToolTipText(Bundle.getMessage("CreatesTextFileTip"));
047
048        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
049
050        // build report
051        JPanel pReport = new JPanel();
052        pReport.setLayout(new GridBagLayout());
053        pReport.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutReportOptions")));
054
055        // build report options
056        addItemWidth(pReport, buildReportCheckBox, 3, 1, 1);
057        addItemWidth(pReport, buildReportIndentCheckBox, 3, 1, 2);
058        addItemWidth(pReport, buildReportAlwaysPreviewCheckBox, 3, 1, 3);
059
060        JPanel pFontSize = new JPanel();
061        pFontSize.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutFontSize")));
062        pFontSize.add(fontSizeComboBox);
063
064        JPanel pLevel = new JPanel();
065        pLevel.setLayout(new GridBagLayout());
066        pLevel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BuildReport")));
067
068        // build report level radio buttons
069        addItemLeft(pLevel, buildReportMin, 1, 0);
070        addItemLeft(pLevel, buildReportNor, 2, 0);
071        addItemLeft(pLevel, buildReportMax, 3, 0);
072        addItemLeft(pLevel, buildReportVD, 4, 0);
073
074        JPanel pRouterLevel = new JPanel();
075        pRouterLevel.setLayout(new GridBagLayout());
076        pRouterLevel.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BuildReportRouter")));
077
078        // build report level radio buttons
079        addItemLeft(pRouterLevel, buildReportRouterNor, 2, 0);
080        addItemLeft(pRouterLevel, buildReportRouterMax, 3, 0);
081        addItemLeft(pRouterLevel, buildReportRouterVD, 4, 0);
082
083        // controls
084        JPanel pControl = new JPanel();
085        pControl.setBorder(BorderFactory.createTitledBorder(""));
086        pControl.setLayout(new GridBagLayout());
087        addItem(pControl, saveButton, 0, 0);
088
089        add(pReport);
090        add(pLevel);
091        add(pRouterLevel);
092        add(pFontSize);
093        add(pControl);
094
095        buildReportCheckBox.setSelected(Setup.isBuildReportEditorEnabled());
096        buildReportIndentCheckBox.setSelected(Setup.isBuildReportIndentEnabled());
097        buildReportIndentCheckBox.setEnabled(buildReportCheckBox.isSelected());
098        buildReportAlwaysPreviewCheckBox.setSelected(Setup.isBuildReportAlwaysPreviewEnabled());
099
100        ButtonGroup buildReportGroup = new ButtonGroup();
101        buildReportGroup.add(buildReportMin);
102        buildReportGroup.add(buildReportNor);
103        buildReportGroup.add(buildReportMax);
104        buildReportGroup.add(buildReportVD);
105
106        ButtonGroup buildReportRouterGroup = new ButtonGroup();
107        buildReportRouterGroup.add(buildReportRouterNor);
108        buildReportRouterGroup.add(buildReportRouterMax);
109        buildReportRouterGroup.add(buildReportRouterVD);
110
111        setBuildReportRadioButton();
112        setBuildReportRouterRadioButton();
113
114        loadFontSizeComboBox(fontSizeComboBox);
115        fontSizeComboBox.setSelectedItem(Setup.getBuildReportFontSize());
116
117        addButtonAction(saveButton);
118        addCheckBoxAction(buildReportCheckBox);
119
120        addRadioButtonAction(buildReportMin);
121        addRadioButtonAction(buildReportNor);
122        addRadioButtonAction(buildReportMax);
123        addRadioButtonAction(buildReportVD);
124    }
125
126    // Save button
127    @Override
128    public void buttonActionPerformed(ActionEvent ae) {
129        if (ae.getSource() == saveButton) {
130            this.savePreferences();
131            var topLevelAncestor = getTopLevelAncestor();
132            if (Setup.isCloseWindowOnSaveEnabled() && topLevelAncestor instanceof BuildReportOptionFrame) {
133                ((BuildReportOptionFrame) topLevelAncestor).dispose();
134            }
135        }
136    }
137
138    @Override
139    protected void checkBoxActionPerformed(ActionEvent ae) {
140        buildReportIndentCheckBox.setEnabled(buildReportCheckBox.isSelected());
141        // use the smallest font to create the longest lines in the build report
142        if (buildReportCheckBox.isSelected()) {
143            fontSizeComboBox.setSelectedItem(7);
144        }
145    }
146
147    @Override
148    protected void radioButtonActionPerformed(ActionEvent ae) {
149        setBuildReportRouterRadioButton(); // enable detailed and very detailed if needed
150    }
151
152    private void setBuildReportRadioButton() {
153        buildReportMin.setSelected(Setup.getBuildReportLevel().equals(Setup.BUILD_REPORT_MINIMAL));
154        buildReportNor.setSelected(Setup.getBuildReportLevel().equals(Setup.BUILD_REPORT_NORMAL));
155        buildReportMax.setSelected(Setup.getBuildReportLevel().equals(Setup.BUILD_REPORT_DETAILED));
156        buildReportVD.setSelected(Setup.getBuildReportLevel().equals(Setup.BUILD_REPORT_VERY_DETAILED));
157    }
158
159    private void setBuildReportRouterRadioButton() {
160        // Enabled for the router only if the build report is very detailed
161        buildReportRouterNor.setEnabled(buildReportVD.isSelected());
162        buildReportRouterMax.setEnabled(buildReportVD.isSelected());
163        buildReportRouterVD.setEnabled(buildReportVD.isSelected());
164
165        buildReportRouterMax.setSelected(Setup.getRouterBuildReportLevel()
166                .equals(Setup.BUILD_REPORT_DETAILED));
167        buildReportRouterVD.setSelected(Setup.getRouterBuildReportLevel().equals(
168                Setup.BUILD_REPORT_VERY_DETAILED));
169        buildReportRouterNor.setSelected(Setup.getRouterBuildReportLevel().equals(Setup.BUILD_REPORT_NORMAL)
170                || !buildReportVD.isSelected());
171    }
172
173    @Override
174    public String getTabbedPreferencesTitle() {
175        return Bundle.getMessage("TitleBuildReportOptions");
176    }
177
178    @Override
179    public String getPreferencesTooltip() {
180        return null;
181    }
182
183    @Override
184    public void savePreferences() {
185        // font size
186        Setup.setBuildReportFontSize((Integer) fontSizeComboBox.getSelectedItem());
187
188        // build report level
189        if (buildReportMin.isSelected()) {
190            Setup.setBuildReportLevel(Setup.BUILD_REPORT_MINIMAL);
191        } else if (buildReportNor.isSelected()) {
192            Setup.setBuildReportLevel(Setup.BUILD_REPORT_NORMAL);
193        } else if (buildReportMax.isSelected()) {
194            Setup.setBuildReportLevel(Setup.BUILD_REPORT_DETAILED);
195        } else if (buildReportVD.isSelected()) {
196            Setup.setBuildReportLevel(Setup.BUILD_REPORT_VERY_DETAILED);
197        }
198
199        // router build report level
200        String oldReportLevel = Setup.getRouterBuildReportLevel();
201        if (buildReportRouterNor.isSelected()) {
202            Setup.setRouterBuildReportLevel(Setup.BUILD_REPORT_NORMAL);
203        } else if (buildReportRouterMax.isSelected()) {
204            Setup.setRouterBuildReportLevel(Setup.BUILD_REPORT_DETAILED);
205        } else if (buildReportRouterVD.isSelected()) {
206            Setup.setRouterBuildReportLevel(Setup.BUILD_REPORT_VERY_DETAILED);
207        }
208
209        if (!oldReportLevel.equals(Setup.getRouterBuildReportLevel())) {
210            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("buildReportRouter"), Bundle
211                    .getMessage("buildReportRouterTitle"), JmriJOptionPane.INFORMATION_MESSAGE);
212        }
213
214        Setup.setBuildReportEditorEnabled(buildReportCheckBox.isSelected());
215        Setup.setBuildReportIndentEnabled(buildReportIndentCheckBox.isSelected());
216        Setup.setBuildReportAlwaysPreviewEnabled(buildReportAlwaysPreviewCheckBox.isSelected());
217
218        InstanceManager.getDefault(OperationsSetupXml.class).writeOperationsFile();
219    }
220
221    @Override
222    public boolean isDirty() {
223        String reportLevel = Setup.getBuildReportLevel();
224        if (buildReportMin.isSelected()) {
225            reportLevel = Setup.BUILD_REPORT_MINIMAL;
226        } else if (buildReportNor.isSelected()) {
227            reportLevel = Setup.BUILD_REPORT_NORMAL;
228        } else if (buildReportMax.isSelected()) {
229            reportLevel = Setup.BUILD_REPORT_DETAILED;
230        } else if (buildReportVD.isSelected()) {
231            reportLevel = Setup.BUILD_REPORT_VERY_DETAILED;
232        }
233
234        String routerReportLevel = Setup.getRouterBuildReportLevel();
235        if (buildReportRouterNor.isSelected()) {
236            routerReportLevel = Setup.BUILD_REPORT_NORMAL;
237        } else if (buildReportRouterMax.isSelected()) {
238            routerReportLevel = Setup.BUILD_REPORT_DETAILED;
239        } else if (buildReportRouterVD.isSelected()) {
240            routerReportLevel = Setup.BUILD_REPORT_VERY_DETAILED;
241        }
242
243        return (Setup.getBuildReportFontSize() != (Integer) fontSizeComboBox.getSelectedItem()
244                || !reportLevel.equals(Setup.getBuildReportLevel())
245                || !routerReportLevel.equals(Setup.getRouterBuildReportLevel())
246                || Setup.isBuildReportEditorEnabled() != buildReportCheckBox.isSelected()
247                || Setup.isBuildReportIndentEnabled() != buildReportIndentCheckBox.isSelected()
248                || Setup.isBuildReportAlwaysPreviewEnabled() != buildReportAlwaysPreviewCheckBox.isSelected());
249    }
250}