001    // Copyright 2007, 2008 The Apache Software Foundation
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 org.apache.tapestry5.corelib.components;
016    
017    import org.apache.tapestry5.BindingConstants;
018    import org.apache.tapestry5.ComponentResources;
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.beaneditor.BeanModel;
023    import org.apache.tapestry5.beaneditor.PropertyModel;
024    import org.apache.tapestry5.internal.beaneditor.BeanModelUtils;
025    import org.apache.tapestry5.ioc.annotations.Inject;
026    import org.apache.tapestry5.services.BeanModelSource;
027    
028    /**
029     * Used to display the properties of a bean, using an underlying {@link BeanModel}. The output definition list: a
030     * <dl> element containing a series of <dt>/<dd> pairs.  The property label is used as the <dt>
031     * and the property value (formatted as per the datatype) is the <dd>. Only properties that have a known data type
032     * are displayed.
033     * <p/>
034     * The property id is used as the class attribute of the &lt;dt&gt; and &lt;dd&gt; element, allowing CSS customization
035     * per property.  This does not occur when lean is bound to true.
036     * <p/>
037     * The outer &lt;dl&gt; element has the CSS class "t-beandisplay".
038     *
039     * @see org.apache.tapestry5.beaneditor.DataType
040     * @see BeanModel
041     */
042    @SupportsInformalParameters
043    public class BeanDisplay
044    {
045    
046        /**
047         * The object to be rendered; if not explicitly bound, a default binding to a property whose name matches this
048         * component's id will be used.
049         */
050        @Parameter(required = true, allowNull = false, autoconnect = true)
051        @Property(write = false)
052        private Object object;
053    
054        /**
055         * If true, then the CSS class attribute on the &lt;dt&gt; and &lt;dd&gt; elements will be ommitted.
056         */
057        @Parameter(value = "false")
058        private boolean lean;
059    
060        /**
061         * The model that identifies the parameters to be displayed, their order, and every other aspect. If not specified,
062         * a default bean model will be created from the type of the object bound to the object parameter.
063         */
064        @Parameter
065        @Property(write = false)
066        private BeanModel model;
067    
068        /**
069         * A comma-separated list of property names to be retained from the {@link org.apache.tapestry5.beaneditor.BeanModel}.
070         * Only these properties will be retained, and the properties will also be reordered. The names are
071         * case-insensitive.
072         */
073        @SuppressWarnings("unused")
074        @Parameter(defaultPrefix = BindingConstants.LITERAL)
075        private String include;
076    
077        /**
078         * A comma-separated list of property names to be removed from the {@link BeanModel}. The names are
079         * case-insensitive.
080         */
081        @Parameter(defaultPrefix = BindingConstants.LITERAL)
082        private String exclude;
083    
084        /**
085         * A comma-separated list of property names indicating the order in which the properties should be presented. The
086         * names are case insensitive. Any properties not indicated in the list will be appended to the end of the display
087         * order.
088         */
089        @Parameter(defaultPrefix = BindingConstants.LITERAL)
090        private String reorder;
091    
092        /**
093         * Where to search for local overrides of property display blocks as block parameters. Further, the container of the
094         * overrides is used as the source for overridden validation messages. This is normally the component itself, but
095         * when the component is used within a BeanEditForm, it will be the BeanEditForm's block parameter that will be
096         * searched.
097         */
098        @Parameter(value = "componentResources")
099        @Property(write = false)
100        private ComponentResources overrides;
101    
102        /**
103         * A comma-separated list of property names to be added to the {@link org.apache.tapestry5.beaneditor.BeanModel}.
104         */
105        @Parameter(defaultPrefix = BindingConstants.LITERAL)
106        private String add;
107    
108        @Inject
109        private ComponentResources resources;
110    
111        @Inject
112        private BeanModelSource modelSource;
113    
114        @Property
115        private String propertyName;
116    
117        void setupRender()
118        {
119            if (model == null) model = modelSource.createDisplayModel(object.getClass(), overrides.getContainerMessages());
120    
121            BeanModelUtils.modify(model, add, include, exclude, reorder);
122        }
123    
124        /**
125         * Returns the property model for the current property.
126         */
127        public PropertyModel getPropertyModel()
128        {
129            return model.get(propertyName);
130        }
131    
132    
133        public String getPropertyClass()
134        {
135            return lean ? null : getPropertyModel().getId();
136        }
137    }