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