001package jmri.jmrit.operations.setup;
002
003import java.awt.GridBagLayout;
004
005import javax.swing.*;
006
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010import jmri.InstanceManager;
011import jmri.jmrit.operations.trains.TrainManager;
012
013/**
014 * Frame for user edit of additional manifest print options
015 *
016 * @author Dan Boudreau Copyright (C) 2012
017 * 
018 */
019public class PrintMoreOptionPanel extends OperationsPreferencesPanel {
020
021    private static final Logger log = LoggerFactory.getLogger(PrintMoreOptionPanel.class);
022
023    // labels
024    // major buttons
025    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
026
027    // radio buttons
028    // check boxes
029    // text field
030    JTextField tab1TextField = new JTextField(2);
031    JTextField tab2TextField = new JTextField(2);
032    JTextField tab3TextField = new JTextField(2);
033
034    // text area
035    // combo boxes
036    public PrintMoreOptionPanel() {
037
038        // the following code sets the frame's initial state
039        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
040
041        // row 1 font type and size
042        JPanel p1 = new JPanel();
043        p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
044
045        JPanel pTab = new JPanel();
046        pTab.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutTab1")));
047        pTab.add(tab1TextField);
048        p1.add(pTab);
049
050        JPanel pTab2 = new JPanel();
051        pTab2.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutTab2")));
052        pTab2.add(tab2TextField);
053        p1.add(pTab2);
054
055        JPanel pTab3 = new JPanel();
056        pTab3.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutTab3")));
057        pTab3.add(tab3TextField);
058        p1.add(pTab3);
059
060        tab1TextField.setText(Integer.toString(Setup.getTab1Length()));
061        tab2TextField.setText(Integer.toString(Setup.getTab2Length()));
062        tab3TextField.setText(Integer.toString(Setup.getTab3Length()));
063
064        // add tool tips
065        saveButton.setToolTipText(Bundle.getMessage("SaveToolTip"));
066
067        // row 11
068        JPanel pControl = new JPanel();
069        pControl.setBorder(BorderFactory.createTitledBorder(""));
070        pControl.setLayout(new GridBagLayout());
071        addItem(pControl, saveButton, 0, 0);
072
073        add(p1);
074        add(pControl);
075
076        // setup buttons
077        addButtonAction(saveButton);
078    }
079
080    // Save buttons
081    @Override
082    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
083        if (ae.getSource() == saveButton) {
084            this.savePreferences();
085            var topLevelAncestor = getTopLevelAncestor();
086            if (Setup.isCloseWindowOnSaveEnabled() && topLevelAncestor instanceof PrintMoreOptionFrame) {
087                ((PrintMoreOptionFrame) topLevelAncestor).dispose();
088            }
089        }
090    }
091
092    @Override
093    public String getTabbedPreferencesTitle() {
094        return Bundle.getMessage("TitlePrintMoreOptions");
095    }
096
097    @Override
098    public String getPreferencesTooltip() {
099        return null;
100    }
101
102    @Override
103    public void savePreferences() {
104
105        try {
106            Setup.setTab1length(Integer.parseInt(tab1TextField.getText()));
107            Setup.setTab2length(Integer.parseInt(tab2TextField.getText()));
108            Setup.setTab3length(Integer.parseInt(tab3TextField.getText()));
109        } catch (NumberFormatException e) {
110            log.error("Tab wasn't a number");
111        }
112
113        InstanceManager.getDefault(OperationsSetupXml.class).writeOperationsFile();
114
115        // recreate all train manifests
116        InstanceManager.getDefault(TrainManager.class).setTrainsModified();
117    }
118
119    @Override
120    public boolean isDirty() {
121        try {
122            return (Setup.getTab1Length() != Integer.parseInt(tab1TextField.getText())
123                    || Setup.getTab2Length() != Integer.parseInt(tab2TextField.getText())
124                    || Setup.getTab3Length() != Integer.parseInt(tab3TextField.getText()));
125        } catch (NumberFormatException e) {
126            log.error("Tab wasn't a number");
127        }
128        return true;
129    }
130}