001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.swing;
014:
015: import java.io.Serializable;
016: import java.util.Vector;
017:
018: import javax.swing.AbstractListModel;
019: import javax.swing.MutableComboBoxModel;
020:
021: public class ExtendedComboBoxModel extends AbstractListModel implements
022: MutableComboBoxModel, Serializable {
023: Vector objects;
024: Object selectedObject;
025:
026: /**
027: * Constructs an empty DefaultComboBoxModel object.
028: */
029: public ExtendedComboBoxModel() {
030: objects = new Vector();
031: }
032:
033: /**
034: * Constructs a DefaultComboBoxModel object initialized with
035: * an array of objects.
036: *
037: * @param items an array of Object objects
038: */
039: public ExtendedComboBoxModel(final Object items[]) {
040: objects = new Vector();
041: objects.ensureCapacity(items.length);
042:
043: int i, c;
044: for (i = 0, c = items.length; i < c; i++)
045: objects.addElement(items[i]);
046:
047: if (getSize() > 0) {
048: selectedObject = getElementAt(0);
049: }
050: }
051:
052: /**
053: * Constructs a DefaultComboBoxModel object initialized with
054: * a vector.
055: *
056: * @param v a Vector object ...
057: */
058: public ExtendedComboBoxModel(Vector<?> v) {
059: objects = v;
060:
061: if (getSize() > 0) {
062: selectedObject = getElementAt(0);
063: }
064: }
065:
066: // implements javax.swing.ComboBoxModel
067: /**
068: * Set the value of the selected item. The selected item may be null.
069: * <p>
070: * @param anObject The combo box value or null for no selection.
071: */
072: public void setSelectedItem(Object anObject) {
073: if ((selectedObject != null && !selectedObject.equals(anObject))
074: || selectedObject == null && anObject != null) {
075: selectedObject = anObject;
076: fireContentsChanged(this , -1, -1);
077: }
078: }
079:
080: // implements javax.swing.ComboBoxModel
081: public Object getSelectedItem() {
082: return selectedObject;
083: }
084:
085: // implements javax.swing.ListModel
086: public int getSize() {
087: return objects.size();
088: }
089:
090: // implements javax.swing.ListModel
091: public Object getElementAt(int index) {
092: if (index >= 0 && index < objects.size())
093: return objects.elementAt(index);
094: else
095: return null;
096: }
097:
098: /**
099: * Returns the index-position of the specified object in the list.
100: *
101: * @param anObject
102: * @return an int representing the index position, where 0 is
103: * the first position
104: */
105: public int getIndexOf(Object anObject) {
106: return objects.indexOf(anObject);
107: }
108:
109: // implements javax.swing.MutableComboBoxModel
110: public void addElement(Object anObject) {
111: objects.addElement(anObject);
112: fireIntervalAdded(this , objects.size() - 1, objects.size() - 1);
113: if (objects.size() == 1 && selectedObject == null
114: && anObject != null) {
115: setSelectedItem(anObject);
116: }
117: }
118:
119: // implements javax.swing.MutableComboBoxModel
120: public void insertElementAt(Object anObject, int index) {
121: objects.insertElementAt(anObject, index);
122: fireIntervalAdded(this , index, index);
123: }
124:
125: // implements javax.swing.MutableComboBoxModel
126: public void removeElementAt(int index) {
127: if (getElementAt(index) == selectedObject) {
128: if (index == 0) {
129: setSelectedItem(getSize() == 1 ? null
130: : getElementAt(index + 1));
131: } else {
132: setSelectedItem(getElementAt(index - 1));
133: }
134: }
135:
136: objects.removeElementAt(index);
137:
138: fireIntervalRemoved(this , index, index);
139: }
140:
141: // implements javax.swing.MutableComboBoxModel
142: public void removeElement(Object anObject) {
143: int index = objects.indexOf(anObject);
144: if (index != -1) {
145: removeElementAt(index);
146: }
147: }
148:
149: /**
150: * Empties the list.
151: */
152: public void removeAllElements() {
153: if (objects.size() > 0) {
154: int firstIndex = 0;
155: int lastIndex = objects.size() - 1;
156: objects.removeAllElements();
157: selectedObject = null;
158: fireIntervalRemoved(this , firstIndex, lastIndex);
159: } else {
160: selectedObject = null;
161: }
162: }
163:
164: public void setElementAt(Object obj, int index) {
165: objects.setElementAt(obj, index);
166: fireContentsChanged(this, index, index);
167: }
168: }
|