001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.binding;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.util.Defense;
019    import org.apache.tapestry.IComponent;
020    import org.apache.tapestry.coerce.ValueConverter;
021    
022    /**
023     * A binding that connects directly to a localized string for a component.
024     * <p>
025     * Note: Renamed from StringBinding to MessageBinding in release 4.0.
026     * 
027     * @see IComponent#getMessages()
028     * @author Howard Lewis Ship
029     * @since 2.0.4
030     */
031    
032    public class MessageBinding extends AbstractBinding
033    {
034        private final IComponent _component;
035    
036        protected MessageBinding(String description, ValueConverter valueConverter, Location location,
037                IComponent component, String key)
038        {
039            super(key, valueConverter, location);
040    
041            Defense.notNull(component, "component");
042            Defense.notNull(key, "key");
043    
044            _component = component;
045        }
046    
047        public Object getComponent()
048        {
049            return _component;
050        }
051    
052        public String getKey()
053        {
054            return _description;
055        }
056    
057        /**
058         * Accesses the specified localized string. Never returns null.
059         */
060    
061        public Object getObject()
062        {
063            return _component.getMessages().getMessage(_description);
064        }
065    
066        public String toString()
067        {
068            StringBuffer buffer = new StringBuffer("MessageBinding");
069            buffer.append('[');
070            buffer.append(_component.getExtendedId());
071            buffer.append(' ');
072            buffer.append(_description);
073            buffer.append(']');
074    
075            return buffer.toString();
076        }
077    }