You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerGroup.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.serverlists;
  23. import java.net.URI;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * A server group contains an ordered collection of server entries or other
  31. * server groups.
  32. *
  33. * @since 0.6.4
  34. * @author chris
  35. */
  36. public class ServerGroup extends ServerGroupItemBase {
  37. /** The name of the network for the group. */
  38. private String network;
  39. /** A description of this group. */
  40. private String description = "";
  41. /** This group's parent. */
  42. private final ServerGroup parent;
  43. /** A set of links relevant to this group (e.g. homepages). */
  44. private final Map<String, URI> links = new HashMap<String, URI>();
  45. /** The items contained within the group. */
  46. private final List<ServerGroupItem> entries = new ArrayList<ServerGroupItem>();
  47. /**
  48. * Creates a new server group with the specified name and no parent.
  49. *
  50. * @param name The name to be used for this group
  51. */
  52. public ServerGroup(final String name) {
  53. this(null, name);
  54. }
  55. /**
  56. * Creates a new server group with the specified name.
  57. *
  58. * @param parent The parent of this group, or <code>null</code> for roots.
  59. * @param name The name to be used for this group
  60. */
  61. public ServerGroup(final ServerGroup parent, final String name) {
  62. this.parent = parent;
  63. setName(name);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public ServerGroup getGroup() {
  68. return this;
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. protected ServerGroup getParent() {
  73. return parent;
  74. }
  75. /**
  76. * Sets the network name of this group.
  77. *
  78. * @param network The new network name for the group
  79. */
  80. public void setNetwork(final String network) {
  81. setModified(true);
  82. this.network = network;
  83. }
  84. /**
  85. * Adds a new item to this server group.
  86. *
  87. * @param item The item to be added
  88. */
  89. public void addItem(final ServerGroupItem item) {
  90. setModified(true);
  91. item.setModified(true);
  92. entries.add(item);
  93. }
  94. /**
  95. * Retrieves a list of items belonging to this group.
  96. *
  97. * @return An immutable list of items contained within this group
  98. */
  99. public List<ServerGroupItem> getItems() {
  100. return Collections.unmodifiableList(entries);
  101. }
  102. /**
  103. * Retrieves a ServerGroupItem with the specified name, if one exists. This
  104. * method ignores the case of item's name when comparing.
  105. *
  106. * @param name The name of the item to be retrieved
  107. * @return A correspondingly named item, or null if none exists
  108. */
  109. public ServerGroupItem getItemByName(final String name) {
  110. for (ServerGroupItem item : getItems()) {
  111. if (item.getName().equalsIgnoreCase(name)) {
  112. return item;
  113. }
  114. }
  115. return null;
  116. }
  117. /**
  118. * Retrieves the description of this group.
  119. *
  120. * @return This group's description
  121. */
  122. public String getDescription() {
  123. return description;
  124. }
  125. /**
  126. * Retrieves the network that this group represents, if any.
  127. *
  128. * @return This group's network name, or null if the group does not
  129. * correspond to a known network.
  130. */
  131. public String getNetwork() {
  132. return network;
  133. }
  134. /**
  135. * Sets the description of this group.
  136. *
  137. * @param description The new description for this group.
  138. */
  139. public void setDescription(final String description) {
  140. setModified(true);
  141. this.description = description;
  142. }
  143. /**
  144. * Retrieves a map of link titles to {@link URI}s which are associated
  145. * with this server group. Links will typically include network homepages,
  146. * forums, or support channels.
  147. *
  148. * @return An immutable map of links
  149. */
  150. public Map<String, URI> getLinks() {
  151. return Collections.unmodifiableMap(links);
  152. }
  153. /**
  154. * Adds a new link with the specified title and address. If a link with the
  155. * same title existed previously, it will be replaced.
  156. *
  157. * @param title The title of the new link
  158. * @param address The address of the link
  159. */
  160. public void addLink(final String title, final URI address) {
  161. setModified(true);
  162. links.put(title, address);
  163. }
  164. /**
  165. * {@inheritDoc}
  166. *
  167. * Current implementation just selects the first item in this group and
  168. * asks for its URI.
  169. */
  170. @Override
  171. public URI getAddress() {
  172. if (!entries.isEmpty()) {
  173. return entries.get(0).getAddress();
  174. }
  175. return null;
  176. }
  177. /**
  178. * {@inheritDoc}
  179. *
  180. * Current implementation just selects the first item in this group and
  181. * asks it to connect.
  182. */
  183. @Override
  184. public void connect() {
  185. if (!entries.isEmpty()) {
  186. entries.get(0).connect();
  187. }
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public String toString() {
  192. return "[" + getName() + ": links: " + getLinks() + "; desc: "
  193. + getDescription() + "; content: " + getItems() + "]";
  194. }
  195. }