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