01: /*
02: * ListComboBoxModel.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.filter;
13:
14: import java.util.ArrayList;
15: import java.util.List;
16: import javax.swing.AbstractListModel;
17: import javax.swing.ComboBoxModel;
18:
19: /**
20: * A ComboBoxModel backed by a List
21: *
22: * @author support@sql-workbench.net
23: */
24: public class ListComboBoxModel extends AbstractListModel implements
25: ComboBoxModel {
26: private List data = new ArrayList();
27: private Object selectedItem;
28:
29: public ListComboBoxModel() {
30: data = new ArrayList();
31: }
32:
33: public ListComboBoxModel(List<ComparatorListItem> l) {
34: data = l;
35: }
36:
37: public void setData(List<ComparatorListItem> l) {
38: this .data = l;
39: fireContentsChanged(this , -1, -1);
40: }
41:
42: public Object getElementAt(int index) {
43: if (data == null)
44: return null;
45: return data.get(index);
46: }
47:
48: public Object getSelectedItem() {
49: return selectedItem;
50: }
51:
52: public int getSize() {
53: if (data == null)
54: return 0;
55: return data.size();
56: }
57:
58: public int findItemIgnoreCase(String item) {
59: if (item == null)
60: return -1;
61: int size = data.size();
62: for (int i = 0; i < size; i++) {
63: Object o = data.get(i);
64: if (o == null)
65: continue;
66: if (item.equalsIgnoreCase(o.toString()))
67: return i;
68: }
69: return -1;
70: }
71:
72: public void setSelectedItem(Object anItem) {
73: this .selectedItem = anItem;
74: if (selectedItem != null && !selectedItem.equals(anItem)) {
75: selectedItem = anItem;
76: fireContentsChanged(this , -1, -1);
77: }
78: }
79:
80: }
|