選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GenericListModel.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2006-2015 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. this.list = Collections.synchronizedList(new ArrayList<>());
  43. }
  44. /**
  45. * Instantiates a list model containing the specified list items.
  46. *
  47. * @param list Data to be included in the model
  48. */
  49. public GenericListModel(final Collection<T> list) {
  50. this.list = Collections.synchronizedList(new ArrayList<>(list));
  51. }
  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 specified 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 specified 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. final boolean success = list.remove(obj);
  153. if (index >= 0) {
  154. fireIntervalRemoved(this, index, index);
  155. }
  156. return success;
  157. }
  158. /**
  159. * Replaces the element at the specified index with a new object.
  160. *
  161. * @param oldValue Old value
  162. * @param newValue New value
  163. */
  164. public void replace(final T oldValue, final T newValue) {
  165. final int index = list.indexOf(oldValue);
  166. list.set(index, newValue);
  167. fireContentsChanged(this, index, index);
  168. }
  169. /**
  170. * Clears this model of all items.
  171. */
  172. public void clear() {
  173. final int lastIndex = list.size() - 1;
  174. list.clear();
  175. if (lastIndex >= 0) {
  176. fireIntervalRemoved(this, 0, lastIndex);
  177. }
  178. }
  179. @Override
  180. public String toString() {
  181. return list.toString();
  182. }
  183. /**
  184. * Removes the specified range of items from the list.
  185. *
  186. * @param start Index to start removing
  187. * @param end Index to stop removing
  188. */
  189. public void removeRange(final int start, final int end) {
  190. if (start > end) {
  191. throw new IllegalArgumentException("start must be greater than "
  192. + "or equal to end");
  193. }
  194. for (int i = end; i >= start; i--) {
  195. list.remove(i);
  196. }
  197. fireIntervalRemoved(this, start, end);
  198. }
  199. /**
  200. * Returns a list of items in this model
  201. *
  202. * @return List available in this model
  203. */
  204. public List<T> elements() {
  205. return Collections.unmodifiableList(list);
  206. }
  207. /**
  208. * Adds all the objects in the specified collection to this list.
  209. *
  210. * @param collection Collection to add
  211. */
  212. public void addAll(final Collection<T> collection) {
  213. list.addAll(collection);
  214. }
  215. /**
  216. * Adds all the objects in the specified collection to this list at the specified index.
  217. *
  218. * @param index Index to add the items
  219. * @param collection Collection to add
  220. */
  221. public void addAll(final int index, final Collection<T> collection) {
  222. list.addAll(index, collection);
  223. }
  224. @Override
  225. public T getElementAt(final int index) {
  226. return get(index);
  227. }
  228. }