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.ajax.markup.html.form;
018
019import org.apache.wicket.ajax.AjaxRequestTarget;
020import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
021import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
022import org.apache.wicket.markup.html.form.CheckBox;
023import org.apache.wicket.model.IModel;
024
025/**
026 * A CheckBox which is updated via ajax when the user changes its value
027 * 
028 * @since 1.2
029 * 
030 * @author Igor Vaynberg (ivaynberg)
031 */
032public abstract class AjaxCheckBox extends CheckBox
033{
034        private static final long serialVersionUID = 1L;
035
036        /**
037         * Construct.
038         * 
039         * @param id
040         */
041        public AjaxCheckBox(final String id)
042        {
043                this(id, null);
044        }
045
046        /**
047         * Construct.
048         * 
049         * @param id
050         * @param model
051         */
052        public AjaxCheckBox(final String id, final IModel<Boolean> model)
053        {
054                super(id, model);
055
056                setOutputMarkupId(true);
057
058                add(new AjaxFormComponentUpdatingBehavior("click")
059                {
060                        private static final long serialVersionUID = 1L;
061
062                        @Override
063                        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
064                        {
065                                super.updateAjaxAttributes(attributes);
066                                AjaxCheckBox.this.updateAjaxAttributes(attributes);
067                        }
068
069                        @Override
070                        protected void onUpdate(AjaxRequestTarget target)
071                        {
072                                AjaxCheckBox.this.onUpdate(target);
073                        }
074                });
075        }
076
077        /**
078         * @param attributes
079         *      the attributes to use for the Ajax request
080         * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#updateAjaxAttributes(org.apache.wicket.ajax.attributes.AjaxRequestAttributes)
081         */
082        protected void updateAjaxAttributes(final AjaxRequestAttributes attributes)
083        {
084        }
085
086        /**
087         * Listener method invoked on an ajax update call
088         * 
089         * @param target
090         */
091        protected abstract void onUpdate(AjaxRequestTarget target);
092}