001    // Copyright 2006-2007 Daniel Gredler
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 net.sf.beanform;
016    
017    import org.apache.tapestry.IForm;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRender;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.engine.DirectServiceParameter;
022    import org.apache.tapestry.engine.ILink;
023    import org.apache.tapestry.form.Form;
024    import org.apache.tapestry.form.FormSupport;
025    import org.apache.tapestry.form.FormSupportImpl;
026    
027    /**
028     * A form that expects to be contained by a {@link BeanForm} instance and whose direct
029     * service link corresponds to the containing {@link BeanForm}, rather than itself.
030     *
031     * @author Daniel Gredler
032     */
033    public abstract class LinkForm extends Form {
034    
035        @Override
036        protected FormSupport newFormSupport( IMarkupWriter writer, IRequestCycle cycle ) {
037            return new LinkFormSupport( writer, cycle, this );
038        }
039    
040        /**
041         * Providing an instance of this class in {@link LinkForm#newFormSupport(IMarkupWriter, IRequestCycle)}
042         * is the functional equivalent of overriding {@link Form#getLink(IRequestCycle)} so that it returns a
043         * link to the containing {@link BeanForm}, which we can't do because it is a private method.
044         */
045        public class LinkFormSupport extends FormSupportImpl {
046            public LinkFormSupport( IMarkupWriter writer, IRequestCycle cycle, IForm form ) {
047                super( writer, cycle, form );
048            }
049            @Override
050            public void render( String method, IRender render, ILink link, String scheme, Integer port ) {
051                IRequestCycle cycle = LinkForm.this.getPage().getRequestCycle();
052                BeanForm beanForm = (BeanForm) cycle.getAttribute( BeanForm.BEAN_FORM_ATTRIBUTE );
053                Object parameter = new DirectServiceParameter( beanForm );
054                link = LinkForm.this.getDirectService().getLink( true, parameter );
055                super.render( method, render, link, scheme, port );
056            }
057        }
058    
059    }