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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2006-2007 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.ui;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import javax.swing.AbstractListModel;
  27. import com.dmdirc.Config;
  28. import com.dmdirc.parser.ChannelClientInfo;
  29. /**
  30. * Stores and provides means to modify nicklist data for a channel.
  31. */
  32. public final class NicklistListModel extends AbstractListModel {
  33. /**
  34. * A version number for this class. It should be changed whenever the class
  35. * structure is changed (or anything else that would prevent serialized
  36. * objects being unserialized with the new class).
  37. */
  38. private static final long serialVersionUID = 1;
  39. /**
  40. * stores the nicknames to be shown in this list.
  41. */
  42. private final List<ChannelClientInfo> nicknames;
  43. /**
  44. * Creates a new empty model.
  45. */
  46. public NicklistListModel() {
  47. super();
  48. nicknames = new ArrayList<ChannelClientInfo>();
  49. }
  50. /**
  51. * Creates a new model and initiliases it with the data provided.
  52. * @param newNicknames list of nicknames used for initialisation
  53. */
  54. public NicklistListModel(final List<ChannelClientInfo> newNicknames) {
  55. super();
  56. this.nicknames = newNicknames;
  57. this.sort();
  58. }
  59. /**
  60. * Returns the size of the current nicklist.
  61. * @return nicklist size
  62. */
  63. public int getSize() {
  64. return nicknames.size();
  65. }
  66. /**
  67. * Returns the element at the specified place in the nicklist.
  68. * @param index index of nick required
  69. * @return nicklist entry requested
  70. */
  71. public ChannelClientInfo getElementAt(final int index) {
  72. return nicknames.get(index);
  73. }
  74. /**
  75. * Sorts the nicklist based on settings in the Config, only needed if
  76. * the sort.
  77. * method changes
  78. */
  79. public void sort() {
  80. final boolean sortByMode = Config.getOptionBool("ui", "sortByMode");
  81. final boolean sortByCase = Config.getOptionBool("ui", "sortByCase");
  82. Collections.sort(nicknames,
  83. new NicklistComparator(sortByMode, sortByCase));
  84. this.rerender();
  85. }
  86. /**
  87. * Replaces the entire nicklist with the arraylist specified.
  88. * @param clients replacement nicklist
  89. * @return boolean success
  90. */
  91. public boolean replace(final List<ChannelClientInfo> clients) {
  92. boolean returnValue = false;
  93. nicknames.clear();
  94. returnValue = nicknames.addAll(clients);
  95. this.sort();
  96. return returnValue;
  97. }
  98. /**
  99. * Adds the specified client to the nicklist.
  100. * @param client client to add to the nicklist
  101. * @return boolean success
  102. */
  103. public boolean add(final ChannelClientInfo client) {
  104. boolean returnValue = false;
  105. returnValue = nicknames.add(client);
  106. this.sort();
  107. return returnValue;
  108. }
  109. /**
  110. * Removes the specified client from the nicklist.
  111. * @param client client to remove
  112. * @return boolean success
  113. */
  114. public boolean remove(final ChannelClientInfo client) {
  115. boolean returnValue;
  116. returnValue = nicknames.remove(client);
  117. this.rerender();
  118. return returnValue;
  119. }
  120. /**
  121. * Removes the specified index from the nicklist.
  122. * @param index index to remove
  123. * @return ChannelClientInfo client removed
  124. */
  125. public ChannelClientInfo remove(final int index) {
  126. ChannelClientInfo returnValue;
  127. returnValue = nicknames.remove(index);
  128. this.rerender();
  129. return returnValue;
  130. }
  131. /**
  132. *Fires the model changed event forcing the model to re-render.
  133. */
  134. public void rerender() {
  135. this.fireContentsChanged(this, 0, nicknames.size());
  136. }
  137. }