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 * Rename a roster group.
017 * <p>
018 * This action prevents a user from renaming a roster group to same name as an
019 * existing roster group.
020 * <hr>
021 * This file is part of JMRI.
022 * <p>
023 * JMRI is free software; you can redistribute it and/or modify it under the
024 * terms of version 2 of the GNU General Public License as published by the Free
025 * Software Foundation. See the "COPYING" file for a copy of this license.
026 * <p>
027 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
028 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
029 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
030 *
031 * @author Kevin Dickerson Copyright (C) 2009
032 * @author Randall Wood Copyright (C) 2011
033 * @see Roster
034 */
035public class RenameRosterGroupAction extends JmriAbstractAction {
036
037    public RenameRosterGroupAction(String s, WindowInterface wi) {
038        super(s, wi);
039    }
040
041    public RenameRosterGroupAction(String s, Icon i, WindowInterface wi) {
042        super(s, i, wi);
043    }
044
045    /**
046     * @param s   Name of this action, e.g. in menus
047     * @param who Component that action is associated with, used to ensure
048     *            proper position in of dialog boxes
049     */
050    public RenameRosterGroupAction(String s, Component who) {
051        super(s);
052        _who = who;
053    }
054    Component _who;
055
056    /**
057     * Call setParameter("group", oldName) prior to calling
058     * actionPerformed(event) to bypass the roster group selection dialog if the
059     * name of the group to be copied is already known and is not the
060     * selectedRosterGroup property of the WindowInterface.
061     *
062     */
063    @Override
064    public void actionPerformed(ActionEvent event) {
065        String group = null;
066        if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) {
067            group = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP);
068        }
069        // null might be valid output from getting the selectedRosterGroup,
070        // so we have to check for null again.
071        if (group == null) {
072            group = (String) JmriJOptionPane.showInputDialog(_who,
073                    Bundle.getMessage("RenameRosterGroupDialog"),
074                    Bundle.getMessage("RenameRosterGroupTitle", ""),
075                    JmriJOptionPane.INFORMATION_MESSAGE,
076                    null,
077                    Roster.getDefault().getRosterGroupList().toArray(),
078                    null);
079        }
080        // can't rename the groups that represent the entire roster 
081        if (group == null || group.equals(Roster.ALLENTRIES)) {
082            return;
083        }
084
085        String entry = (String) JmriJOptionPane.showInputDialog(_who,
086                Bundle.getMessage("RenameRosterGroupNewName", group),
087                Bundle.getMessage("RenameRosterGroupTitle", group),
088                JmriJOptionPane.INFORMATION_MESSAGE,
089                null,
090                null,
091                null);
092        if (entry != null) {
093            entry = entry.trim(); // remove white space around name, also prevent "Space" as a Group name
094        }
095        if (entry == null || entry.length() == 0 || entry.equals(Roster.ALLENTRIES)) {
096            return;
097        } else if (Roster.getDefault().getRosterGroupList().contains(entry)) {
098            JmriJOptionPane.showMessageDialog(_who,
099                    Bundle.getMessage("RenameRosterGroupSameName", entry),
100                    Bundle.getMessage("RenameRosterGroupTitle", group),
101                    JmriJOptionPane.ERROR_MESSAGE);
102        }
103
104        // rename the roster grouping
105        Roster.getDefault().renameRosterGroupList(group, entry);
106        Roster.getDefault().writeRoster();
107    }
108
109    // never invoked, because we overrode actionPerformed above
110    @Override
111    public jmri.util.swing.JmriPanel makePanel() {
112        throw new IllegalArgumentException("Should not be invoked");
113    }
114}