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.markup.html.form;
018
019import org.apache.wicket.WicketRuntimeException;
020
021/**
022 * Selects and deselects all Check components under the same CheckGroup as itself. Selection
023 * toggling is accomplished by generating an onclick javascript event handler. This component must
024 * be attached to markup of type <input type="checkbox"/>. Additionally, if
025 * {@link #CheckGroupSelector(String)} constuctor is used the selector must be a descendant of the
026 * CheckGroup it is meant to affect.
027 * 
028 * @see org.apache.wicket.markup.html.form.CheckGroup
029 * @see org.apache.wicket.markup.html.form.Check
030 * 
031 * @author Igor Vaynberg
032 * @author Carl-Eric Menzel
033 */
034public class CheckGroupSelector extends AbstractCheckSelector
035{
036        /** */
037        private static final long serialVersionUID = 1L;
038
039        private CheckGroup<?> group;
040
041        /**
042         * A Selector that will look for a {@link CheckGroup} in its parent hierarchy.
043         * 
044         * @param id
045         *            component id
046         */
047        public CheckGroupSelector(String id)
048        {
049                this(id, null);
050        }
051
052        /**
053         * A Selector that will work with the given group.
054         * 
055         * @param id
056         *            component id
057         * @param group
058         *            group to work with
059         */
060        public CheckGroupSelector(String id, CheckGroup<?> group)
061        {
062                super(id);
063
064                this.group = group;
065        }
066
067        private CheckGroup<?> getGroup()
068        {
069                CheckGroup<?> group = this.group;
070                if (group == null)
071                {
072                        group = findParent(CheckGroup.class);
073                        this.group = group;
074                }
075                return group;
076        }
077
078        @Override
079        protected void onBeforeRender()
080        {
081                super.onBeforeRender();
082
083                CheckGroup<?> group = getGroup();
084
085                // make sure the form we need outputs its markup id.
086                group.getForm().setOutputMarkupId(true);
087        }
088
089        @Override
090        public boolean isEnabled()
091        {
092                CheckGroup<?> group = getGroup();
093                if (group == null)
094                {
095                        return true;
096                }
097                else
098                {
099                        return group.isEnableAllowed() && group.isEnabledInHierarchy();
100                }
101        }
102
103        /**
104         * Find all checkboxes in the containing form with the same input name as the {@link CheckGroup}
105         * .
106         */
107        @Override
108        protected CharSequence getFindCheckboxesFunction()
109        {
110                CheckGroup<?> group = getGroup();
111                if (group == null)
112                {
113                        throw new WicketRuntimeException(
114                                "CheckGroupSelector component [" +
115                                        getPath() +
116                                        "] cannot find its parent CheckGroup. All CheckGroupSelector components must be a child of or below in the hierarchy of a CheckGroup component.");
117                }
118
119                // we search the complete form because the CheckGroup might not output its markup tag or be
120                // located on a <wicket:container>
121                return String.format("Wicket.CheckboxSelector.findCheckboxesFunction('%s','%s')",
122                        group.getForm().getMarkupId(), group.getInputName());
123        }
124}