Mavenize jSite.
[jSite.git] / src / main / java / de / todesbaum / util / swing / SortedListModel.java
1 /*
2  * jSite - SortedListModel.java - Copyright © 2006–2012 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.util.swing;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.ListIterator;
27
28 import javax.swing.AbstractListModel;
29
30 /**
31  * @param <T>
32  *            The type of the elements
33  * @author David Roden &lt;droden@gmail.com&gt;
34  */
35 public class SortedListModel<T extends Comparable<T>> extends AbstractListModel implements List<T> {
36
37         /** The elements. */
38         private List<T> elements = new ArrayList<T>();
39
40         /**
41          * {@inheritDoc}
42          */
43         public int getSize() {
44                 return size();
45         }
46
47         /**
48          * {@inheritDoc}
49          */
50         public Object getElementAt(int index) {
51                 return elements.get(index);
52         }
53
54         /**
55          * {@inheritDoc}
56          */
57         public void add(int index, T element) {
58                 elements.add(index, element);
59                 Collections.sort(elements);
60                 fireContentsChanged(this, 0, size());
61         }
62
63         /**
64          * {@inheritDoc}
65          */
66         public boolean add(T o) {
67                 boolean result = elements.add(o);
68                 Collections.sort(elements);
69                 fireContentsChanged(this, 0, size());
70                 return result;
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         public boolean addAll(Collection<? extends T> c) {
77                 boolean result = elements.addAll(c);
78                 Collections.sort(elements);
79                 fireContentsChanged(this, 0, size());
80                 return result;
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         public boolean addAll(int index, Collection<? extends T> c) {
87                 boolean result = elements.addAll(index, c);
88                 Collections.sort(elements);
89                 fireContentsChanged(this, 0, size());
90                 return result;
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         public void clear() {
97                 elements.clear();
98                 fireContentsChanged(this, 0, size());
99         }
100
101         /**
102          * {@inheritDoc}
103          */
104         public boolean contains(Object o) {
105                 return elements.contains(o);
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         public boolean containsAll(Collection<?> c) {
112                 return elements.containsAll(c);
113         }
114
115         /**
116          * {@inheritDoc}
117          */
118         @Override
119         public boolean equals(Object o) {
120                 return elements.equals(o);
121         }
122
123         /**
124          * {@inheritDoc}
125          */
126         public T get(int index) {
127                 return elements.get(index);
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         @Override
134         public int hashCode() {
135                 return elements.hashCode();
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         public int indexOf(Object o) {
142                 return elements.indexOf(o);
143         }
144
145         /**
146          * {@inheritDoc}
147          */
148         public boolean isEmpty() {
149                 return elements.isEmpty();
150         }
151
152         /**
153          * {@inheritDoc}
154          */
155         public Iterator<T> iterator() {
156                 return elements.iterator();
157         }
158
159         /**
160          * {@inheritDoc}
161          */
162         public int lastIndexOf(Object o) {
163                 return elements.lastIndexOf(o);
164         }
165
166         /**
167          * {@inheritDoc}
168          */
169         public ListIterator<T> listIterator() {
170                 return elements.listIterator();
171         }
172
173         /**
174          * {@inheritDoc}
175          */
176         public ListIterator<T> listIterator(int index) {
177                 return elements.listIterator(index);
178         }
179
180         /**
181          * {@inheritDoc}
182          */
183         public T remove(int index) {
184                 fireContentsChanged(this, 0, size());
185                 return elements.remove(index);
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         public boolean remove(Object o) {
192                 fireContentsChanged(this, 0, size());
193                 return elements.remove(o);
194         }
195
196         /**
197          * {@inheritDoc}
198          */
199         public boolean removeAll(Collection<?> c) {
200                 fireContentsChanged(this, 0, size());
201                 return elements.removeAll(c);
202         }
203
204         /**
205          * {@inheritDoc}
206          */
207         public boolean retainAll(Collection<?> c) {
208                 fireContentsChanged(this, 0, size());
209                 return elements.retainAll(c);
210         }
211
212         /**
213          * {@inheritDoc}
214          */
215         public T set(int index, T element) {
216                 T result = elements.set(index, element);
217                 Collections.sort(elements);
218                 fireContentsChanged(this, 0, size());
219                 return result;
220         }
221
222         /**
223          * {@inheritDoc}
224          */
225         public int size() {
226                 return elements.size();
227         }
228
229         /**
230          * {@inheritDoc}
231          */
232         public List<T> subList(int fromIndex, int toIndex) {
233                 return elements.subList(fromIndex, toIndex);
234         }
235
236         /**
237          * {@inheritDoc}
238          */
239         public Object[] toArray() {
240                 return elements.toArray();
241         }
242
243         /**
244          * {@inheritDoc}
245          */
246         public <U extends Object> U[] toArray(U[] a) {
247                 return elements.toArray(a);
248         }
249
250 }