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.ActionLink} that, given an
032 * object 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 PrimaryKeyActionLink {
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 * Binding the zone parameter turns the link into a an Ajax control that causes the related zone to be updated.
051 */
052 @SuppressWarnings("unused")
053 @Parameter(defaultPrefix = BindingConstants.LITERAL)
054 @Property
055 private String zone;
056
057 @Inject
058 private PrimaryKeyEncoderSource primaryKeyEncoderSource;
059
060 @SuppressWarnings("unchecked")
061 public Object getContext() {
062
063 PrimaryKeyEncoder encoder = primaryKeyEncoderSource.get(object.getClass());
064 return encoder.toKey(object);
065
066 }
067
068 }