GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* DefaultComboBoxModel.java -- 2: Copyright (C) 2002, 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package javax.swing; 39: 40: import java.io.Serializable; 41: import java.util.Arrays; 42: import java.util.Vector; 43: 44: 45: /** 46: * The default implementation of {@link MutableComboBoxModel}. 47: * This model keeps track 48: * of elements contained in the JComboBox as well as the current combo box 49: * selection. Whenever selection in the JComboBox changes, the ComboBoxModel 50: * will fire ListDataEvents to ComboBox's ListDataListeners. 51: * 52: * @author Andrew Selkirk 53: * @author Olga Rodimina 54: * @author Robert Schuster 55: */ 56: public class DefaultComboBoxModel extends AbstractListModel 57: implements MutableComboBoxModel, Serializable 58: { 59: private static final long serialVersionUID = 6698657703676921904L; 60: 61: /** 62: * List containing items in the combo box 63: */ 64: private Vector list; 65: 66: /** 67: * Currently selected item in the combo box list 68: */ 69: private Object selectedItem = null; 70: 71: /** 72: * Constructor DefaultComboBoxModel. Create empty JComboBox. 73: */ 74: public DefaultComboBoxModel() 75: { 76: list = new Vector(); 77: } 78: 79: /** 80: * Constructs new DefaultComboBoxModel object and initializes its item list 81: * to values in the given array. 82: * 83: * @param items array containing items of the combo box. 84: */ 85: public DefaultComboBoxModel(Object[] items) 86: { 87: list = new Vector(Arrays.asList(items)); 88: } 89: 90: /** 91: * Consturcts new DefaultComboBoxModel object and initializes its item list 92: * to values in the given vector. 93: * 94: * @param vector Vector containing items for this combo box. 95: */ 96: public DefaultComboBoxModel(Vector vector) 97: { 98: this.list = vector; 99: } 100: 101: /** 102: * This method adds element to the combo box list. It fires ListDataEvent 103: * indicating that component was added to the combo box to all of the 104: * JComboBox's registered ListDataListeners. 105: * 106: * @param object item to add to the combo box list 107: */ 108: public void addElement(Object object) 109: { 110: list.add(object); 111: fireIntervalAdded(this, list.size() - 1, list.size()); 112: } 113: 114: /** 115: * This method removes element at the specified index from the combo box 116: * list. It fires ListDataEvent indicating that component was removed from 117: * the combo box list to all of the JComboBox's registered 118: * ListDataListeners. 119: * 120: * @param index index specifying location of the element to remove in the 121: * combo box list. 122: */ 123: public void removeElementAt(int index) 124: { 125: list.remove(index); 126: fireIntervalRemoved(this, index, index); 127: } 128: 129: /** 130: * This method inserts given object to the combo box list at the specified 131: * index. It fires ListDataEvent indicating that component was inserted to 132: * the combo box list to all of the JComboBox's registered 133: * ListDataListeners. 134: * 135: * @param object element to insert 136: * @param index index specifing position in the list where given element 137: * should be inserted. 138: */ 139: public void insertElementAt(Object object, int index) 140: { 141: list.insertElementAt(object, index); 142: fireIntervalAdded(this, index, index); 143: } 144: 145: /** 146: * Removes given object from the combo box list. It fires ListDataEvent 147: * indicating that component was removed from the combo box list to all of 148: * the JComboBox's registered ListDataListeners. 149: * 150: * @param object Element that will be removed from the combo box list 151: */ 152: public void removeElement(Object object) 153: { 154: int index = getIndexOf(object); 155: if (index != -1) 156: removeElementAt(index); 157: } 158: 159: /** 160: * Removes all the items from the JComboBox's item list. It fires 161: * ListDataEvent indicating that all the elements were removed from the 162: * combo box list to all of the JComboBox's registered ListDataListeners. 163: */ 164: public void removeAllElements() 165: { 166: list.clear(); 167: int listSize = getSize(); 168: fireIntervalAdded(this, 0, listSize); 169: } 170: 171: /** 172: * Returns number of items in the combo box list 173: * 174: * @return number of items in the combo box list 175: */ 176: public int getSize() 177: { 178: return list.size(); 179: } 180: 181: /** 182: * Selects given object in the combo box list. This method fires 183: * ListDataEvent to all registered ListDataListeners of the JComboBox. The 184: * start and end index of the event is set to -1 to indicate combo box's 185: * selection has changed, and not its contents. 186: * 187: * <p>If the given object is not contained in the combo box list then nothing 188: * happens.</p> 189: * 190: * @param object item to select in the JComboBox 191: */ 192: public void setSelectedItem(Object object) 193: { 194: 195: // Updates the selected item only if the given object 196: // is null or in the list (this is how the JDK behaves). 197: if(object == null || list.contains(object)) { 198: selectedItem = object; 199: fireContentsChanged(this, -1, -1); 200: } 201: 202: } 203: 204: /** 205: * Returns currently selected item in the combo box list 206: * 207: * @return currently selected item in the combo box list 208: */ 209: public Object getSelectedItem() 210: { 211: return selectedItem; 212: } 213: 214: /** 215: * Returns element in the combo box list located at the given index 216: * 217: * @param index specifying location of the element in the list 218: * 219: * @return return element in the combo box list located at the given index 220: */ 221: public Object getElementAt(int index) 222: { 223: return list.elementAt(index); 224: } 225: 226: /** 227: * Returns index of the specified object in the combo box list. 228: * 229: * @param object element to look for in the combo box list . 230: * 231: * @return Index specifying position of the specified element in combo box 232: * list. 233: */ 234: public int getIndexOf(Object object) 235: { 236: return list.indexOf(object); 237: } 238: }
GNU Classpath (0.18) |