001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.awt.*;
008: import java.awt.event.*;
009: import java.util.*;
010: import java.io.*;
011:
012: import javax.swing.*;
013:
014: import com.javelin.swinglets.plaf.*;
015:
016: /**
017: * SComboBox defines a drop down list of options. Initially no
018: * option is selected. Only one option can be selected.
019: * <P>
020: * The SComboBox uses the Swing ComboBoxModel.
021: *
022: * @author Robin Sharp
023: */
024:
025: public class SComboBox extends SComponent {
026: /**
027: * Creates a SComboBox using the ComboBoxModel.
028: */
029: public SComboBox(ComboBoxModel model) {
030: if (model != null)
031: setModel(model);
032: }
033:
034: /**
035: * Creates a SComboBox that contains the elements in the specified array.
036: */
037: public SComboBox(final Object[] array) {
038: this (new DefaultComboBoxModel(array));
039: }
040:
041: /**
042: * Creates a SComboBox that contains the elements in the specified vector.
043: */
044: public SComboBox(Vector vector) {
045: this (new DefaultComboBoxModel(vector));
046: }
047:
048: /**
049: * Creates a SComboBox unslected.
050: */
051: public SComboBox() {
052: this (new DefaultComboBoxModel());
053: }
054:
055: /**
056: * Returns the name of the L&F class that renders this component.
057: */
058: public Class getUIClass() {
059: return SComboBox.class;
060: }
061:
062: /**
063: * Sets the data model that the SComboBox uses to obtain the list of items.
064: */
065: public SComboBox setModel(ComboBoxModel model) {
066: firePropertyChange("model", this .model, this .model = model);
067: return this ;
068: }
069:
070: /**
071: * Returns the data model currently used by the SComboBox.
072: */
073: public ComboBoxModel getModel() {
074: return model;
075: }
076:
077: /**
078: * Sets the selected item in the JComboBox by specifying the object in the list.
079: */
080: public SComboBox setSelectedItem(Object selectedItem) {
081: model.setSelectedItem(selectedItem);
082: return this ;
083: }
084:
085: /**
086: * Returns the currently selected item.
087: */
088: public Object getSelectedItem() {
089: return model.getSelectedItem();
090: }
091:
092: /**
093: * Selects the item at index index.
094: */
095: public SComboBox setSelectedIndex(int index) {
096: int size = model.getSize();
097:
098: if (index == -1) {
099: setSelectedItem(null);
100: } else if (index >= size || index < -1) {
101: throw new IllegalArgumentException("setSelectedIndex: "
102: + index + " out of bounds");
103: } else {
104: setSelectedItem(model.getElementAt(index));
105: }
106:
107: return this ;
108: }
109:
110: /**
111: * Returns the index of the currently selected item in the list.
112: */
113: public int getSelectedIndex() {
114: Object selectedObject = model.getSelectedItem();
115: Object object;
116:
117: for (int index = 0, size = model.getSize(); index < size; index++) {
118: object = model.getElementAt(index);
119: if (object.equals(selectedObject)) {
120: return index;
121: }
122: }
123: return -1;
124: }
125:
126: /**
127: * Returns the number of items in the list.
128: */
129: public int getItemCount() {
130: return model.getSize();
131: }
132:
133: /**
134: * Returns the list item at the specified index.
135: */
136: public Object getItemAt(int index) {
137: return model.getElementAt(index);
138: }
139:
140: /**
141: * Adds an item to the item list.
142: */
143: public void addItem(Object object) {
144: checkMutableComboBoxModel();
145: ((MutableComboBoxModel) model).addElement(object);
146: }
147:
148: /**
149: * Inserts an item into the item list at a given index.
150: */
151: public void insertItemAt(Object object, int index) {
152: checkMutableComboBoxModel();
153: ((MutableComboBoxModel) model).insertElementAt(object, index);
154: }
155:
156: /**
157: * Removes an item from the item list.
158: */
159: public void removeItem(Object object) {
160: checkMutableComboBoxModel();
161: ((MutableComboBoxModel) model).removeElement(object);
162: }
163:
164: /**
165: * Removes the item at index.
166: */
167: public void removeItemAt(int index) {
168: checkMutableComboBoxModel();
169: ((MutableComboBoxModel) model).removeElementAt(index);
170: }
171:
172: /**
173: * Removes all items from the item list.
174: */
175: public void removeAllItems() {
176: checkMutableComboBoxModel();
177: MutableComboBoxModel mutableModel = (MutableComboBoxModel) model;
178: int size = mutableModel.getSize();
179:
180: if (mutableModel instanceof DefaultComboBoxModel) {
181: ((DefaultComboBoxModel) mutableModel).removeAllElements();
182: } else {
183: for (int index = 0; index < size; ++index) {
184: Object element = mutableModel.getElementAt(0);
185: mutableModel.removeElement(element);
186: }
187: }
188: }
189:
190: /**
191: * Set the value as Text. This sets the text property. It is
192: * assumes that the text is the index.
193: */
194: public SComponent setValueAsText(String text) {
195: try {
196: setSelectedIndex(Integer.parseInt(text));
197: } catch (Exception e) {
198: //Fail silently
199: }
200:
201: return this ;
202: }
203:
204: // DISPATCH & EVENTS //////////////////////////////////////////////////////
205:
206: /**
207: * Adds the specified text event listener to recieve text events
208: * from this textcomponent.
209: */
210: public SComboBox addTextListener(TextListener listener) {
211: getUI().addListener(listener);
212: return this ;
213: }
214:
215: /**
216: * Removes the specified text event listener so that it no longer
217: * receives text events from this textcomponent.
218: */
219: public SComboBox removeTextListener(TextListener listener) {
220: getUI().removeListener(listener);
221: return this ;
222: }
223:
224: // PRIVATE /////////////////////////////////////////////////////////////
225:
226: protected void checkMutableComboBoxModel() {
227: if (!(model instanceof MutableComboBoxModel)) {
228: throw new RuntimeException(
229: "Cannot use this method with a non-Mutable data model.");
230: }
231: }
232:
233: protected ComboBoxModel model;
234:
235: }
|