AbstractCollectionComboBoxModel — specify list type

This commit is contained in:
Vladimir Krivosheev
2014-01-29 17:57:37 +01:00
parent 4428d76b1f
commit 9f28f9d29f
2 changed files with 17 additions and 12 deletions
@@ -24,25 +24,30 @@ import java.util.List;
/**
* @author traff
*/
public abstract class AbstractCollectionComboBoxModel extends AbstractListModel implements ComboBoxModel {
private Object mySelection;
public abstract class AbstractCollectionComboBoxModel<T> extends AbstractListModel implements ComboBoxModel {
private T mySelection;
public AbstractCollectionComboBoxModel(Object selection) {
public AbstractCollectionComboBoxModel(@Nullable T selection) {
mySelection = selection;
}
@Override
public int getSize() {
return getItems().size();
}
public Object getElementAt(final int index) {
@Override
public T getElementAt(final int index) {
return getItems().get(index);
}
public void setSelectedItem(@Nullable final Object anItem) {
mySelection = anItem;
@Override
public void setSelectedItem(@Nullable Object anItem) {
//noinspection unchecked
mySelection = (T)anItem;
}
@Override
@Nullable
public Object getSelectedItem() {
return mySelection;
@@ -52,10 +57,10 @@ public abstract class AbstractCollectionComboBoxModel extends AbstractListModel
super.fireContentsChanged(this, -1, -1);
}
public boolean contains(Object item) {
public boolean contains(T item) {
return getItems().contains(item);
}
@NotNull
abstract protected List getItems();
abstract protected List<T> getItems();
}
@@ -23,16 +23,16 @@ import java.util.List;
/**
* @author traff
*/
public class MutableCollectionComboBoxModel<T> extends AbstractCollectionComboBoxModel {
public class MutableCollectionComboBoxModel<T> extends AbstractCollectionComboBoxModel<T> {
private List<T> myItems;
public MutableCollectionComboBoxModel(@NotNull List<T> items, @Nullable Object selection) {
public MutableCollectionComboBoxModel(@NotNull List<T> items, @Nullable T selection) {
super(selection);
myItems = items;
}
public MutableCollectionComboBoxModel(List<T> items) {
public MutableCollectionComboBoxModel(@NotNull List<T> items) {
super(items.isEmpty() ? null : items.get(0));
myItems = items;
}
@@ -43,7 +43,7 @@ public class MutableCollectionComboBoxModel<T> extends AbstractCollectionComboBo
return myItems;
}
public void update(List<T> items) {
public void update(@NotNull List<T> items) {
myItems = items;
super.update();
}