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.ajax;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.ajax.attributes.AjaxCallListener;
021
022/**
023 * An {@link AjaxCallListener} to disable the associated component while the AJAX request is running.
024 * Please note that under the hood this class uses DOM attribute 'disabled' to disable a component,
025 * hence it can be used only with those HTML components that support this attribute.
026 * If you want to use it with other kinds of components you should override {@link #generateHandlerJavaScript}
027 * to generate the proper enable/disable JavaScript.
028 * 
029 * @author Andrea Del Bene
030 *
031 */
032public class AjaxDisableComponentListener extends AjaxCallListener
033{
034        /**
035         * 
036         */
037        private static final long serialVersionUID = -3772784701483881109L;
038        private static final String DISABLE_ENABLE_JS = ";document.getElementById('%s').disabled = %s;";
039
040        @Override
041        public CharSequence getBeforeHandler(Component component)
042        {
043                return generateHandlerJavaScript(component, true);
044        }
045
046        @Override
047        public CharSequence getCompleteHandler(Component component)
048        {
049                return generateHandlerJavaScript(component, false);
050        }
051
052        @Override
053        public CharSequence getFailureHandler(Component component)
054        {
055                return generateHandlerJavaScript(component, false);
056        }
057
058        /**
059         * Generate the proper enable/disable JavaScript code for the given component.
060         * By default component is enabled/disabled using DOM attribute 'disabled'.
061         * 
062         */
063        protected String generateHandlerJavaScript(Component component, boolean disabled)
064        {
065                return String.format(DISABLE_ENABLE_JS, component.getMarkupId(), disabled);
066        }
067}