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    package org.apache.tapestry.form;
015    
016    import org.apache.tapestry.IMarkupWriter;
017    import org.apache.tapestry.IRequestCycle;
018    
019    
020    /**
021     * The default implementation of {@link IOptionRenderer} which is used by 
022     * the {@link PropertySelection} component if no other renderer is specified
023     * in the component parameters.
024     */
025    public class DefaultOptionRenderer implements IOptionRenderer
026    {
027    
028        /**
029         * Default instance used by {@link PropertySelection} if no custom renderer is 
030         * specified.
031         */
032        
033        public static final IOptionRenderer DEFAULT_INSTANCE = new DefaultOptionRenderer();
034        
035        /**
036         * {@inheritDoc}
037         */
038        public void renderOptions(IMarkupWriter writer, IRequestCycle cycle, IPropertySelectionModel model, Object selected)
039        {
040            int count = model.getOptionCount();
041            boolean foundSelected = false;
042            
043            for (int i = 0; i < count; i++)
044            {
045                Object option = model.getOption(i);
046                
047                writer.begin("option");
048                writer.attribute("value", model.getValue(i));
049    
050                if (!foundSelected && isEqual(option, selected))
051                {
052                    writer.attribute("selected", "selected");
053                    
054                    foundSelected = true;
055                }
056                
057                if (model.isDisabled(i))
058                    writer.attribute("disabled", "true");
059                
060                writer.print(model.getLabel(i));
061    
062                writer.end();
063    
064                writer.println();
065            }
066        }
067        
068        protected boolean isEqual(Object left, Object right)
069        {
070            // Both null, or same object, then are equal
071    
072            if (left == right)
073                return true;
074            
075            // If one is null, the other isn't, then not equal.
076            
077            if (left == null || right == null)
078                return false;
079            
080            // Both non-null; use standard comparison.
081            
082            return left.equals(right);
083        }
084    }