Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

NicklistListModel.java 5.7KB

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