001: /*
002: * $Id: ListMultipleChoice.java 462434 2006-10-03 22:41:33Z ivaynberg $
003: * $Revision: 462434 $ $Date: 2006-10-04 00:41:33 +0200 (Wed, 04 Oct 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.markup.html.form;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.StringTokenizer;
025:
026: import wicket.markup.ComponentTag;
027: import wicket.model.IModel;
028: import wicket.util.convert.ConversionException;
029: import wicket.util.string.AppendingStringBuffer;
030: import wicket.util.string.Strings;
031:
032: /**
033: * A multiple choice list component.
034: *
035: * @author Jonathan Locke
036: * @author Johan Compagner
037: * @author Martijn Dashorst
038: */
039: public class ListMultipleChoice extends AbstractChoice {
040: private static final long serialVersionUID = 1L;
041:
042: /** The default maximum number of rows to display. */
043: private static int defaultMaxRows = 8;
044:
045: /** The maximum number of rows to display. */
046: private int maxRows = defaultMaxRows;
047:
048: /**
049: * Gets the default maximum number of rows to display.
050: *
051: * @return Returns the defaultMaxRows.
052: */
053: protected static int getDefaultMaxRows() {
054: return defaultMaxRows;
055: }
056:
057: /**
058: * Sets the default maximum number of rows to display.
059: *
060: * @param defaultMaxRows
061: * The defaultMaxRows to set.
062: */
063: protected static void setDefaultMaxRows(final int defaultMaxRows) {
064: ListMultipleChoice.defaultMaxRows = defaultMaxRows;
065: }
066:
067: /**
068: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String)
069: */
070: public ListMultipleChoice(final String id) {
071: super (id);
072: }
073:
074: /**
075: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String, List)
076: */
077: public ListMultipleChoice(final String id, final List choices) {
078: super (id, choices);
079: }
080:
081: /**
082: * Creates a multiple choice list with a maximum number of visible rows.
083: *
084: * @param id
085: * component id
086: * @param choices
087: * list of choices
088: * @param maxRows
089: * the maximum number of visible rows.
090: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String, List)
091: */
092: public ListMultipleChoice(final String id, final List choices,
093: final int maxRows) {
094: super (id, choices);
095: this .maxRows = maxRows;
096: }
097:
098: /**
099: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
100: * List,IChoiceRenderer)
101: */
102: public ListMultipleChoice(final String id, final List choices,
103: final IChoiceRenderer renderer) {
104: super (id, choices, renderer);
105: }
106:
107: /**
108: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
109: * IModel, List)
110: */
111: public ListMultipleChoice(final String id, IModel object,
112: final List choices) {
113: super (id, object, choices);
114: }
115:
116: /**
117: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
118: * IModel, List,IChoiceRenderer)
119: */
120: public ListMultipleChoice(final String id, IModel object,
121: final List choices, final IChoiceRenderer renderer) {
122: super (id, object, choices, renderer);
123: }
124:
125: /**
126: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
127: * IModel)
128: */
129: public ListMultipleChoice(String id, IModel choices) {
130: super (id, choices);
131: }
132:
133: /**
134: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
135: * IModel,IModel)
136: */
137: public ListMultipleChoice(String id, IModel model, IModel choices) {
138: super (id, model, choices);
139: }
140:
141: /**
142: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
143: * IModel,IChoiceRenderer)
144: */
145: public ListMultipleChoice(String id, IModel choices,
146: IChoiceRenderer renderer) {
147: super (id, choices, renderer);
148: }
149:
150: /**
151: * @see wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
152: * IModel, IModel,IChoiceRenderer)
153: */
154: public ListMultipleChoice(String id, IModel model, IModel choices,
155: IChoiceRenderer renderer) {
156: super (id, model, choices, renderer);
157: }
158:
159: /**
160: * Sets the number of visible rows in the listbox.
161: *
162: * @param maxRows
163: * the number of visible rows
164: * @return this
165: */
166: public final ListMultipleChoice setMaxRows(final int maxRows) {
167: this .maxRows = maxRows;
168: return this ;
169: }
170:
171: /**
172: * @see FormComponent#getModelValue()
173: */
174: public final String getModelValue() {
175: // Get the list of selected values
176: final Collection selectedValues = (Collection) getModelObject();
177: final AppendingStringBuffer buffer = new AppendingStringBuffer();
178: if (selectedValues != null) {
179: final List choices = getChoices();
180: for (final Iterator iterator = selectedValues.iterator(); iterator
181: .hasNext();) {
182: final Object object = iterator.next();
183:
184: int index = choices.indexOf(object);
185: buffer.append(getChoiceRenderer().getIdValue(object,
186: index));
187: buffer.append(VALUE_SEPARATOR);
188: }
189: }
190: return buffer.toString();
191: }
192:
193: /**
194: * @see wicket.markup.html.form.AbstractChoice#isSelected(Object,int,
195: * String)
196: */
197: protected final boolean isSelected(Object choice, int index,
198: String selected) {
199: // Have a value at all?
200: if (selected != null) {
201: // Loop through ids
202: for (final StringTokenizer tokenizer = new StringTokenizer(
203: selected, VALUE_SEPARATOR); tokenizer
204: .hasMoreTokens();) {
205: final String id = tokenizer.nextToken();
206: if (id.equals(getChoiceRenderer().getIdValue(choice,
207: index))) {
208: return true;
209: }
210: }
211: }
212: return false;
213: }
214:
215: /**
216: * @see wicket.Component#onComponentTag(ComponentTag)
217: */
218: protected final void onComponentTag(final ComponentTag tag) {
219: super .onComponentTag(tag);
220: tag.put("multiple", "multiple");
221:
222: if (!tag.getAttributes().containsKey("size")) {
223: tag.put("size", Math.min(maxRows, getChoices().size()));
224: }
225: }
226:
227: /**
228: * @see wicket.markup.html.form.FormComponent#convertValue(String[])
229: */
230: protected Object convertValue(String[] ids)
231: throws ConversionException {
232: ArrayList selectedValues = new ArrayList();
233:
234: // If one or more ids is selected
235: if (ids != null && ids.length > 0 && !Strings.isEmpty(ids[0])) {
236: // Get values that could be selected
237: final List choices = getChoices();
238:
239: // Loop through selected indices
240: for (int i = 0; i < ids.length; i++) {
241: for (int index = 0; index < choices.size(); index++) {
242: // Get next choice
243: final Object choice = choices.get(index);
244: if (getChoiceRenderer().getIdValue(choice, index)
245: .equals(ids[i])) {
246: selectedValues.add(choice);
247: break;
248: }
249: }
250: }
251: }
252: return selectedValues;
253: }
254:
255: /**
256: * @see FormComponent#updateModel()
257: */
258: public void updateModel() {
259: Collection selectedValues = (Collection) getModelObject();
260: if (selectedValues != null) {
261: modelChanging();
262: selectedValues.clear();
263: selectedValues.addAll((Collection) getConvertedInput());
264: modelChanged();
265: } else {
266: selectedValues = (Collection) getConvertedInput();
267: setModelObject(selectedValues);
268: }
269: }
270: }
|