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.tree.content;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.ajax.AjaxRequestTarget;
021import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
022import org.apache.wicket.extensions.markup.html.repeater.tree.AbstractTree;
023import org.apache.wicket.markup.html.form.CheckBox;
024import org.apache.wicket.model.IModel;
025import org.apache.wicket.model.Model;
026
027/**
028 * This class adds a {@link CheckBox} to a {@link Folder}. Subclasses have to override
029 * {@link #newCheckBoxModel(IModel)} to do anything useful with the checkbox state.
030 * 
031 * @see #newCheckBoxModel(IModel)
032 * 
033 * @author svenmeier
034 */
035public class CheckedFolder<T> extends Folder<T>
036{
037
038        private static final long serialVersionUID = 1L;
039
040        public CheckedFolder(String id, AbstractTree<T> tree, IModel<T> model)
041        {
042                super(id, tree, model);
043
044                add(newCheckBox("checkbox", model));
045        }
046
047        /**
048         * Hook method to create a new checkbox component. This default implementation uses an
049         * {@link AjaxCheckBox}.
050         * 
051         * @param id
052         * @param model
053         * @return created component
054         * 
055         * @see #newCheckBoxModel(IModel)
056         * @see #onUpdate(AjaxRequestTarget)
057         */
058        protected Component newCheckBox(String id, IModel<T> model)
059        {
060                return new AjaxCheckBox(id, newCheckBoxModel(model))
061                {
062                        private static final long serialVersionUID = 1L;
063
064                        @Override
065                        protected void onUpdate(AjaxRequestTarget target)
066                        {
067                                CheckedFolder.this.onUpdate(target);
068                        }
069                };
070        }
071
072        /**
073         * Create the model for the checkbox, defaults to {@link Boolean#FALSE}.
074         * 
075         * @param model
076         * @return wrapping model
077         */
078        protected IModel<Boolean> newCheckBoxModel(IModel<T> model)
079        {
080                return Model.of(Boolean.FALSE);
081        }
082
083        /**
084         * Hook method to be notified of an update of the checkbox.
085         * 
086         * @param target
087         * @see #newCheckBox(String, IModel)
088         */
089        protected void onUpdate(AjaxRequestTarget target)
090        {
091        }
092}