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 * <p/>
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     */
092    protected Object renderPropertyValue(MarkupWriter writer, String overrideBlockId)
093    {
094        Block override = overrides.getOverrideBlock(overrideBlockId);
095
096        if (override != null) return override;
097
098        String datatype = model.getDataType();
099
100        if (beanBlockSource.hasDisplayBlock(datatype))
101        {
102            PropertyOutputContext context = new PropertyOutputContext()
103            {
104                public Messages getMessages()
105                {
106                    return overrides.getOverrideMessages();
107                }
108
109                public Object getPropertyValue()
110                {
111                    return readPropertyForObject();
112                }
113
114                public String getPropertyId()
115                {
116                    return model.getId();
117                }
118
119                public String getPropertyName()
120                {
121                    return model.getPropertyName();
122                }
123            };
124
125            environment.push(PropertyOutputContext.class, context);
126            mustPopEnvironment = true;
127
128            return beanBlockSource.getDisplayBlock(datatype);
129        }
130
131        Object value = readPropertyForObject();
132
133        String text = value == null ? "" : value.toString();
134
135        if (InternalUtils.isNonBlank(text))
136        {
137            writer.write(text);
138        }
139
140        // Don't render anything else
141
142        return false;
143    }
144
145    Object readPropertyForObject()
146    {
147        PropertyConduit conduit = model.getConduit();
148
149        try
150        {
151            return conduit == null ? null : conduit.get(object);
152        } catch (NullPointerException ex)
153        {
154            throw new TapestryException(String.format("Property '%s' contains a null value in the path.", model.getPropertyName()),
155                    resources.getLocation(),
156                    ex);
157        }
158    }
159
160    /**
161     * Returns false; there's no template and this prevents the body from rendering.
162     */
163    boolean beforeRenderTemplate()
164    {
165        return false;
166    }
167
168    void afterRender()
169    {
170        if (mustPopEnvironment)
171        {
172            environment.pop(PropertyOutputContext.class);
173            mustPopEnvironment = false;
174        }
175    }
176
177    // Used for testing.
178    void inject(final PropertyModel model, final Object object, final ComponentResources resources)
179    {
180        this.model = model;
181        this.object = object;
182        this.resources = resources;
183    }
184}