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