01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.corelib.components;
16:
17: import java.text.Format;
18:
19: import org.apache.tapestry.Binding;
20: import org.apache.tapestry.ComponentResources;
21: import org.apache.tapestry.MarkupWriter;
22: import org.apache.tapestry.annotations.Inject;
23: import org.apache.tapestry.annotations.Parameter;
24: import org.apache.tapestry.annotations.SupportsInformalParameters;
25: import org.apache.tapestry.ioc.internal.util.InternalUtils;
26: import org.apache.tapestry.services.ComponentDefaultProvider;
27:
28: /**
29: * A component for formatting output. If the component is represented in the template using an
30: * element, then the element (plus any informal parameters) will be output around the formatted
31: * value.
32: */
33: @SupportsInformalParameters
34: public class Output {
35: /**
36: * The value to be output (before formatting). If the formatted value is blank, no output is
37: * produced.
38: */
39: @Parameter(required=true)
40: private Object _value;
41:
42: /** The format to be applied to the object. */
43: @Parameter(required=true)
44: private Format _format;
45:
46: /**
47: * The element name, derived from the component template. This can even be overridden manually
48: * if desired (for example, to sometimes render a surrounding element and other times not).
49: */
50: @Parameter("componentResources.elementName")
51: private String _elementName;
52:
53: @Inject
54: private ComponentDefaultProvider _defaultProvider;
55:
56: @Inject
57: private ComponentResources _resources;
58:
59: Binding defaultValue() {
60: return _defaultProvider.defaultBinding("value", _resources);
61: }
62:
63: boolean beginRender(MarkupWriter writer) {
64: String formatted = _format.format(_value);
65:
66: if (InternalUtils.isNonBlank(formatted)) {
67: if (_elementName != null) {
68: writer.element(_elementName);
69:
70: _resources.renderInformalParameters(writer);
71: }
72:
73: writer.write(formatted);
74:
75: if (_elementName != null)
76: writer.end();
77: }
78:
79: return false;
80: }
81:
82: // For testing.
83:
84: void setup(Object value, Format format, String elementName,
85: ComponentResources resources) {
86: _value = value;
87: _format = format;
88: _elementName = elementName;
89: _resources = resources;
90: }
91: }
|