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 org.apache.wicket.core.util.lang.WicketObjects;
020import org.apache.wicket.markup.html.form.Button;
021import org.apache.wicket.markup.html.form.Form;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.model.ResourceModel;
024
025/**
026 * Filter component that generates a 'go' and 'clear' buttons.
027 * 
028 * @author Igor Vaynberg (ivaynberg)
029 * 
030 */
031public class GoAndClearFilter extends GoFilter
032{
033        private static final long serialVersionUID = 1L;
034
035        private static final IModel<String> DEFAULT_CLEAR_MODEL = new ResourceModel("datatable.clear",
036                "clear");
037
038        private final Button clear;
039
040        private final Object originalState;
041
042        /**
043         * Constructor
044         * 
045         * This constructor will use default models for the 'clear' and 'go' button labels.
046         * Uses the form's model object as an original state
047         * 
048         * @param id
049         *            component id
050         * @param form
051         *            filter form of the filter toolbar
052         */
053        public GoAndClearFilter(final String id, final FilterForm<?> form)
054        {
055                this(id, form, DEFAULT_GO_MODEL, DEFAULT_CLEAR_MODEL);
056        }
057
058        /**
059         * Constructor.
060         * Uses the form's model object as an original state
061         * 
062         * @param id
063         *            component id
064         * @param form
065         *            filter form of the filter toolbar
066         * @param goModel
067         *            model for the label of the 'go' button
068         * @param clearModel
069         *            model for the label of the 'clear' button
070         */
071        public GoAndClearFilter(final String id, final FilterForm<?> form,
072                final IModel<String> goModel, final IModel<String> clearModel)
073        {
074                this(id, goModel, clearModel, WicketObjects.cloneObject(form.getDefaultModelObject()));
075        }
076
077        /**
078         * Constructor
079         *
080         * @param id
081         *            component id
082         * @param goModel
083         *            model for the label of the 'go' button
084         * @param clearModel
085         *            model for the label of the 'clear' button
086         * @param originalState
087         *            the object to use as original state
088         */
089        public GoAndClearFilter(final String id, final IModel<String> goModel,
090                        final IModel<String> clearModel, Object originalState)
091        {
092                super(id, goModel);
093
094                this.originalState = originalState;
095
096                clear = new Button("clear", clearModel)
097                {
098                        private static final long serialVersionUID = 1L;
099
100                        @Override
101                        public void onSubmit()
102                        {
103                                onClearSubmit(this);
104                        }
105                };
106
107                clear.setDefaultFormProcessing(true);
108
109                add(clear);
110        }
111
112        /**
113         * @return button component representing the clear button
114         */
115        protected Button getClearButton()
116        {
117                return clear;
118        }
119
120        /**
121         * This method should be implemented by subclasses to provide behavior for the clear button.
122         * 
123         * @param button
124         *            the 'clear' button
125         * 
126         */
127        @SuppressWarnings("unchecked")
128        protected void onClearSubmit(final Button button)
129        {
130                Form<Object> form = (Form<Object>)button.getForm();
131                form.setDefaultModelObject(WicketObjects.cloneObject(originalState));
132        }
133
134}