    001// Licensed under the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005// http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.corelib.components;
    014
    015import org.apache.tapestry5.BindingConstants;
    016import org.apache.tapestry5.ComponentResources;
    017import org.apache.tapestry5.SymbolConstants;
    018import org.apache.tapestry5.annotations.Parameter;
    019import org.apache.tapestry5.annotations.Property;
    020import org.apache.tapestry5.annotations.SupportsInformalParameters;
    021import org.apache.tapestry5.beanmodel.BeanModel;
    022import org.apache.tapestry5.beanmodel.BeanModelUtils;
    023import org.apache.tapestry5.beanmodel.PropertyModel;
    024import org.apache.tapestry5.beanmodel.services.BeanModelSource;
    025import org.apache.tapestry5.ioc.annotations.Inject;
    026import org.apache.tapestry5.ioc.annotations.Symbol;
    027
    028/**
    029 * A component that displays the properties of a bean (or POJO, or any object with properties) as a
    030 * simple HTML <em>definition</em> list. Uses a {@link BeanModel} (either explicitly provided, or generated by Tapestry).
    031 * <p>
    032 * The output is a &lt;dl&gt; element containing a series of &lt;dt&gt; - &lt;dd&gt; pairs, where the &lt;dt&gt; contains
    033 * the property's label and the &lt;dd&gt; contains the  property's value (formatted as per its datatype). Only properties that have a known data type
    034 * are displayed.
    035 * <p>
    036 * The property id is used as the class attribute of the &lt;dt&gt; and &lt;dd&gt; element (allowing CSS customization
    037 * per property), unless the lean parameter is true.
    038 * <p>
    039 * The outer &lt;dl&gt; element has the CSS class "well dl-horizontal" (Tapestry 5.4 and later) or "t-beandisplay" (Tapestry 5.3.x and earlier).
    040 *
    041 * @see org.apache.tapestry5.beaneditor.DataType
    042 * @see BeanModel
    043 * @tapestrydoc
    044 * @see BeanEditForm
    045 * @see Grid
    046 */
    047@SupportsInformalParameters
    048public class BeanDisplay
    049{
    050
    051    @Inject
    052    @Symbol(SymbolConstants.BEAN_DISPLAY_CSS_CLASS)
    053    @Property
    054    private String cssClass;
    055
    056    /**
    057     * The object to be rendered; if not explicitly bound, a default binding to a property whose name matches this
    058     * component's id will be used.
    059     */
    060    @Parameter(required = true, allowNull = false, autoconnect = true)
    061    @Property(write = false)
    062    private Object object;
    063
    064    /**
    065     * If true, then the CSS class attribute on the &lt;dt&gt; and &lt;dd&gt; elements will be ommitted.
    066     */
    067    @Parameter(value = "false")
    068    private boolean lean;
    069
    070    /**
    071     * The model that identifies the parameters to be edited, their order, and every other aspect. If not specified, a
    072     * default bean model will be created from the type of the object bound to the object parameter. The add, include,
    073     * exclude and reorder
    074     * parameters are <em>only</em> applied to a default model, not an explicitly provided one.
    075     */
    076    @Parameter
    077    private BeanModel model;
    078    /**
    079     * A comma-separated list of property names to be retained from the
    080     * {@link org.apache.tapestry5.beanmodel.BeanModel} (only used
    081     * when a default model is created automatically).
    082     * Only these properties will be retained, and the properties will also be reordered. The names are
    083     * case-insensitive.
    084     */
    085    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    086    private String include;
    087
    088    /**
    089     * A comma-separated list of property names to be removed from the {@link org.apache.tapestry5.beanmodel.BeanModel}
    090     * (only used
    091     * when a default model is created automatically).
    092     * The names are case-insensitive.
    093     */
    094    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    095    private String exclude;
    096
    097    /**
    098     * A comma-separated list of property names indicating the order in which the properties should be presented. The
    099     * names are case insensitive. Any properties not indicated in the list will be appended to the end of the display
    100     * orde. Only used
    101     * when a default model is created automatically.
    102     */
    103    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    104    private String reorder;
    105
    106    /**
    107     * A comma-separated list of property names to be added to the {@link org.apache.tapestry5.beanmodel.BeanModel}
    108     * (only used
    109     * when a default model is created automatically).
    110     */
    111    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    112    private String add;
    113
    114    /**
    115     * Where to search for local overrides of property display blocks as block parameters. Further, the container of the
    116     * overrides is used as the source for overridden validation messages. This is normally the component itself, but
    117     * when the component is used within a BeanEditForm, it will be the BeanEditForm's block parameter that will be
    118     * searched.
    119     */
    120    @Parameter(value = "componentResources")
    121    @Property(write = false)
    122    private ComponentResources overrides;
    123
    124    @Inject
    125    private ComponentResources resources;
    126
    127    @Inject
    128    private BeanModelSource modelSource;
    129
    130    @Property
    131    private String propertyName;
    132
    133    public BeanModel getModel()
    134    {
    135        if (model == null)
    136        {
    137            model = modelSource.createDisplayModel(object.getClass(), overrides.getContainerMessages());
    138
    139            BeanModelUtils.modify(model, add, include, exclude, reorder);
    140        }
    141        return model;
    142    }
    143
    144    /**
    145     * Returns the property model for the current property.
    146     */
    147    public PropertyModel getPropertyModel()
    148    {
    149        return getModel().get(propertyName);
    150    }
    151
    152    public String getPropertyClass()
    153    {
    154        return lean ? null : getPropertyModel().getId();
    155    }
    156}