001    // Copyright 2008 Thiago H. de Paula Figueiredo
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 br.com.arsmachina.tapestrycrud.components;
016    
017    
018    import org.apache.tapestry5.BindingConstants;
019    import org.apache.tapestry5.PrimaryKeyEncoder;
020    import org.apache.tapestry5.annotations.Parameter;
021    import org.apache.tapestry5.annotations.Property;
022    import org.apache.tapestry5.annotations.SupportsInformalParameters;
023    import org.apache.tapestry5.ioc.annotations.Inject;
024    
025    import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
026    import br.com.arsmachina.tapestrycrud.encoder.Encoder;
027    import br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource;
028    
029    /**
030     * <p>
031     * An alternative to {@link org.apache.tapestry5.corelib.components.EventLink} that, given an object
032     * passed as parameter, uses the corresponding {@link Encoder} to get the context
033     * {@link Encoder#toKey(Object) } activation value.
034     * </p>
035     * 
036     * @author Thiago H. de Paula Figueiredo
037     * @see org.apache.tapestry5.corelib.components.ActionLink
038     * @see ActivationContextEncoder
039     */
040    @SupportsInformalParameters
041    public class PrimaryKeyEventLink {
042    
043            /**
044             * The object that will be used to generate the context for the link.
045             */
046            @Parameter
047            private Object object;
048    
049            /**
050             * The name of the event to be triggered in the parent component. Defaults to the id of the
051             * component. An {@link org.apache.tapestry5.corelib.components.ActionLink} triggers an "action"
052             * event on itself, and EventLink component triggers any arbitrary event on
053             * <em>its container</em>.
054             */
055            @Parameter(defaultPrefix = BindingConstants.LITERAL)
056            @Property
057            @SuppressWarnings("unused")
058            private String event;
059    
060            /**
061             * Binding the zone parameter turns the link into a an Ajax control that causes the related zone
062             * to be updated.
063             */
064            @Parameter(defaultPrefix = BindingConstants.LITERAL)
065            @Property
066            @SuppressWarnings("unused")
067            private String zone;
068    
069            @Inject
070            private PrimaryKeyEncoderSource primaryKeyEncoderSource;
071    
072            @SuppressWarnings("unchecked")
073            public Object getContext() {
074    
075                    PrimaryKeyEncoder encoder = primaryKeyEncoderSource.get(object.getClass());
076                    return encoder.toKey(object);
077    
078            }
079    
080    }