001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import java.awt.event.ActionEvent;
005import java.util.ArrayList;
006
007import javax.swing.Icon;
008
009import jmri.jmrit.roster.Roster;
010import jmri.jmrit.roster.RosterEntry;
011import jmri.util.swing.JmriAbstractAction;
012import jmri.util.swing.JmriJOptionPane;
013import jmri.util.swing.WindowInterface;
014
015/**
016 * Create a roster group.
017 *
018 *
019 * <hr>
020 * This file is part of JMRI.
021 * <p>
022 * JMRI is free software; you can redistribute it and/or modify it under the
023 * terms of version 2 of the GNU General Public License as published by the Free
024 * Software Foundation. See the "COPYING" file for a copy of this license.
025 * <p>
026 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
027 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
028 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
029 *
030 * @author Kevin Dickerson Copyright (C) 2009
031 */
032public class CreateRosterGroupAction extends JmriAbstractAction {
033
034    public CreateRosterGroupAction(String s, WindowInterface wi) {
035        super(s, wi);
036    }
037
038    public CreateRosterGroupAction(String s, Icon i, WindowInterface wi) {
039        super(s, i, wi);
040    }
041
042    /**
043     * @param s   Name of this action, e.g. in menus
044     * @param who Component that action is associated with, used to ensure
045     *            proper position in of dialog boxes
046     */
047    public CreateRosterGroupAction(String s, Component who) {
048        super(s);
049        _who = who;
050    }
051
052    Component _who;
053    ArrayList<RosterEntry> rosterEntries;
054
055    @Override
056    public void actionPerformed(ActionEvent event) {
057
058        String entry = (String) JmriJOptionPane.showInputDialog(_who,
059                Bundle.getMessage("CreateRosterGroupDialog", Bundle.getMessage("MenuGroupCreate")),
060                Bundle.getMessage("MenuGroupCreate"),
061                JmriJOptionPane.INFORMATION_MESSAGE,
062                null, // icon
063                null, // initial values
064                null);// preselected initial value
065        if (entry != null) {
066            entry = entry.trim(); // remove white space around name, also prevent "Space" as a Group name
067        }
068        if (entry == null || entry.length() == 0 || entry.equals(Roster.ALLENTRIES)) {
069            return;
070        }
071        if (rosterEntries != null) {
072            for (RosterEntry re : rosterEntries) {
073                log.debug("Adding RosterEntry {} to new group {}", re.getId(), entry);
074                re.putAttribute(Roster.ROSTER_GROUP_PREFIX + entry, "yes");
075                re.updateFile();
076            }
077        }
078        Roster.getDefault().addRosterGroup(entry);
079        Roster.getDefault().writeRoster();
080    }
081
082    // never invoked, because we overrode actionPerformed above
083    @Override
084    public jmri.util.swing.JmriPanel makePanel() {
085        throw new IllegalArgumentException("Should not be invoked");
086    }
087
088    /**
089     * Set a parameter
090     * <p>
091     * This method accepts the following key, with the following value:
092     * <dl>
093     * <dt>RosterEntries</dt>
094     * <dd>An ArrayList&lt;RosterEntry&gt; of roster entries.
095     * </dl>
096     *
097     */
098    @Override
099    @SuppressWarnings("unchecked")
100    public void setParameter(String key, Object value) {
101        if (key.equals("RosterEntries") && value.getClass().equals(ArrayList.class)) {
102            rosterEntries = (ArrayList<RosterEntry>) value;
103        }
104    }
105
106    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CreateRosterGroupAction.class);
107}