001package jmri.jmrit.roster.rostergroup;
002
003import java.util.List;
004import jmri.beans.Bean;
005import jmri.jmrit.roster.Roster;
006import jmri.jmrit.roster.RosterEntry;
007import jmri.jmrit.roster.RosterObject;
008
009/**
010 * A RosterGroup object contains information about groupings of entries within
011 * the {@link jmri.jmrit.roster.Roster}.
012 *
013 * This object allows groups to be manipulated as Java beans.
014 *
015 * @author Randall Wood randall.h.wood@alexandriasoftware.com
016 */
017public class RosterGroup extends Bean implements RosterObject {
018
019    private String name;
020
021    /**
022     * Create a roster group.
023     *
024     * This sets the name without calling {@link #setName(java.lang.String) }.
025     * @param name roster group name.
026     */
027    public RosterGroup(String name) {
028        this.name = name;
029    }
030
031    /**
032     * Get the list of entries associated with this group.
033     *
034     * @return the list of entries or an empty list.
035     */
036    public List<RosterEntry> getEntries() {
037        return Roster.getDefault().getEntriesInGroup(this.getName());
038    }
039
040    /**
041     * Get the RosterGroup's name.
042     *
043     * Use {@link #getDisplayName() } to get the name to be displayed to a user.
044     *
045     * @return the name
046     * @see #getDisplayName()
047     */
048    public String getName() {
049        return this.name;
050    }
051
052    /**
053     * Set the RosterGroup's name, changing it in every entry associated with
054     * the roster.
055     *
056     * @param newName the new name
057     */
058    public void setName(String newName) {
059        if (Roster.getDefault().getRosterGroups().containsKey(newName)) {
060            return;
061        }
062        String oldName = this.name;
063        String oldGroup = Roster.getRosterGroupProperty(oldName);
064        String newGroup = Roster.getRosterGroupProperty(newName);
065        Roster.getDefault().remapRosterGroup(this, newName);
066        for (RosterEntry re : this.getEntries()) {
067            re.putAttribute(newGroup, "yes"); // NOI18N
068            re.deleteAttribute(oldGroup);
069        }
070        this.name = newName;
071        Roster.getDefault().rosterGroupRenamed(oldName, newName);
072    }
073
074    @Override
075    public String getDisplayName() {
076        return this.getName();
077    }
078
079    /**
080     * Flag indicating that this RosterGroup can be edited by the user.
081     *
082     * The default implementation always returns true.
083     *
084     * @return true if the group can be edited.
085     */
086    public boolean canEdit() {
087        return true;
088    }
089
090    /**
091     * Flag indicating that this RosterGroup can be deleted by the user.
092     *
093     * The default implementation always returns true.
094     *
095     * @return true if the group can be deleted.
096     */
097    public boolean canDelete() {
098        return true;
099    }
100
101    /**
102     * Flag indicating that this RosterGroup can be duplicated by the user.
103     *
104     * The default implementation always returns true.
105     *
106     * @return true if the group can be copied.
107     */
108    public boolean canCopy() {
109        return true;
110    }
111
112    /**
113     * Flag indicating that the contents of this RosterGroup can be changed by
114     * the user.
115     *
116     * The default implementation always returns true.
117     *
118     * @return true if entries in this group can be changed.
119     */
120    public boolean canChangeContents() {
121        return true;
122    }
123}