001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.base;
016    
017    import org.apache.tapestry5.BindingConstants;
018    import org.apache.tapestry5.ClientElement;
019    import org.apache.tapestry5.ComponentResources;
020    import org.apache.tapestry5.ValidationDecorator;
021    import org.apache.tapestry5.annotations.Environmental;
022    import org.apache.tapestry5.annotations.Mixin;
023    import org.apache.tapestry5.annotations.Parameter;
024    import org.apache.tapestry5.annotations.SetupRender;
025    import org.apache.tapestry5.corelib.mixins.DiscardBody;
026    import org.apache.tapestry5.corelib.mixins.RenderDisabled;
027    import org.apache.tapestry5.ioc.annotations.Inject;
028    import org.apache.tapestry5.services.javascript.JavaScriptSupport;
029    
030    /**
031     * @version $Id: AbstractAjaxField.java 674 2010-07-29 12:47:25Z homburgs $
032     */
033    abstract public class AbstractAjaxField implements ClientElement
034    {
035        /**
036         * If true, then the field will render out with a disabled attribute (to turn off client-side
037         * behavior). Further, a disabled field ignores any value in the request when the form is
038         * submitted.
039         */
040        @Parameter("false")
041        private boolean disabled;
042    
043        @Mixin
044        private RenderDisabled renderDisabled;
045    
046        @Mixin
047        private DiscardBody discardBody;
048    
049        @Environmental
050        private ValidationDecorator decorator;
051    
052        /**
053         * The id used to generate a page-unique client-side identifier for the component. If a
054         * component renders multiple times, a suffix will be appended to the to id to ensure
055         * uniqueness. The uniqued value may be accessed via the
056         * {@link #getClientId() clientId property}.
057         */
058        @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
059        private String clientId;
060    
061        private String assignedClientId;
062    
063        @Environmental
064        private JavaScriptSupport javascriptSupport;
065    
066        @Inject
067        private ComponentResources resources;
068    
069        @SetupRender
070        void setupRender()
071        {
072            assignedClientId = javascriptSupport.allocateClientId(clientId);
073        }
074    
075        public final String getClientId()
076        {
077            return assignedClientId;
078        }
079    
080        public final boolean isDisabled()
081        {
082            return disabled;
083        }
084    }