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.annotations.Parameter;
020    import org.apache.tapestry5.annotations.Property;
021    import org.apache.tapestry5.annotations.SupportsInformalParameters;
022    import org.apache.tapestry5.corelib.base.AbstractLink;
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.ActivationContextEncoderSource;
028    
029    /**
030     * A replacement to {@link org.apache.tapestry5.corelib.components.PageLink} that, given an object
031     * passed as parameter, uses the corresponding {@link Encoder} to get the context
032     * {@link Encoder#toKey(Object) } activation value.
033     * 
034     * @author Thiago H. de Paula Figueiredo
035     */
036    @SupportsInformalParameters
037    public class ActivationContextPageLink extends AbstractLink {
038    
039            /**
040             * The logical name of the page to link to.
041             */
042            @SuppressWarnings("unused")
043            @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
044            @Property
045            private String page;
046            
047            /**
048             * The object from which the activation context value will be extracted.
049             */
050            @Parameter(required = true)
051            private Object object;
052    
053            @Inject
054            private ActivationContextEncoderSource activationContextEncoderSource;
055    
056            @SuppressWarnings("unchecked")
057            public Object getContext() {
058    
059                    ActivationContextEncoder encoder;
060                    encoder = activationContextEncoderSource.get(object.getClass());
061                    return encoder.toActivationContext(object);
062                    
063            }
064    
065    }