001    // Copyright 2008, 2009 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.corelib.components;
016    
017    import org.apache.tapestry5.ComponentAction;
018    import org.apache.tapestry5.ComponentResources;
019    import org.apache.tapestry5.annotations.Environmental;
020    import org.apache.tapestry5.annotations.Events;
021    import org.apache.tapestry5.ioc.annotations.Inject;
022    import org.apache.tapestry5.services.FormSupport;
023    
024    /**
025     * A non visual component used to provide notifications to its container during a form submission. Records actions into
026     * the form on {@link org.apache.tapestry5.annotations.BeginRender} and {@link org.apache.tapestry5.annotations.AfterRender}
027     * that (during the form submission) triggers "BeginSubmit" and "AfterSubmit" events.  The container can receive these
028     * events to perform setup before a group of components process their submission, and perform cleanup afterwards.
029     */
030    @Events({ "BeginSubmit", "AfterSubmit" })
031    public class SubmitNotifier
032    {
033        private static final class TriggerEvent implements ComponentAction<SubmitNotifier>
034        {
035            private final String eventType;
036    
037            public TriggerEvent(String eventType)
038            {
039                this.eventType = eventType;
040            }
041    
042            public void execute(SubmitNotifier component)
043            {
044                component.trigger(eventType);
045            }
046    
047            @Override
048            public String toString()
049            {
050                return String.format("SubmitNotifier.TriggerEvent[%s]", eventType);
051            }
052        }
053    
054    
055        @Inject
056        private ComponentResources resources;
057    
058        @Environmental
059        private FormSupport formSupport;
060    
061        void beginRender()
062        {
063            formSupport.store(this, new TriggerEvent("BeginSubmit"));
064        }
065    
066        void afterRender()
067        {
068            formSupport.store(this, new TriggerEvent("AfterSubmit"));
069        }
070    
071        private void trigger(String eventType)
072        {
073            resources.triggerEvent(eventType, null, null);
074        }
075    }