GNU Classpath (0.20) | |
Frames | No Frames |
1: /* DefaultComboBoxModel.java -- 2: Copyright (C) 2002, 2004, 2005 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: import javax.swing.event.ListDataEvent; 45: 46: 47: /** 48: * A model that stores a list of elements and a selected item (which may be 49: * <code>null</code>). Changes to the model are signalled to listeners using 50: * {@link ListDataEvent}. This model is designed for use by the 51: * {@link JComboBox} component. 52: * 53: * @author Andrew Selkirk 54: * @author Olga Rodimina 55: * @author Robert Schuster 56: */ 57: public class DefaultComboBoxModel extends AbstractListModel 58: implements MutableComboBoxModel, Serializable 59: { 60: private static final long serialVersionUID = 6698657703676921904L; 61: 62: /** 63: * Storage for the elements in the model's list. 64: */ 65: private Vector list; 66: 67: /** 68: * The selected item (<code>null</code> indicates no selection). 69: */ 70: private Object selectedItem = null; 71: 72: /** 73: * Creates a new model, initially empty. 74: */ 75: public DefaultComboBoxModel() 76: { 77: list = new Vector(); 78: } 79: 80: /** 81: * Creates a new model and initializes its item list to the values in the 82: * given array. The selected item is set to the first item in the array, or 83: * <code>null</code> if the array length is zero. 84: * 85: * @param items an array containing items for the model (<code>null</code> 86: * not permitted). 87: * 88: * @throws NullPointerException if <code>items</code> is <code>null</code>. 89: */ 90: public DefaultComboBoxModel(Object[] items) 91: { 92: list = new Vector(Arrays.asList(items)); 93: if (list.size() > 0) 94: selectedItem = list.get(0); 95: } 96: 97: /** 98: * Creates a new model and initializes its item list to the values in the 99: * given vector. The selected item is set to the first item in the vector, 100: * or <code>null</code> if the vector length is zero. 101: * 102: * @param vector a vector containing items for the model (<code>null</code> 103: * not permitted). 104: * 105: * @throws NullPointerException if <code>vector</code> is <code>null</code>. 106: */ 107: public DefaultComboBoxModel(Vector vector) 108: { 109: this.list = vector; 110: if (vector.size() > 0) 111: selectedItem = vector.get(0); 112: } 113: 114: /** 115: * Adds an element to the model's item list and sends a {@link ListDataEvent} 116: * to all registered listeners. If the new element is the first item added 117: * to the list, it is set as the selected item. 118: * 119: * @param object item to add to the model's item list. 120: */ 121: public void addElement(Object object) 122: { 123: list.add(object); 124: fireIntervalAdded(this, list.size() - 1, list.size() - 1); 125: if (list.size() == 1) 126: setSelectedItem(object); 127: } 128: 129: /** 130: * Removes the element at the specified index from the model's item list 131: * and sends a {@link ListDataEvent} to all registered listeners. If the 132: * element removed was the selected item, then the preceding element becomes 133: * the new selected item (or the next element, if there is no preceding 134: * element). 135: * 136: * @param index the index of the item to remove. 137: * 138: * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of 139: * bounds. 140: */ 141: public void removeElementAt(int index) 142: { 143: int selected = getIndexOf(selectedItem); 144: list.remove(index); 145: if (selected == index) // choose a new selected item 146: { 147: if (selected > 0) 148: selectedItem = getElementAt(selected - 1); 149: else 150: selectedItem = getElementAt(selected); 151: } 152: fireIntervalRemoved(this, index, index); 153: } 154: 155: /** 156: * Adds an element at the specified index in the model's item list 157: * and sends a {@link ListDataEvent} to all registered listeners. 158: * 159: * @param object element to insert 160: * @param index index specifing position in the list where given element 161: * should be inserted. 162: * 163: * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of 164: * bounds. 165: * 166: * @see #addElement(Object) 167: */ 168: public void insertElementAt(Object object, int index) 169: { 170: list.insertElementAt(object, index); 171: fireIntervalAdded(this, index, index); 172: } 173: 174: /** 175: * Removes an element from the model's item list and sends a 176: * {@link ListDataEvent} to all registered listeners. If the item to be 177: * removed is the current selected item, a new selected item will be set. 178: * If the element is not found in the model's item list, this method does 179: * nothing. 180: * 181: * @param object the element to remove. 182: */ 183: public void removeElement(Object object) 184: { 185: int index = getIndexOf(object); 186: if (index != -1) 187: removeElementAt(index); 188: } 189: 190: /** 191: * Removes all the items from the model's item list, resets and selected item 192: * to <code>null</code>, and sends a {@link ListDataEvent} to all registered 193: * listeners. 194: */ 195: public void removeAllElements() 196: { 197: selectedItem = null; 198: int size = getSize(); 199: if (size > 0) 200: { 201: list.clear(); 202: fireIntervalRemoved(this, 0, size - 1); 203: } 204: } 205: 206: /** 207: * Returns the number of items in the model's item list. 208: * 209: * @return The number of items in the model's item list. 210: */ 211: public int getSize() 212: { 213: return list.size(); 214: } 215: 216: /** 217: * Sets the selected item for the model and sends a {@link ListDataEvent} to 218: * all registered listeners. The start and end index of the event is set to 219: * -1 to indicate the model's selection has changed, and not its contents. 220: * 221: * @param object the new selected item (<code>null</code> permitted). 222: */ 223: public void setSelectedItem(Object object) 224: { 225: if (selectedItem == null) 226: { 227: if (object == null) 228: return; 229: } 230: else 231: { 232: if (selectedItem.equals(object)) 233: return; 234: } 235: selectedItem = object; 236: fireContentsChanged(this, -1, -1); 237: } 238: 239: /** 240: * Returns the selected item. 241: * 242: * @return The selected item (possibly <code>null</code>). 243: */ 244: public Object getSelectedItem() 245: { 246: return selectedItem; 247: } 248: 249: /** 250: * Returns the element at the specified index in the model's item list. 251: * 252: * @param index the element index. 253: * 254: * @return The element at the specified index in the model's item list, or 255: * <code>null</code> if the <code>index</code> is outside the bounds 256: * of the list. 257: */ 258: public Object getElementAt(int index) 259: { 260: if (index < 0 || index >= list.size()) 261: return null; 262: return list.elementAt(index); 263: } 264: 265: /** 266: * Returns the index of the specified element in the model's item list. 267: * 268: * @param object the element. 269: * 270: * @return The index of the specified element in the model's item list. 271: */ 272: public int getIndexOf(Object object) 273: { 274: return list.indexOf(object); 275: } 276: }
GNU Classpath (0.20) |