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.

ServerListModel.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.serverlistdialog;
  23. import com.dmdirc.actions.wrappers.PerformType;
  24. import com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription;
  25. import com.dmdirc.addons.serverlists.ServerEntry;
  26. import com.dmdirc.addons.serverlists.ServerGroup;
  27. import com.dmdirc.addons.serverlists.ServerGroupItem;
  28. import com.dmdirc.addons.serverlists.ServerList;
  29. import com.dmdirc.interfaces.ConnectionManager;
  30. import com.dmdirc.interfaces.config.IdentityController;
  31. import com.dmdirc.util.collections.ListenerList;
  32. import java.net.URI;
  33. import java.util.List;
  34. import javax.inject.Inject;
  35. import javax.swing.tree.DefaultMutableTreeNode;
  36. import javax.swing.tree.DefaultTreeModel;
  37. /**
  38. * Model proxying requests from the core server list model to the swing ui.
  39. */
  40. public class ServerListModel {
  41. /** Server list. */
  42. private final ServerList list;
  43. /** Listener list. */
  44. private final ListenerList listeners;
  45. /** Active server item. */
  46. private ServerGroupItem activeItem;
  47. /** ServerManager that ServerEntrys use to create servers */
  48. private final ConnectionManager connectionManager;
  49. /** The controller to read/write settings with. */
  50. private final IdentityController identityController;
  51. /**
  52. * Creates a new server list model.
  53. *
  54. * @param connectionManager ServerManager currently in use.
  55. * @param identityController The controller to read/write settings with.
  56. * @param serverList The server list to use for the top-level entries.
  57. */
  58. @Inject
  59. public ServerListModel(
  60. final ConnectionManager connectionManager,
  61. final IdentityController identityController,
  62. final ServerList serverList) {
  63. this.connectionManager = connectionManager;
  64. this.identityController = identityController;
  65. list = serverList;
  66. listeners = new ListenerList();
  67. }
  68. /**
  69. * Returns a populated tree model for this server list model.
  70. *
  71. * @return Populated tree model
  72. */
  73. public DefaultTreeModel getTreeModel() {
  74. return populateModel(new DefaultTreeModel(
  75. new DefaultMutableTreeNode("All Servers")));
  76. }
  77. /**
  78. * Has this model got any groups?
  79. *
  80. * @return true iif there are server groups
  81. */
  82. public boolean hasItems() {
  83. return !list.getServerGroups().isEmpty();
  84. }
  85. /**
  86. * Populates a tree model for this server list model.
  87. *
  88. * @param model Un-populated tree model to populate
  89. *
  90. * @return Populated tree model
  91. */
  92. public DefaultTreeModel populateModel(final DefaultTreeModel model) {
  93. for (ServerGroup group : list.getServerGroups()) {
  94. final DefaultMutableTreeNode child = new DefaultMutableTreeNode(
  95. group);
  96. model.insertNodeInto(child, (DefaultMutableTreeNode) model
  97. .getRoot(), model.getChildCount(model.getRoot()));
  98. model.nodeStructureChanged((DefaultMutableTreeNode) model
  99. .getRoot());
  100. addGroups(model, child, group.getItems());
  101. }
  102. return model;
  103. }
  104. /**
  105. * Recursively adds groups to the specified tree model.
  106. *
  107. * @param model Tree model
  108. * @param parent Parent node to populate
  109. * @param items Items to add to parent node
  110. */
  111. private void addGroups(final DefaultTreeModel model,
  112. final DefaultMutableTreeNode parent,
  113. final List<ServerGroupItem> items) {
  114. for (ServerGroupItem group : items) {
  115. final DefaultMutableTreeNode child = new DefaultMutableTreeNode(
  116. group);
  117. model.insertNodeInto(child, parent, model.getChildCount(parent));
  118. if (group instanceof ServerGroup) {
  119. addGroups(model, child, ((ServerGroup) group).getItems());
  120. }
  121. }
  122. }
  123. /**
  124. * Adds a server list listener to be notified of changes.
  125. *
  126. * @param listener Listener to add
  127. */
  128. public void addServerListListener(final ServerListListener listener) {
  129. listeners.add(ServerListListener.class, listener);
  130. }
  131. /**
  132. * Sets the selected item in this model.
  133. *
  134. * @param item Newly selected item
  135. */
  136. public void setSelectedItem(final ServerGroupItem item) {
  137. activeItem = item;
  138. for (ServerListListener listener : listeners.get(
  139. ServerListListener.class)) {
  140. listener.serverGroupChanged(item);
  141. }
  142. }
  143. /**
  144. * Gets the perform description for the selected item.
  145. *
  146. * @return Perform description for the active sever group item
  147. */
  148. public PerformDescription getSelectedItemPerformDescription() {
  149. final PerformDescription perform;
  150. if (activeItem instanceof ServerEntry) {
  151. perform = new PerformDescription(PerformType.SERVER, activeItem
  152. .getName());
  153. } else if (activeItem instanceof ServerGroup
  154. && ((ServerGroup) activeItem).getNetwork() != null) {
  155. perform = new PerformDescription(PerformType.NETWORK,
  156. ((ServerGroup) activeItem).getNetwork());
  157. } else {
  158. perform = null;
  159. }
  160. return perform;
  161. }
  162. /**
  163. * Gets the currently selected item.
  164. *
  165. * @return Currently selected item
  166. */
  167. public ServerGroupItem getSelectedItem() {
  168. return activeItem;
  169. }
  170. /**
  171. * Saves the changes.
  172. *
  173. * @param save Do we need to save changes
  174. */
  175. public void dialogClosed(final boolean save) {
  176. for (ServerListListener listener : listeners.get(
  177. ServerListListener.class)) {
  178. listener.dialogClosed(save);
  179. }
  180. }
  181. /**
  182. * Adds a group to this model.
  183. *
  184. * @param parentGroup Parent group
  185. * @param groupName Group name (not null or empty)
  186. * @param networkName Network name (may be null or empty)
  187. */
  188. public void addGroup(final ServerGroup parentGroup,
  189. final String groupName, final String networkName) {
  190. final ServerGroup sg = new ServerGroup(identityController, groupName);
  191. if (networkName != null && !networkName.isEmpty()) {
  192. sg.setNetwork(networkName);
  193. }
  194. if (parentGroup == null) {
  195. list.addServerGroup(sg);
  196. } else {
  197. parentGroup.addItem(sg);
  198. }
  199. for (ServerListListener listener : listeners.get(ServerListListener.class)) {
  200. listener.serverGroupAdded(parentGroup, sg);
  201. }
  202. }
  203. /**
  204. * Adds a group to this model.
  205. *
  206. * @param parentGroup Parent group
  207. * @param entryName name (not null or empty)
  208. * @param url Valid URI
  209. */
  210. public void addEntry(final ServerGroup parentGroup, final String entryName,
  211. final URI url) {
  212. final ServerGroupItem sg = new ServerEntry(identityController, connectionManager,
  213. parentGroup, entryName, url, null);
  214. parentGroup.addItem(sg);
  215. for (ServerListListener listener : listeners.get(
  216. ServerListListener.class)) {
  217. listener.serverGroupAdded(parentGroup, sg);
  218. }
  219. }
  220. /**
  221. * Forces the server list to save all changes.
  222. */
  223. public void save() {
  224. list.save();
  225. }
  226. }