001package jmri.jmrit.operations.locations.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005import java.text.MessageFormat;
006
007import javax.swing.*;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import jmri.jmrit.operations.OperationsFrame;
013import jmri.jmrit.operations.OperationsXml;
014import jmri.jmrit.operations.locations.Location;
015import jmri.jmrit.operations.locations.LocationEditFrame;
016import jmri.jmrit.operations.locations.Track;
017import jmri.jmrit.operations.setup.Control;
018
019/**
020 * Action to change all of tracks at a location to the same type of track. Track
021 * types are Spurs, Yards, Interchanges and Staging.
022 *
023 * @author Daniel Boudreau Copyright (C) 2011
024 * 
025 */
026class ChangeTracksFrame extends OperationsFrame {
027
028    // radio buttons
029    JRadioButton spurRadioButton = new JRadioButton(Bundle.getMessage("Spur"));
030    JRadioButton yardRadioButton = new JRadioButton(Bundle.getMessage("Yard"));
031    JRadioButton interchangeRadioButton = new JRadioButton(Bundle.getMessage("Interchange"));
032    JRadioButton stagingRadioButton = new JRadioButton(Bundle.getMessage("Staging"));
033
034    // major buttons
035    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
036
037    private LocationEditFrame _lef;
038    private Location _location;
039
040    public ChangeTracksFrame(LocationEditFrame lef) {
041        super(Bundle.getMessage("MenuItemChangeTrackType"));
042
043        // the following code sets the frame's initial state
044        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
045
046        _lef = lef;
047        if (_lef._location == null) {
048            log.debug("location is null, change location track types not possible");
049            return;
050        }
051        _location = _lef._location;
052
053        // load the panel
054        // row 1a
055        JPanel p1 = new JPanel();
056        p1.setLayout(new GridBagLayout());
057        p1.setBorder(BorderFactory.createTitledBorder(MessageFormat.format(Bundle.getMessage("TrackType"),
058                new Object[]{_location.getName()})));
059        addItem(p1, spurRadioButton, 0, 0);
060        addItem(p1, yardRadioButton, 1, 0);
061        addItem(p1, interchangeRadioButton, 2, 0);
062        addItem(p1, stagingRadioButton, 3, 0);
063        
064        JPanel p2 = new JPanel();
065        p2.add(saveButton);
066
067        // group and set current track type
068        ButtonGroup group = new ButtonGroup();
069        group.add(spurRadioButton);
070        group.add(yardRadioButton);
071        group.add(interchangeRadioButton);
072        group.add(stagingRadioButton);
073        
074        stagingRadioButton.setSelected(_location.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_ChangeTrackTypeLocation", 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()) {
093                changeTracks(Track.SPUR);
094            } else if (yardRadioButton.isSelected()) {
095                changeTracks(Track.YARD);
096            } else if (interchangeRadioButton.isSelected()) {
097                changeTracks(Track.INTERCHANGE);
098            } else if (stagingRadioButton.isSelected()) {
099                changeTracks(Track.STAGING);
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}