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.

GenericListModel.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.google.common.collect.Ordering;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import javax.swing.AbstractListModel;
  24. /**
  25. * Generic list model, closely following the DefaultListModel API.
  26. *
  27. * @param <T> Generic type of the list
  28. */
  29. public class GenericListModel<T> extends AbstractListModel<T> {
  30. /** A version number for this class. */
  31. private static final long serialVersionUID = -4227892376992714545L;
  32. /** Data stored in list. */
  33. private final List<T> list;
  34. /**
  35. * Instantiates an empty generic list model.
  36. */
  37. public GenericListModel() {
  38. this.list = Collections.synchronizedList(new ArrayList<>());
  39. }
  40. /**
  41. * Instantiates a list model containing the specified list items.
  42. *
  43. * @param list Data to be included in the model
  44. */
  45. public GenericListModel(final Collection<T> list) {
  46. this.list = Collections.synchronizedList(new ArrayList<>(list));
  47. this.list.sort(Ordering.usingToString());
  48. }
  49. @Override
  50. public int getSize() {
  51. return list.size();
  52. }
  53. /**
  54. * Returns the specified index in the list.
  55. *
  56. * @param index Index of the item to return
  57. *
  58. * @return Item at index
  59. */
  60. public T get(final int index) {
  61. return list.get(index);
  62. }
  63. /**
  64. * Checks whether the list mode is empty.
  65. *
  66. * @return true iif the is the list if empty
  67. */
  68. public boolean isEmpty() {
  69. return list.isEmpty();
  70. }
  71. /**
  72. * Checks whether this list model contains the specified object.
  73. *
  74. * @param object Object to check for in the model
  75. *
  76. * @return true iif the model object is in this model
  77. */
  78. public boolean contains(final T object) {
  79. return list.contains(object);
  80. }
  81. /**
  82. * Checks the first index of the specified option.
  83. *
  84. * @param object Object to check the index of
  85. *
  86. * @return first index of the object or -1 if not found
  87. */
  88. public int indexOf(final T object) {
  89. return list.indexOf(object);
  90. }
  91. /**
  92. * Checks the last index of the specified option.
  93. *
  94. * @param object Object to check the index of
  95. *
  96. * @return last index of the object or -1 if not found
  97. */
  98. public int lastIndexOf(final T object) {
  99. return list.lastIndexOf(object);
  100. }
  101. /**
  102. * Sets the object at the specified index to be the specified object.
  103. *
  104. * @param index Index of the object
  105. * @param object Object to set
  106. */
  107. public void set(final int index, final T object) {
  108. list.set(index, object);
  109. fireContentsChanged(this, index, index);
  110. }
  111. /**
  112. * Removes the object at the specified index.
  113. *
  114. * @param index Index to remove
  115. */
  116. public void remove(final int index) {
  117. list.remove(index);
  118. fireIntervalRemoved(this, index, index);
  119. }
  120. /**
  121. * Adds the specified object at the specified index.
  122. *
  123. * @param index Index to insert object at
  124. * @param object Object to insert
  125. */
  126. public void add(final int index, final T object) {
  127. list.add(index, object);
  128. fireIntervalAdded(this, index, index);
  129. }
  130. /**
  131. * Adds the specified object.
  132. *
  133. * @param object Object to add
  134. */
  135. public void add(final T object) {
  136. final int index = list.size();
  137. list.add(object);
  138. this.list.sort(Ordering.usingToString());
  139. fireIntervalAdded(this, index, index);
  140. }
  141. /**
  142. * Removes the specified object.
  143. *
  144. * @param obj Object to remove
  145. *
  146. * @return true iif object was removed
  147. */
  148. public boolean remove(final T obj) {
  149. final int index = indexOf(obj);
  150. final boolean success = list.remove(obj);
  151. if (index >= 0) {
  152. fireIntervalRemoved(this, index, index);
  153. }
  154. return success;
  155. }
  156. /**
  157. * Replaces the element at the specified index with a new object.
  158. *
  159. * @param oldValue Old value
  160. * @param newValue New value
  161. */
  162. public void replace(final T oldValue, final T newValue) {
  163. final int index = list.indexOf(oldValue);
  164. list.set(index, newValue);
  165. this.list.sort(Ordering.usingToString());
  166. fireContentsChanged(this, index, index);
  167. }
  168. /**
  169. * Clears this model of all items.
  170. */
  171. public void clear() {
  172. final int lastIndex = list.size() - 1;
  173. list.clear();
  174. if (lastIndex >= 0) {
  175. fireIntervalRemoved(this, 0, lastIndex);
  176. }
  177. }
  178. @Override
  179. public String toString() {
  180. return list.toString();
  181. }
  182. /**
  183. * Removes the specified range of items from the list.
  184. *
  185. * @param start Index to start removing
  186. * @param end Index to stop removing
  187. */
  188. public void removeRange(final int start, final int end) {
  189. if (start > end) {
  190. throw new IllegalArgumentException("start must be greater than "
  191. + "or equal to end");
  192. }
  193. for (int i = end; i >= start; i--) {
  194. list.remove(i);
  195. }
  196. fireIntervalRemoved(this, start, end);
  197. }
  198. /**
  199. * Returns a list of items in this model
  200. *
  201. * @return List available in this model
  202. */
  203. public List<T> elements() {
  204. return Collections.unmodifiableList(list);
  205. }
  206. /**
  207. * Adds all the objects in the specified collection to this list.
  208. *
  209. * @param collection Collection to add
  210. */
  211. public void addAll(final Collection<T> collection) {
  212. final int lastIndex = list.size() - 1;
  213. list.addAll(collection);
  214. this.list.sort(Ordering.usingToString());
  215. fireIntervalAdded(this, lastIndex < 0 ? 0 : lastIndex, collection.size());
  216. }
  217. /**
  218. * Adds all the objects in the specified collection to this list at the specified index.
  219. *
  220. * @param index Index to add the items
  221. * @param collection Collection to add
  222. */
  223. public void addAll(final int index, final Collection<T> collection) {
  224. list.addAll(index, collection);
  225. this.list.sort(Ordering.usingToString());
  226. fireIntervalAdded(this, index, collection.size());
  227. }
  228. @Override
  229. public T getElementAt(final int index) {
  230. return get(index);
  231. }
  232. }