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 java.util.List; 020 021import org.apache.wicket.Application; 022import org.apache.wicket.ajax.WicketAjaxJQueryResourceReference; 023import org.apache.wicket.markup.ComponentTag; 024import org.apache.wicket.markup.head.HeaderItem; 025import org.apache.wicket.markup.head.IHeaderResponse; 026import org.apache.wicket.markup.head.JavaScriptHeaderItem; 027import org.apache.wicket.markup.head.OnLoadHeaderItem; 028import org.apache.wicket.markup.html.IHeaderContributor; 029import org.apache.wicket.request.resource.JavaScriptResourceReference; 030import org.apache.wicket.request.resource.ResourceReference; 031 032/** 033 * Base class for all Javascript-based "select-all" checkboxes. Provides a simple "select all" 034 * checkbox which can be automatically updated based on the selection state of the checkboxes it 035 * controls (see {@link #wantAutomaticUpdate()}). 036 * 037 * @see CheckboxMultipleChoiceSelector 038 * @see CheckGroupSelector 039 * @see CheckBoxSelector 040 * 041 * @author Carl-Eric Menzel 042 */ 043public abstract class AbstractCheckSelector extends LabeledWebMarkupContainer 044 implements 045 IHeaderContributor 046{ 047 private static final long serialVersionUID = 1L; 048 049 private static final ResourceReference JS = new JavaScriptResourceReference( 050 AbstractCheckSelector.class, "CheckSelector.js") 051 { 052 053 /** 054 */ 055 private static final long serialVersionUID = 1L; 056 057 @Override 058 public List<HeaderItem> getDependencies() 059 { 060 List<HeaderItem> dependencies = super.getDependencies(); 061 ResourceReference wicketEventReference = WicketAjaxJQueryResourceReference.get(); 062 if (Application.exists()) { 063 wicketEventReference = Application.get().getJavaScriptLibrarySettings().getWicketAjaxReference(); 064 } 065 dependencies.add(JavaScriptHeaderItem.forReference(wicketEventReference)); 066 return dependencies; 067 } 068 }; 069 070 /** 071 * Construct. 072 * 073 * @param id 074 * the component id 075 */ 076 public AbstractCheckSelector(String id) 077 { 078 super(id); 079 setOutputMarkupId(true); 080 } 081 082 /** 083 * @return Whether the individual checkboxes should update the state of the Selector. If true, 084 * then when a checkbox is clicked, the state of all checkboxes is tested - if all are 085 * checked, the selector is checked too. If not, the selector is unchecked. 086 */ 087 protected boolean wantAutomaticUpdate() 088 { 089 return true; 090 } 091 092 @Override 093 public void renderHead(IHeaderResponse response) 094 { 095 response.render(JavaScriptHeaderItem.forReference(JS)); 096 097 String findCheckboxes = getFindCheckboxesFunction().toString(); 098 099 // initialize the selector 100 response.render(OnLoadHeaderItem.forScript("Wicket.CheckboxSelector.initializeSelector('" + 101 this.getMarkupId() + "', " + findCheckboxes + ");")); 102 if (wantAutomaticUpdate()) 103 { 104 // initialize the handlers for automatic updating of the selector state 105 response.render(OnLoadHeaderItem.forScript("Wicket.CheckboxSelector.attachUpdateHandlers('" + 106 this.getMarkupId() + "', " + findCheckboxes + ");")); 107 } 108 } 109 110 @Override 111 protected void onComponentTag(ComponentTag tag) 112 { 113 super.onComponentTag(tag); 114 if (isEnableAllowed() && isEnabledInHierarchy()) 115 { 116 tag.remove("disabled"); 117 } 118 else 119 { 120 tag.put("disabled", "disabled"); 121 } 122 checkComponentTag(tag, "input"); 123 checkComponentTagAttribute(tag, "type", "checkbox"); 124 } 125 126 /** 127 * Concrete subclasses must override this to provide a Javascript function that returns the IDs 128 * of all checkboxes that should be controlled by this selector. 129 * 130 * @return a String containing a Javascript expression that evaluates to a function(!). This 131 * function must return an array containing the IDs of all checkbox input elements that 132 * this selector should control. 133 */ 134 protected abstract CharSequence getFindCheckboxesFunction(); 135}