KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultComboBoxModel


1 /*
2  * @(#)DefaultComboBoxModel.java 1.19 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

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 JavaDoc;
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19
20 import javax.swing.event.*;
21 import javax.swing.plaf.*;
22 import javax.swing.border.*;
23
24 import javax.accessibility.*;
25
26 /**
27  * The default model for combo boxes.
28  *
29  * @version 1.19 05/05/04
30  * @author Arnaud Weber
31  * @author Tom Santos
32  */

33
34 public class DefaultComboBoxModel extends AbstractListModel JavaDoc implements MutableComboBoxModel JavaDoc, Serializable JavaDoc {
35     Vector objects;
36     Object JavaDoc selectedObject;
37
38     /**
39      * Constructs an empty DefaultComboBoxModel object.
40      */

41     public DefaultComboBoxModel() {
42         objects = new Vector();
43     }
44
45     /**
46      * Constructs a DefaultComboBoxModel object initialized with
47      * an array of objects.
48      *
49      * @param items an array of Object objects
50      */

51     public DefaultComboBoxModel(final Object JavaDoc 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     /**
65      * Constructs a DefaultComboBoxModel object initialized with
66      * a vector.
67      *
68      * @param v a Vector object ...
69      */

70     public DefaultComboBoxModel(Vector<?> v) {
71         objects = v;
72
73         if ( getSize() > 0 ) {
74             selectedObject = getElementAt( 0 );
75         }
76     }
77
78     // implements javax.swing.ComboBoxModel
79
/**
80      * Set the value of the selected item. The selected item may be null.
81      * <p>
82      * @param anObject The combo box value or null for no selection.
83      */

84     public void setSelectedItem(Object JavaDoc 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     // implements javax.swing.ComboBoxModel
93
public Object JavaDoc getSelectedItem() {
94         return selectedObject;
95     }
96
97     // implements javax.swing.ListModel
98
public int getSize() {
99         return objects.size();
100     }
101
102     // implements javax.swing.ListModel
103
public Object JavaDoc getElementAt(int index) {
104         if ( index >= 0 && index < objects.size() )
105             return objects.elementAt(index);
106         else
107             return null;
108     }
109
110     /**
111      * Returns the index-position of the specified object in the list.
112      *
113      * @param anObject
114      * @return an int representing the index position, where 0 is
115      * the first position
116      */

117     public int getIndexOf(Object JavaDoc anObject) {
118         return objects.indexOf(anObject);
119     }
120
121     // implements javax.swing.MutableComboBoxModel
122
public void addElement(Object JavaDoc 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     // implements javax.swing.MutableComboBoxModel
131
public void insertElementAt(Object JavaDoc anObject,int index) {
132         objects.insertElementAt(anObject,index);
133         fireIntervalAdded(this, index, index);
134     }
135
136     // implements javax.swing.MutableComboBoxModel
137
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     // implements javax.swing.MutableComboBoxModel
153
public void removeElement(Object JavaDoc anObject) {
154         int index = objects.indexOf(anObject);
155         if ( index != -1 ) {
156             removeElementAt(index);
157         }
158     }
159
160     /**
161      * Empties the list.
162      */

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