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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. *
  3. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.addons.ui_swing.components;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import javax.swing.AbstractListModel;
  29. /**
  30. * Generic list model, closely following the DefaultListModel API.
  31. *
  32. * @param <T> Generic type of the list
  33. */
  34. public class GenericListModel<T> extends AbstractListModel {
  35. /**
  36. * A version number for this class. It should be changed whenever the class
  37. * structure is changed (or anything else that would prevent serialized
  38. * objects being unserialized with the new class).
  39. */
  40. private static final long serialVersionUID = -4227892376992714545L;
  41. /** Data stored in list. */
  42. private final List<T> list;
  43. /**
  44. * Instantiates an empty generic list model.
  45. */
  46. public GenericListModel() {
  47. super();
  48. this.list = Collections.synchronizedList(new ArrayList<T>());
  49. }
  50. /**
  51. * Instantiates a list model containing the specified list items.
  52. *
  53. * @param list Data to be included in the model
  54. */
  55. public GenericListModel(final List<T> list) {
  56. super();
  57. this.list = Collections.synchronizedList(new ArrayList<T>(list));
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public int getSize() {
  62. return list.size();
  63. }
  64. /**
  65. * Returns the specified index in the list.
  66. *
  67. * @param index Index of the item to return
  68. *
  69. * @return Item at index
  70. */
  71. public T get(final int index) {
  72. return list.get(index);
  73. }
  74. /**
  75. * Checks whether the list mode is empty.
  76. *
  77. * @return true iif the is the list if empty
  78. */
  79. public boolean isEmpty() {
  80. return list.isEmpty();
  81. }
  82. /**
  83. * Checks whether this list model contains the specified object.
  84. *
  85. * @param object Object to check for in the model
  86. *
  87. * @return true iif the model object is in this model
  88. */
  89. public boolean contains(final T object) {
  90. return list.contains(object);
  91. }
  92. /**
  93. * Checks the first index of the speficied option.
  94. *
  95. * @param object Object to check the index of
  96. *
  97. * @return first index of the object or -1 if not found
  98. */
  99. public int indexOf(final T object) {
  100. return list.indexOf(object);
  101. }
  102. /**
  103. * Checks the last index of the speficied option.
  104. *
  105. * @param object Object to check the index of
  106. *
  107. * @return last index of the object or -1 if not found
  108. */
  109. public int lastIndexOf(final T object) {
  110. return list.lastIndexOf(object);
  111. }
  112. /**
  113. * Sets the object at the specified index to be the specified object.
  114. *
  115. * @param index Index of the object
  116. * @param object Object to set
  117. */
  118. public void set(final int index, final T object) {
  119. list.set(index, object);
  120. fireContentsChanged(this, index, index);
  121. }
  122. /**
  123. * Removes the object at the specified index.
  124. *
  125. * @param index Index to remove
  126. */
  127. public void remove(final int index) {
  128. list.remove(index);
  129. fireIntervalRemoved(this, index, index);
  130. }
  131. /**
  132. * Adds the specified object at the specified index.
  133. *
  134. * @param index Index to insert object at
  135. * @param object Object to insert
  136. */
  137. public void add(final int index, final T object) {
  138. list.add(index, object);
  139. fireIntervalAdded(this, index, index);
  140. }
  141. /**
  142. * Adds the specified object.
  143. *
  144. * @param object Object to add
  145. */
  146. public void add(final T object) {
  147. final int index = list.size();
  148. list.add(object);
  149. fireIntervalAdded(this, index, index);
  150. }
  151. /**
  152. * Removes the specified object.
  153. *
  154. * @param obj Object to remove
  155. *
  156. * @return true iif object was removed
  157. */
  158. public boolean remove(final T obj) {
  159. final int index = indexOf(obj);
  160. final boolean succes = list.remove(obj);
  161. if (index >= 0) {
  162. fireIntervalRemoved(this, index, index);
  163. }
  164. return succes;
  165. }
  166. /**
  167. * Clears this model of all items.
  168. */
  169. public void clear() {
  170. final int lastIndex = list.size() - 1;
  171. list.clear();
  172. if (lastIndex >= 0) {
  173. fireIntervalRemoved(this, 0, lastIndex);
  174. }
  175. }
  176. /** {@inheritDoc} */
  177. @Override
  178. public String toString() {
  179. return list.toString();
  180. }
  181. /**
  182. * Removes the specified range of items from the list.
  183. *
  184. * @param start Index to start removing
  185. * @param end Index to stop removing
  186. */
  187. public void removeRange(final int start, final int end) {
  188. if (start > end) {
  189. throw new IllegalArgumentException("start must be greater than "
  190. + "or equal to end");
  191. }
  192. for (int i = end; i >= start; i--) {
  193. list.remove(i);
  194. }
  195. fireIntervalRemoved(this, start, end);
  196. }
  197. /**
  198. * Adds all the objects in the specified collection to this list.
  199. *
  200. * @param collection Collection to add
  201. */
  202. public void addAll(final Collection<T> collection) {
  203. list.addAll(collection);
  204. }
  205. /**
  206. * Adds all the objects in the specified collection to this list at the
  207. * specified index.
  208. *
  209. * @param index Index to add the items
  210. * @param collection Collection to add
  211. */
  212. public void addAll(final int index, final Collection<T> collection) {
  213. list.addAll(index, collection);
  214. }
  215. /** {@inheritDoc} */
  216. @Override
  217. public Object getElementAt(final int index) {
  218. return get(index);
  219. }
  220. }