1 package com.explosion.utilities;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.swing.AbstractListModel;
27 import javax.swing.MutableComboBoxModel;
28
29 /***
30 * @author Stephen
31 * Created on Apr 17, 2004
32 * Changes the Sun DefaultComboBoxModel to work with Lists instead of Lists
33 */
34 public class ListComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
35 List objects;
36 Object selectedObject;
37
38 /***
39 * Constructs an empty NewComboBoxModel object.
40 */
41 public ListComboBoxModel() {
42 objects = new ArrayList();
43 }
44
45 /***
46 * Constructs a NewComboBoxModel object initialized with
47 * an array of objects.
48 *
49 * @param items an array of Object objects
50 */
51 public ListComboBoxModel(final Object items[]) {
52 objects = new ArrayList();
53
54 int i,c;
55 for ( i=0,c=items.length;i<c;i++ )
56 objects.add(items[i]);
57
58 if ( getSize() > 0 ) {
59 selectedObject = getElementAt( 0 );
60 }
61 }
62
63 /***
64 * Constructs a NewComboBoxModel object initialized with
65 * a vector.
66 *
67 * @param v a List object ...
68 */
69 public ListComboBoxModel(List l) {
70 objects = l;
71
72 if ( getSize() > 0 ) {
73 selectedObject = getElementAt( 0 );
74 }
75 }
76
77
78 /***
79 * Set the value of the selected item. The selected item may be null.
80 * <p>
81 * @param anObject The combo box value or null for no selection.
82 */
83 public void setSelectedItem(Object anObject) {
84 if ((selectedObject != null && !selectedObject.equals( anObject )) ||
85 selectedObject == null && anObject != null) {
86 selectedObject = anObject;
87 fireContentsChanged(this, -1, -1);
88 }
89 }
90
91
92 public Object getSelectedItem() {
93 return selectedObject;
94 }
95
96
97 public int getSize() {
98 return objects.size();
99 }
100
101
102 public Object getElementAt(int index) {
103 if ( index >= 0 && index < objects.size() )
104 return objects.get(index);
105 else
106 return null;
107 }
108
109 /***
110 * Returns the index-position of the specified object in the list.
111 *
112 * @param anObject
113 * @return an int representing the index position, where 0 is
114 * the first position
115 */
116 public int getIndexOf(Object anObject) {
117 return objects.indexOf(anObject);
118 }
119
120
121 public void addElement(Object anObject) {
122 objects.add(anObject);
123 fireIntervalAdded(this,objects.size()-1, objects.size()-1);
124 if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
125 setSelectedItem( anObject );
126 }
127 }
128
129
130 public void insertElementAt(Object anObject,int index) {
131 objects.add(index, anObject);
132 fireIntervalAdded(this, index, index);
133 }
134
135
136 public void removeElementAt(int index) {
137 if ( getElementAt( index ) == selectedObject ) {
138 if ( index == 0 ) {
139 setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
140 }
141 else {
142 setSelectedItem( getElementAt( index - 1 ) );
143 }
144 }
145
146 objects.remove(index);
147
148 fireIntervalRemoved(this, index, index);
149 }
150
151
152 public void removeElement(Object anObject) {
153 int index = objects.indexOf(anObject);
154 if ( index != -1 ) {
155 removeElementAt(index);
156 }
157 }
158
159 /***
160 * Empties the list.
161 */
162 public void removeAllElements() {
163 if ( objects.size() > 0 ) {
164 int firstIndex = 0;
165 int lastIndex = objects.size() - 1;
166 objects.clear();
167 selectedObject = null;
168 fireIntervalRemoved(this, firstIndex, lastIndex);
169 } else {
170 selectedObject = null;
171 }
172 }
173 }
174