Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

NicklistListModel.java 5.9KB

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