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            if (Setup.isCloseWindowOnSaveEnabled()) {
086                dispose();
087            }
088        }
089    }
090
091    @Override
092    public String getTabbedPreferencesTitle() {
093        return Bundle.getMessage("TitlePrintMoreOptions");
094    }
095
096    @Override
097    public String getPreferencesTooltip() {
098        return null;
099    }
100
101    @Override
102    public void savePreferences() {
103
104        try {
105            Setup.setTab1length(Integer.parseInt(tab1TextField.getText()));
106            Setup.setTab2length(Integer.parseInt(tab2TextField.getText()));
107            Setup.setTab3length(Integer.parseInt(tab3TextField.getText()));
108        } catch (NumberFormatException e) {
109            log.error("Tab wasn't a number");
110        }
111
112        InstanceManager.getDefault(OperationsSetupXml.class).writeOperationsFile();
113
114        // recreate all train manifests
115        InstanceManager.getDefault(TrainManager.class).setTrainsModified();
116    }
117
118    @Override
119    public boolean isDirty() {
120        try {
121            return (Setup.getTab1Length() != Integer.parseInt(tab1TextField.getText())
122                    || Setup.getTab2Length() != Integer.parseInt(tab2TextField.getText())
123                    || Setup.getTab3Length() != Integer.parseInt(tab3TextField.getText()));
124        } catch (NumberFormatException e) {
125            log.error("Tab wasn't a number");
126        }
127        return true;
128    }
129}