001    package org.tynamo.mixins;
002    
003    import org.apache.tapestry5.BindingConstants;
004    import org.apache.tapestry5.annotations.InjectContainer;
005    import org.apache.tapestry5.annotations.Log;
006    import org.apache.tapestry5.annotations.Parameter;
007    import org.apache.tapestry5.corelib.components.BeanDisplay;
008    import org.apache.tapestry5.ioc.annotations.Inject;
009    import org.apache.tapestry5.ioc.services.PropertyAccess;
010    import org.apache.tapestry5.services.Environment;
011    import org.tynamo.services.TynamoBeanContext;
012    
013    /**
014     * This mixin pushes the {@link TynamoBeanContext} into the {@link Environment} it's meant to be used in a {@link
015     * BeanDisplay}
016     */
017    public class BeanContextPusher
018    {
019    
020            /**
021             * The component to which this mixin is attached.
022             */
023            @InjectContainer
024            private Object container;
025    
026            @Inject
027            private Environment environment;
028    
029            @Inject
030            private PropertyAccess propertyAccess;
031    
032            /**
033             * The container's property
034             */
035            @Parameter(defaultPrefix = BindingConstants.LITERAL)
036            private String property;
037    
038            @Log
039            void setupRender()
040            {
041                    final TynamoBeanContext tynamoBeanContext = environment.peek(TynamoBeanContext.class);
042    
043                    if (tynamoBeanContext == null)
044                    {
045                            TynamoBeanContext context = new TynamoBeanContext()
046                            {
047                                    public Class getBeanType()
048                                    {
049                                            return getObject().getClass();
050                                    }
051    
052                                    public Object getBeanInstance()
053                                    {
054                                            return getObject();
055                                    }
056    
057                                    public String getCurrentProperty()
058                                    {
059                                            return null;
060                                    }
061    
062                                    public void setCurrentProperty(String s)
063                                    {
064                                    }
065    
066                                    private Object getObject()
067                                    {
068                                            return propertyAccess.get(container, property);
069                                    }
070                            };
071                            environment.push(TynamoBeanContext.class, context);
072                    }
073            }
074    
075            @Log
076            void cleanupRender()
077            {
078                    environment.pop(TynamoBeanContext.class);
079            }
080    
081    }