001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import java.awt.event.ActionEvent;
005import javax.swing.Icon;
006
007import jmri.beans.BeanUtil;
008import jmri.jmrit.roster.Roster;
009import jmri.jmrit.roster.rostergroup.RosterGroupSelector;
010import jmri.util.swing.JmriAbstractAction;
011import jmri.util.swing.JmriJOptionPane;
012import jmri.util.swing.WindowInterface;
013
014/**
015 * Duplicate roster group.
016 * <p>
017 * This action prevents a user from creating a new roster group with the same
018 * name as an existing roster group.
019 * <p>
020 * If performAction(event) is being called in a context where the name of the
021 * group to be duplicated is already known, call setContext(groupName) prior to
022 * calling performAction(event) to bypass the group selection dialog.
023 *
024 * <hr>
025 * This file is part of JMRI.
026 * <p>
027 * JMRI is free software; you can redistribute it and/or modify it under the
028 * terms of version 2 of the GNU General Public License as published by the Free
029 * Software Foundation. See the "COPYING" file for a copy of this license.
030 * <p>
031 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
032 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
033 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
034 *
035 * @author Kevin Dickerson Copyright (C) 2009
036 */
037public class CopyRosterGroupAction extends JmriAbstractAction {
038
039    public CopyRosterGroupAction(String s, WindowInterface wi) {
040        super(s, wi);
041    }
042
043    public CopyRosterGroupAction(String s, Icon i, WindowInterface wi) {
044        super(s, i, wi);
045    }
046
047    /**
048     * @param s   Name of this action, e.g. in menus
049     * @param who Component that action is associated with, used to ensure
050     *            proper position in of dialog boxes
051     */
052    public CopyRosterGroupAction(String s, Component who) {
053        super(s);
054        _who = who;
055    }
056    Component _who;
057
058    /**
059     * Call setParameter("group", oldName) prior to calling
060     * actionPerformed(event) to bypass the roster group selection dialog if the
061     * name of the group to be copied is already known and is not the
062     * selectedRosterGroup property of the WindowInterface.
063     *
064     */
065    @Override
066    public void actionPerformed(ActionEvent event) {
067        String group = null;
068        // only query wi for group if group was not set using setParameter
069        // prior to call
070        if (BeanUtil.hasProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP)) {
071            group = (String) BeanUtil.getProperty(wi, RosterGroupSelector.SELECTED_ROSTER_GROUP);
072        }
073        // null might be valid output from getting the selectedRosterGroup,
074        // so we have to check for null again.
075        if (group == null) {
076            group = (String) JmriJOptionPane.showInputDialog(_who,
077                    Bundle.getMessage("DuplicateRosterGroupDialog"),
078                    Bundle.getMessage("DuplicateRosterGroupTitle", ""),
079                    JmriJOptionPane.INFORMATION_MESSAGE,
080                    null,
081                    Roster.getDefault().getRosterGroupList().toArray(),
082                    null);
083        }
084        // don't duplicate the null and ALLENTRIES groups (they are the entire roster)
085        if (group == null || group.equals(Roster.ALLENTRIES)) {
086            return;
087        }
088
089        String entry = (String) JmriJOptionPane.showInputDialog(_who,
090                Bundle.getMessage("DuplicateRosterGroupNewName"),
091                Bundle.getMessage("DuplicateRosterGroupTitle",group),
092                JmriJOptionPane.INFORMATION_MESSAGE,
093                null,
094                null,
095                null);
096        if (entry != null) {
097            entry = entry.trim(); // remove white space around name, also prevent "Space" as a Group name
098        }
099        if (entry == null || entry.length() == 0 || entry.equals(Roster.ALLENTRIES)) {
100            return;
101        } else if (Roster.getDefault().getRosterGroupList().contains(entry)) {
102            JmriJOptionPane.showMessageDialog(_who,
103                    Bundle.getMessage("DuplicateRosterGroupSameName", entry),
104                    Bundle.getMessage("DuplicateRosterGroupTitle", group),
105                    JmriJOptionPane.ERROR_MESSAGE);
106        }
107
108        // rename the roster grouping
109        Roster.getDefault().copyRosterGroupList(group, entry);
110        Roster.getDefault().writeRoster();
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}