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