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: import nextapp.echo2.app.list.ListSelectionModel;
036:
037: /**
038: * A component which provides the ability to select one or more items.
039: */
040: public class ListBox extends AbstractListComponent {
041:
042: /**
043: * Creates an empty <code>ListBox</code>.
044: * A <code>DefaultListModel</code> will be created.
045: * A <code>DefaultListSelectionModel</code> will be created and used
046: * to describe selections.
047: */
048: public ListBox() {
049: super ();
050: }
051:
052: /**
053: * Creates a <code>ListBox</code> visualizing the specified model.
054: * A <code>DefaultListSelectionModel</code> will be created and used
055: * to describe selections.
056: *
057: * @param model the initial model
058: */
059: public ListBox(ListModel model) {
060: super (model, null);
061: }
062:
063: /**
064: * Creates a <code>ListBox</code> visualizing the specified
065: * <code>ListModel</code> and describing selections using the specified
066: * <code>ListSelectionModel</code>.
067: *
068: * @param model the initial model
069: * @param selectionModel the initial selection model
070: */
071: public ListBox(ListModel model, ListSelectionModel selectionModel) {
072: super (model, selectionModel);
073: }
074:
075: /**
076: * Creates a <code>ListBox</code> with a <code>DefaultListModel</code>
077: * that initially contains the specified array of items.
078: * A <code>DefaultListSelectionModel</code> will be created and used
079: * to describe selections.
080: *
081: * @param itemArray an array of items that will initially populate
082: * this <code>ListBox</code>
083: */
084: public ListBox(Object[] itemArray) {
085: super (new DefaultListModel(itemArray), null);
086: }
087:
088: /**
089: * Returns the maximum selected index.
090: *
091: * @return the maximum selected index
092: */
093: public int getMaxSelectedIndex() {
094: return getSelectionModel().getMaxSelectedIndex();
095: }
096:
097: /**
098: * Returns the minimum selected index.
099: *
100: * @return The minimum selected index
101: */
102: public int getMinSelectedIndex() {
103: return getSelectionModel().getMinSelectedIndex();
104: }
105:
106: /**
107: * Returns all selected indices.
108: *
109: * @return an array containing all the selected indices
110: */
111: public int[] getSelectedIndices() {
112: ListModel model = getModel();
113: int min = getMinSelectedIndex();
114: if (min == -1) {
115: // No indices are selected, return empty array.
116: return new int[0];
117: }
118:
119: int selectionCount = 0;
120: int max = getMaxSelectedIndex();
121: int size = model.size();
122: if (max >= size - 1) {
123: max = size - 1;
124: }
125:
126: for (int index = min; index <= max; ++index) {
127: if (isSelectedIndex(index)) {
128: ++selectionCount;
129: }
130: }
131:
132: int[] selectedIndices = new int[selectionCount];
133: selectionCount = 0;
134: for (int index = min; index <= max; ++index) {
135: if (isSelectedIndex(index)) {
136: selectedIndices[selectionCount] = index;
137: ++selectionCount;
138: }
139: }
140:
141: return selectedIndices;
142: }
143:
144: /**
145: * Returns the selected item. This method is intended to be used when the
146: * when the selection model is configured to only allow one item to be
147: * selected at a time. In the event multiple item selection is enabled,
148: * this method will return the item at the lowest selected index.
149: *
150: * @return the selected item
151: */
152: public Object getSelectedValue() {
153: ListModel model = getModel();
154: int selectedIndex = getMinSelectedIndex();
155: Object value;
156:
157: if (selectedIndex == -1) {
158: value = null;
159: } else {
160: value = model.get(getMinSelectedIndex());
161: }
162:
163: return value;
164: }
165:
166: /**
167: * Returns all selected items.
168: *
169: * @return an array containing all the selected items
170: */
171: public Object[] getSelectedValues() {
172: ListModel model = getModel();
173: int min = getMinSelectedIndex();
174:
175: if (min == -1) {
176: // No values are selected, return empty array.
177: return new Object[0];
178: }
179:
180: int selectionCount = 0;
181: int max = getMaxSelectedIndex();
182: int size = model.size();
183: if (max >= size - 1) {
184: max = size - 1;
185: }
186:
187: for (int index = min; index <= max; ++index) {
188: if (isSelectedIndex(index)) {
189: ++selectionCount;
190: }
191: }
192:
193: Object[] selectedValues = new Object[selectionCount];
194: selectionCount = 0;
195: for (int index = min; index <= max; ++index) {
196: if (isSelectedIndex(index)) {
197: selectedValues[selectionCount] = model.get(index);
198: ++selectionCount;
199: }
200: }
201:
202: return selectedValues;
203: }
204:
205: /**
206: * Returns the selection mode.
207: *
208: * @return the selection mode, one of the following values:
209: * <ul>
210: * <li><code>ListSelectionModel.SINGLE_SELECTION</code>: only one
211: * list element may be selected.</li>
212: * <li><code>ListSelectionModel.MULTIPLE_SELECTION</code>:
213: * multiple list elements may be selected.</li>
214: * </ul>
215: */
216: public int getSelectionMode() {
217: return getSelectionModel().getSelectionMode();
218: }
219:
220: /**
221: * Determines whether an index is selected.
222: *
223: * @param index the index
224: * @return the selection state of the index
225: */
226: public boolean isSelectedIndex(int index) {
227: return getSelectionModel().isSelectedIndex(index);
228: }
229:
230: /**
231: * Selects only the given index.
232: *
233: * @param index the index
234: */
235: public void setSelectedIndex(int index) {
236: ListSelectionModel selectionModel = getSelectionModel();
237: selectionModel.clearSelection();
238: selectionModel.setSelectedIndex(index, true);
239: }
240:
241: /**
242: * Sets the selection state of the given index.
243: *
244: * @param index the index
245: * @param selected the selection state
246: */
247: public void setSelectedIndex(int index, boolean selected) {
248: getSelectionModel().setSelectedIndex(index, selected);
249: }
250:
251: /**
252: * Selects the specified indices, deselecting any other indices.
253: *
254: * @param indices the indices to be selected
255: */
256: public void setSelectedIndices(int[] indices) {
257: ListSelectionModel selectionModel = getSelectionModel();
258: selectionModel.clearSelection();
259: for (int i = 0; i < indices.length; ++i) {
260: selectionModel.setSelectedIndex(indices[i], true);
261: }
262: }
263:
264: /**
265: * Sets the selection mode.
266: *
267: * @param newValue the selection mode, one of the following values:
268: * <ul>
269: * <li><code>ListSelectionModel.SINGLE_SELECTION</code>: only one
270: * list element may be selected.</li>
271: * <li><code>ListSelectionModel.MULTIPLE_SELECTION</code>:
272: * multiple list elements may be selected.</li>
273: * </ul>
274: */
275: public void setSelectionMode(int newValue) {
276: getSelectionModel().setSelectionMode(newValue);
277: }
278: }
|