001package jmri.jmrit.operations.rollingstock.cars.tools;
002
003import java.awt.event.ActionEvent;
004
005import javax.swing.AbstractAction;
006
007import jmri.InstanceManager;
008import jmri.jmrit.operations.rollingstock.cars.*;
009import jmri.jmrit.operations.rollingstock.cars.gui.CarsTableFrame;
010import jmri.util.swing.JmriJOptionPane;
011
012/**
013 * This routine will delete all cars from the operation database, or delete all
014 * cars at a location, or sitting on a track.
015 *
016 * @author Dan Boudreau Copyright (C) 2007, 2016, 2022
017 */
018public class DeleteCarRosterAction extends AbstractAction {
019
020    CarsTableFrame _carsTableFrame;
021
022    public DeleteCarRosterAction(CarsTableFrame carsTableFrame) {
023        super(Bundle.getMessage("MenuItemDelete"));
024        // delete all cars on a track or location
025        if (carsTableFrame.carsTableModel.trackName != null) {
026            String actionName = Bundle.getMessage("MenuDeleteCarsTrack",
027                    carsTableFrame.carsTableModel.trackName);
028            putValue(NAME, actionName);
029        } else if (carsTableFrame.carsTableModel.locationName != null) {
030            String actionName = Bundle.getMessage("MenuDeleteCarsLocation",
031                    carsTableFrame.carsTableModel.locationName);
032            putValue(NAME, actionName);
033        }
034        _carsTableFrame = carsTableFrame;
035    }
036
037    @Override
038    public void actionPerformed(ActionEvent ae) {
039        if (_carsTableFrame.carsTableModel.trackName == null && _carsTableFrame.carsTableModel.locationName == null) {
040            if (JmriJOptionPane.showConfirmDialog(null, Bundle.getMessage("carSureDelete"),
041                    Bundle.getMessage("carDeleteAll"), JmriJOptionPane.OK_CANCEL_OPTION) == JmriJOptionPane.OK_OPTION) {
042                log.debug("removing all cars from roster");
043                InstanceManager.getDefault(CarManager.class).deleteAll();
044            }
045        } else {
046            // delete all cars on track or location
047            String message = Bundle.getMessage("carDeleteCarsTrack",
048                    _carsTableFrame.carsTableModel.trackName);
049            if (_carsTableFrame.carsTableModel.trackName == null) {
050                message = Bundle.getMessage("carDeleteCarsLocation",
051                        _carsTableFrame.carsTableModel.locationName);
052            }
053            if (JmriJOptionPane.showConfirmDialog(null, message,
054                    Bundle.getMessage("carDeleteAll"), JmriJOptionPane.OK_CANCEL_OPTION) == JmriJOptionPane.OK_OPTION) {
055                for (Car car : _carsTableFrame.carsTableModel.getSelectedCarList()) {
056                    InstanceManager.getDefault(CarManager.class).deregister(car);
057                }
058            }
059        }
060    }
061
062    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DeleteCarRosterAction.class);
063}