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#getString(String)
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        private final String _key;
037    
038        protected MessageBinding(String description, ValueConverter valueConverter, Location location,
039                IComponent component, String key)
040        {
041            super(description, valueConverter, location);
042    
043            Defense.notNull(component, "component");
044            Defense.notNull(key, "key");
045    
046            _component = component;
047            _key = key;
048        }
049    
050        public Object getComponent()
051        {
052            return _component;
053        }
054    
055        public String getKey()
056        {
057            return _key;
058        }
059    
060        /**
061         * Accesses the specified localized string. Never returns null.
062         */
063    
064        public Object getObject()
065        {
066            return _component.getMessages().getMessage(_key);
067        }
068    
069        public String toString()
070        {
071            StringBuffer buffer = new StringBuffer("StringBinding");
072            buffer.append('[');
073            buffer.append(_component.getExtendedId());
074            buffer.append(' ');
075            buffer.append(_key);
076            buffer.append(']');
077    
078            return buffer.toString();
079        }
080    }