001: /*
002: * DynamicComboBoxModel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.util.Vector;
025: import javax.swing.DefaultComboBoxModel;
026:
027: /* ----------------------------------------------------------
028: * CVS NOTE: Changes to the CVS repository prior to the
029: * release of version 3.0.0beta1 has meant a
030: * resetting of CVS revision numbers.
031: * ----------------------------------------------------------
032: */
033:
034: /**
035: * Simple combo box model that allows complete removal
036: * and resetting of all values.
037: *
038: * @author Takis Diakoumis
039: * @version $Revision: 1.6 $
040: * @date $Date: 2006/05/14 06:56:07 $
041: */
042: public class DynamicComboBoxModel extends DefaultComboBoxModel {
043:
044: /** the selected object */
045: private Object selectedObject;
046:
047: /** the items vector */
048: private Vector objects;
049:
050: /** Creates a new instance of DynamicComboBoxModel */
051: public DynamicComboBoxModel() {
052: }
053:
054: /** Creates a new instance of DynamicComboBoxModel */
055: public DynamicComboBoxModel(Vector objects) {
056: this .objects = objects;
057: if (objects != null && !objects.isEmpty()) {
058: setSelectedItem(objects.elementAt(0));
059: }
060: }
061:
062: public void setElements(Object[] items) {
063: if (objects != null && !objects.isEmpty()) {
064: removeAllElements();
065: /*
066: int size = objects.size();
067: objects.clear();
068: fireIntervalRemoved(this, 0, size - 1);
069: */
070: } else {
071: objects = new Vector(items.length);
072: }
073: for (int i = 0; i < items.length; i++) {
074: objects.add(items[i]);
075: }
076: int size = objects.size();
077: fireContentsChanged(this , 0, (size > 0) ? size - 1 : 0);
078: setSelectedItem(objects.get(0));
079: }
080:
081: public void setElements(Vector elements) {
082: if (objects != null && !objects.isEmpty()) {
083: removeAllElements();
084: /*
085: int size = objects.size();
086: objects.clear();
087: if (size > 0) {
088: fireIntervalRemoved(this, 0, size - 1);
089: } else {
090: fireIntervalRemoved(this, 0, 0);
091: }
092: */
093: }
094: objects = elements;
095: int size = objects.size();
096: //fireContentsChanged(this, 0, (size > 0) ? size - 1 : 0);
097: fireContentsChanged(this , -1, -1);
098: if (size > 0) {
099: setSelectedItem(objects.get(0));
100: }
101: }
102:
103: /**
104: * Set the value of the selected item. The selected item may be null.
105: * <p>
106: * @param anObject The combo box value or null for no selection.
107: */
108: public void setSelectedItem(Object anObject) {
109: if ((selectedObject != null && !selectedObject.equals(anObject))
110: || selectedObject == null && anObject != null) {
111: selectedObject = anObject;
112: fireContentsChanged(this , -1, -1);
113: }
114: }
115:
116: // implements javax.swing.ComboBoxModel
117: public Object getSelectedItem() {
118: return selectedObject;
119: }
120:
121: // implements javax.swing.ListModel
122: public int getSize() {
123: if (objects == null) {
124: return 0;
125: }
126: return objects.size();
127: }
128:
129: // implements javax.swing.ListModel
130: public Object getElementAt(int index) {
131: if (index >= 0 && index < objects.size())
132: return objects.elementAt(index);
133: else
134: return null;
135: }
136:
137: /**
138: * Returns the index-position of the specified object in the list.
139: *
140: * @param anObject
141: * @return an int representing the index position, where 0 is
142: * the first position
143: */
144: public int getIndexOf(Object anObject) {
145: return objects.indexOf(anObject);
146: }
147:
148: // implements javax.swing.MutableComboBoxModel
149: public void addElement(Object anObject) {
150: if (objects == null) {
151: objects = new Vector();
152: }
153: objects.addElement(anObject);
154: fireIntervalAdded(this , objects.size() - 1, objects.size() - 1);
155: if (objects.size() == 1 && selectedObject == null
156: && anObject != null) {
157: setSelectedItem(anObject);
158: }
159: }
160:
161: public void contentsChanged() {
162: fireContentsChanged(this , -1, -1);
163: }
164:
165: // implements javax.swing.MutableComboBoxModel
166: public void insertElementAt(Object anObject, int index) {
167: objects.insertElementAt(anObject, index);
168: fireIntervalAdded(this , index, index);
169: }
170:
171: // implements javax.swing.MutableComboBoxModel
172: public void removeElementAt(int index) {
173: if (getElementAt(index) == selectedObject) {
174: if (index == 0) {
175: setSelectedItem(getSize() == 1 ? null
176: : getElementAt(index + 1));
177: } else {
178: setSelectedItem(getElementAt(index - 1));
179: }
180: }
181:
182: objects.removeElementAt(index);
183: fireIntervalRemoved(this , index, index);
184: }
185:
186: // implements javax.swing.MutableComboBoxModel
187: public void removeElement(Object anObject) {
188: int index = objects.indexOf(anObject);
189: if (index != -1) {
190: removeElementAt(index);
191: }
192: }
193:
194: /**
195: * Empties the list.
196: */
197: public void removeAllElements() {
198: if (objects == null) {
199: return;
200: }
201: if (objects.size() > 0) {
202: int firstIndex = 0;
203: int lastIndex = objects.size() - 1;
204: objects.removeAllElements();
205: selectedObject = null;
206: fireIntervalRemoved(this , firstIndex, lastIndex);
207: } else {
208: selectedObject = null;
209: }
210: }
211:
212: /*
213: public void addElement(Object object) {
214: items.add(object);
215: int index = items.indexOf(object);
216: fireContentsChanged(this, index, index);
217: }
218:
219: public void removeAllElements() {
220: items.clear();
221: }
222:
223: public Object getElementAt(int index) {
224: if (index >= 0 && index < items.size()) {
225: return items.elementAt(index);
226: } else {
227: return null;
228: }
229: }
230:
231: public void removeElement(Object object) {
232: items.remove(object);
233: }
234:
235: public int getIndexOf(Object object) {
236: return items.indexOf(object);
237: }
238:
239: public int getSize() {
240: if (items == null) {
241: return 0;
242: }
243: return items.size();
244: }
245: */
246: }
|