View Javadoc

1   // Copyright 2008 Thiago H. de Paula Figueiredo
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package br.com.arsmachina.tapestrycrud.components;
16  
17  
18  import org.apache.tapestry5.BindingConstants;
19  import org.apache.tapestry5.PrimaryKeyEncoder;
20  import org.apache.tapestry5.annotations.Parameter;
21  import org.apache.tapestry5.annotations.Property;
22  import org.apache.tapestry5.annotations.SupportsInformalParameters;
23  import org.apache.tapestry5.ioc.annotations.Inject;
24  
25  import br.com.arsmachina.tapestrycrud.encoder.ActivationContextEncoder;
26  import br.com.arsmachina.tapestrycrud.encoder.Encoder;
27  import br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource;
28  
29  /**
30   * <p>
31   * An alternative to {@link org.apache.tapestry5.corelib.components.ActionLink} that, given an
32   * object passed as parameter, uses the corresponding {@link Encoder} to get the context
33   * {@link Encoder#toKey(Object) } activation value.
34   * </p>
35   * 
36   * @author Thiago H. de Paula Figueiredo
37   * @see org.apache.tapestry5.corelib.components.ActionLink
38   * @see ActivationContextEncoder
39   */
40  @SupportsInformalParameters
41  public class PrimaryKeyActionLink {
42  
43  	/**
44  	 * The object that will be used to generate the context for the link.
45  	 */
46  	@Parameter
47  	private Object object;
48  	
49      /**
50       * Binding the zone parameter turns the link into a an Ajax control that causes the related zone to be updated.
51       */
52      @SuppressWarnings("unused")
53  	@Parameter(defaultPrefix = BindingConstants.LITERAL)
54      @Property
55      private String zone;
56  	
57  	@Inject
58  	private PrimaryKeyEncoderSource primaryKeyEncoderSource;
59  	
60  	@SuppressWarnings("unchecked")
61  	public Object getContext() {
62  
63  		PrimaryKeyEncoder encoder = primaryKeyEncoderSource.get(object.getClass());
64  		return encoder.toKey(object);
65  
66  	}
67  
68  }