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>
35 public class SortedListModel extends AbstractListModel implements List {
37 private List elements = new ArrayList();
42 public int getSize() {
49 public Object getElementAt(int index) {
50 return elements.get(index);
56 public void add(int index, Object element) {
57 elements.add(index, element);
58 Collections.sort(elements);
59 fireContentsChanged(this, 0, size());
65 public boolean add(Object o) {
66 boolean result = elements.add(o);
67 Collections.sort(elements);
68 fireContentsChanged(this, 0, size());
75 public boolean addAll(Collection c) {
76 boolean result = elements.addAll(c);
77 Collections.sort(elements);
78 fireContentsChanged(this, 0, size());
85 public boolean addAll(int index, Collection c) {
86 boolean result = elements.addAll(index, c);
87 Collections.sort(elements);
88 fireContentsChanged(this, 0, size());
97 fireContentsChanged(this, 0, size());
103 public boolean contains(Object o) {
104 return elements.contains(o);
110 public boolean containsAll(Collection c) {
111 return elements.containsAll(c);
117 public boolean equals(Object o) {
118 return elements.equals(o);
124 public Object get(int index) {
125 return elements.get(index);
131 public int hashCode() {
132 return elements.hashCode();
138 public int indexOf(Object o) {
139 return elements.indexOf(o);
145 public boolean isEmpty() {
146 return elements.isEmpty();
152 public Iterator iterator() {
153 return elements.iterator();
159 public int lastIndexOf(Object o) {
160 return elements.lastIndexOf(o);
166 public ListIterator listIterator() {
167 return elements.listIterator();
173 public ListIterator listIterator(int index) {
174 return elements.listIterator(index);
180 public Object remove(int index) {
181 fireContentsChanged(this, 0, size());
182 return elements.remove(index);
188 public boolean remove(Object o) {
189 fireContentsChanged(this, 0, size());
190 return elements.remove(o);
196 public boolean removeAll(Collection c) {
197 fireContentsChanged(this, 0, size());
198 return elements.removeAll(c);
204 public boolean retainAll(Collection c) {
205 fireContentsChanged(this, 0, size());
206 return elements.retainAll(c);
212 public Object set(int index, Object element) {
213 Object result = elements.set(index, element);
214 Collections.sort(elements);
215 fireContentsChanged(this, 0, size());
223 return elements.size();
229 public List subList(int fromIndex, int toIndex) {
230 return elements.subList(fromIndex, toIndex);
236 public Object[] toArray() {
237 return elements.toArray();
243 public Object[] toArray(Object[] a) {
244 return elements.toArray(a);