001package jmri.jmrit.operations.locations.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.TrackEditFrame;
013import jmri.jmrit.operations.setup.Control;
014import jmri.jmrit.operations.setup.Setup;
015import jmri.util.swing.JmriJOptionPane;
016
017/**
018 * Frame that allows user to select alternate track and options.
019 *
020 * @author Daniel Boudreau Copyright (C) 2011, 2015
021 */
022class AlternateTrackFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
023
024    // combo boxes
025    JComboBox<Track> trackBox = new JComboBox<>();
026
027    // radio buttons
028    // major buttons
029    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
030
031    Track _track;
032
033    public AlternateTrackFrame(TrackEditFrame tef) {
034        super(Bundle.getMessage("AlternateTrack"));
035
036        // the following code sets the frame's initial state
037        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
038
039        _track = tef._track;
040
041        // load the panel
042        // row 2
043        JPanel pAlternate = new JPanel();
044        pAlternate.setLayout(new GridBagLayout());
045        pAlternate.setBorder(BorderFactory.createTitledBorder(""));
046        addItem(pAlternate, trackBox, 0, 0);
047
048        if (_track != null) {
049            updateTrackCombobox();
050            _track.getLocation().addPropertyChangeListener(this);
051        }
052
053        JPanel pControls = new JPanel();
054        pControls.add(saveButton);
055        saveButton.setEnabled(_track != null);
056
057        // button action
058        addButtonAction(saveButton);
059
060        getContentPane().add(pAlternate);
061        getContentPane().add(pControls);
062        
063        addHelpMenu("package.jmri.jmrit.operations.Operations_AlternateTrack", true); // NOI18N
064
065        initMinimumSize(new Dimension(Control.panelWidth300, Control.panelHeight200));
066    }
067
068    private void updateTrackCombobox() {
069        _track.getLocation().updateComboBox(trackBox);
070        trackBox.removeItem(_track); // remove this track from consideration
071        trackBox.setSelectedItem(_track.getAlternateTrack());
072    }
073
074    @Override
075    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
076        if (ae.getSource() == saveButton) {
077            // warn user that planned pickups and alternate track don't work
078            // together
079            if (trackBox.getSelectedItem() != null && _track.getIgnoreUsedLengthPercentage() > Track.IGNORE_0) {
080                JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("PPWarningAlternate"),
081                        Bundle.getMessage("PPWarningConfiguration"),
082                        JmriJOptionPane.ERROR_MESSAGE);
083            }
084            _track.setAlternateTrack((Track) trackBox.getSelectedItem());
085            OperationsXml.save();
086            if (Setup.isCloseWindowOnSaveEnabled()) {
087                dispose();
088            }
089        }
090    }
091
092    @Override
093    public void propertyChange(java.beans.PropertyChangeEvent e) {
094        if (Control.SHOW_PROPERTY) {
095            log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), e
096                    .getNewValue());
097        }
098        if (e.getPropertyName().equals(Location.TRACK_LISTLENGTH_CHANGED_PROPERTY)) {
099            updateTrackCombobox();
100        }
101    }
102
103    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AlternateTrackFrame.class);
104}