001
002package jmri.jmrit.operations.locations.tools;
003
004import java.awt.Dimension;
005import java.awt.GridBagLayout;
006
007import javax.swing.*;
008
009import jmri.jmrit.operations.OperationsFrame;
010import jmri.jmrit.operations.OperationsXml;
011import jmri.jmrit.operations.locations.Track;
012import jmri.jmrit.operations.locations.TrackEditFrame;
013import jmri.jmrit.operations.setup.Control;
014import jmri.jmrit.operations.setup.Setup;
015import jmri.util.swing.JmriJOptionPane;
016
017/**
018 * Planned Pick ups.
019 * Frame to allow a user to define how much used track space is to be ignored
020 * by the program when placing new rolling stock onto a track.
021 *
022 * @author Daniel Boudreau Copyright (C) 2012, 2017
023 * 
024 */
025class IgnoreUsedTrackFrame extends OperationsFrame {
026
027    // radio buttons
028    JRadioButton zeroPercent = new JRadioButton(Bundle.getMessage("Disabled"));
029    JRadioButton twentyfivePercent = new JRadioButton(Track.IGNORE_25+"%"); // NOI18N
030    JRadioButton fiftyPercent = new JRadioButton(Track.IGNORE_50+"%");  // NOI18N
031    JRadioButton seventyfivePercent = new JRadioButton(Track.IGNORE_75+"%"); // NOI18N
032    JRadioButton hundredPercent = new JRadioButton(Track.IGNORE_100+"%");  // NOI18N
033
034    // major buttons
035    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
036
037    protected Track _track;
038
039    public IgnoreUsedTrackFrame(TrackEditFrame tef) {
040        super();
041
042        setTitle(Bundle.getMessage("MenuItemPlannedPickups"));
043
044        _track = tef._track;
045        if (_track == null) {
046            log.debug("track is null!");
047            return;
048        }
049
050        // load the panel
051        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
052        
053        // row 1
054        JPanel p1 = new JPanel();
055        p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
056        p1.setMaximumSize(new Dimension(2000, 250));
057
058        // row 1a
059        JPanel pTrackName = new JPanel();
060        pTrackName.setLayout(new GridBagLayout());
061        pTrackName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Track")));
062        addItem(pTrackName, new JLabel(_track.getName()), 0, 0);
063
064        // row 1b
065        JPanel pLocationName = new JPanel();
066        pLocationName.setLayout(new GridBagLayout());
067        pLocationName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Location")));
068        addItem(pLocationName, new JLabel(_track.getLocation().getName()), 0, 0);
069
070        p1.add(pTrackName);
071        p1.add(pLocationName);
072
073        JPanel p2 = new JPanel();
074        p2.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("PrePlanedPickups")));
075
076        p2.add(zeroPercent);
077        if (!_track.isStaging()) {
078            p2.add(twentyfivePercent);
079            p2.add(fiftyPercent);
080            p2.add(seventyfivePercent);
081        }
082        p2.add(hundredPercent);
083
084        ButtonGroup buttonGroup = new ButtonGroup();
085        buttonGroup.add(zeroPercent);
086        buttonGroup.add(twentyfivePercent);
087        buttonGroup.add(fiftyPercent);
088        buttonGroup.add(seventyfivePercent);
089        buttonGroup.add(hundredPercent);
090
091        // select the correct radio button
092        int percentage = _track.getIgnoreUsedLengthPercentage();
093        zeroPercent.setSelected(percentage >= Track.IGNORE_0);
094        twentyfivePercent.setSelected(percentage >= Track.IGNORE_25);
095        fiftyPercent.setSelected(percentage >= Track.IGNORE_50);
096        seventyfivePercent.setSelected(percentage >= Track.IGNORE_75);
097        hundredPercent.setSelected(percentage >= Track.IGNORE_100);
098        
099        // warning text for planned pick ups.
100        JPanel p3 = new JPanel();
101        p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
102        p3.add(new JLabel(Bundle.getMessage("PPWarningMessage")));
103        p3.add(new JLabel(Bundle.getMessage("PPWarningMessage2")));
104        
105        JPanel pW = new JPanel();
106        pW.setLayout(new GridBagLayout());
107        addItem(pW, p3, 0, 1);
108        addItem(pW, saveButton, 0, 2);
109
110        getContentPane().add(p1);
111        getContentPane().add(p2);
112        getContentPane().add(pW);
113
114        addButtonAction(saveButton);
115        
116        addHelpMenu("package.jmri.jmrit.operations.Operations_PlannedPickUps", true); // NOI18N
117
118        initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight300));
119    }
120
121    @Override
122    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
123        if (ae.getSource() == saveButton) {
124            // save percentage selected
125            int percentage = 0;
126            if (twentyfivePercent.isSelected()) {
127                percentage = Track.IGNORE_25;
128            } else if (fiftyPercent.isSelected()) {
129                percentage = Track.IGNORE_50;
130            } else if (seventyfivePercent.isSelected()) {
131                percentage = Track.IGNORE_75;
132            } else if (hundredPercent.isSelected()) {
133                percentage = Track.IGNORE_100;
134            }
135            if (_track != null) {
136                _track.setIgnoreUsedLengthPercentage(percentage);
137                // issue error message if using an alternate track
138                if (_track.getAlternateTrack() != null && percentage > 0) {
139                    JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("PPWarningAlternate"),
140                            Bundle.getMessage("PPWarningConfiguration"),
141                            JmriJOptionPane.ERROR_MESSAGE);
142                }
143            }
144            // save location file
145            OperationsXml.save();
146            if (Setup.isCloseWindowOnSaveEnabled()) {
147                dispose();
148            }
149        }
150    }
151
152    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IgnoreUsedTrackFrame.class);
153}