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

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