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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components;
  18. import com.dmdirc.interfaces.GroupChatUser;
  19. import com.dmdirc.config.provider.AggregateConfigProvider;
  20. import com.dmdirc.config.provider.ConfigChangeListener;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import javax.swing.AbstractListModel;
  26. /** Stores and provides means to modify nicklist data for a channel. */
  27. public final class NicklistListModel extends AbstractListModel<GroupChatUser> implements
  28. ConfigChangeListener {
  29. /** A version number for this class. */
  30. private static final long serialVersionUID = 1;
  31. /** stores the nicknames to be shown in this list. */
  32. private final List<GroupChatUser> nicknames;
  33. /** Config manager. */
  34. private final AggregateConfigProvider config;
  35. /** Sort by mode? */
  36. private boolean sortByMode;
  37. /** Sort by case? */
  38. private boolean sortByCase;
  39. /**
  40. * Creates a new empty model.
  41. *
  42. * @param config Config manager
  43. */
  44. public NicklistListModel(final AggregateConfigProvider config) {
  45. this(config, Collections.synchronizedList(new ArrayList<>()));
  46. }
  47. /**
  48. * Creates a new model and initiliases it with the data provided.
  49. *
  50. * @param config Config manager
  51. * @param newNicknames list of nicknames used for initialisation
  52. */
  53. public NicklistListModel(final AggregateConfigProvider config,
  54. final List<GroupChatUser> newNicknames) {
  55. this.config = config;
  56. sortByMode = config.getOptionBool("nicklist", "sortByMode");
  57. sortByCase = config.getOptionBool("nicklist", "sortByCase");
  58. config.addChangeListener("nicklist", "sortByMode", this);
  59. config.addChangeListener("nicklist", "sortByCase", this);
  60. nicknames = Collections.synchronizedList(newNicknames);
  61. sort();
  62. }
  63. /**
  64. * Returns the size of the current nicklist.
  65. *
  66. * @return nicklist size
  67. */
  68. @Override
  69. public int getSize() {
  70. return nicknames.size();
  71. }
  72. /**
  73. * Returns the element at the specified place in the nicklist.
  74. *
  75. * @param index index of nick required
  76. *
  77. * @return nicklist entry requested
  78. */
  79. @Override
  80. public GroupChatUser getElementAt(final int index) {
  81. return nicknames.get(index);
  82. }
  83. /**
  84. * Sorts the nicklist based on settings in the Config.
  85. */
  86. public void sort() {
  87. synchronized (nicknames) {
  88. nicknames.sort(new NicklistComparator(sortByMode, sortByCase));
  89. }
  90. rerender();
  91. }
  92. /**
  93. * Replaces the entire nicklist with the arraylist specified.
  94. *
  95. * @param clients replacement nicklist
  96. *
  97. * @return boolean success
  98. */
  99. public boolean replace(final Collection<GroupChatUser> clients) {
  100. nicknames.clear();
  101. nicknames.addAll(clients);
  102. sort();
  103. return true;
  104. }
  105. /**
  106. * Adds the specified client to the nicklist.
  107. *
  108. * @param client client to add to the nicklist
  109. *
  110. * @return boolean success
  111. */
  112. public boolean add(final GroupChatUser client) {
  113. nicknames.add(client);
  114. sort();
  115. return true;
  116. }
  117. /**
  118. * Removes the specified client from the nicklist.
  119. *
  120. * @param client client to remove
  121. *
  122. * @return boolean success
  123. */
  124. public boolean remove(final GroupChatUser client) {
  125. final boolean returnValue = nicknames.remove(client);
  126. rerender();
  127. return returnValue;
  128. }
  129. /**
  130. * Removes the specified index from the nicklist.
  131. *
  132. * @param index index to remove
  133. *
  134. * @return ChannelClientInfo client removed
  135. */
  136. public GroupChatUser remove(final int index) {
  137. final GroupChatUser returnValue = nicknames.remove(index);
  138. rerender();
  139. return returnValue;
  140. }
  141. /**
  142. * Fires the model changed event forcing the model to re-render.
  143. */
  144. public void rerender() {
  145. fireContentsChanged(this, 0, nicknames.size());
  146. }
  147. @Override
  148. public void configChanged(final String domain, final String key) {
  149. switch (key) {
  150. case "sortByMode":
  151. sortByMode = config.getOptionBool("nicklist", "sortByMode");
  152. break;
  153. case "sortByCase":
  154. sortByCase = config.getOptionBool("nicklist", "sortByCase");
  155. break;
  156. }
  157. sort();
  158. }
  159. }