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.

NicklistListModel.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.ui_swing.components;
  23. import com.dmdirc.config.ConfigManager;
  24. import com.dmdirc.interfaces.ConfigChangeListener;
  25. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import javax.swing.AbstractListModel;
  31. /** Stores and provides means to modify nicklist data for a channel. */
  32. public final class NicklistListModel extends AbstractListModel implements
  33. ConfigChangeListener {
  34. /**
  35. * A version number for this class. It should be changed whenever the class
  36. * structure is changed (or anything else that would prevent serialized
  37. * objects being unserialized with the new class).
  38. */
  39. private static final long serialVersionUID = 1;
  40. /** stores the nicknames to be shown in this list. */
  41. private final List<ChannelClientInfo> nicknames;
  42. /** Config manager. */
  43. private ConfigManager config;
  44. /** Sort by mode? */
  45. private boolean sortByMode;
  46. /** Sort by case? */
  47. private boolean sortByCase;
  48. /**
  49. * Creates a new empty model.
  50. *
  51. * @param config Config manager
  52. */
  53. public NicklistListModel(final ConfigManager config) {
  54. this(config, Collections.synchronizedList(
  55. new ArrayList<ChannelClientInfo>()));
  56. }
  57. /**
  58. * Creates a new model and initiliases it with the data provided.
  59. *
  60. * @param config Config manager
  61. * @param newNicknames list of nicknames used for initialisation
  62. */
  63. public NicklistListModel(final ConfigManager config,
  64. final List<ChannelClientInfo> newNicknames) {
  65. super();
  66. this.config = config;
  67. sortByMode = config.getOptionBool("nicklist", "sortByMode");
  68. sortByCase = config.getOptionBool("nicklist", "sortByCase");
  69. config.addChangeListener("nicklist", "sortByMode", this);
  70. config.addChangeListener("nicklist", "sortByCase", this);
  71. nicknames = Collections.synchronizedList(newNicknames);
  72. sort();
  73. }
  74. /**
  75. * Returns the size of the current nicklist.
  76. * @return nicklist size
  77. */
  78. @Override
  79. public int getSize() {
  80. return nicknames.size();
  81. }
  82. /**
  83. * Returns the element at the specified place in the nicklist.
  84. *
  85. * @param index index of nick required
  86. *
  87. * @return nicklist entry requested
  88. */
  89. @Override
  90. public ChannelClientInfo getElementAt(final int index) {
  91. return nicknames.get(index);
  92. }
  93. /**
  94. * Sorts the nicklist based on settings in the Config.
  95. */
  96. public void sort() {
  97. synchronized (nicknames) {
  98. Collections.sort(nicknames,
  99. new NicklistComparator(sortByMode, sortByCase));
  100. }
  101. rerender();
  102. }
  103. /**
  104. * Replaces the entire nicklist with the arraylist specified.
  105. *
  106. * @param clients replacement nicklist
  107. *
  108. * @return boolean success
  109. */
  110. public boolean replace(final Collection<ChannelClientInfo> clients) {
  111. nicknames.clear();
  112. nicknames.addAll(clients);
  113. sort();
  114. return true;
  115. }
  116. /**
  117. * Adds the specified client to the nicklist.
  118. *
  119. * @param client client to add to the nicklist
  120. *
  121. * @return boolean success
  122. */
  123. public boolean add(final ChannelClientInfo client) {
  124. nicknames.add(client);
  125. sort();
  126. return true;
  127. }
  128. /**
  129. * Removes the specified client from the nicklist.
  130. *
  131. * @param client client to remove
  132. *
  133. * @return boolean success
  134. */
  135. public boolean remove(final ChannelClientInfo client) {
  136. boolean returnValue;
  137. returnValue = nicknames.remove(client);
  138. rerender();
  139. return returnValue;
  140. }
  141. /**
  142. * Removes the specified index from the nicklist.
  143. *
  144. * @param index index to remove
  145. *
  146. * @return ChannelClientInfo client removed
  147. */
  148. public ChannelClientInfo remove(final int index) {
  149. ChannelClientInfo returnValue;
  150. returnValue = nicknames.remove(index);
  151. rerender();
  152. return returnValue;
  153. }
  154. /**
  155. * Fires the model changed event forcing the model to re-render.
  156. */
  157. public void rerender() {
  158. fireContentsChanged(this, 0, nicknames.size());
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public void configChanged(final String domain, final String key) {
  163. if ("sortByMode".equals(key)) {
  164. sortByMode = config.getOptionBool("nicklist", "sortByMode");
  165. } else if ("sortByCase".equals(key)) {
  166. sortByCase = config.getOptionBool("nicklist", "sortByCase");
  167. }
  168. sort();
  169. }
  170. }