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

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