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