001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import java.util.Arrays;
021: import java.util.Vector;
022:
023: @SuppressWarnings("unchecked")
024: public class DefaultComboBoxModel extends AbstractListModel implements
025: MutableComboBoxModel {
026: private static final long serialVersionUID = 3906372640380892983L;
027:
028: private Vector listData;
029:
030: private Object selection;
031:
032: public DefaultComboBoxModel() {
033: super ();
034: listData = new Vector();
035: }
036:
037: public DefaultComboBoxModel(Object[] items) {
038: super ();
039: listData = new Vector();
040: listData.addAll(Arrays.asList(items));
041: if (items.length > 0) {
042: selection = items[0];
043: }
044: }
045:
046: public DefaultComboBoxModel(Vector<?> items) {
047: listData = items;
048: if (items.size() > 0) {
049: selection = items.get(0);
050: }
051: }
052:
053: public void addElement(Object element) {
054: listData.add(element);
055: fireIntervalAdded(this , listData.size() - 1,
056: listData.size() - 1);
057: if (getSelectedItem() == null) {
058: setSelectedItem(element);
059: }
060: }
061:
062: public Object getElementAt(int index) {
063: return index < getSize() ? listData.get(index) : null;
064: }
065:
066: public int getIndexOf(Object element) {
067: return listData.indexOf(element);
068: }
069:
070: public int getSize() {
071: return listData.size();
072: }
073:
074: public void insertElementAt(Object element, int index) {
075: listData.insertElementAt(element, index);
076: fireIntervalAdded(this , index, index);
077: }
078:
079: public void removeAllElements() {
080: int size = getSize();
081: if (size > 0) {
082: listData.clear();
083: fireIntervalRemoved(this , 0, size - 1);
084: }
085: selection = null;
086: }
087:
088: public void removeElement(Object element) {
089: int index = getIndexOf(element);
090: if (index != -1) {
091: removeElementAt(index);
092: }
093: }
094:
095: public void removeElementAt(int index) {
096: Object removingElement = getElementAt(index);
097: listData.remove(index);
098: if (selection == removingElement || selection != null
099: && selection.equals(removingElement)) {
100: if (index == 0 && getSize() > 0) {
101: setSelectedItem(getElementAt(0));
102: } else if (index > 0) {
103: setSelectedItem(getElementAt(index - 1));
104: } else {
105: setSelectedItem(null);
106: }
107: }
108: fireIntervalRemoved(this , index, index);
109: }
110:
111: public Object getSelectedItem() {
112: return selection;
113: }
114:
115: public void setSelectedItem(Object element) {
116: if (element != null && !element.equals(selection)
117: || element == null && selection != null) {
118: selection = element;
119: fireContentsChanged(this , -1, -1);
120: }
121: }
122: }
|