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.Track;
014import jmri.jmrit.operations.locations.TrackEditFrame;
015import jmri.jmrit.operations.setup.Control;
016import jmri.jmrit.operations.setup.Setup;
017
018/**
019 * Action to change the type of track. Track types are Spurs, Yards,
020 * Interchanges and Staging.
021 *
022 * @author Daniel Boudreau Copyright (C) 2010
023 */
024class ChangeTrackFrame extends OperationsFrame {
025
026    // radio buttons
027    JRadioButton spurRadioButton = new JRadioButton(Bundle.getMessage("Spur"));
028    JRadioButton yardRadioButton = new JRadioButton(Bundle.getMessage("Yard"));
029    JRadioButton interchangeRadioButton = new JRadioButton(Bundle.getMessage("Interchange"));
030    ButtonGroup group = new ButtonGroup();
031
032    // major buttons
033    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
034
035    private Track _track;
036    private TrackEditFrame _tef;
037
038    public ChangeTrackFrame(TrackEditFrame tef) {
039        super(Bundle.getMessage("MenuItemChangeTrackType"));
040
041        // the following code sets the frame's initial state
042        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
043
044        _tef = tef;
045        _track = tef._track;
046        if (_track == null) {
047            log.debug("track is null, change track not possible");
048            return;
049        }
050        String trackName = _track.getName();
051
052        // load the panel
053        // row 1a
054        JPanel p1 = new JPanel();
055        p1.setLayout(new GridBagLayout());
056        p1.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TrackType", trackName)));
057        addItem(p1, spurRadioButton, 0, 0);
058        addItem(p1, yardRadioButton, 1, 0);
059        addItem(p1, interchangeRadioButton, 2, 0);
060        
061        JPanel p2 = new JPanel();
062        p2.add(saveButton);
063
064        // group and set current track type
065        group.add(spurRadioButton);
066        group.add(yardRadioButton);
067        group.add(interchangeRadioButton);
068
069        spurRadioButton.setSelected(_track.isSpur());
070        yardRadioButton.setSelected(_track.isYard());
071        interchangeRadioButton.setSelected(_track.isInterchange());
072
073        // Can not change staging tracks!
074        saveButton.setEnabled(!_track.isStaging());
075
076        // button action
077        addButtonAction(saveButton);
078
079        getContentPane().add(p1);
080        getContentPane().add(p2);
081        
082        // add help menu to window
083        addHelpMenu("package.jmri.jmrit.operations.Operations_ChangeTrackType", true); // NOI18N
084        
085        initMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight200));
086    }
087
088    @Override
089    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
090        if (ae.getSource() == saveButton) {
091            // check to see if button has changed
092            if (spurRadioButton.isSelected() && !_track.isSpur()) {
093                changeTrack(Track.SPUR);
094            } else if (yardRadioButton.isSelected() && !_track.isYard()) {
095                changeTrack(Track.YARD);
096            } else if (interchangeRadioButton.isSelected() && !_track.isInterchange()) {
097                changeTrack(Track.INTERCHANGE);
098            }
099            if (Setup.isCloseWindowOnSaveEnabled()) {
100                dispose();
101            }
102        }
103    }
104
105    private void changeTrack(String type) {
106        log.debug("change track to {}", type);
107        _track.setTrackType(type);
108        OperationsXml.save();
109        _tef.dispose();
110        dispose();
111    }
112
113    private final static Logger log = LoggerFactory.getLogger(ChangeTrackFrame.class);
114}