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