001    package org.trails.component;
002    
003    import java.util.List;
004    import java.util.ArrayList;
005    
006    import org.apache.tapestry.annotations.Parameter;
007    import org.apache.tapestry.components.Block;
008    import org.trails.descriptor.IClassDescriptor;
009    import org.trails.descriptor.IPropertyDescriptor;
010    
011    public abstract class ClassDescriptorComponent extends TrailsComponent
012    {
013    
014            @Parameter(required = false, defaultValue = "page.classDescriptor", cache = true)
015            public abstract IClassDescriptor getClassDescriptor();
016    
017            @Parameter(required = false, defaultValue = "ognl:null", cache = true)
018            public abstract List<String> getPropertyNames();
019    
020            public List<IPropertyDescriptor> getPropertyDescriptors()
021            {
022                    if (getPropertyNames() == null || getPropertyNames().size() == 0)
023                    {
024                            List<IPropertyDescriptor> displayingPropertyDescriptors = new ArrayList<IPropertyDescriptor>();
025                            for (IPropertyDescriptor propertyDescriptor : getClassDescriptor().getPropertyDescriptors())
026                            {
027                                    if (shouldDisplay(propertyDescriptor))
028                                    {
029                                            displayingPropertyDescriptors.add(propertyDescriptor);
030                                    }
031                            }
032                            return displayingPropertyDescriptors;
033                    } else
034                    {
035                            return getClassDescriptor().getPropertyDescriptors(getPropertyNames());
036                    }
037            }
038    
039            /**
040             * Hook method to allow subclasses to modify when an IPropertyDescriptor should be displayed.
041             *
042             * @param descriptor
043             * @return
044             */
045            protected boolean shouldDisplay(IPropertyDescriptor descriptor)
046            {
047                    return !descriptor.isHidden();
048            }
049    
050            public boolean hasBlock(String propertyName)
051            {
052                    return getPage().getComponents().containsKey(propertyName);
053            }
054    
055            public Block getBlock(String propertyName)
056            {
057                    if (hasBlock(propertyName))
058                    {
059                            return (Block) getPage().getComponent(propertyName);
060                    }
061                    return null;
062            }
063    }