001    // Copyright 2007 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.tapestry5.util;
016    
017    import org.apache.tapestry5.OptionGroupModel;
018    import org.apache.tapestry5.OptionModel;
019    import org.apache.tapestry5.SelectModel;
020    import org.apache.tapestry5.SelectModelVisitor;
021    
022    import java.util.List;
023    
024    /**
025     * Base class for {@link SelectModel} implementations, whose primary job is to provide the {@link
026     * #visit(SelectModelVisitor)} method.
027     */
028    public abstract class AbstractSelectModel implements SelectModel
029    {
030        public final void visit(SelectModelVisitor visitor)
031        {
032            List<OptionGroupModel> groups = getOptionGroups();
033    
034            if (groups != null)
035            {
036                for (OptionGroupModel groupModel : groups)
037                {
038                    visitor.beginOptionGroup(groupModel);
039    
040                    visitOptions(groupModel.getOptions(), visitor);
041    
042                    visitor.endOptionGroup(groupModel);
043                }
044            }
045    
046            visitOptions(getOptions(), visitor);
047        }
048    
049        private void visitOptions(List<OptionModel> options, SelectModelVisitor vistor)
050        {
051            if (options != null)
052            {
053                for (OptionModel optionModel : options)
054                    vistor.option(optionModel);
055            }
056        }
057    
058    }