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.Component; 022import org.apache.wicket.markup.html.form.IChoiceRenderer; 023import org.apache.wicket.model.IModel; 024import org.apache.wicket.model.PropertyModel; 025 026/** 027 * A filtered property column that creates a textfield filter component. The default model of the 028 * created textfield is a property model with the same property expression as the one used to 029 * display data. This works well when the filter state object is of the same type as the objects in 030 * the data table. 031 * 032 * @author Igor Vaynberg (ivaynberg) 033 * @param <T> 034 * The model object type 035 * @param <Y> 036 * The column model object type 037 * @param <S> 038 * the type of the sort property 039 */ 040public class ChoiceFilteredPropertyColumn<T, Y, S> extends FilteredPropertyColumn<T, S> 041{ 042 private static final long serialVersionUID = 1L; 043 private final IModel<? extends List<? extends Y>> filterChoices; 044 045 /** 046 * @param displayModel 047 * @param sortProperty 048 * @param propertyExpression 049 * @param filterChoices 050 * collection choices used in the choice filter 051 */ 052 public ChoiceFilteredPropertyColumn(final IModel<String> displayModel, 053 final S sortProperty, final String propertyExpression, 054 final IModel<? extends List<? extends Y>> filterChoices) 055 { 056 super(displayModel, sortProperty, propertyExpression); 057 this.filterChoices = filterChoices; 058 } 059 060 /** 061 * @param displayModel 062 * @param propertyExpression 063 * @param filterChoices 064 * collection of choices used in the choice filter 065 */ 066 public ChoiceFilteredPropertyColumn(final IModel<String> displayModel, 067 final String propertyExpression, final IModel<? extends List<? extends Y>> filterChoices) 068 { 069 super(displayModel, propertyExpression); 070 this.filterChoices = filterChoices; 071 } 072 073 /** 074 * @see org.apache.wicket.model.IDetachable#detach() 075 */ 076 @Override 077 public void detach() 078 { 079 super.detach(); 080 if (filterChoices != null) 081 { 082 filterChoices.detach(); 083 } 084 } 085 086 /** 087 * @see org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilteredColumn#getFilter(java.lang.String, 088 * org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm) 089 */ 090 @Override 091 public Component getFilter(final String componentId, final FilterForm<?> form) 092 { 093 ChoiceFilter<Y> filter = new ChoiceFilter<>(componentId, getFilterModel(form), form, 094 filterChoices, enableAutoSubmit()); 095 096 IChoiceRenderer<Y> renderer = getChoiceRenderer(); 097 if (renderer != null) 098 { 099 filter.getChoice().setChoiceRenderer(renderer); 100 } 101 return filter; 102 } 103 104 /** 105 * Returns the model that will be passed on to the text filter. Users can override this method 106 * to change the model. 107 * 108 * @param form 109 * filter form 110 * @return model passed on to the text filter 111 */ 112 protected IModel<Y> getFilterModel(final FilterForm<?> form) 113 { 114 return new PropertyModel<>(form.getDefaultModel(), getPropertyExpression()); 115 } 116 117 /** 118 * Returns true if the constructed choice filter should autosubmit the form when its value is 119 * changed. 120 * 121 * @return true to make choice filter autosubmit, false otherwise 122 */ 123 protected boolean enableAutoSubmit() 124 { 125 return true; 126 } 127 128 /** 129 * Returns choice renderer that will be used to create the choice filter 130 * 131 * @return choice renderer that will be used to create the choice filter 132 */ 133 protected IChoiceRenderer<Y> getChoiceRenderer() 134 { 135 return null; 136 } 137 138 /** 139 * @return filter choices model 140 */ 141 protected final IModel<? extends List<? extends Y>> getFilterChoices() 142 { 143 return filterChoices; 144 } 145 146 147}