3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.util.swing;
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;
29 import javax.swing.AbstractListModel;
33 * @author David Roden <droden@gmail.com>
34 * @version $Id: SortedListModel.java 338 2006-03-20 15:40:48Z bombe $
36 public class SortedListModel extends AbstractListModel implements List {
38 private List elements = new ArrayList();
43 public int getSize() {
50 public Object getElementAt(int index) {
51 return elements.get(index);
57 public void add(int index, Object element) {
58 elements.add(index, element);
59 Collections.sort(elements);
60 fireContentsChanged(this, 0, size());
66 public boolean add(Object o) {
67 boolean result = elements.add(o);
68 Collections.sort(elements);
69 fireContentsChanged(this, 0, size());
76 public boolean addAll(Collection c) {
77 boolean result = elements.addAll(c);
78 Collections.sort(elements);
79 fireContentsChanged(this, 0, size());
86 public boolean addAll(int index, Collection c) {
87 boolean result = elements.addAll(index, c);
88 Collections.sort(elements);
89 fireContentsChanged(this, 0, size());
98 fireContentsChanged(this, 0, size());
104 public boolean contains(Object o) {
105 return elements.contains(o);
111 public boolean containsAll(Collection c) {
112 return elements.containsAll(c);
118 public boolean equals(Object o) {
119 return elements.equals(o);
125 public Object get(int index) {
126 return elements.get(index);
132 public int hashCode() {
133 return elements.hashCode();
139 public int indexOf(Object o) {
140 return elements.indexOf(o);
146 public boolean isEmpty() {
147 return elements.isEmpty();
153 public Iterator iterator() {
154 return elements.iterator();
160 public int lastIndexOf(Object o) {
161 return elements.lastIndexOf(o);
167 public ListIterator listIterator() {
168 return elements.listIterator();
174 public ListIterator listIterator(int index) {
175 return elements.listIterator(index);
181 public Object remove(int index) {
182 fireContentsChanged(this, 0, size());
183 return elements.remove(index);
189 public boolean remove(Object o) {
190 fireContentsChanged(this, 0, size());
191 return elements.remove(o);
197 public boolean removeAll(Collection c) {
198 fireContentsChanged(this, 0, size());
199 return elements.removeAll(c);
205 public boolean retainAll(Collection c) {
206 fireContentsChanged(this, 0, size());
207 return elements.retainAll(c);
213 public Object set(int index, Object element) {
214 Object result = elements.set(index, element);
215 Collections.sort(elements);
216 fireContentsChanged(this, 0, size());
224 return elements.size();
230 public List subList(int fromIndex, int toIndex) {
231 return elements.subList(fromIndex, toIndex);
237 public Object[] toArray() {
238 return elements.toArray();
244 public Object[] toArray(Object[] a) {
245 return elements.toArray(a);