001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.form;
018
019import java.util.List;
020
021import org.apache.wicket.markup.ComponentTag;
022import org.apache.wicket.model.IModel;
023
024
025/**
026 * Essentially a drop down choice that doesn't drop down. Instead, it scrolls and displays a given
027 * number of rows.
028 * 
029 * @author Jonathan Locke
030 * @author Johan Compagner
031 * @author Eelco Hillenius
032 * 
033 * @param <T>
034 *            The model object type
035 */
036public class ListChoice<T> extends DropDownChoice<T>
037{
038        private static final long serialVersionUID = 1L;
039
040        /** The default maximum number of rows to display. */
041        private static final int DEFAULT_MAX_ROWS = 8;
042
043        /** The maximum number of rows to display. */
044        private int maxRows;
045
046        /**
047         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String)
048         */
049        public ListChoice(final String id)
050        {
051                this(id, null, (List<? extends T>)null, null, DEFAULT_MAX_ROWS);
052        }
053
054        /**
055         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, List)
056         */
057        public ListChoice(final String id, final List<? extends T> choices)
058        {
059                this(id, null, choices, null, DEFAULT_MAX_ROWS);
060        }
061
062        /**
063         * @param id
064         *            See Component
065         * @param choices
066         *            The list of values in the list
067         * @param renderer
068         *            See AbstractChoice
069         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
070         *      List,IChoiceRenderer)
071         */
072        public ListChoice(final String id, final List<? extends T> choices,
073                final IChoiceRenderer<? super T> renderer)
074        {
075                this(id, null, choices, renderer, DEFAULT_MAX_ROWS);
076        }
077
078
079        /**
080         * @param id
081         *            See Component
082         * @param model
083         *            See Component
084         * @param choices
085         *            The list of values in the list
086         * @see DropDownChoice#DropDownChoice(String, IModel, List)
087         */
088        public ListChoice(final String id, final IModel<T> model, final List<? extends T> choices)
089        {
090                this(id, model, choices, null, DEFAULT_MAX_ROWS);
091        }
092
093        /**
094         * @param id
095         *            See Component
096         * @param model
097         *            See Component
098         * @param choices
099         *            The list of values in the list
100         * @param maxRows
101         *            Maximum number of rows to show
102         * @see DropDownChoice#DropDownChoice(String, IModel, List)
103         */
104        public ListChoice(final String id, final IModel<T> model, final List<? extends T> choices,
105                final int maxRows)
106        {
107                this(id, model, choices, null, maxRows);
108        }
109
110        /**
111         * @param id
112         *            See Component
113         * @param model
114         *            See Component
115         * @param choices
116         *            The list of values in the list
117         * @param renderer
118         *            See AbstractChoice
119         * @see DropDownChoice#DropDownChoice(String, IModel, List)
120         */
121        public ListChoice(final String id, final IModel<T> model, final List<? extends T> choices,
122                final IChoiceRenderer<? super T> renderer)
123        {
124                this(id, model, choices, renderer, DEFAULT_MAX_ROWS);
125        }
126
127        /**
128         * @param id
129         *            See Component
130         * @param model
131         *            See Component
132         * @param choices
133         *            The list of values in the list
134         * @param renderer
135         *            See AbstractChoice
136         * @param maxRows
137         *            Maximum number of rows to show
138         * @see DropDownChoice#DropDownChoice(String, IModel, List)
139         */
140        public ListChoice(final String id, final IModel<T> model, final List<? extends T> choices,
141                final IChoiceRenderer<? super T> renderer, final int maxRows)
142        {
143                super(id, model, choices, renderer);
144                this.maxRows = maxRows;
145        }
146
147        /**
148         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel)
149         */
150        public ListChoice(String id, IModel<? extends List<? extends T>> choices)
151        {
152                this(id, null, choices, null, DEFAULT_MAX_ROWS);
153        }
154
155        /**
156         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel,IModel)
157         */
158        public ListChoice(String id, IModel<T> model, IModel<? extends List<? extends T>> choices)
159        {
160                this(id, model, choices, null, DEFAULT_MAX_ROWS);
161        }
162
163        /**
164         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String,
165         *      IModel,IChoiceRenderer)
166         */
167        public ListChoice(String id, IModel<? extends List<? extends T>> choices,
168                IChoiceRenderer<? super T> renderer)
169        {
170                this(id, null, choices, renderer, DEFAULT_MAX_ROWS);
171        }
172
173
174        /**
175         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel,
176         *      IModel,IChoiceRenderer)
177         */
178        public ListChoice(String id, IModel<T> model, IModel<? extends List<? extends T>> choices,
179                IChoiceRenderer<? super T> renderer)
180        {
181                this(id, model, choices, renderer, DEFAULT_MAX_ROWS);
182        }
183
184        /**
185         * @param id
186         * @param model
187         * @param choices
188         * @param renderer
189         * @param maxRows
190         * @see org.apache.wicket.markup.html.form.AbstractChoice#AbstractChoice(String, IModel,
191         *      IModel,IChoiceRenderer)
192         */
193        public ListChoice(String id, IModel<T> model, IModel<? extends List<? extends T>> choices,
194                IChoiceRenderer<? super T> renderer, int maxRows)
195        {
196                super(id, model, choices, renderer);
197                this.maxRows = maxRows;
198        }
199
200        /**
201         * Gets the maximum number of rows to display.
202         * 
203         * @return the maximum number of rows to display
204         */
205        public final int getMaxRows()
206        {
207                return maxRows;
208        }
209
210        /**
211         * Sets the maximum number of rows to display.
212         * 
213         * @param maxRows
214         *            the maximum number of rows to display
215         * @return This
216         */
217        public final ListChoice<T> setMaxRows(int maxRows)
218        {
219                this.maxRows = maxRows;
220                return this;
221        }
222
223        /**
224         * @see org.apache.wicket.Component#onComponentTag(ComponentTag)
225         */
226        @Override
227        protected void onComponentTag(final ComponentTag tag)
228        {
229                super.onComponentTag(tag);
230                if (!tag.getAttributes().containsKey("size"))
231                {
232                        tag.put("size", maxRows);
233                }
234        }
235}