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.extensions.markup.html.repeater.data.table.filter;
018
019import java.util.List;
020
021import org.apache.wicket.AttributeModifier;
022import org.apache.wicket.markup.html.form.ChoiceRenderer;
023import org.apache.wicket.markup.html.form.DropDownChoice;
024import org.apache.wicket.markup.html.form.IChoiceRenderer;
025import org.apache.wicket.model.IModel;
026import org.apache.wicket.model.Model;
027
028
029/**
030 * Filter that can be represented by a drop down list of choices
031 * 
032 * @see DropDownChoice
033 * 
034 * @author Igor Vaynberg (ivaynberg)
035 * @param <T>
036 *            The model object type
037 */
038public class ChoiceFilter<T> extends AbstractFilter
039{
040        private static final long serialVersionUID = 1L;
041
042        private final DropDownChoice<T> choice;
043
044        /**
045         * Constructor.
046         *
047         * @param id
048         *            component id
049         * @param model
050         *            model for the drop down choice component
051         * @param form
052         *            filter form this component will be attached to
053         * @param choices
054         *            list of choices, see {@link DropDownChoice}
055         * @param autoSubmit
056         *            if true this filter will submit the form on selection change
057         */
058        public ChoiceFilter(final String id, final IModel<T> model, final FilterForm<?> form,
059                final IModel<? extends List<? extends T>> choices, final boolean autoSubmit)
060        {
061                this(id, model, form, choices, new ChoiceRenderer<>(), autoSubmit);
062        }
063
064        /**
065         * Constructor
066         *
067         * @param id
068         *            component id
069         * @param model
070         *            model for the drop down choice component
071         * @param form
072         *            filter form this component will be attached to
073         * @param choices
074         *            list of choices, see {@link DropDownChoice}
075         * @param autoSubmit
076         *            if true this filter will submit the form on selection change
077         */
078        public ChoiceFilter(final String id, final IModel<T> model, final FilterForm<?> form,
079                final List<? extends T> choices, final boolean autoSubmit)
080        {
081                this(id, model, form, Model.ofList(choices), new ChoiceRenderer<>(), autoSubmit);
082        }
083
084        /**
085         * Constructor
086         *
087         * @param id
088         *            component id
089         * @param model
090         *            model for the drop down choice component
091         * @param form
092         *            filter form this component will be attached to
093         * @param choices
094         *            list of choices, see {@link DropDownChoice}
095         * @param renderer
096         *            choice renderer, see {@link DropDownChoice}
097         * @param autoSubmit
098         *            if true this filter will submit the form on selection change
099         */
100        public ChoiceFilter(final String id, final IModel<T> model, final FilterForm<?> form,
101                final List<? extends T> choices, final IChoiceRenderer<? super T> renderer,
102                final boolean autoSubmit)
103        {
104                this(id, model, form, Model.ofList(choices), renderer, autoSubmit);
105        }
106
107        /**
108         * @param id
109         *            component id
110         * @param model
111         *            model for the drop down choice component
112         * @param form
113         *            filter form this component will be attached to
114         * @param choices
115         *            list of choices, see {@link DropDownChoice}
116         * @param renderer
117         *            choice renderer, see {@link DropDownChoice}
118         * @param autoSubmit
119         *            if true this filter will submit the form on selection change
120         * @see DropDownChoice
121         */
122        public ChoiceFilter(final String id, final IModel<T> model, final FilterForm<?> form,
123                final IModel<? extends List<? extends T>> choices,
124                final IChoiceRenderer<? super T> renderer,
125                final boolean autoSubmit)
126        {
127                super(id, form);
128
129                choice = newDropDownChoice("filter", model, choices, renderer);
130
131                if (autoSubmit)
132                {
133                        choice.add(AttributeModifier.replace("onchange", "this.form.submit();"));
134                }
135                enableFocusTracking(choice);
136
137                add(choice);
138        }
139
140        /**
141         * Factory method for the drop down choice component
142         * 
143         * @param id
144         *            component id
145         * @param model
146         *            component model
147         * @param choices
148         *            choices model
149         * @param renderer
150         *            choice renderer
151         * @return created drop down component
152         */
153        protected DropDownChoice<T> newDropDownChoice(final String id, final IModel<T> model,
154                final IModel<? extends List<? extends T>> choices, final IChoiceRenderer<? super T> renderer)
155        {
156                DropDownChoice<T> dropDownChoice = new DropDownChoice<>(id, model, choices, renderer);
157                dropDownChoice.setNullValid(true);
158                return dropDownChoice;
159        }
160
161        /**
162         * @return the DropDownChoice form component created to represent this filter
163         */
164        public final DropDownChoice<T> getChoice()
165        {
166                return choice;
167        }
168
169}