Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

GenericListModel.java 7.1KB

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