1 7 package javax.swing; 8 9 import java.beans.*; 10 import java.util.*; 11 12 import java.awt.*; 13 import java.awt.event.*; 14 15 import java.io.Serializable ; 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectInputStream ; 18 import java.io.IOException ; 19 20 import javax.swing.event.*; 21 import javax.swing.plaf.*; 22 import javax.swing.border.*; 23 24 import javax.accessibility.*; 25 26 33 34 public class DefaultComboBoxModel extends AbstractListModel implements MutableComboBoxModel , Serializable { 35 Vector objects; 36 Object selectedObject; 37 38 41 public DefaultComboBoxModel() { 42 objects = new Vector(); 43 } 44 45 51 public DefaultComboBoxModel(final Object items[]) { 52 objects = new Vector(); 53 objects.ensureCapacity( items.length ); 54 55 int i,c; 56 for ( i=0,c=items.length;i<c;i++ ) 57 objects.addElement(items[i]); 58 59 if ( getSize() > 0 ) { 60 selectedObject = getElementAt( 0 ); 61 } 62 } 63 64 70 public DefaultComboBoxModel(Vector<?> v) { 71 objects = v; 72 73 if ( getSize() > 0 ) { 74 selectedObject = getElementAt( 0 ); 75 } 76 } 77 78 84 public void setSelectedItem(Object anObject) { 85 if ((selectedObject != null && !selectedObject.equals( anObject )) || 86 selectedObject == null && anObject != null) { 87 selectedObject = anObject; 88 fireContentsChanged(this, -1, -1); 89 } 90 } 91 92 public Object getSelectedItem() { 94 return selectedObject; 95 } 96 97 public int getSize() { 99 return objects.size(); 100 } 101 102 public Object getElementAt(int index) { 104 if ( index >= 0 && index < objects.size() ) 105 return objects.elementAt(index); 106 else 107 return null; 108 } 109 110 117 public int getIndexOf(Object anObject) { 118 return objects.indexOf(anObject); 119 } 120 121 public void addElement(Object anObject) { 123 objects.addElement(anObject); 124 fireIntervalAdded(this,objects.size()-1, objects.size()-1); 125 if ( objects.size() == 1 && selectedObject == null && anObject != null ) { 126 setSelectedItem( anObject ); 127 } 128 } 129 130 public void insertElementAt(Object anObject,int index) { 132 objects.insertElementAt(anObject,index); 133 fireIntervalAdded(this, index, index); 134 } 135 136 public void removeElementAt(int index) { 138 if ( getElementAt( index ) == selectedObject ) { 139 if ( index == 0 ) { 140 setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) ); 141 } 142 else { 143 setSelectedItem( getElementAt( index - 1 ) ); 144 } 145 } 146 147 objects.removeElementAt(index); 148 149 fireIntervalRemoved(this, index, index); 150 } 151 152 public void removeElement(Object anObject) { 154 int index = objects.indexOf(anObject); 155 if ( index != -1 ) { 156 removeElementAt(index); 157 } 158 } 159 160 163 public void removeAllElements() { 164 if ( objects.size() > 0 ) { 165 int firstIndex = 0; 166 int lastIndex = objects.size() - 1; 167 objects.removeAllElements(); 168 selectedObject = null; 169 fireIntervalRemoved(this, firstIndex, lastIndex); 170 } else { 171 selectedObject = null; 172 } 173 } 174 } 175 | Popular Tags |