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

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