001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import java.awt.event.ActionEvent;
005
006import javax.swing.Icon;
007
008import jmri.beans.BeanUtil;
009import jmri.jmrit.roster.Roster;
010import jmri.jmrit.roster.rostergroup.RosterGroupSelector;
011import jmri.util.swing.JmriAbstractAction;
012import jmri.util.swing.JmriJOptionPane;
013import jmri.util.swing.WindowInterface;
014
015/**
016 * Remove roster group.
017 *
018 * <hr>
019 * This file is part of JMRI.
020 * <p>
021 * JMRI is free software; you can redistribute it and/or modify it under the
022 * terms of version 2 of the GNU General Public License as published by the Free
023 * Software Foundation. See the "COPYING" file for a copy of this license.
024 * <p>
025 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
026 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
027 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
028 *
029 * @author Kevin Dickerson Copyright (C) 2009
030 */
031public class DeleteRosterGroupAction extends JmriAbstractAction {
032
033    public DeleteRosterGroupAction(String s, WindowInterface wi) {
034        super(s, wi);
035    }
036
037    public DeleteRosterGroupAction(String s, Icon i, WindowInterface wi) {
038        super(s, i, wi);
039    }
040
041    /**
042     * @param s   Name of this action, e.g. in menus
043     * @param who Component that action is associated with, used to ensure
044     *            proper position in of dialog boxes
045     */
046    public DeleteRosterGroupAction(String s, Component who) {
047        super(s);
048        _who = who;
049    }
050
051    Component _who;
052
053    /**
054     * Call setParameter("group", oldName) prior to calling
055     * actionPerformed(event) to bypass the roster group selection dialog if the
056     * name of the group to be copied is already known and is not the
057     * selectedRosterGroup property of the WindowInterface.
058     *
059     */
060    @Override
061    public void actionPerformed(ActionEvent event) {
062        String group = null;
063        if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) {
064            group = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP);
065        }
066        // null might be valid output from getting the selectedRosterGroup,
067        // so we have to check for null again.
068        if (group == null) {
069            group = (String) JmriJOptionPane.showInputDialog(_who,
070                    Bundle.getMessage("DeleteRosterGroupDialog"),
071                    Bundle.getMessage("DeleteRosterGroupTitle", ""),
072                    JmriJOptionPane.INFORMATION_MESSAGE,
073                    null,
074                    Roster.getDefault().getRosterGroupList().toArray(),
075                    null);
076        }
077        // can't delete the roster itself (ALLENTRIES and null represent the roster)
078        if (group == null || group.equals(Roster.ALLENTRIES)) {
079            return;
080        }
081        // prompt for one last chance
082        if (!userOK(group)) {
083            return;
084        }
085
086        // delete the roster grouping
087        Roster.getDefault().delRosterGroupList(group);
088        Roster.getDefault().writeRoster();
089    }
090
091    /**
092     * Can provide some mechanism to prompt for user for one last chance to
093     * change his/her mind
094     * @param entry roster group to confirm deletion of
095     *
096     * @return true if user says to continue
097     */
098    boolean userOK(String entry) {
099        String[] titles = {Bundle.getMessage("ButtonDelete"), Bundle.getMessage("ButtonCancel")};
100        // TODO: replace "Are you sure..." string with JPanel containing string
101        //       and checkbox silencing this message in the future
102        return ( 0 == // array position 0, ButtonDelete 
103                JmriJOptionPane.showOptionDialog(_who,
104                Bundle.getMessage("DeleteRosterGroupSure", entry),
105                Bundle.getMessage("DeleteRosterGroupTitle", entry),
106                JmriJOptionPane.DEFAULT_OPTION,
107                JmriJOptionPane.QUESTION_MESSAGE,
108                null,
109                titles,
110                null));
111    }
112
113    // never invoked, because we overrode actionPerformed above
114    @Override
115    public jmri.util.swing.JmriPanel makePanel() {
116        throw new IllegalArgumentException("Should not be invoked");
117    }
118
119}