KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > ListBox


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31
32 import nextapp.echo2.app.list.AbstractListComponent;
33 import nextapp.echo2.app.list.DefaultListModel;
34 import nextapp.echo2.app.list.ListModel;
35 import nextapp.echo2.app.list.ListSelectionModel;
36
37
38 /**
39  * A component which provides the ability to select one or more items.
40  */

41 public class ListBox extends AbstractListComponent {
42
43     /**
44      * Creates an empty <code>ListBox</code>.
45      * A <code>DefaultListModel</code> will be created.
46      * A <code>DefaultListSelectionModel</code> will be created and used
47      * to describe selections.
48      */

49     public ListBox() {
50         super();
51     }
52     
53     /**
54      * Creates a <code>ListBox</code> visualizing the specified model.
55      * A <code>DefaultListSelectionModel</code> will be created and used
56      * to describe selections.
57      *
58      * @param model the initial model
59      */

60     public ListBox(ListModel model) {
61         super(model, null);
62     }
63     
64     /**
65      * Creates a <code>ListBox</code> visualizing the specified
66      * <code>ListModel</code> and describing selections using the specified
67      * <code>ListSelectionModel</code>.
68      *
69      * @param model the initial model
70      * @param selectionModel the initial selection model
71      */

72     public ListBox(ListModel model, ListSelectionModel selectionModel) {
73         super(model, selectionModel);
74     }
75     
76     /**
77      * Creates a <code>ListBox</code> with a <code>DefaultListModel</code>
78      * that initially contains the specified array of items.
79      * A <code>DefaultListSelectionModel</code> will be created and used
80      * to describe selections.
81      *
82      * @param itemArray an array of items that will initially populate
83      * this <code>ListBox</code>
84      */

85     public ListBox(Object JavaDoc[] itemArray) {
86         super(new DefaultListModel(itemArray), null);
87     }
88      
89     /**
90      * Returns the maximum selected index.
91      *
92      * @return the maximum selected index
93      */

94     public int getMaxSelectedIndex() {
95         return getSelectionModel().getMaxSelectedIndex();
96     }
97         
98     /**
99      * Returns the minimum selected index.
100      *
101      * @return The minimum selected index
102      */

103     public int getMinSelectedIndex() {
104         return getSelectionModel().getMinSelectedIndex();
105     }
106     
107     /**
108      * Returns all selected indices.
109      *
110      * @return an array containing all the selected indices
111      */

112     public int[] getSelectedIndices() {
113         ListModel model = getModel();
114         int min = getMinSelectedIndex();
115         if (min == -1) {
116             // No indices are selected, return empty array.
117
return new int[0];
118         }
119         
120         int selectionCount = 0;
121         int max = getMaxSelectedIndex();
122         int size = model.size();
123         if (max >= size - 1) {
124             max = size - 1;
125         }
126
127         for (int index = min; index <= max; ++index) {
128             if (isSelectedIndex(index)) {
129                 ++selectionCount;
130             }
131         }
132
133         int[] selectedIndices = new int[selectionCount];
134         selectionCount = 0;
135         for (int index = min; index <= max; ++index) {
136             if (isSelectedIndex(index)) {
137                 selectedIndices[selectionCount] = index;
138                 ++selectionCount;
139             }
140         }
141         
142         return selectedIndices;
143     }
144         
145     /**
146      * Returns the selected item. This method is intended to be used when the
147      * when the selection model is configured to only allow one item to be
148      * selected at a time. In the event multiple item selection is enabled,
149      * this method will return the item at the lowest selected index.
150      *
151      * @return the selected item
152      */

153     public Object JavaDoc getSelectedValue() {
154         ListModel model = getModel();
155         int selectedIndex = getMinSelectedIndex();
156         Object JavaDoc value;
157         
158         if (selectedIndex == -1) {
159             value = null;
160         } else {
161             value = model.get(getMinSelectedIndex());
162         }
163
164         return value;
165     }
166     
167     /**
168      * Returns all selected items.
169      *
170      * @return an array containing all the selected items
171      */

172     public Object JavaDoc[] getSelectedValues() {
173         ListModel model = getModel();
174         int min = getMinSelectedIndex();
175
176         if (min == -1) {
177             // No values are selected, return empty array.
178
return new Object JavaDoc[0];
179         }
180         
181         int selectionCount = 0;
182         int max = getMaxSelectedIndex();
183         int size = model.size();
184         if (max >= size - 1) {
185             max = size - 1;
186         }
187
188         for (int index = min; index <= max; ++index) {
189             if (isSelectedIndex(index)) {
190                 ++selectionCount;
191             }
192         }
193
194         Object JavaDoc[] selectedValues = new Object JavaDoc[selectionCount];
195         selectionCount = 0;
196         for (int index = min; index <= max; ++index) {
197             if (isSelectedIndex(index)) {
198                 selectedValues[selectionCount] = model.get(index);
199                 ++selectionCount;
200             }
201         }
202         
203         return selectedValues;
204     }
205
206     /**
207      * Returns the selection mode.
208      *
209      * @return the selection mode, one of the following values:
210      * <ul>
211      * <li><code>ListSelectionModel.SINGLE_SELECTION</code>: only one
212      * list element may be selected.</li>
213      * <li><code>ListSelectionModel.MULTIPLE_SELECTION</code>:
214      * multiple list elements may be selected.</li>
215      * </ul>
216      */

217     public int getSelectionMode() {
218         return getSelectionModel().getSelectionMode();
219     }
220     
221     /**
222      * Determines whether an index is selected.
223      *
224      * @param index the index
225      * @return the selection state of the index
226      */

227     public boolean isSelectedIndex(int index) {
228         return getSelectionModel().isSelectedIndex(index);
229     }
230     
231     /**
232      * Selects only the given index.
233      *
234      * @param index the index
235      */

236     public void setSelectedIndex(int index) {
237         ListSelectionModel selectionModel = getSelectionModel();
238         selectionModel.clearSelection();
239         selectionModel.setSelectedIndex(index, true);
240     }
241     
242     /**
243      * Sets the selection state of the given index.
244      *
245      * @param index the index
246      * @param selected the selection state
247      */

248     public void setSelectedIndex(int index, boolean selected) {
249         getSelectionModel().setSelectedIndex(index, selected);
250     }
251     
252     /**
253      * Selects the specified indices, deselecting any other indices.
254      *
255      * @param indices the indices to be selected
256      */

257     public void setSelectedIndices(int[] indices) {
258         ListSelectionModel selectionModel = getSelectionModel();
259         selectionModel.clearSelection();
260         for (int i = 0; i < indices.length; ++i) {
261             selectionModel.setSelectedIndex(indices[i], true);
262         }
263     }
264     
265     /**
266      * Sets the selection mode.
267      *
268      * @param newValue the selection mode, one of the following values:
269      * <ul>
270      * <li><code>ListSelectionModel.SINGLE_SELECTION</code>: only one
271      * list element may be selected.</li>
272      * <li><code>ListSelectionModel.MULTIPLE_SELECTION</code>:
273      * multiple list elements may be selected.</li>
274      * </ul>
275      */

276     public void setSelectionMode(int newValue) {
277         getSelectionModel().setSelectionMode(newValue);
278     }
279 }
280
Popular Tags