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.internal.services;
16:
17: import org.apache.tapestry.Binding;
18: import org.apache.tapestry.ComponentResources;
19: import org.apache.tapestry.TapestryConstants;
20: import org.apache.tapestry.internal.TapestryInternalUtils;
21: import org.apache.tapestry.ioc.Messages;
22: import org.apache.tapestry.ioc.services.PropertyAccess;
23: import org.apache.tapestry.runtime.Component;
24: import org.apache.tapestry.services.BindingSource;
25: import org.apache.tapestry.services.ComponentDefaultProvider;
26:
27: public class ComponentDefaultProviderImpl implements
28: ComponentDefaultProvider {
29: private final PropertyAccess _propertyAccess;
30:
31: private final BindingSource _bindingSource;
32:
33: public ComponentDefaultProviderImpl(PropertyAccess propertyAccess,
34: BindingSource bindingSource) {
35: _propertyAccess = propertyAccess;
36: _bindingSource = bindingSource;
37: }
38:
39: public String defaultLabel(ComponentResources resources) {
40: String componentId = resources.getId();
41: String key = componentId + "-label";
42:
43: Messages containerMessages = resources.getContainerResources()
44: .getMessages();
45:
46: if (containerMessages.contains(key))
47: return containerMessages.get(key);
48:
49: return TapestryInternalUtils.toUserPresentable(componentId);
50: }
51:
52: public Binding defaultBinding(String parameterName,
53: ComponentResources componentResources) {
54: String componentId = componentResources.getId();
55:
56: Component container = componentResources.getContainer();
57:
58: // Only provide a default binding if the container actually contains the property.
59: // This sets up an error condition for when the parameter is not bound, and
60: // the binding can't be deduced.
61:
62: if (_propertyAccess.getAdapter(container).getPropertyAdapter(
63: componentId) == null)
64: return null;
65:
66: ComponentResources containerResources = componentResources
67: .getContainerResources();
68:
69: return _bindingSource.newBinding("default " + parameterName,
70: containerResources,
71: TapestryConstants.PROP_BINDING_PREFIX, componentId);
72: }
73: }
|