Creating a ListModel, say a DefaultListModel and then having to call addElement in a loop to fill it is annoying. It would be more convenient to be able to use any Collection as the basis for a ListModel. The following attempts to 'play nice' with collections, hence the title. The source is HERE
package net.proteanit.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;
public class CollectionsNiceListModel<T> extends AbstractListModel
implements ListModel, List, MutableComboBoxModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<T> delegate;
private T selectedObject;
public CollectionsNiceListModel() {
super();
delegate = new ArrayList<T>();
}
public CollectionsNiceListModel(int size) {
this();
delegate.ensureCapacity(size);
}
public CollectionsNiceListModel(Collection<T> items) {
this();
delegate.addAll(items);
fireIntervalAdded(this, 0, delegate.size() - 1);
}
public CollectionsNiceListModel(T[] items) {
this();
delegate.addAll(Arrays.asList(items));
fireIntervalAdded(this, 0, delegate.size() - 1);
}
public Object getElementAt(int index) {
return delegate.get(index);
}
public T get(int index) {
return delegate.get(index);
}
public int getSize() {
return delegate.size();
}
@SuppressWarnings("unchecked")
public void addElement(Object obj) {
int index = delegate.size();
delegate.add((T) obj);
fireIntervalAdded(this, index, index);
}
public void removeElementAt(int index) {
delegate.remove(index);
fireIntervalRemoved(this, index, index);
}
@Override
@SuppressWarnings("unchecked")
public void insertElementAt(Object o, int index) {
delegate.add(index, (T) o);
fireIntervalAdded(this, index, index);
}
@Override
public void removeElement(Object o) {
delegate.remove(o);
}
@SuppressWarnings("unchecked")
public void add(int index, Object o) {
delegate.add(index, (T) o);
fireIntervalAdded(this, index, index);
}
@SuppressWarnings("unchecked")
public boolean add(Object o) {
int index = delegate.size();
delegate.add((T) o);
fireIntervalAdded(this, index, index);
return true;
}
@SuppressWarnings("unchecked")
public boolean addAll(Collection c) {
int originalSize = delegate.size();
delegate.addAll(c);
this.fireIntervalAdded(this, originalSize, c.size());
return true;
}
public boolean addAll(int index, Collection c) {
int origIndex = index;
for (Object o : c) {
this.add(index++, o);
}
this.fireIntervalAdded(this, origIndex, c.size());
return true;
}
public void clear() {
int size = delegate.size();
if (size > 0) {
delegate.clear();
this.fireIntervalRemoved(this, 0, size - 1);
}
}
public boolean contains(Object o) {
return delegate.contains(o);
}
public boolean containsAll(Collection c) {
boolean result = true;
for (Iterator iter = c.iterator(); result && iter.hasNext();) {
result = delegate.contains(iter.next());
}
return result;
}
public int indexOf(Object o) {
return delegate.indexOf(o);
}
public boolean isEmpty() {
return delegate.isEmpty();
}
public Iterator iterator() {
return delegate.iterator();
}
public int lastIndexOf(Object o) {
return delegate.lastIndexOf(o);
}
public ListIterator listIterator() {
return delegate.listIterator();
}
public ListIterator listIterator(int index) {
return delegate.listIterator(index);
}
public boolean remove(Object o) {
int index = delegate.indexOf(o);
fireIntervalRemoved(this, index, index);
return delegate.remove(o);
}
public Object remove(int index) {
fireIntervalRemoved(this, index, index);
return delegate.remove(index);
}
public boolean removeAll(Collection c) {
boolean result = false;
for (Iterator iter = c.iterator(); iter.hasNext();) {
Object o = iter.next();
int elementIndex = delegate.indexOf(o);
if (elementIndex > -1) {
boolean removed = delegate.remove(o);
if (removed) {
result = true;
this.fireIntervalRemoved(this, elementIndex, elementIndex);
}
}
}
return result;
}
public boolean retainAll(Collection c) {
/**
* See if 'c' contains each of our delegate's elements. If it's not
* there, remove it
*/
boolean result = false;
for (ListIterator iter = delegate.listIterator(); iter.hasNext();) {
Object o = iter.next();
if (!c.contains(o)) {
System.out.printf("Removing item %s\n", o);
result = true;
iter.remove();
int ix = iter.nextIndex();
this.fireIntervalRemoved(this, ix, ix);
}
}
return result;
}
@SuppressWarnings("unchecked")
public Object set(int index, Object o) {
fireContentsChanged(this, index, index);
return delegate.set(index, (T) o);
}
public int size() {
return delegate.size();
}
public List subList(int fromIndex, int toIndex) {
return delegate.subList(fromIndex, toIndex);
}
public Object[] toArray() {
return delegate.toArray();
}
@SuppressWarnings("unchecked")
public Object[] toArray(Object[] a) {
return delegate.toArray(a);
}
public Object getSelectedItem() {
return selectedObject;
}
@SuppressWarnings("unchecked")
public void setSelectedItem(Object o) {
if (((selectedObject != null) && !selectedObject.equals(o)) ||
((selectedObject == null) && (o != null))) {
selectedObject = (T) o;
fireContentsChanged(this, -1, -1);
}
}
/**
* Convenience method to set the first item selected
*
*/
public void setFirstSelected() {
if (delegate.size() > 0) {
setSelectedItem(delegate.get(0));
}
}
public String toString() {
return delegate.toString();
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;
public class CollectionsNiceListModel<T> extends AbstractListModel
implements ListModel, List, MutableComboBoxModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<T> delegate;
private T selectedObject;
public CollectionsNiceListModel() {
super();
delegate = new ArrayList<T>();
}
public CollectionsNiceListModel(int size) {
this();
delegate.ensureCapacity(size);
}
public CollectionsNiceListModel(Collection<T> items) {
this();
delegate.addAll(items);
fireIntervalAdded(this, 0, delegate.size() - 1);
}
public CollectionsNiceListModel(T[] items) {
this();
delegate.addAll(Arrays.asList(items));
fireIntervalAdded(this, 0, delegate.size() - 1);
}
public Object getElementAt(int index) {
return delegate.get(index);
}
public T get(int index) {
return delegate.get(index);
}
public int getSize() {
return delegate.size();
}
@SuppressWarnings("unchecked")
public void addElement(Object obj) {
int index = delegate.size();
delegate.add((T) obj);
fireIntervalAdded(this, index, index);
}
public void removeElementAt(int index) {
delegate.remove(index);
fireIntervalRemoved(this, index, index);
}
@Override
@SuppressWarnings("unchecked")
public void insertElementAt(Object o, int index) {
delegate.add(index, (T) o);
fireIntervalAdded(this, index, index);
}
@Override
public void removeElement(Object o) {
delegate.remove(o);
}
@SuppressWarnings("unchecked")
public void add(int index, Object o) {
delegate.add(index, (T) o);
fireIntervalAdded(this, index, index);
}
@SuppressWarnings("unchecked")
public boolean add(Object o) {
int index = delegate.size();
delegate.add((T) o);
fireIntervalAdded(this, index, index);
return true;
}
@SuppressWarnings("unchecked")
public boolean addAll(Collection c) {
int originalSize = delegate.size();
delegate.addAll(c);
this.fireIntervalAdded(this, originalSize, c.size());
return true;
}
public boolean addAll(int index, Collection c) {
int origIndex = index;
for (Object o : c) {
this.add(index++, o);
}
this.fireIntervalAdded(this, origIndex, c.size());
return true;
}
public void clear() {
int size = delegate.size();
if (size > 0) {
delegate.clear();
this.fireIntervalRemoved(this, 0, size - 1);
}
}
public boolean contains(Object o) {
return delegate.contains(o);
}
public boolean containsAll(Collection c) {
boolean result = true;
for (Iterator iter = c.iterator(); result && iter.hasNext();) {
result = delegate.contains(iter.next());
}
return result;
}
public int indexOf(Object o) {
return delegate.indexOf(o);
}
public boolean isEmpty() {
return delegate.isEmpty();
}
public Iterator iterator() {
return delegate.iterator();
}
public int lastIndexOf(Object o) {
return delegate.lastIndexOf(o);
}
public ListIterator listIterator() {
return delegate.listIterator();
}
public ListIterator listIterator(int index) {
return delegate.listIterator(index);
}
public boolean remove(Object o) {
int index = delegate.indexOf(o);
fireIntervalRemoved(this, index, index);
return delegate.remove(o);
}
public Object remove(int index) {
fireIntervalRemoved(this, index, index);
return delegate.remove(index);
}
public boolean removeAll(Collection c) {
boolean result = false;
for (Iterator iter = c.iterator(); iter.hasNext();) {
Object o = iter.next();
int elementIndex = delegate.indexOf(o);
if (elementIndex > -1) {
boolean removed = delegate.remove(o);
if (removed) {
result = true;
this.fireIntervalRemoved(this, elementIndex, elementIndex);
}
}
}
return result;
}
public boolean retainAll(Collection c) {
/**
* See if 'c' contains each of our delegate's elements. If it's not
* there, remove it
*/
boolean result = false;
for (ListIterator iter = delegate.listIterator(); iter.hasNext();) {
Object o = iter.next();
if (!c.contains(o)) {
System.out.printf("Removing item %s\n", o);
result = true;
iter.remove();
int ix = iter.nextIndex();
this.fireIntervalRemoved(this, ix, ix);
}
}
return result;
}
@SuppressWarnings("unchecked")
public Object set(int index, Object o) {
fireContentsChanged(this, index, index);
return delegate.set(index, (T) o);
}
public int size() {
return delegate.size();
}
public List subList(int fromIndex, int toIndex) {
return delegate.subList(fromIndex, toIndex);
}
public Object[] toArray() {
return delegate.toArray();
}
@SuppressWarnings("unchecked")
public Object[] toArray(Object[] a) {
return delegate.toArray(a);
}
public Object getSelectedItem() {
return selectedObject;
}
@SuppressWarnings("unchecked")
public void setSelectedItem(Object o) {
if (((selectedObject != null) && !selectedObject.equals(o)) ||
((selectedObject == null) && (o != null))) {
selectedObject = (T) o;
fireContentsChanged(this, -1, -1);
}
}
/**
* Convenience method to set the first item selected
*
*/
public void setFirstSelected() {
if (delegate.size() > 0) {
setSelectedItem(delegate.get(0));
}
}
public String toString() {
return delegate.toString();
}
}