Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ObservableListDecorator.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2006-2013 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.util.collections;
  23. import java.util.Collection;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.ListIterator;
  27. /**
  28. * Decorates a {@link List} to add observable functionality.
  29. *
  30. * @param <T> The type of object the list contains
  31. */
  32. public class ObservableListDecorator<T> implements ObservableList<T> {
  33. /** The list being decorated. */
  34. private final List<T> list;
  35. /** The listeners for this list. */
  36. private final ListenerList listeners = new ListenerList();
  37. /**
  38. * Creates a new {@link ObservableListDecorator} which will decorate the
  39. * given list.
  40. *
  41. * @param list The list to be decorated
  42. */
  43. public ObservableListDecorator(final List<T> list) {
  44. this.list = list;
  45. }
  46. /** {@inheritDoc} */
  47. @Override
  48. public void addListListener(final ListObserver listener) {
  49. listeners.add(ListObserver.class, listener);
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public void removeListListener(final ListObserver listener) {
  54. listeners.remove(ListObserver.class, listener);
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public int size() {
  59. return list.size();
  60. }
  61. /** {@inheritDoc} */
  62. @Override
  63. public boolean isEmpty() {
  64. return list.isEmpty();
  65. }
  66. /** {@inheritDoc} */
  67. @Override
  68. public boolean contains(final Object o) {
  69. return list.contains(o);
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public Iterator<T> iterator() {
  74. return list.iterator();
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public Object[] toArray() {
  79. return list.toArray();
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public <T> T[] toArray(final T[] a) {
  84. return list.toArray(a);
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public boolean add(final T e) {
  89. list.add(e);
  90. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  91. list.size() - 1, list.size() - 1);
  92. return true;
  93. }
  94. /** {@inheritDoc} */
  95. @Override
  96. public boolean remove(final Object o) {
  97. final int index = list.indexOf(o);
  98. if (list.remove(o)) {
  99. listeners.getCallable(ListObserver.class).onItemsRemoved(this,
  100. index, index);
  101. return true;
  102. }
  103. return false;
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public boolean containsAll(final Collection<?> c) {
  108. return list.containsAll(c);
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public boolean addAll(final Collection<? extends T> c) {
  113. if (list.addAll(c)) {
  114. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  115. list.size() - c.size(), list.size() - 1);
  116. return true;
  117. }
  118. return false;
  119. }
  120. /** {@inheritDoc} */
  121. @Override
  122. public boolean addAll(final int index, final Collection<? extends T> c) {
  123. if (list.addAll(index, c)) {
  124. listeners.getCallable(ListObserver.class).onItemsAdded(this,
  125. index, index + c.size());
  126. return true;
  127. }
  128. return false;
  129. }
  130. /** {@inheritDoc} */
  131. @Override
  132. public boolean removeAll(final Collection<?> c) {
  133. final int length = list.size();
  134. if (list.removeAll(c)) {
  135. listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
  136. length - 1);
  137. return true;
  138. }
  139. return false;
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public boolean retainAll(final Collection<?> c) {
  144. final int length = list.size();
  145. if (list.retainAll(c)) {
  146. listeners.getCallable(ListObserver.class).onItemsChanged(this, 0,
  147. length - 1);
  148. return true;
  149. }
  150. return false;
  151. }
  152. /** {@inheritDoc} */
  153. @Override
  154. public void clear() {
  155. final int length = list.size();
  156. list.clear();
  157. if (length > 0) {
  158. listeners.getCallable(ListObserver.class).onItemsRemoved(this, 0,
  159. length - 1);
  160. }
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public T get(final int index) {
  165. return list.get(index);
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public T set(final int index, final T element) {
  170. final T res = list.set(index, element);
  171. listeners.getCallable(ListObserver.class).onItemsChanged(this, index, index);
  172. return res;
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public void add(final int index, final T element) {
  177. list.add(index, element);
  178. listeners.getCallable(ListObserver.class).onItemsAdded(this, index, index);
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public T remove(final int index) {
  183. final T res = list.remove(index);
  184. listeners.getCallable(ListObserver.class).onItemsRemoved(this, index, index);
  185. return res;
  186. }
  187. /** {@inheritDoc} */
  188. @Override
  189. public int indexOf(final Object o) {
  190. return list.indexOf(o);
  191. }
  192. /** {@inheritDoc} */
  193. @Override
  194. public int lastIndexOf(final Object o) {
  195. return list.lastIndexOf(o);
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public ListIterator<T> listIterator() {
  200. return list.listIterator();
  201. }
  202. /** {@inheritDoc} */
  203. @Override
  204. public ListIterator<T> listIterator(final int index) {
  205. return list.listIterator(index);
  206. }
  207. /** {@inheritDoc} */
  208. @Override
  209. public List<T> subList(final int fromIndex, final int toIndex) {
  210. return list.subList(fromIndex, toIndex);
  211. }
  212. }