Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ServerListModel.java 8.1KB

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