683368c9911989acb28a1049ef937c511abf5589
[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         @Override
120         public boolean equals(Object o) {
121                 return elements.equals(o);
122         }
123
124         /**
125          * {@inheritDoc}
126          */
127         public T get(int index) {
128                 return elements.get(index);
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         @Override
135         public int hashCode() {
136                 return elements.hashCode();
137         }
138
139         /**
140          * {@inheritDoc}
141          */
142         public int indexOf(Object o) {
143                 return elements.indexOf(o);
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         public boolean isEmpty() {
150                 return elements.isEmpty();
151         }
152
153         /**
154          * {@inheritDoc}
155          */
156         public Iterator<T> iterator() {
157                 return elements.iterator();
158         }
159
160         /**
161          * {@inheritDoc}
162          */
163         public int lastIndexOf(Object o) {
164                 return elements.lastIndexOf(o);
165         }
166
167         /**
168          * {@inheritDoc}
169          */
170         public ListIterator<T> listIterator() {
171                 return elements.listIterator();
172         }
173
174         /**
175          * {@inheritDoc}
176          */
177         public ListIterator<T> listIterator(int index) {
178                 return elements.listIterator(index);
179         }
180
181         /**
182          * {@inheritDoc}
183          */
184         public T remove(int index) {
185                 fireContentsChanged(this, 0, size());
186                 return elements.remove(index);
187         }
188
189         /**
190          * {@inheritDoc}
191          */
192         public boolean remove(Object o) {
193                 fireContentsChanged(this, 0, size());
194                 return elements.remove(o);
195         }
196
197         /**
198          * {@inheritDoc}
199          */
200         public boolean removeAll(Collection<?> c) {
201                 fireContentsChanged(this, 0, size());
202                 return elements.removeAll(c);
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         public boolean retainAll(Collection<?> c) {
209                 fireContentsChanged(this, 0, size());
210                 return elements.retainAll(c);
211         }
212
213         /**
214          * {@inheritDoc}
215          */
216         public T set(int index, T element) {
217                 T result = elements.set(index, element);
218                 Collections.sort(elements);
219                 fireContentsChanged(this, 0, size());
220                 return result;
221         }
222
223         /**
224          * {@inheritDoc}
225          */
226         public int size() {
227                 return elements.size();
228         }
229
230         /**
231          * {@inheritDoc}
232          */
233         public List<T> subList(int fromIndex, int toIndex) {
234                 return elements.subList(fromIndex, toIndex);
235         }
236
237         /**
238          * {@inheritDoc}
239          */
240         public Object[] toArray() {
241                 return elements.toArray();
242         }
243
244         /**
245          * {@inheritDoc}
246          */
247         public <U extends Object> U[] toArray(U[] a) {
248                 return elements.toArray(a);
249         }
250
251 }