001 // Copyright 2010 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.tapestry5.internal.services;
015
016 import java.util.List;
017
018 import org.apache.tapestry5.OptionModel;
019 import org.apache.tapestry5.SelectModel;
020 import org.apache.tapestry5.ValueEncoder;
021 import org.apache.tapestry5.internal.OptionModelImpl;
022 import org.apache.tapestry5.internal.SelectModelImpl;
023 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
024 import org.apache.tapestry5.ioc.services.ClassPropertyAdapter;
025 import org.apache.tapestry5.ioc.services.PropertyAccess;
026 import org.apache.tapestry5.ioc.services.PropertyAdapter;
027 import org.apache.tapestry5.services.SelectModelFactory;
028 import org.apache.tapestry5.services.ValueEncoderSource;
029
030 public class SelectModelFactoryImpl implements SelectModelFactory
031 {
032 private final PropertyAccess propertyAccess;
033 private final ValueEncoderSource valueEncoderSource;
034
035 public SelectModelFactoryImpl(final PropertyAccess propertyAccess,
036 final ValueEncoderSource valueEncoderSource)
037 {
038 super();
039 this.propertyAccess = propertyAccess;
040 this.valueEncoderSource = valueEncoderSource;
041 }
042
043 @SuppressWarnings("unchecked")
044 public SelectModel create(final List<?> objects, final String labelProperty)
045 {
046 final List<OptionModel> options = CollectionFactory.newList();
047
048 for (final Object object : objects)
049 {
050 final ClassPropertyAdapter classPropertyAdapter = this.propertyAccess
051 .getAdapter(object);
052
053 final PropertyAdapter propertyAdapter = classPropertyAdapter.getPropertyAdapter(labelProperty);
054
055 final ValueEncoder encoder = this.valueEncoderSource.getValueEncoder(propertyAdapter.getType());
056
057 final Object label = propertyAdapter.get(object);
058
059 options.add(new OptionModelImpl(encoder.toClient(label), object));
060
061 }
062
063 return new SelectModelImpl(null, options);
064 }
065 }