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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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 com.dmdirc.interfaces.config.IdentityController;
  24. import java.net.URI;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * A server group contains an ordered collection of server entries or other server groups.
  32. *
  33. * @since 0.6.4
  34. */
  35. public class ServerGroup extends ServerGroupItemBase {
  36. /** The name of the network for the group. */
  37. private String network;
  38. /** A description of this group. */
  39. private String description = "";
  40. /** This group's parent. */
  41. private final ServerGroup parent;
  42. /** A set of links relevant to this group (e.g. homepages). */
  43. private final Map<String, URI> links = new HashMap<>();
  44. /** The items contained within the group. */
  45. private final List<ServerGroupItem> entries = new ArrayList<>();
  46. /**
  47. * Creates a new server group with the specified name and no parent.
  48. *
  49. * @param identityController The controller to read/write settings with.
  50. * @param name The name to be used for this group
  51. */
  52. public ServerGroup(final IdentityController identityController, final String name) {
  53. this(identityController, null, name);
  54. }
  55. /**
  56. * Creates a new server group with the specified name.
  57. *
  58. * @param identityController The controller to read/write settings with.
  59. * @param parent The parent of this group, or <code>null</code> for roots.
  60. * @param name The name to be used for this group
  61. */
  62. public ServerGroup(
  63. final IdentityController identityController,
  64. final ServerGroup parent,
  65. final String name) {
  66. super(identityController);
  67. this.parent = parent;
  68. setName(name);
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public ServerGroup getGroup() {
  73. return this;
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. protected ServerGroup getParent() {
  78. return parent;
  79. }
  80. /**
  81. * Sets the network name of this group.
  82. *
  83. * @param network The new network name for the group
  84. */
  85. public void setNetwork(final String network) {
  86. setModified(true);
  87. this.network = network;
  88. }
  89. /**
  90. * Adds a new item to this server group.
  91. *
  92. * @param item The item to be added
  93. */
  94. public void addItem(final ServerGroupItem item) {
  95. setModified(true);
  96. item.setModified(true);
  97. entries.add(item);
  98. }
  99. /**
  100. * Retrieves a list of items belonging to this group.
  101. *
  102. * @return An immutable list of items contained within this group
  103. */
  104. public List<ServerGroupItem> getItems() {
  105. return Collections.unmodifiableList(entries);
  106. }
  107. /**
  108. * Retrieves a ServerGroupItem with the specified name, if one exists. This method ignores the
  109. * case of item's name when comparing.
  110. *
  111. * @param name The name of the item to be retrieved
  112. *
  113. * @return A correspondingly named item, or null if none exists
  114. */
  115. public ServerGroupItem getItemByName(final String name) {
  116. for (ServerGroupItem item : getItems()) {
  117. if (item.getName().equalsIgnoreCase(name)) {
  118. return item;
  119. }
  120. }
  121. return null;
  122. }
  123. /**
  124. * Retrieves the description of this group.
  125. *
  126. * @return This group's description
  127. */
  128. public String getDescription() {
  129. return description;
  130. }
  131. /**
  132. * Retrieves the network that this group represents, if any.
  133. *
  134. * @return This group's network name, or null if the group does not correspond to a known
  135. * network.
  136. */
  137. public String getNetwork() {
  138. return network;
  139. }
  140. /**
  141. * Sets the description of this group.
  142. *
  143. * @param description The new description for this group.
  144. */
  145. public void setDescription(final String description) {
  146. setModified(true);
  147. this.description = description;
  148. }
  149. /**
  150. * Retrieves a map of link titles to {@link URI}s which are associated with this server group.
  151. * Links will typically include network homepages, forums, or support channels.
  152. *
  153. * @return An immutable map of links
  154. */
  155. public Map<String, URI> getLinks() {
  156. return Collections.unmodifiableMap(links);
  157. }
  158. /**
  159. * Adds a new link with the specified title and address. If a link with the same title existed
  160. * previously, it will be replaced.
  161. *
  162. * @param title The title of the new link
  163. * @param address The address of the link
  164. */
  165. public void addLink(final String title, final URI address) {
  166. setModified(true);
  167. links.put(title, address);
  168. }
  169. /**
  170. * {@inheritDoc}
  171. *
  172. * Current implementation just selects the first item in this group and asks for its URI.
  173. */
  174. @Override
  175. public URI getAddress() {
  176. if (!entries.isEmpty()) {
  177. return entries.get(0).getAddress();
  178. }
  179. return null;
  180. }
  181. /**
  182. * {@inheritDoc}
  183. *
  184. * Current implementation just selects the first item in this group and asks it to connect.
  185. */
  186. @Override
  187. public void connect() {
  188. if (!entries.isEmpty()) {
  189. entries.get(0).connect();
  190. }
  191. }
  192. /** {@inheritDoc} */
  193. @Override
  194. public String toString() {
  195. return "[" + getName() + ": links: " + getLinks() + "; desc: "
  196. + getDescription() + "; content: " + getItems() + "]";
  197. }
  198. }