001package jmri.jmrit.operations.trains.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005import java.io.File;
006
007import javax.swing.*;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import jmri.jmrit.operations.OperationsFrame;
013import jmri.jmrit.operations.OperationsXml;
014import jmri.jmrit.operations.setup.Control;
015import jmri.jmrit.operations.setup.Setup;
016import jmri.jmrit.operations.trains.Train;
017import jmri.jmrit.operations.trains.TrainEditFrame;
018import jmri.util.FileUtil;
019
020/**
021 * Frame for user edit of the train manifest options
022 *
023 * @author Dan Boudreau Copyright (C) 2010, 2013
024 */
025public class TrainManifestOptionFrame extends OperationsFrame {
026
027    Train _train = null;
028
029    // labels
030    JLabel textPad = new JLabel("   ");
031    JLabel logoURL = new JLabel("");
032
033    // major buttons
034    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
035    JButton addLogoButton = new JButton(Bundle.getMessage("AddLogo"));
036    JButton removeLogoButton = new JButton(Bundle.getMessage("RemoveLogo"));
037
038    // radio buttons
039    // check boxes
040    JCheckBox showTimesCheckBox = new JCheckBox(Bundle.getMessage("ShowTimes"));
041
042    // text fields
043    JTextField railroadNameTextField = new JTextField(35);
044    JTextField logoTextField = new JTextField(35);
045
046    // combo boxes
047    public TrainManifestOptionFrame() {
048        super(Bundle.getMessage("TitleOptions"));
049    }
050
051    public void initComponents(TrainEditFrame parent) {
052
053        // the following code sets the frame's initial state
054        parent.setChildFrame(this);
055        _train = parent._train;
056
057        // add tool tips
058        addLogoButton.setToolTipText(Bundle.getMessage("AddLogoToolTip"));
059        removeLogoButton.setToolTipText(Bundle.getMessage("RemoveLogoToolTip"));
060
061        // Option panel
062        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
063
064        JPanel pOptionName = new JPanel();
065        pOptionName.setLayout(new GridBagLayout());
066        JScrollPane pOptionNamePane = new JScrollPane(pOptionName);
067        pOptionNamePane.setBorder(BorderFactory.createTitledBorder(Bundle
068                .getMessage("BorderLayoutRailRoadName")));
069        addItem(pOptionName, railroadNameTextField, 0, 0);
070
071        // manifest logo
072        JPanel pOptionLogo = new JPanel();
073        pOptionLogo.setLayout(new GridBagLayout());
074        JScrollPane pOptionLogoPane = new JScrollPane(pOptionLogo);
075        pOptionLogoPane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutLogo")));
076        addItem(pOptionLogo, textPad, 2, 18);
077        addItem(pOptionLogo, addLogoButton, 2, 20);
078        addItemLeft(pOptionLogo, removeLogoButton, 0, 21);
079        addItemWidth(pOptionLogo, logoURL, 6, 1, 21);
080        updateLogoButtons();
081
082        // Checkboxes
083        JPanel pCheckboxes = new JPanel();
084        pCheckboxes.setLayout(new GridBagLayout());
085        JScrollPane pCheckboxesPane = new JScrollPane(pCheckboxes);
086        pCheckboxesPane
087                .setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutManifest")));
088        addItem(pCheckboxes, showTimesCheckBox, 0, 0);
089
090        // row 11
091        JPanel pControl = new JPanel();
092        pControl.setLayout(new GridBagLayout());
093        addItem(pControl, saveButton, 3, 9);
094
095        getContentPane().add(pOptionNamePane);
096        getContentPane().add(pOptionLogoPane);
097        getContentPane().add(pCheckboxesPane);
098        getContentPane().add(pControl);
099
100        // setup buttons
101        addButtonAction(addLogoButton);
102        addButtonAction(removeLogoButton);
103        addButtonAction(saveButton);
104
105        // load fields
106        if (_train != null) {
107            railroadNameTextField.setText(_train.getRailroadName());
108            showTimesCheckBox.setSelected(_train.isShowArrivalAndDepartureTimesEnabled());
109        }
110
111        // build menu
112        addHelpMenu("package.jmri.jmrit.operations.Operations_TrainManifestOptions", true);// NOI18N
113
114        pack();
115        setMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight250));
116        setVisible(true);
117    }
118
119    private void updateLogoButtons() {
120        if (_train != null) {
121            boolean flag = _train.getManifestLogoPathName().isEmpty();
122            addLogoButton.setVisible(flag);
123            removeLogoButton.setVisible(!flag);
124            logoURL.setText(_train.getManifestLogoPathName());
125            pack();
126        }
127    }
128
129    /**
130     * We always use the same file chooser in this class, so that the user's
131     * last-accessed directory remains available.
132     */
133    JFileChooser fc = jmri.jmrit.XmlFile.userFileChooser(Bundle.getMessage("Images"));
134
135    private File selectFile() {
136        if (fc == null) {
137            log.error("Could not find user directory");
138        } else {
139            fc.setDialogTitle(Bundle.getMessage("FindDesiredImage"));
140            // when reusing the chooser, make sure new files are included
141            fc.rescanCurrentDirectory();
142            int retVal = fc.showOpenDialog(null);
143            // handle selection or cancel
144            if (retVal == JFileChooser.APPROVE_OPTION) {
145                return fc.getSelectedFile();
146            }
147        }
148        return null;
149    }
150
151    // Save button
152    @Override
153    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
154        if (ae.getSource() == addLogoButton) {
155            log.debug("add logo button pressed");
156            File f = selectFile();
157            if (f != null && _train != null) {
158                _train.setManifestLogoPathName(FileUtil.getPortableFilename(f));
159            }
160            updateLogoButtons();
161        }
162        if (ae.getSource() == removeLogoButton) {
163            log.debug("remove logo button pressed");
164            if (_train != null) {
165                _train.setManifestLogoPathName("");
166            }
167            updateLogoButtons();
168        }
169        if (ae.getSource() == saveButton) {
170            if (_train != null) {
171                _train.setRailroadName(railroadNameTextField.getText());
172                _train.setShowArrivalAndDepartureTimes(showTimesCheckBox.isSelected());
173                _train.setModified(true);
174            }
175            OperationsXml.save();
176            if (Setup.isCloseWindowOnSaveEnabled()) {
177                dispose();
178            }
179        }
180    }
181
182    private final static Logger log = LoggerFactory.getLogger(TrainManifestOptionFrame.class);
183}