001package jmri.jmrit.operations.locations.schedules.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005
006import javax.swing.*;
007
008import jmri.jmrit.operations.OperationsFrame;
009import jmri.jmrit.operations.OperationsXml;
010import jmri.jmrit.operations.locations.Location;
011import jmri.jmrit.operations.locations.Track;
012import jmri.jmrit.operations.locations.schedules.ScheduleEditFrame;
013import jmri.jmrit.operations.setup.Control;
014import jmri.jmrit.operations.setup.Setup;
015import jmri.util.swing.JmriJOptionPane;
016
017/**
018 * Frame used to edit alternate track selection and percentage of loads from
019 * staging.
020 *
021 * @author Daniel Boudreau Copyright (C) 2010, 2011, 2015
022 */
023class ScheduleOptionsFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
024
025    // text field
026    JTextField factorTextField = new JTextField(5);
027
028    // combo boxes
029    JComboBox<Track> trackBox = new JComboBox<>();
030
031    // radio buttons
032    // major buttons
033    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
034
035    Track _track;
036
037    public ScheduleOptionsFrame(ScheduleEditFrame sef) {
038        super(Bundle.getMessage("MenuItemScheduleOptions"));
039
040        // the following code sets the frame's initial state
041        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
042
043        _track = sef._track;
044
045        // load the panel
046        // row 1
047        JPanel pFactor = new JPanel();
048        pFactor.setLayout(new GridBagLayout());
049        pFactor.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("ScheduleFactor")));
050        addItem(pFactor, factorTextField, 0, 0);
051
052        factorTextField.setToolTipText(Bundle.getMessage("TipScheduleFactor"));
053        factorTextField.setText(Integer.toString(_track.getReservationFactor()));
054
055        // row 2
056        JPanel pAlternate = new JPanel();
057        pAlternate.setLayout(new GridBagLayout());
058        pAlternate.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("AlternateTrack")));
059        addItem(pAlternate, trackBox, 0, 0);
060
061        updateTrackCombobox();
062
063        JPanel pControls = new JPanel();
064        pControls.add(saveButton);
065
066        // button action
067        addButtonAction(saveButton);
068
069        getContentPane().add(pFactor);
070        getContentPane().add(pAlternate);
071        getContentPane().add(pControls);
072        
073        _track.getLocation().addPropertyChangeListener(this);
074
075        addHelpMenu("package.jmri.jmrit.operations.Operations_ControllingCarsToSpur", true); // NOI18N
076
077        initMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight200));
078        
079    }
080
081    @Override
082    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
083        if (ae.getSource() == saveButton) {
084            // confirm that factor is between 0 and 1000
085            try {
086                int factor = Integer.parseInt(factorTextField.getText());
087                if (factor < 0 || factor > 1000) {
088                    JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("FactorMustBeNumber"),
089                            Bundle.getMessage("ErrorFactor"), JmriJOptionPane.ERROR_MESSAGE);
090                    return;
091                }
092            } catch (NumberFormatException e) {
093                JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("FactorMustBeNumber"),
094                        Bundle.getMessage("ErrorFactor"), JmriJOptionPane.ERROR_MESSAGE);
095                return;
096            }
097            _track.setReservationFactor(Integer.parseInt(factorTextField.getText()));
098            if (trackBox.getSelectedItem() != null && !trackBox.getSelectedItem().equals(Location.NONE)) {
099                _track.setAlternateTrack((Track) trackBox.getSelectedItem());
100            } else {
101                _track.setAlternateTrack(null);
102            }
103            OperationsXml.save();
104            if (Setup.isCloseWindowOnSaveEnabled()) {
105                dispose();
106            }
107        }
108    }
109
110    private void updateTrackCombobox() {
111        _track.getLocation().updateComboBox(trackBox);
112        trackBox.removeItem(_track); // remove this track from consideration
113        trackBox.setSelectedItem(_track.getAlternateTrack());
114    }
115
116    @Override
117    public void propertyChange(java.beans.PropertyChangeEvent e) {
118        if (Control.SHOW_PROPERTY) {
119            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), e
120                    .getNewValue());
121        }
122        if (e.getPropertyName().equals(Location.TRACK_LISTLENGTH_CHANGED_PROPERTY)) {
123            updateTrackCombobox();
124        }
125    }
126
127    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleOptionsFrame.class);
128
129}