001package jmri.jmrit.display;
002
003import java.awt.event.ActionEvent;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Set;
007import javax.swing.AbstractAction;
008
009import jmri.InstanceManager;
010import jmri.util.swing.JmriJOptionPane;
011
012/**
013 * Display a combo list of panels. A selected panel will be deleted if selected
014 * and OK is pressed.  The panel is immediately removed from memory using dispose().
015 * <p>
016 * The delete is final when the Store action is performed.
017 *
018 * @author Dave Sand Copyright: Copyright (c) 2021
019 */
020public class PanelDeleteAction extends AbstractAction {
021
022    /**
023     * Constructor just initializes parent class.
024     *
025     * @param name Action name
026     */
027    public PanelDeleteAction(String name) {
028        super(name);
029    }
030
031    public PanelDeleteAction() {
032        super("Delete Panel ...");  // NOI18N
033    }
034
035    @Override
036    public void actionPerformed(ActionEvent e) {
037        Set<Editor> editors = InstanceManager.getDefault(EditorManager.class).getAll();
038        if (editors.isEmpty()) {
039            log.warn("PanelDeleteAction::actionPerformed nothing to delete");  // NOI18N
040        } else {
041            List<String> panelNames = new ArrayList<>();
042            editors.forEach(editor -> {
043                panelNames.add(editor.getTitle());
044            });
045
046            String panelName = (String) JmriJOptionPane.showInputDialog(null,
047                    Bundle.getMessage("PanelDeleteMessage"),  // NOI18N
048                    Bundle.getMessage("PanelDeleteTitle"),    // NOI18N
049                    JmriJOptionPane.PLAIN_MESSAGE,
050                    null,
051                    panelNames.toArray(),
052                    null);
053
054            if (panelName != null && !panelName.isEmpty()) {
055                Editor selected = InstanceManager.getDefault(EditorManager.class).get(panelName);
056                if (selected != null) {
057                    if (selected instanceof jmri.jmrit.display.layoutEditor.LayoutEditor) {
058                        if (!((jmri.jmrit.display.layoutEditor.LayoutEditor)selected).canDeletePanel()) {
059                            return;
060                        }
061                    }
062                    if (selected instanceof jmri.jmrit.display.panelEditor.PanelEditor ||
063                             selected instanceof jmri.jmrit.display.switchboardEditor.SwitchboardEditor) {
064                        selected.getTargetFrame().dispose();
065                    }
066                    selected.dispose();
067                }
068            }
069        }
070    }
071
072    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PanelDeleteAction.class);
073}
074