001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app;
031:
032: import nextapp.echo2.app.list.AbstractListComponent;
033: import nextapp.echo2.app.list.DefaultListModel;
034: import nextapp.echo2.app.list.ListModel;
035:
036: /**
037: * A drop-down select field.
038: */
039: public class SelectField extends AbstractListComponent {
040:
041: /**
042: * Creates a new, empty <code>SelectField</code>.
043: */
044: public SelectField() {
045: this ((ListModel) null);
046: }
047:
048: /**
049: * Creates a new <code>SelectField</code> with the provided model.
050: *
051: * @param model the model for the <code>SelectField</code>
052: */
053: public SelectField(ListModel model) {
054: super (model, null);
055: }
056:
057: /**
058: * Creates a new <code>SelectField</code> that will initially contain the
059: * provided array of items.
060: *
061: * @param items the items the <code>SelectField</code> will initially
062: * contain
063: */
064: public SelectField(Object[] items) {
065: this (new DefaultListModel(items));
066: }
067:
068: /**
069: * Returns the index of the currently selected item.
070: *
071: * @return the index of the currently selected item
072: */
073: public int getSelectedIndex() {
074: return getSelectionModel().getMinSelectedIndex();
075: }
076:
077: /**
078: * Returns the currently selected item.
079: *
080: * @return the currently selected item
081: */
082: public Object getSelectedItem() {
083: int selectedIndex = getSelectionModel().getMinSelectedIndex();
084: return selectedIndex == -1 ? null : getModel().get(
085: selectedIndex);
086: }
087:
088: /**
089: * Sets the selected index.
090: *
091: * @param index the new selected index
092: */
093: public void setSelectedIndex(int index) {
094: if (index == -1) {
095: getSelectionModel().clearSelection();
096: } else {
097: getSelectionModel().setSelectedIndex(index, true);
098: }
099: }
100:
101: /**
102: * Sets the selected item.
103: *
104: * @param item the new selected item, or null, to select nothing
105: */
106: public void setSelectedItem(Object item) {
107: if (item != null) {
108: ListModel model = getModel();
109: int size = model.size();
110: for (int i = 0; i < size; i++) {
111: if (item.equals(model.get(i))) {
112: setSelectedIndex(i);
113: return;
114: }
115: }
116: }
117: setSelectedIndex(-1);
118: }
119: }
|