You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2011/06/07 13:07:30 UTC
svn commit: r1132957 - in /wicket/trunk:
wicket-core/src/main/java/org/apache/wicket/markup/html/form/
wicket-examples/src/main/java/org/apache/wicket/examples/compref/
Author: mgrigorov
Date: Tue Jun 7 11:07:29 2011
New Revision: 1132957
URL: http://svn.apache.org/viewvc?rev=1132957&view=rev
Log:
WICKET-3718 Introduce select-all components for all types of checkbox
Apply the patch and transform the quickstart application to example application.
Added:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.js
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.js
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.js
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.js
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.html
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.java
wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/Index.html
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.java?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.java (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.java Tue Jun 7 11:07:29 2011
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form;
+
+import org.apache.wicket.ajax.WicketAjaxReference;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.html.IHeaderContributor;
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.markup.html.WicketEventReference;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+
+/**
+ * Base class for all Javascript-based "select-all" checkboxes. Provides a simple "select all"
+ * checkbox which can be automatically updated based on the selection state of the checkboxes it
+ * controls (see {@link #wantAutomaticUpdate()}).
+ *
+ * @see CheckboxMultipleChoiceSelector
+ * @see CheckGroupSelector
+ * @see CheckBoxSelector
+ *
+ * @author Carl-Eric Menzel <cm...@wicketbuch.de>
+ */
+public abstract class AbstractCheckSelector extends LabeledWebMarkupContainer
+ implements
+ IHeaderContributor
+{
+ private static final long serialVersionUID = 1L;
+
+ private static final ResourceReference JS = new PackageResourceReference(
+ AbstractCheckSelector.class, "AbstractCheckSelector.js");
+
+ /**
+ * Construct.
+ *
+ * @param id
+ * the component id
+ */
+ public AbstractCheckSelector(String id)
+ {
+ super(id);
+ setOutputMarkupId(true);
+ }
+
+ /**
+ * @return Whether the individual checkboxes should update the state of the Selector. If true,
+ * then when a checkbox is clicked, the state of all checkboxes is tested - if all are
+ * checked, the selector is checked too. If not, the selector is unchecked.
+ */
+ protected boolean wantAutomaticUpdate()
+ {
+ return true;
+ }
+
+ @Override
+ public void renderHead(IHeaderResponse response)
+ {
+ // make sure we have all the javascript we need
+ response.renderJavaScriptReference(WicketEventReference.INSTANCE);
+ response.renderJavaScriptReference(WicketAjaxReference.INSTANCE);
+ response.renderJavaScriptReference(JS);
+ String findCheckboxes = getFindCheckboxesFunction().toString();
+
+ // initialize the selector
+ response.renderOnLoadJavaScript("Wicket.CheckboxSelector.initializeSelector('" +
+ this.getMarkupId() + "', " + findCheckboxes + ");");
+ if (wantAutomaticUpdate())
+ {
+ // initialize the handlers for automatic updating of the selector state
+ response.renderOnLoadJavaScript("Wicket.CheckboxSelector.attachUpdateHandlers('" +
+ this.getMarkupId() + "', " + findCheckboxes + ");");
+ }
+ }
+
+ @Override
+ protected void onComponentTag(ComponentTag tag)
+ {
+ super.onComponentTag(tag);
+ if (isEnableAllowed() && isEnabledInHierarchy())
+ {
+ tag.remove("disabled");
+ }
+ else
+ {
+ tag.put("disabled", "disabled");
+ }
+ checkComponentTag(tag, "input");
+ checkComponentTagAttribute(tag, "type", "checkbox");
+ }
+
+ /**
+ * Concrete subclasses must override this to provide a Javascript function that returns the IDs
+ * of all checkboxes that should be controlled by this selector.
+ *
+ * @return a String containing a Javascript expression that evaluates to a function(!). This
+ * function must return an array containing the IDs of all checkbox input elements that
+ * this selector should control.
+ */
+ protected abstract CharSequence getFindCheckboxesFunction();
+}
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.js
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.js?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.js (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractCheckSelector.js Tue Jun 7 11:07:29 2011
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// introduce a namespace
+if (typeof (Wicket.CheckboxSelector) == "undefined") {
+ Wicket.CheckboxSelector = {};
+
+ /**
+ * Called in the onclick handler of the select-all-checkbox. Updates all
+ * associated checkboxes by simulating a click on those that need to have
+ * their state changed.
+ *
+ * @param selectorId
+ * the ID of the selector checkbox
+ * @param findCheckboxes
+ * a function that returns an array containing the IDs of all
+ * associated checkboxes
+ */
+ // adapted from AjaxFormChoiceComponentUpdatingBehavior
+ Wicket.CheckboxSelector.updateAllCheckboxes = function(newCheckedState, findCheckboxes) {
+ var checkboxes = findCheckboxes();
+ for (i in checkboxes) {
+ var checkbox = checkboxes[i];
+ if (checkbox.checked != newCheckedState) {
+ checkbox.click();
+ }
+ }
+ };
+
+ /**
+ * Called in the onclick handlers of the associated checkboxes if the auto
+ * update feature is active. Checks the state of all checkboxes - if all are
+ * checked, the selector is checked too. Otherwise the selector is
+ * unchecked.
+ *
+ * @param selectorId
+ * the ID of the selector checkbox
+ * @param findCheckboxes
+ * a function that returns an array containing the IDs of all
+ * associated checkboxes
+ */
+ Wicket.CheckboxSelector.updateSelectorState = function(selectorId, findCheckboxes) {
+ var checkboxes = findCheckboxes();
+ var allChecked = true;
+ for (i in checkboxes) {
+ if (!(checkboxes[i].checked)) {
+ allChecked = false;
+ break;
+ }
+ }
+ var selector = wicketGet(selectorId);
+ selector.checked = allChecked;
+ };
+
+ /**
+ * Called in the onLoad event if the auto update feature is active. Attaches
+ * an onclick handler to all associated checkboxes.
+ *
+ * @param selectorId
+ * the ID of the selector checkbox
+ * @param findCheckboxes
+ * a function that returns an array containing the IDs of all
+ * associated checkboxes
+ */
+ Wicket.CheckboxSelector.attachUpdateHandlers = function(selectorId, findCheckboxes) {
+ var checkboxes = findCheckboxes();
+ for (i in checkboxes) {
+ Wicket.Event.add(checkboxes[i], 'click', function() {
+ Wicket.CheckboxSelector.updateSelectorState(selectorId,
+ findCheckboxes);
+ });
+ }
+ // update selector state once to get the right initial state
+ Wicket.CheckboxSelector.updateSelectorState(selectorId, findCheckboxes);
+ };
+
+ /**
+ * Called in the onLoad event to initialize the selector checkbox.
+ * @param selectorId
+ * the ID of the selector checkbox
+ * @param findCheckboxes
+ * a function that returns an array containing the IDs of all
+ * associated checkboxes
+ */
+ Wicket.CheckboxSelector.initializeSelector = function(selectorId, findCheckboxes) {
+ var selector = wicketGet(selectorId);
+ Wicket.Event.add(selector, 'click', function() {
+ Wicket.CheckboxSelector.updateAllCheckboxes(selector.checked,
+ findCheckboxes);
+ });
+ }
+}
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.java?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.java (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.java Tue Jun 7 11:07:29 2011
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form;
+
+import java.util.Arrays;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+
+
+/**
+ * A Javascript-based "Select All" checkbox component that works with a loose collection of
+ * {@link CheckBox} components. By default, clicking on any of the controlled checkboxes
+ * automatically updates the state of the "select all" checkbox. Override
+ * {@link AbstractCheckSelector#wantAutomaticUpdate()} to change this.
+ *
+ * @author Carl-Eric Menzel <cm...@wicketbuch.de>
+ */
+public class CheckBoxSelector extends AbstractCheckSelector
+{
+ private static final long serialVersionUID = 1L;
+
+ private final static ResourceReference JS = new PackageResourceReference(
+ CheckBoxSelector.class, "CheckBoxSelector.js");
+
+ /**
+ * Javascript array literal containing the markup IDs of the checkboxes we want to
+ * check/uncheck. Example: "['foo', 'bar', 'baz']". Generated by
+ * JavascriptUtils.buildMarkupIdJSArrayLiteral().
+ */
+ private final String checkBoxIdArrayLiteral;
+
+ /**
+ * @param id
+ * The component ID
+ * @param boxes
+ * The CheckBoxes this selector will control.
+ */
+ public CheckBoxSelector(String id, CheckBox... boxes)
+ {
+ super(id);
+ setOutputMarkupId(true);
+ checkBoxIdArrayLiteral = buildMarkupIdJSArrayLiteral(Arrays.asList(boxes));
+ }
+
+ @Override
+ protected CharSequence getFindCheckboxesFunction()
+ {
+ return "Wicket.CheckboxSelector.Checkboxes.findCheckboxesFunction(" +
+ checkBoxIdArrayLiteral + ")";
+ }
+
+ @Override
+ public void renderHead(IHeaderResponse response)
+ {
+ super.renderHead(response);
+ response.renderJavaScriptReference(JS);
+ }
+
+
+ /**
+ * Builds a JavaScript array literal containing the markup IDs of the given components. Example:
+ * "['foo', 'bar', 'baz']".
+ *
+ * @param components
+ * The components whose IDs we need
+ * @return a properly formatted JS array literal
+ */
+ private String buildMarkupIdJSArrayLiteral(final Iterable<? extends Component> components)
+ {
+ StringBuilder buf = new StringBuilder();
+ buf.append("[");
+ if (components.iterator().hasNext())
+ {
+ for (Component component : components)
+ {
+ component.setOutputMarkupId(true);
+ buf.append("'").append(component.getMarkupId()).append("', ");
+ }
+ buf.delete(buf.length() - 2, buf.length());
+ }
+ buf.append("]");
+ return buf.toString();
+ }
+
+}
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.js
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.js?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.js (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxSelector.js Tue Jun 7 11:07:29 2011
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// introduce a namespace, just to be nice
+if (typeof (Wicket.CheckboxSelector.Checkboxes) == "undefined") {
+ Wicket.CheckboxSelector.Checkboxes = {};
+ /**
+ * Returns a closure that finds all checkboxes with the given IDs.
+ *
+ * @param parentChoiceId
+ * An array containing the markup IDs of all checkboxes this
+ * selector should control.
+ */
+ Wicket.CheckboxSelector.Checkboxes.findCheckboxesFunction = function(checkBoxIDs) {
+ return function() {
+ var result = new Array();
+ for (i in checkBoxIDs) {
+ var checkBox = wicketGet(checkBoxIDs[i]);
+ result.push(checkBox)
+ }
+ return result;
+ }
+ };
+}
Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.java?rev=1132957&r1=1132956&r2=1132957&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.java Tue Jun 7 11:07:29 2011
@@ -17,8 +17,9 @@
package org.apache.wicket.markup.html.form;
import org.apache.wicket.WicketRuntimeException;
-import org.apache.wicket.markup.ComponentTag;
-import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
/**
* Selects and deselects all Check components under the same CheckGroup as itself. Selection
@@ -31,73 +32,104 @@ import org.apache.wicket.markup.html.Web
* @see org.apache.wicket.markup.html.form.Check
*
* @author Igor Vaynberg
- *
+ * @author Carl-Eric Menzel
*/
-public class CheckGroupSelector extends LabeledWebMarkupContainer
+public class CheckGroupSelector extends AbstractCheckSelector
{
/** */
private static final long serialVersionUID = 1L;
- private final CheckGroup<?> group;
+ private final static ResourceReference JS = new PackageResourceReference(
+ CheckGroupSelector.class, "CheckGroupSelector.js");
+
+ private CheckGroup<?> group;
/**
- * @see WebMarkupContainer#WebMarkupContainer(String)
+ * A Selector that will look for a {@link CheckGroup} in its parent hierarchy.
+ *
+ * @param id
+ * component id
*/
public CheckGroupSelector(String id)
{
this(id, null);
}
+ @Override
+ public void renderHead(IHeaderResponse response)
+ {
+ super.renderHead(response);
+ response.renderJavaScriptReference(JS);
+ }
+
/**
+ * A Selector that will work with the given group.
+ *
* @param id
+ * component id
* @param group
- * @see WebMarkupContainer#WebMarkupContainer(String)
+ * group to work with
*/
public CheckGroupSelector(String id, CheckGroup<?> group)
{
super(id);
this.group = group;
+ setOutputMarkupId(true);
}
- /**
- *
- * @see org.apache.wicket.Component#onComponentTag(org.apache.wicket.markup.ComponentTag)
- */
- @Override
- protected void onComponentTag(ComponentTag tag)
+ private CheckGroup<?> getGroup()
{
- // must be attached to <input type="checkbox" .../> tag
- checkComponentTag(tag, "input");
- checkComponentTagAttribute(tag, "type", "checkbox");
-
CheckGroup<?> group = this.group;
if (group == null)
{
group = findParent(CheckGroup.class);
- if (group == null)
+ this.group = group;
+ }
+ return group;
+ }
+
+ @Override
+ protected void onInitialize()
+ {
+ // try and make sure the form we need outputs its markup id.
+ super.onInitialize();
+ CheckGroup<?> group = getGroup();
+ if (group != null)
+ {
+ Form<?> form = group.getForm();
+ if (form != null)
{
- throw new WicketRuntimeException(
- "CheckGroupSelector component [" +
- getPath() +
- "] cannot find its parent CheckGroup. All CheckGroupSelector components must be a child of or below in the hierarchy of a CheckGroup component.");
+ form.setOutputMarkupId(true);
}
}
+ }
- final boolean groupEnabled = group.isEnabledInHierarchy() && group.isEnableAllowed();
- final boolean selfEnabled = isEnabledInHierarchy() && isEnableAllowed();
- if (groupEnabled && selfEnabled)
+ @Override
+ public boolean isEnabled()
+ {
+ CheckGroup<?> group = getGroup();
+ if (group == null)
{
- tag.put(
- "onclick",
- "var cb=this.form['" +
- group.getInputName() +
- "']; if (cb!=null) { if (!isNaN(cb.length)) { for(var i=0;i<cb.length;i++) { if (cb[i].disabled) continue; if (cb[i].checked != this.checked) {cb[i].click();} } } else { if (!cb.disabled&&cb.checked != this.checked) {cb.click();} } }");
+ return true;
}
else
{
- tag.put("disabled", "disabled");
+ return group.isEnableAllowed() && group.isEnabledInHierarchy();
}
+ }
- super.onComponentTag(tag);
+ @Override
+ protected CharSequence getFindCheckboxesFunction()
+ {
+ CheckGroup<?> group = getGroup();
+ if (group == null)
+ {
+ throw new WicketRuntimeException(
+ "CheckGroupSelector component [" +
+ getPath() +
+ "] cannot find its parent CheckGroup. All CheckGroupSelector components must be a child of or below in the hierarchy of a CheckGroup component.");
+ }
+ return "Wicket.CheckboxSelector.Group.findCheckboxesFunction('" +
+ group.getForm().getMarkupId() + "','" + group.getInputName() + "')";
}
}
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.js
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.js?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.js (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckGroupSelector.js Tue Jun 7 11:07:29 2011
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// introduce a namespace, just to be nice
+if (typeof (Wicket.CheckboxSelector.Group) == "undefined") {
+ Wicket.CheckboxSelector.Group = {};
+ /**
+ * Returns a closure that finds all checkboxes associated with the given
+ * CheckGroup.
+ *
+ * @param formId
+ * The markup ID of the containing form (needed because the
+ * selector might be outside the form)
+ * @param groupName
+ * The input name of the CheckGroup
+ */
+ Wicket.CheckboxSelector.Group.findCheckboxesFunction = function(formId, groupName) {
+ return function() {
+ var result = new Array();
+ var parentForm = wicketGet(formId);
+ var parentGroup = parentForm[groupName];
+ for ( var i = 0; i < parentGroup.length; i++) {
+ var checkbox = parentGroup[i];
+ result.push(checkbox);
+ }
+ return result;
+ }
+ };
+}
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.java?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.java (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.java Tue Jun 7 11:07:29 2011
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form;
+
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.request.resource.PackageResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+
+
+/**
+ * A Javascript-based "Select All" checkbox component that works with {@link CheckBoxMultipleChoice}
+ * . By default, clicking on any of the controlled checkboxes automatically updates the state of the
+ * "select all" checkbox. Override {@link AbstractCheckSelector#wantAutomaticUpdate()} to change
+ * this.
+ *
+ * @author Carl-Eric Menzel <cm...@wicketbuch.de>
+ */
+public class CheckboxMultipleChoiceSelector extends AbstractCheckSelector
+{
+ private static final long serialVersionUID = 1L;
+
+ private final static ResourceReference JS = new PackageResourceReference(
+ CheckboxMultipleChoiceSelector.class, "CheckboxMultipleChoiceSelector.js");
+
+ private final CheckBoxMultipleChoice<?> choiceComponent;
+
+ /**
+ * @param id
+ * The component ID
+ * @param choiceComponent
+ * The checkbox choice component this Selector will manage.
+ */
+ public CheckboxMultipleChoiceSelector(String id, CheckBoxMultipleChoice<?> choiceComponent)
+ {
+ super(id);
+ this.choiceComponent = choiceComponent;
+ choiceComponent.setOutputMarkupId(true);
+ setOutputMarkupId(true);
+ }
+
+ @Override
+ public void renderHead(IHeaderResponse response)
+ {
+ super.renderHead(response);
+ response.renderJavaScriptReference(JS);
+ }
+
+ @Override
+ protected CharSequence getFindCheckboxesFunction()
+ {
+ return "Wicket.CheckboxSelector.Choice.findCheckboxesFunction('" +
+ choiceComponent.getMarkupId() + "')";
+ }
+}
Added: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.js
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.js?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.js (added)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckboxMultipleChoiceSelector.js Tue Jun 7 11:07:29 2011
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// introduce a namespace, just to be nice
+if (typeof (Wicket.CheckboxSelector.Choice) == "undefined") {
+ Wicket.CheckboxSelector.Choice = {};
+
+ /**
+ * Returns a closure that finds all checkboxes associated with the given
+ * CheckboxMultipleChoice.
+ *
+ * @param parentChoiceId
+ * The markup ID of the CheckboxMultipleChoise
+ */
+ // adapted from AjaxFormChoiceComponentUpdatingBehavior
+ Wicket.CheckboxSelector.Choice.findCheckboxesFunction = function(
+ parentChoiceId) {
+ return function() {
+ var result = new Array();
+ var inputNodes = wicketGet(parentChoiceId).getElementsByTagName(
+ 'input');
+ for ( var i = 0; i < inputNodes.length; i++) {
+ var inputNode = inputNodes[i];
+ if (inputNode.id.indexOf(parentChoiceId + '-') >= 0) {
+ result.push(inputNode);
+ }
+ }
+ return result;
+ }
+ };
+}
Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.html?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.html (added)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.html Tue Jun 7 11:07:29 2011
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org">
+ <head>
+ <title>Wicket Examples - component reference</title>
+ <link rel="stylesheet" type="text/css" href="style.css"/>
+ </head>
+ <body>
+ <span wicket:id="mainNavigation"/>
+
+ <h1>wicket.markup.html.form.CheckGroup</h1>
+ <wicket:link><a href="Index.html">[back to the reference]</a></wicket:link>
+
+ <p>
+ A CheckBoxSelector selects and deselects all Check components under the same CheckGroup as itself
+ </p>
+ <p>
+ <form wicket:id="form">
+ CheckGroup:<br/>
+ <div wicket:id="checkgroup">
+ <input type="checkbox" wicket:id="check1"/> 1
+ <input type="checkbox" wicket:id="check2"/> 2
+ <input type="checkbox" wicket:id="check3"/> 3
+ <input type="checkbox" wicket:id="check4"/> 4
+ <input type="checkbox" wicket:id="groupSelector"/> select all
+ </div>
+ <input type="checkbox" wicket:id="groupSelectorOutside"/> select all (outside group)
+ <hr/>
+ CheckboxMultipleChoice:
+ <div wicket:id="choice"/>
+ <input type="checkbox" wicket:id="choiceSelector"/> select all
+ <hr/>
+ Loose Checkboxes:
+ <input type="checkbox" wicket:id="looseCheck1"/> 1
+ <input type="checkbox" wicket:id="looseCheck2"/> 2
+ <input type="checkbox" wicket:id="looseCheck3"/> 3
+ <input type="checkbox" wicket:id="looseCheck4"/> 4
+ <input type="checkbox" wicket:id="looseSelector"/> select all
+ </form>
+ </p>
+ </body>
+</html>
Added: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.java?rev=1132957&view=auto
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.java (added)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckBoxSelectorPage.java Tue Jun 7 11:07:29 2011
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.examples.compref;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.apache.wicket.examples.WicketExamplePage;
+import org.apache.wicket.markup.html.form.Check;
+import org.apache.wicket.markup.html.form.CheckBox;
+import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
+import org.apache.wicket.markup.html.form.CheckBoxSelector;
+import org.apache.wicket.markup.html.form.CheckGroup;
+import org.apache.wicket.markup.html.form.CheckGroupSelector;
+import org.apache.wicket.markup.html.form.CheckboxMultipleChoiceSelector;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+
+/**
+ * An example page for {@link CheckBoxSelector}
+ */
+public class CheckBoxSelectorPage extends WicketExamplePage
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Constructor
+ *
+ * @param parameters
+ * Page parameters
+ */
+ public CheckBoxSelectorPage(final PageParameters parameters)
+ {
+ super(parameters);
+
+ final Form<?> form = new Form<Void>("form");
+ add(form);
+ final CheckGroup<Integer> checkgroup = new CheckGroup<Integer>("checkgroup", Arrays.asList(
+ 1, 2, 3, 4));
+ form.add(checkgroup);
+ checkgroup.add(new Check<Integer>("check1", Model.of(1)));
+ checkgroup.add(new Check<Integer>("check2", Model.of(2)));
+ checkgroup.add(new Check<Integer>("check3", Model.of(3)));
+ checkgroup.add(new Check<Integer>("check4", Model.of(4)));
+ // one selector inside the checkgroup...
+ checkgroup.add(new CheckGroupSelector("groupSelector")
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected boolean wantAutomaticUpdate()
+ {
+ return true; // override again to enable auto update. in
+ // CheckGroupSelector it is disabled for
+ // backwards compatibility.
+ }
+ });
+
+ // ...and one selector outside the checkgroup
+ form.add(new CheckGroupSelector("groupSelectorOutside", checkgroup)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected boolean wantAutomaticUpdate()
+ {
+ return true; // override again to enable auto update. in
+ // CheckGroupSelector it is disabled for
+ // backwards compatibility.
+ }
+ });
+ final CheckBoxMultipleChoice<Integer> choice = new CheckBoxMultipleChoice<Integer>(
+ "choice", Model.ofList(new ArrayList()), Arrays.asList(1, 2, 3, 4));
+ form.add(choice);
+ form.add(new CheckboxMultipleChoiceSelector("choiceSelector", choice));
+ final CheckBox loose1 = new CheckBox("looseCheck1", Model.of(Boolean.FALSE));
+ final CheckBox loose2 = new CheckBox("looseCheck2", Model.of(Boolean.FALSE));
+ final CheckBox loose3 = new CheckBox("looseCheck3", Model.of(Boolean.FALSE));
+ final CheckBox loose4 = new CheckBox("looseCheck4", Model.of(Boolean.FALSE));
+ form.add(loose1, loose2, loose3, loose4);
+ form.add(new CheckBoxSelector("looseSelector", loose1, loose2, loose3, loose4));
+ }
+}
Modified: wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/Index.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/Index.html?rev=1132957&r1=1132956&r2=1132957&view=diff
==============================================================================
--- wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/Index.html (original)
+++ wicket/trunk/wicket-examples/src/main/java/org/apache/wicket/examples/compref/Index.html Tue Jun 7 11:07:29 2011
@@ -63,8 +63,9 @@
<li><a href="TextAreaPage.html">wicket.markup.html.form.TextArea</a></li>
<li><a href="CheckBoxPage.html">wicket.markup.html.form.CheckBox</a></li>
<li><a href="CheckGroupPage.html">wicket.markup.html.form.CheckGroup, wicket.markup.html.form.Check, and wicket.markup.html.form.CheckGroupSelector</a></li>
- <li><a href="CheckGroupPage2.html">Embedded wicket.markup.html.form.CheckGroup, wicket.markup.html.form.Check, and wicket.markup.html.form.CheckGroupSelector</a></li>
+ <li><a href="CheckGroupPage2.html">Embedded wicket.markup.html.form.CheckGroup, wicket.markup.html.form.Check, and wicket.markup.html.form.CheckGroupSelector</a></li>
<li><a href="CheckBoxMultipleChoicePage.html">wicket.markup.html.form.CheckBoxMultipleChoice</a></li>
+ <li><a href="CheckBoxSelectorPage.html">wicket.markup.html.form.CheckBoxSelector</a></li>
<li><a href="PalettePage.html">wicket.markup.html.form.palette.Palette</a></li>
<li><a href="DropDownChoicePage.html">wicket.markup.html.form.DropDownChoice</a></li>
<li><a href="ListChoicePage.html">wicket.markup.html.form.ListChoice</a></li>