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.base;
014
015import org.apache.tapestry5.*;
016import org.apache.tapestry5.annotations.Parameter;
017import org.apache.tapestry5.beaneditor.PropertyModel;
018import org.apache.tapestry5.ioc.Messages;
019import org.apache.tapestry5.ioc.annotations.Inject;
020import org.apache.tapestry5.ioc.internal.util.InternalUtils;
021import org.apache.tapestry5.ioc.internal.util.TapestryException;
022import org.apache.tapestry5.services.BeanBlockSource;
023import org.apache.tapestry5.services.Core;
024import org.apache.tapestry5.services.Environment;
025import org.apache.tapestry5.services.PropertyOutputContext;
026
027/**
028 * Base class for components that output a property value using a {@link PropertyModel}. There's a relationship between
029 * such a component and its container, as the container may provide messages in its message catalog needed by the {@link
030 * Block}s that render the values. In addition, the component may be passed Block parameters that are output overrides
031 * for specified properties.
032 * 
033 * Subclasses will implement a <code>beginRender()</code> method that invokes {@link #renderPropertyValue(MarkupWriter,
034 * String)}.
035 *
036 * @see BeanBlockSource
037 */
038public abstract class AbstractPropertyOutput
039{
040    /**
041     * Model for property displayed by the cell.
042     */
043    @Parameter(required = true, allowNull = false)
044    private PropertyModel model;
045
046    /**
047     * Used to search for block parameter overrides (this is normally the enclosing Grid component's resources).
048     */
049    @Parameter(required = true, allowNull = false)
050    private PropertyOverrides overrides;
051
052    /**
053     * Identifies the object being rendered. The component will extract a property from the object and render its value
054     * (or delegate to a {@link org.apache.tapestry5.Block} that will do so).
055     */
056    @Parameter(required = true)
057    private Object object;
058
059    /**
060     * Source for property display blocks. This defaults to the default implementation of {@link
061     * org.apache.tapestry5.services.BeanBlockSource}.
062     */
063    @Parameter(required = true, allowNull = false)
064    private BeanBlockSource beanBlockSource;
065
066    @Inject
067    @Core
068    private BeanBlockSource defaultBeanBlockSource;
069
070    @Inject
071    private Environment environment;
072
073    private boolean mustPopEnvironment;
074
075    @Inject
076    private ComponentResources resources;
077
078    BeanBlockSource defaultBeanBlockSource()
079    {
080        return defaultBeanBlockSource;
081    }
082
083    protected PropertyModel getPropertyModel()
084    {
085        return model;
086    }
087
088    /**
089     * Invoked from subclasses to do the rendering. The subclass controls the naming convention for locating an
090     * overriding Block parameter (it is the name of the property possibly suffixed with a value).
091     * @param writer a MarkupWriter
092     * @param overrideBlockId the override block id
093     * @return a Block
094     */
095    protected Object renderPropertyValue(MarkupWriter writer, String overrideBlockId)
096    {
097        Block override = overrides.getOverrideBlock(overrideBlockId);
098
099        if (override != null) return override;
100
101        String datatype = model.getDataType();
102
103        if (beanBlockSource.hasDisplayBlock(datatype))
104        {
105            PropertyOutputContext context = new PropertyOutputContext()
106            {
107                public Messages getMessages()
108                {
109                    return overrides.getOverrideMessages();
110                }
111
112                public Object getPropertyValue()
113                {
114                    return readPropertyForObject();
115                }
116
117                public String getPropertyId()
118                {
119                    return model.getId();
120                }
121
122                public String getPropertyName()
123                {
124                    return model.getPropertyName();
125                }
126            };
127
128            environment.push(PropertyOutputContext.class, context);
129            mustPopEnvironment = true;
130
131            return beanBlockSource.getDisplayBlock(datatype);
132        }
133
134        Object value = readPropertyForObject();
135
136        String text = value == null ? "" : value.toString();
137
138        if (InternalUtils.isNonBlank(text))
139        {
140            writer.write(text);
141        }
142
143        // Don't render anything else
144
145        return false;
146    }
147
148    Object readPropertyForObject()
149    {
150        PropertyConduit conduit = model.getConduit();
151
152        try
153        {
154            return conduit == null ? null : conduit.get(object);
155        } catch (NullPointerException ex)
156        {
157            throw new TapestryException(String.format("Property '%s' contains a null value in the path.", model.getPropertyName()),
158                    resources.getLocation(),
159                    ex);
160        }
161    }
162
163    /**
164     * Returns false; there's no template and this prevents the body from rendering.
165     */
166    boolean beforeRenderTemplate()
167    {
168        return false;
169    }
170
171    void afterRender()
172    {
173        if (mustPopEnvironment)
174        {
175            environment.pop(PropertyOutputContext.class);
176            mustPopEnvironment = false;
177        }
178    }
179
180    // Used for testing.
181    void inject(final PropertyModel model, final Object object, final ComponentResources resources)
182    {
183        this.model = model;
184        this.object = object;
185        this.resources = resources;
186    }
187}