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

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