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