/* * Copyright (c) 2006-2014 DMDirc Developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.dmdirc.addons.serverlists; import com.dmdirc.interfaces.config.IdentityController; import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; /** * A server group contains an ordered collection of server entries or other server groups. * * @since 0.6.4 */ public class ServerGroup extends ServerGroupItemBase { /** The name of the network for the group. */ private String network; /** A description of this group. */ private String description = ""; /** This group's parent. */ private final ServerGroup parent; /** A set of links relevant to this group (e.g. homepages). */ private final Map links = new HashMap<>(); /** The items contained within the group. */ private final List entries = new ArrayList<>(); /** * Creates a new server group with the specified name and no parent. * * @param identityController The controller to read/write settings with. * @param name The name to be used for this group */ public ServerGroup(final IdentityController identityController, final String name) { this(identityController, null, name); } /** * Creates a new server group with the specified name. * * @param identityController The controller to read/write settings with. * @param parent The parent of this group, or null for roots. * @param name The name to be used for this group */ public ServerGroup( final IdentityController identityController, final ServerGroup parent, final String name) { super(identityController); this.parent = parent; setName(name); } /** {@inheritDoc} */ @Override public ServerGroup getGroup() { return this; } /** {@inheritDoc} */ @Override protected ServerGroup getParent() { return parent; } /** * Sets the network name of this group. * * @param network The new network name for the group */ public void setNetwork(final String network) { setModified(true); this.network = network; } /** * Adds a new item to this server group. * * @param item The item to be added */ public void addItem(final ServerGroupItem item) { setModified(true); item.setModified(true); entries.add(item); } /** * Retrieves a list of items belonging to this group. * * @return An immutable list of items contained within this group */ public List getItems() { return Collections.unmodifiableList(entries); } /** * Retrieves a ServerGroupItem with the specified name, if one exists. This method ignores the * case of item's name when comparing. * * @param name The name of the item to be retrieved * * @return A correspondingly named item, or null if none exists */ public ServerGroupItem getItemByName(final String name) { for (ServerGroupItem item : getItems()) { if (item.getName().equalsIgnoreCase(name)) { return item; } } return null; } /** * Retrieves the description of this group. * * @return This group's description */ public String getDescription() { return description; } /** * Retrieves the network that this group represents, if any. * * @return This group's network name, or null if the group does not correspond to a known * network. */ public String getNetwork() { return network; } /** * Sets the description of this group. * * @param description The new description for this group. */ public void setDescription(final String description) { setModified(true); this.description = description; } /** * Retrieves a map of link titles to {@link URI}s which are associated with this server group. * Links will typically include network homepages, forums, or support channels. * * @return An immutable map of links */ public Map getLinks() { return Collections.unmodifiableMap(links); } /** * Adds a new link with the specified title and address. If a link with the same title existed * previously, it will be replaced. * * @param title The title of the new link * @param address The address of the link */ public void addLink(final String title, final URI address) { setModified(true); links.put(title, address); } /** * {@inheritDoc} * * Current implementation just selects the first item in this group and asks for its URI. */ @Override public URI getAddress() { if (!entries.isEmpty()) { return entries.get(0).getAddress(); } return null; } /** * {@inheritDoc} * * Current implementation just selects the first item in this group and asks it to connect. */ @Override public void connect() { if (!entries.isEmpty()) { entries.get(0).connect(); } } /** {@inheritDoc} */ @Override public String toString() { return "[" + getName() + ": links: " + getLinks() + "; desc: " + getDescription() + "; content: " + getItems() + "]"; } }